add.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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: {},
  11. user: {},
  12. // 教练学校
  13. coachSchool: {},
  14. // 学校学员
  15. studentList: [],
  16. // 折扣设置
  17. discount_typeList: []
  18. },
  19. initValidate() {
  20. const rules = { student_id: { required: true } }
  21. // 验证字段的提示信息,若不传则调用默认的信息
  22. const messages = { student_id: { required: '请选择学员' } };
  23. this.WxValidate = new WxValidate(rules, messages)
  24. },
  25. // 返回
  26. back: function () {
  27. wx.navigateBack({ delta: 1 })
  28. },
  29. // 选择学生
  30. stuChange: function (e) {
  31. const that = this;
  32. let data = that.data.studentList[e.detail.value];
  33. if (data) {
  34. console.log(data);
  35. that.setData({ 'form.student_id': data.student_id })
  36. that.setData({ 'form.student_id_name': data.student_id_name })
  37. }
  38. },
  39. // 选择折扣类型
  40. typeChange: function (e) {
  41. const that = this;
  42. let data = that.data.discount_typeList[e.detail.value];
  43. if (data) {
  44. that.setData({ 'form.config.discount_type': data.value });
  45. that.setData({ 'form.config.number': null })
  46. }
  47. },
  48. // 折扣判断
  49. moneyInput: function (e) {
  50. const that = this;
  51. const form = that.data.form;
  52. if (form.config && form.config.discount_type && form.config.discount_type == 'discount') {
  53. var regex = /^(10|\d)(\.\d{1,1})?$/;
  54. if (!regex.test(e.detail.value)) {
  55. wx.showToast({ title: `数据不允许`, icon: 'error', duration: 1000 });
  56. that.setData({ 'form.config.number': null })
  57. }
  58. }
  59. },
  60. onSubmit: async function (e) {
  61. const that = this;
  62. const params = e.detail.value;
  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. params.config = { discount_type: params['config.discount_type'], number: params['config.number'] }
  69. let arr;
  70. if (that.data.id) arr = await app.$post(`/rsc/${that.data.id}`, params);
  71. else arr = await app.$post(`/rsc`, params);
  72. if (arr.errcode == '0') {
  73. wx.showToast({ title: `信息维护成功`, icon: 'success', duration: 2000 });
  74. that.back()
  75. } else { wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 }) }
  76. }
  77. },
  78. /**
  79. * 生命周期函数--监听页面加载
  80. */
  81. onLoad: async function (options) {
  82. const that = this;
  83. that.setData({ id: options.id || '' });
  84. //验证规则函数
  85. that.initValidate();
  86. // 查询其他信息
  87. await that.searchOther();
  88. // 监听用户是否登录
  89. await that.watchLogin();
  90. },
  91. searchOther: async function () {
  92. const that = this;
  93. let arr;
  94. // 折扣设置
  95. arr = await app.$get(`/dict`, { code: 'rsc_type' });
  96. if (arr.errcode == '0' && arr.total > 0) { that.setData({ discount_typeList: arr.data[0].list }) }
  97. },
  98. // 监听用户是否登录
  99. watchLogin: async function () {
  100. const that = this;
  101. wx.getStorage({
  102. key: 'user',
  103. success: async res => {
  104. that.setData({ user: res.data });
  105. // 查询教练学校
  106. let arr;
  107. arr = await app.$get(`/rcs`, { coach_id: res.data.info.id });
  108. if (arr.errcode == '0' && arr.total > 0) that.setData({ coachSchool: arr.data[0] });
  109. // 查询学校学员
  110. arr = await app.$get(`/rss`, { school_id: that.data.coachSchool.school_id });
  111. if (arr.errcode == '0') that.setData({ studentList: arr.data });
  112. let form = { school_id: that.data.coachSchool.school_id, coach_id: that.data.user.info.id }
  113. if (that.data.id) {
  114. arr = await app.$get(`/rsc/${that.data.id}`);
  115. if (arr.errcode == '0') {
  116. let student = that.data.studentList.find(i => i.student_id == arr.data.student_id);
  117. if (student) arr.data.student_id_name = student.student_id_name;
  118. form = arr.data;
  119. }
  120. }
  121. that.setData({ form })
  122. },
  123. fail: async res => {
  124. wx.redirectTo({ url: '/pages/index/index' })
  125. }
  126. })
  127. },
  128. /**
  129. * 生命周期函数--监听页面初次渲染完成
  130. */
  131. onReady: function () {
  132. },
  133. /**
  134. * 生命周期函数--监听页面显示
  135. */
  136. onShow: function () { },
  137. /**
  138. * 生命周期函数--监听页面隐藏
  139. */
  140. onHide: function () {
  141. },
  142. /**
  143. * 生命周期函数--监听页面卸载
  144. */
  145. onUnload: function () {
  146. },
  147. /**
  148. * 页面相关事件处理函数--监听用户下拉动作
  149. */
  150. onPullDownRefresh: function () {
  151. },
  152. /**
  153. * 页面上拉触底事件的处理函数
  154. */
  155. onReachBottom: function () {
  156. },
  157. /**
  158. * 用户点击右上角分享
  159. */
  160. onShareAppMessage: function () {
  161. }
  162. })