index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. wx.setStorage({ key: "token", data: form })
  94. that.setData({ form: form });
  95. that.setData({ icon: form.icon });
  96. }
  97. },
  98. fail: res => {
  99. wx.redirectTo({ url: '/pages/index/index', })
  100. }
  101. })
  102. },
  103. // 计算高度
  104. searchHeight: function () {
  105. let frameStyle = this.data.frameStyle;
  106. let client = app.globalData.client;
  107. let infoHeight = client.windowHeight;
  108. // 是否去掉状态栏
  109. if (frameStyle.useTop) infoHeight = infoHeight - (client.statusBarHeight + client.getMenu.height + (client.getMenu.top - client.statusBarHeight) * 2);
  110. // 是否减去底部菜单
  111. if (frameStyle.useBar) infoHeight = infoHeight - 50;
  112. if (infoHeight) this.setData({ infoHeight: infoHeight })
  113. },
  114. /**
  115. * 生命周期函数--监听页面初次渲染完成
  116. */
  117. onReady: function () {
  118. },
  119. /**
  120. * 生命周期函数--监听页面显示
  121. */
  122. onShow: function () {
  123. },
  124. /**
  125. * 生命周期函数--监听页面隐藏
  126. */
  127. onHide: function () {
  128. },
  129. /**
  130. * 生命周期函数--监听页面卸载
  131. */
  132. onUnload: function () {
  133. },
  134. /**
  135. * 页面相关事件处理函数--监听用户下拉动作
  136. */
  137. onPullDownRefresh: function () {
  138. },
  139. /**
  140. * 页面上拉触底事件的处理函数
  141. */
  142. onReachBottom: function () {
  143. },
  144. /**
  145. * 用户点击右上角分享
  146. */
  147. onShareAppMessage: function () {
  148. }
  149. })