index.js 5.0 KB

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