add.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. // 性别
  12. genderList: [],
  13. // 等级列表
  14. levelList: [],
  15. },
  16. initValidate() {
  17. const rules = { icon: { required: true }, name: { required: true }, gender: { required: true }, age: { required: true }, phone: { required: true, tel: true } }
  18. // 验证字段的提示信息,若不传则调用默认的信息
  19. const messages = { icon: { required: '请选择头像', }, name: { required: '请输入用户姓名', }, gender: { required: '请选择性别', }, age: { required: '请输入年龄', }, phone: { required: '请输入联系电话', } };
  20. this.WxValidate = new WxValidate(rules, messages)
  21. },
  22. // 返回
  23. back: function () {
  24. wx.navigateBack({ delta: 1 })
  25. },
  26. imgUpl: function (e) {
  27. const that = this;
  28. let data = that.data.form.icon;
  29. data.push(e.detail)
  30. that.setData({ 'form.icon': data })
  31. },
  32. // 删除图片
  33. imgDel: function (e) {
  34. const that = this;
  35. let list = that.data.form.icon;
  36. let arr = list.filter((i, index) => index != e.detail.index)
  37. that.setData({ 'form.icon': arr })
  38. },
  39. // 选择性别
  40. genderChange: function (e) {
  41. const that = this;
  42. let data = that.data.genderList[e.detail.value];
  43. if (data) {
  44. that.setData({ 'form.gender': data.value });
  45. that.setData({ 'form.zhGender': data.label });
  46. }
  47. },
  48. //选择等级
  49. coachChange: function (e) {
  50. const that = this;
  51. let data = that.data.levelList[e.detail.value];
  52. if (data) {
  53. that.setData({ 'form.level': data.value });
  54. that.setData({ 'form.zhLevel': data.label });
  55. }
  56. },
  57. // 提交登录
  58. onSubmit: async function (e) {
  59. const that = this;
  60. const params = e.detail.value;
  61. const form = that.data.form;
  62. params.icon = form.icon;
  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 (form._id) { arr = await app.$post(`/coach/${form._id}`, params); }
  70. else { arr = await app.$post(`/coach`, params) }
  71. if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.back(); }
  72. else wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  73. }
  74. },
  75. /**
  76. * 生命周期函数--监听页面加载
  77. */
  78. onLoad: function (options) {
  79. const that = this;
  80. that.setData({ id: options.id || '' })
  81. //验证规则函数
  82. that.initValidate();
  83. // 监听用户是否登录
  84. that.watchLogin();
  85. },
  86. // 监听用户是否登录
  87. watchLogin: async function () {
  88. const that = this;
  89. wx.getStorage({
  90. key: 'user',
  91. success: async res => {
  92. const aee = await app.$get(`/dict`, { code: "coach_grade" });
  93. if (aee.errcode == '0' && aee.total > 0) that.setData({ levelList: aee.data[0].list });
  94. const ree = await app.$get(`/dict`, { code: "gender" });
  95. if (ree.errcode == '0' && aee.total > 0) that.setData({ genderList: ree.data[0].list });
  96. if (that.data.id) {
  97. const arr = await app.$get(`/coach/${that.data.id}`);
  98. if (arr.errcode == '0') {
  99. let gender = that.data.genderList.find(i => i.value == arr.data.gender)
  100. if (gender) arr.data.zhGender = gender.label;
  101. let level = that.data.levelList.find(i => i.value == arr.data.level)
  102. if (level) arr.data.zhLevel = level.label;
  103. that.setData({ form: arr.data });
  104. }
  105. }
  106. },
  107. fail: res => {
  108. wx.redirectTo({ url: '/pages/index/index', })
  109. }
  110. })
  111. },
  112. /**
  113. * 生命周期函数--监听页面初次渲染完成
  114. */
  115. onReady: function () {
  116. },
  117. /**
  118. * 生命周期函数--监听页面显示
  119. */
  120. onShow: function () {
  121. },
  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. })