add.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. schoolList: [],
  13. // 性别
  14. genderList: [],
  15. // 等级列表
  16. levelList: [],
  17. },
  18. initValidate() {
  19. const rules = { icon: { required: true }, name: { required: true }, gender: { required: true }, age: { required: true }, phone: { required: true, tel: true } }
  20. // 验证字段的提示信息,若不传则调用默认的信息
  21. const messages = { icon: { required: '请选择头像', }, name: { required: '请输入用户姓名', }, gender: { required: '请选择性别', }, age: { required: '请输入年龄', }, phone: { required: '请输入联系电话', } };
  22. this.WxValidate = new WxValidate(rules, messages)
  23. },
  24. // 返回
  25. back: function () {
  26. wx.navigateBack({ delta: 1 })
  27. },
  28. imgUpl: function (e) {
  29. const that = this;
  30. let data = that.data.form.icon;
  31. data.push(e.detail)
  32. that.setData({ 'form.icon': data })
  33. },
  34. // 删除图片
  35. imgDel: function (e) {
  36. const that = this;
  37. let list = that.data.form.icon;
  38. let arr = list.filter((i, index) => index != e.detail.index)
  39. that.setData({ 'form.icon': arr })
  40. },
  41. // 选择性别
  42. genderChange: function (e) {
  43. const that = this;
  44. let data = that.data.genderList[e.detail.value];
  45. if (data) {
  46. that.setData({ 'form.gender': data.value });
  47. that.setData({ 'form.zhGender': data.label });
  48. }
  49. },
  50. //选择等级
  51. coachChange: function (e) {
  52. const that = this;
  53. let data = that.data.levelList[e.detail.value];
  54. if (data) {
  55. that.setData({ 'form.level': data.value });
  56. that.setData({ 'form.zhLevel': data.label });
  57. }
  58. },
  59. // 选择羽校
  60. schoolChange: function (e) {
  61. const that = this;
  62. let data = that.data.schoolList[e.detail.value];
  63. if (data) {
  64. that.setData({ 'form.school_id': data._id });
  65. that.setData({ 'form.zhSchool': data.name });
  66. }
  67. },
  68. // 提交登录
  69. onSubmit: async function (e) {
  70. const that = this;
  71. const params = e.detail.value;
  72. const form = that.data.form;
  73. params.icon = form.icon;
  74. if (!this.WxValidate.checkForm(params)) {
  75. const error = this.WxValidate.errorList[0];
  76. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  77. return false
  78. } else {
  79. let arr;
  80. if (form._id) { arr = await app.$post(`/coach/${form._id}`, params); }
  81. else { arr = await app.$post(`/coach`, params) }
  82. if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.back(); }
  83. else wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  84. }
  85. },
  86. /**
  87. * 生命周期函数--监听页面加载
  88. */
  89. onLoad: async function (options) {
  90. const that = this;
  91. that.setData({ id: options.id || '' })
  92. //验证规则函数
  93. that.initValidate();
  94. // 查询其他信息
  95. await that.searchOther();
  96. // 监听用户是否登录
  97. await that.watchLogin();
  98. },
  99. // 查询其他信息
  100. searchOther: async function () {
  101. const that = this;
  102. let arr;
  103. // 学校列表
  104. arr = await app.$get(`/school`);
  105. if (arr.errcode == '0') that.setData({ schoolList: arr.data });
  106. // 教练等级
  107. arr = await app.$get(`/dict`, { code: "coach_grade" });
  108. if (arr.errcode == '0' && arr.total > 0) that.setData({ levelList: arr.data[0].list });
  109. // 性别
  110. arr = await app.$get(`/dict`, { code: "gender" });
  111. if (arr.errcode == '0' && arr.total > 0) that.setData({ genderList: arr.data[0].list });
  112. },
  113. // 监听用户是否登录
  114. watchLogin: async function () {
  115. const that = this;
  116. const levelList = that.data.levelList;
  117. const genderList = that.data.genderList;
  118. wx.getStorage({
  119. key: 'user',
  120. success: async res => {
  121. if (that.data.id) {
  122. const arr = await app.$get(`/coach/${that.data.id}`);
  123. if (arr.errcode == '0') {
  124. // 性别
  125. let gender = genderList.find(i => i.value == arr.data.gender)
  126. if (gender) arr.data.zhGender = gender.label;
  127. // 等级
  128. let level = levelList.find(i => i.value == arr.data.level)
  129. if (level) arr.data.zhLevel = level.label;
  130. // 教练与学校的关系
  131. let school = await app.$get(`/rcs`, { coach_id: that.data.id });
  132. if (school.errcode == '0' && school.total > 0) { arr.data.school_id = school.data[0].school_id; arr.data.zhSchool = school.data[0].school_id_name; }
  133. that.setData({ form: arr.data });
  134. }
  135. }
  136. },
  137. fail: res => {
  138. wx.redirectTo({ url: '/pages/index/index', })
  139. }
  140. })
  141. },
  142. /**
  143. * 生命周期函数--监听页面初次渲染完成
  144. */
  145. onReady: function () {
  146. },
  147. /**
  148. * 生命周期函数--监听页面显示
  149. */
  150. onShow: function () {
  151. },
  152. /**
  153. * 生命周期函数--监听页面隐藏
  154. */
  155. onHide: function () {
  156. },
  157. /**
  158. * 生命周期函数--监听页面卸载
  159. */
  160. onUnload: function () {
  161. },
  162. /**
  163. * 页面相关事件处理函数--监听用户下拉动作
  164. */
  165. onPullDownRefresh: function () {
  166. },
  167. /**
  168. * 页面上拉触底事件的处理函数
  169. */
  170. onReachBottom: function () {
  171. },
  172. /**
  173. * 用户点击右上角分享
  174. */
  175. onShareAppMessage: function () {
  176. }
  177. })