detail.js 6.1 KB

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