add.js 5.3 KB

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