add.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. const app = getApp()
  2. import WxValidate from '../../../utils/wxValidate';
  3. Page({
  4. data: {
  5. frameStyle: { useTop: true, name: '信息维护', leftArrow: true, useBar: false },
  6. // 教练和学校的关系
  7. schoolInfo: {},
  8. form: {},
  9. statusList: []
  10. },
  11. initValidate() {
  12. const rules = { title: { required: true }, start_date: { required: true }, start_time: { required: true }, end_date: { required: true }, end_time: { required: true }, brief: { required: true }, money: { required: true }, limit: { required: true }, refund_date: { required: false }, refund_time: { required: false } }
  13. // 验证字段的提示信息,若不传则调用默认的信息
  14. const messages = { title: { required: '课程标题' }, start_date: { required: '课程开始日期' }, start_time: { required: '课程开始时间' }, end_date: { required: '课程结束日期' }, end_time: { required: '课程结束时间' }, brief: { required: '请输入课程简介' }, money: { required: '报名费用' }, limit: { required: '人数限制' }, refund_date: { required: '退款期限日期' }, refund_time: { required: '退款期限时间' } };
  15. this.WxValidate = new WxValidate(rules, messages)
  16. },
  17. // 返回
  18. back: function () {
  19. wx.navigateBack({ delta: 1 })
  20. },
  21. // 课程开始日期
  22. startdChange: function (e) {
  23. const that = this;
  24. that.setData({ 'form.start_date': e.detail.value })
  25. },
  26. // 课程开始时间
  27. starttChange: function (e) {
  28. const that = this;
  29. that.setData({ 'form.start_time': e.detail.value })
  30. },
  31. // 课程结束日期
  32. enddChange: function (e) {
  33. const that = this;
  34. that.setData({ 'form.end_date': e.detail.value })
  35. },
  36. // 课程结束时间
  37. endtChange: function (e) {
  38. const that = this;
  39. that.setData({ 'form.end_time': e.detail.value })
  40. },
  41. // 退款期限日期
  42. refunddChange: function (e) {
  43. const that = this;
  44. that.setData({ 'form.refund_date': e.detail.value })
  45. },
  46. // 退款期限时间
  47. refundtChange: function (e) {
  48. const that = this;
  49. that.setData({ 'form.refund_time': e.detail.value })
  50. },
  51. // 选择状态
  52. statusChange: function (e) {
  53. const that = this;
  54. let data = that.data.statusList[e.detail.value];
  55. if (data) {
  56. that.setData({ 'form.status': data.value })
  57. that.setData({ 'form.zhStatus': data.label })
  58. }
  59. },
  60. //提交
  61. onSubmit: async function (e) {
  62. const that = this;
  63. const params = e.detail.value;
  64. if (!this.WxValidate.checkForm(params)) {
  65. const error = this.WxValidate.errorList[0];
  66. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  67. return false
  68. } else {
  69. params.time_start = params.start_date + ' ' + params.start_time;
  70. params.time_end = params.end_date + ' ' + params.end_time;
  71. params.refund_hour = params.refund_date + ' ' + params.refund_time;
  72. let arr;
  73. if (params._id) { arr = await app.$post(`/lesson/${params._id}`, params) }
  74. else { arr = await app.$post(`/lesson`, 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: async function (options) {
  83. const that = this;
  84. that.setData({ id: options.id || '' })
  85. //验证规则函数
  86. that.initValidate();
  87. // 查询其他信息
  88. await that.searchOther();
  89. // 监听用户是否登录
  90. await that.watchLogin();
  91. },
  92. // 监听用户是否登录
  93. watchLogin: async function () {
  94. const that = this;
  95. let statusList = that.data.statusList;
  96. wx.getStorage({
  97. key: 'user',
  98. success: async res => {
  99. let data = res.data;
  100. let arr;
  101. // 教练与学校关系
  102. arr = await app.$get(`/rcs`, { coach_id: data.info._id });
  103. if (arr.errcode == '0' && arr.total > 0) that.setData({ schoolInfo: arr.data[0] });
  104. let form = { school_id: that.data.schoolInfo.school_id, type: '1' };
  105. if (that.data.id) {
  106. arr = await app.$get(`/lesson/${that.data.id}`);
  107. if (arr.errcode == '0') {
  108. arr.data.start_date = arr.data.time_start.slice(0, 10);
  109. arr.data.start_time = arr.data.time_start.slice(11, arr.data.time_start.length);
  110. arr.data.end_date = arr.data.time_end.slice(0, 10);
  111. arr.data.end_time = arr.data.time_end.slice(11, arr.data.time_end.length);
  112. arr.data.refund_date = arr.data.refund_hour.slice(0, 10);
  113. arr.data.refund_time = arr.data.refund_hour.slice(11, arr.data.refund_hour.length);
  114. // 查询状态
  115. let status = statusList.find(i => i.value = arr.data.status);
  116. if (status) arr.data.zhStatus = status.label;
  117. form = arr.data;
  118. }
  119. };
  120. that.setData({ form: form })
  121. },
  122. fail: async res => {
  123. wx.redirectTo({ url: '/pages/index/index' })
  124. }
  125. })
  126. },
  127. // 查询其他信息
  128. searchOther: async function () {
  129. const that = this;
  130. let arr;
  131. arr = await app.$get(`/dict`, { code: 'lesson_status' });
  132. if (arr.errcode == '0' && arr.total > 0) that.setData({ statusList: arr.data[0].list })
  133. },
  134. /**
  135. * 生命周期函数--监听页面初次渲染完成
  136. */
  137. onReady: function () {
  138. },
  139. /**
  140. * 生命周期函数--监听页面显示
  141. */
  142. onShow: function () {
  143. },
  144. /**
  145. * 生命周期函数--监听页面隐藏
  146. */
  147. onHide: function () {
  148. },
  149. /**
  150. * 生命周期函数--监听页面卸载
  151. */
  152. onUnload: function () {
  153. },
  154. /**
  155. * 页面相关事件处理函数--监听用户下拉动作
  156. */
  157. onPullDownRefresh: function () {
  158. },
  159. /**
  160. * 页面上拉触底事件的处理函数
  161. */
  162. onReachBottom: function () {
  163. },
  164. /**
  165. * 用户点击右上角分享
  166. */
  167. onShareAppMessage: function () {
  168. }
  169. })