add.js 6.3 KB

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