detail.js 5.4 KB

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