add.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. const app = getApp()
  2. import WxValidate from '../../utils/wxValidate'
  3. const { gender } = require('../../utils/dict')
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. frameStyle: { useTop: true, name: '信息管理', leftArrow: true, useBar: false },
  10. id: '',
  11. form: { icon: [] },
  12. typeList: [
  13. { label: '超级管理员', value: '-1' },
  14. { label: '普通用户', value: '0' },
  15. { label: '管理员', value: '1' },
  16. { label: '教练', value: '2' },
  17. { label: '学员', value: '3' },
  18. { label: '游客', value: '10' },
  19. ],
  20. genderList: gender
  21. },
  22. initValidate() {
  23. const rules = { type: { required: true }, icon: { required: true }, name: { required: true }, gender: { required: true }, phone: { required: true, tel: true } }
  24. // 验证字段的提示信息,若不传则调用默认的信息
  25. const messages = { type: { required: '请选择用户类型', }, icon: { required: '请选择头像', }, name: { required: '请输入用户姓名', }, gender: { required: '请选择性别', }, phone: { required: '请输入手机号', } };
  26. this.WxValidate = new WxValidate(rules, messages)
  27. },
  28. // 返回
  29. back: function () {
  30. wx.navigateBack({ delta: 1 })
  31. },
  32. imgUpl: function (e) {
  33. const that = this;
  34. let data = that.data.form.icon;
  35. data.push(e.detail)
  36. that.setData({ 'form.icon': data })
  37. },
  38. // 删除图片
  39. imgDel: function (e) {
  40. const that = this;
  41. let list = that.data.form.icon;
  42. let arr = list.filter((i, index) => index != e.detail.index)
  43. that.setData({ 'form.icon': arr })
  44. },
  45. // 选择用户类型
  46. typeChange: function (e) {
  47. const that = this;
  48. let data = that.data.typeList[e.detail.value];
  49. if (data) {
  50. that.setData({ 'form.type': data.value });
  51. that.setData({ 'form.type_name': data.label });
  52. }
  53. },
  54. // 选择性别
  55. genderChange: function (e) {
  56. const that = this;
  57. let data = that.data.genderList[e.detail.value];
  58. if (data) {
  59. that.setData({ 'form.gender': data.value });
  60. that.setData({ 'form.gender_name': data.label });
  61. }
  62. },
  63. // 提交登录
  64. onSubmit: async function (e) {
  65. const that = this;
  66. const params = e.detail.value;
  67. const form = that.data.form;
  68. params.icon = form.icon;
  69. if (!this.WxValidate.checkForm(params)) {
  70. const error = this.WxValidate.errorList[0];
  71. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  72. return false
  73. } else {
  74. let arr;
  75. if (that.data.id) arr = await app.$post(`/user/${form._id}`, params);
  76. else arr = await app.$post(`/user`, params);
  77. if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.back(); }
  78. else wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 })
  79. }
  80. },
  81. /**
  82. * 生命周期函数--监听页面加载
  83. */
  84. onLoad: function (options) {
  85. const that = this;
  86. that.setData({ id: options.id || null })
  87. //验证规则函数
  88. that.initValidate();
  89. // 监听用户是否登录
  90. that.watchLogin();
  91. },
  92. // 监听用户是否登录
  93. watchLogin: async function () {
  94. const that = this;
  95. wx.getStorage({
  96. key: 'user',
  97. success: async res => {
  98. let arr;
  99. if (that.data.id) {
  100. arr = await app.$get(`/user/${that.data.id}`);
  101. if (arr.errcode == '0') {
  102. let type = that.data.typeList.find(i => i.value == arr.data.type);
  103. if (type) arr.data.type_name = type.label;
  104. let gender = that.data.genderList.find(i => i.value == arr.data.gender);
  105. if (gender) arr.data.gender_name = gender.label;
  106. that.setData({ form: arr.data })
  107. } else { wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 }) }
  108. }
  109. },
  110. fail: async res => {
  111. wx.redirectTo({ url: '/pages/index/index' })
  112. }
  113. })
  114. },
  115. /**
  116. * 生命周期函数--监听页面初次渲染完成
  117. */
  118. onReady: function () {
  119. },
  120. /**
  121. * 生命周期函数--监听页面显示
  122. */
  123. onShow: function () { },
  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. })