index.js 4.6 KB

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