add.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. ],
  18. genderList: gender
  19. },
  20. initValidate() {
  21. const rules = { type: { required: true }, icon: { required: true }, name: { required: true }, card: { required: true }, gender: { required: true }, phone: { required: true, tel: true } }
  22. // 验证字段的提示信息,若不传则调用默认的信息
  23. const messages = { type: { required: '请选择用户类型', }, icon: { required: '请选择头像', }, name: { required: '请输入用户姓名', }, card: { required: '请输入身份证号', }, gender: { required: '请选择性别', }, phone: { required: '请输入手机号', } };
  24. this.WxValidate = new WxValidate(rules, messages)
  25. },
  26. // 返回
  27. back: function () {
  28. wx.navigateBack({ delta: 1 })
  29. },
  30. imgUpl: function (e) {
  31. const that = this;
  32. let data = that.data.form.icon;
  33. data.push(e.detail)
  34. that.setData({ 'form.icon': data })
  35. },
  36. // 删除图片
  37. imgDel: function (e) {
  38. const that = this;
  39. let list = that.data.form.icon;
  40. let arr = list.filter((i, index) => index != e.detail.index)
  41. that.setData({ 'form.icon': arr })
  42. },
  43. // 选择用户类型
  44. typeChange: function (e) {
  45. const that = this;
  46. let data = that.data.typeList[e.detail.value];
  47. if (data) {
  48. that.setData({ 'form.type': data.value });
  49. that.setData({ 'form.type_name': data.label });
  50. }
  51. },
  52. // 选择性别
  53. genderChange: function (e) {
  54. const that = this;
  55. let data = that.data.genderList[e.detail.value];
  56. if (data) {
  57. that.setData({ 'form.gender': data.value });
  58. that.setData({ 'form.gender_name': data.label });
  59. }
  60. },
  61. // 提交登录
  62. onSubmit: async function (e) {
  63. const that = this;
  64. const params = e.detail.value;
  65. const form = that.data.form;
  66. params.icon = form.icon;
  67. if (!this.WxValidate.checkForm(params)) {
  68. const error = this.WxValidate.errorList[0];
  69. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  70. return false
  71. } else {
  72. let arr;
  73. if (that.data.id) arr = await app.$post(`/newCourt/api/user/${form._id}`, params);
  74. else arr = await app.$post(`/newCourt/api/user`, params);
  75. if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.back(); }
  76. else wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 })
  77. }
  78. },
  79. /**
  80. * 生命周期函数--监听页面加载
  81. */
  82. onLoad: function (options) {
  83. const that = this;
  84. that.setData({ id: options.id || null })
  85. //验证规则函数
  86. that.initValidate();
  87. // 监听用户是否登录
  88. that.watchLogin();
  89. },
  90. // 监听用户是否登录
  91. watchLogin: async function () {
  92. const that = this;
  93. wx.getStorage({
  94. key: 'user',
  95. success: async res => {
  96. let arr;
  97. if (that.data.id) {
  98. arr = await app.$get(`/newCourt/api/user/${that.data.id}`);
  99. if (arr.errcode == '0') {
  100. let type = that.data.typeList.find(i => i.value == arr.data.type);
  101. if (type) arr.data.type_name = type.label;
  102. let gender = that.data.genderList.find(i => i.value == arr.data.gender);
  103. if (gender) arr.data.gender_name = gender.label;
  104. that.setData({ form: arr.data })
  105. } else { wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 }) }
  106. }
  107. },
  108. fail: async res => {
  109. wx.redirectTo({ url: '/pages/index/index' })
  110. }
  111. })
  112. },
  113. /**
  114. * 生命周期函数--监听页面初次渲染完成
  115. */
  116. onReady: function () {
  117. },
  118. /**
  119. * 生命周期函数--监听页面显示
  120. */
  121. onShow: function () { },
  122. /**
  123. * 生命周期函数--监听页面隐藏
  124. */
  125. onHide: function () {
  126. },
  127. /**
  128. * 生命周期函数--监听页面卸载
  129. */
  130. onUnload: function () {
  131. },
  132. /**
  133. * 页面相关事件处理函数--监听用户下拉动作
  134. */
  135. onPullDownRefresh: function () {
  136. },
  137. /**
  138. * 页面上拉触底事件的处理函数
  139. */
  140. onReachBottom: function () {
  141. },
  142. /**
  143. * 用户点击右上角分享
  144. */
  145. onShareAppMessage: function () {
  146. }
  147. })