add.js 7.3 KB

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