add.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. school_id: '',
  11. form: { icon: [] },
  12. // 性别
  13. genderList: [],
  14. // 等级列表
  15. levelList: [],
  16. },
  17. initValidate() {
  18. const rules = { icon: { required: true }, name: { required: true }, gender: { required: true }, age: { required: true }, phone: { required: true, tel: true } }
  19. // 验证字段的提示信息,若不传则调用默认的信息
  20. const messages = { icon: { required: '请选择头像', }, name: { required: '请输入用户姓名', }, gender: { required: '请选择性别', }, age: { required: '请输入年龄', }, phone: { required: '请输入联系电话', } };
  21. this.WxValidate = new WxValidate(rules, messages)
  22. },
  23. // 返回
  24. back: function () {
  25. wx.navigateBack({ delta: 1 })
  26. },
  27. imgUpl: function (e) {
  28. const that = this;
  29. let data = that.data.form.icon;
  30. data.push(e.detail)
  31. that.setData({ 'form.icon': data })
  32. },
  33. // 删除图片
  34. imgDel: function (e) {
  35. const that = this;
  36. let list = that.data.form.icon;
  37. let arr = list.filter((i, index) => index != e.detail.index)
  38. that.setData({ 'form.icon': arr })
  39. },
  40. // 选择性别
  41. genderChange: function (e) {
  42. const that = this;
  43. let data = that.data.genderList[e.detail.value];
  44. if (data) {
  45. that.setData({ 'form.gender': data.value });
  46. that.setData({ 'form.zhGender': data.label });
  47. }
  48. },
  49. //选择等级
  50. coachChange: function (e) {
  51. const that = this;
  52. let data = that.data.levelList[e.detail.value];
  53. if (data) {
  54. that.setData({ 'form.level': data.value });
  55. that.setData({ 'form.zhLevel': data.label });
  56. }
  57. },
  58. // 提交登录
  59. onSubmit: async function (e) {
  60. const that = this;
  61. const params = e.detail.value;
  62. const form = that.data.form;
  63. params.icon = form.icon;
  64. params.school_id = that.data.school_id;
  65. if (!this.WxValidate.checkForm(params)) {
  66. const error = this.WxValidate.errorList[0];
  67. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  68. return false
  69. } else {
  70. let arr;
  71. if (form._id) { arr = await app.$post(`/coach/${form._id}`, params); }
  72. else { arr = await app.$post(`/coach`, params); console.log(params); }
  73. if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.back(); }
  74. else wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  75. }
  76. },
  77. /**
  78. * 生命周期函数--监听页面加载
  79. */
  80. onLoad: function (options) {
  81. const that = this;
  82. that.setData({ id: options.id || '' })
  83. //验证规则函数
  84. that.initValidate();
  85. // 监听用户是否登录
  86. that.watchLogin();
  87. },
  88. // 监听用户是否登录
  89. watchLogin: async function () {
  90. const that = this;
  91. wx.getStorage({
  92. key: 'user',
  93. success: async res => {
  94. that.setData({ school_id: res.data.info.id })
  95. const aee = await app.$get(`/dict`, { code: "coach_grade" });
  96. if (aee.errcode == '0' && aee.total > 0) that.setData({ levelList: aee.data[0].list });
  97. const ree = await app.$get(`/dict`, { code: "gender" });
  98. if (ree.errcode == '0' && ree.total > 0) that.setData({ genderList: ree.data[0].list });
  99. if (that.data.id) {
  100. const arr = await app.$get(`/coach/${that.data.id}`);
  101. if (arr.errcode == '0') {
  102. let gender = that.data.genderList.find(i => i.value == arr.data.gender)
  103. if (gender) arr.data.zhGender = gender.label;
  104. let level = that.data.levelList.find(i => i.value == arr.data.level)
  105. if (level) arr.data.zhLevel = level.label;
  106. that.setData({ form: arr.data });
  107. }
  108. }
  109. },
  110. fail: 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. */
  128. onHide: function () {
  129. },
  130. /**
  131. * 生命周期函数--监听页面卸载
  132. */
  133. onUnload: function () {
  134. },
  135. /**
  136. * 页面相关事件处理函数--监听用户下拉动作
  137. */
  138. onPullDownRefresh: function () {
  139. },
  140. /**
  141. * 页面上拉触底事件的处理函数
  142. */
  143. onReachBottom: function () {
  144. },
  145. /**
  146. * 用户点击右上角分享
  147. */
  148. onShareAppMessage: function () {
  149. }
  150. })