index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // pages/login/login.js
  2. import WxValidate from '../../utils/wxValidate'
  3. const app = getApp()
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. // 主体高度
  10. infoHeight: '',
  11. frameStyle: { useTop: true, name: '个人信息', leftArrow: true, useBar: false },
  12. form: {
  13. gender: ''
  14. },
  15. fileList: [],
  16. genderList: [{ id: '0', name: '男' }, { id: 1, name: '女' },],
  17. index: 0,
  18. },
  19. //验证必填项
  20. initValidate() {
  21. const rules = { phone: { required: true, tel: true }, }
  22. // 验证字段的提示信息,若不传则调用默认的信息
  23. const messages = { phone: { required: '请输入手机号' }, };
  24. this.WxValidate = new WxValidate(rules, messages)
  25. },
  26. back: function () {
  27. wx.navigateBack({ url: '/pages/me/index' })
  28. },
  29. //上传图片
  30. imgUpload: function (e) {
  31. const that = this;
  32. let data = that.data.fileList;
  33. data.push(e.detail)
  34. that.setData({ fileList: data })
  35. },
  36. //删除图片
  37. imgDel: function (e) {
  38. const that = this;
  39. let data = that.data.fileList;
  40. let arr = data.filter((i, index) => index != e.detail.index)
  41. that.setData({ fileList: arr })
  42. },
  43. // 选择用户性别
  44. bindPickerChange: function (e) {
  45. const that = this;
  46. let index = e.detail.value;
  47. let data = that.data.genderList[index];
  48. if (data) that.setData({ 'form.gender': data.name });
  49. },
  50. //提交
  51. formSubmit: function (e) {
  52. const value = e.detail.value;
  53. if (!this.WxValidate.checkForm(value)) {
  54. const error = this.WxValidate.errorList[0];
  55. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  56. return false
  57. } else {
  58. value.icon = this.data.fileList;
  59. value.id = this.data.form.id;
  60. wx.request({
  61. url: `${app.globalData.publicUrl}/courtAdmin/api/user/${value.id}`, //接口地址
  62. method: "post",
  63. data: value,
  64. header: {},
  65. success: res => {
  66. if (res.data.errcode == 0) {
  67. wx.showToast({
  68. title: '保存成功',
  69. icon: 'success',
  70. duration: 2000//延迟两秒
  71. })
  72. } else {
  73. wx.showToast({
  74. title: '保存失败',
  75. icon: 'success',
  76. duration: 2000
  77. })
  78. }
  79. },
  80. })
  81. }
  82. },
  83. /**
  84. * 生命周期函数--监听页面加载
  85. */
  86. onLoad: function (options) {
  87. //验证规则函数
  88. this.initValidate();
  89. // 计算高度
  90. this.searchHeight();
  91. // 监听用户是否登录
  92. this.watchLogin();
  93. },
  94. // 计算高度
  95. searchHeight: function () {
  96. let frameStyle = this.data.frameStyle;
  97. let client = app.globalData.client;
  98. let infoHeight = client.windowHeight;
  99. // 是否去掉状态栏
  100. if (frameStyle.useTop) infoHeight = infoHeight - (client.statusBarHeight + client.getMenu.height + (client.getMenu.top - client.statusBarHeight) * 2);
  101. // 是否减去底部菜单
  102. if (frameStyle.useBar) infoHeight = infoHeight - 50;
  103. if (infoHeight) this.setData({ infoHeight: infoHeight })
  104. },
  105. // 监听用户是否登录
  106. watchLogin: function () {
  107. const that = this;
  108. wx.getStorage({
  109. key: 'token',
  110. success: res => {
  111. //数据请求
  112. wx.request({
  113. url: `${app.globalData.publicUrl}/courtAdmin/api/user/${res.data.id}`, //接口地址
  114. method: "get",
  115. data: {},
  116. header: {},
  117. success: res => {
  118. that.setData({ form: res.data.data })
  119. let icon = res.data.data.icon;
  120. that.setData({ fileList: icon })
  121. },
  122. error: err => {
  123. }
  124. })
  125. },
  126. fail: res => {
  127. wx.redirectTo({ url: '/pages/login/index', })
  128. }
  129. })
  130. },
  131. /**
  132. * 生命周期函数--监听页面初次渲染完成
  133. */
  134. onReady: function () {
  135. },
  136. /**
  137. * 生命周期函数--监听页面显示
  138. */
  139. onShow: function () {
  140. },
  141. /**
  142. * 生命周期函数--监听页面隐藏
  143. */
  144. onHide: function () {
  145. },
  146. /**
  147. * 生命周期函数--监听页面卸载
  148. */
  149. onUnload: function () {
  150. },
  151. /**
  152. * 页面相关事件处理函数--监听用户下拉动作
  153. */
  154. onPullDownRefresh: function () {
  155. },
  156. /**
  157. * 页面上拉触底事件的处理函数
  158. */
  159. onReachBottom: function () {
  160. },
  161. /**
  162. * 用户点击右上角分享
  163. */
  164. onShareAppMessage: function () {
  165. }
  166. })