add.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. typeList: [],
  16. },
  17. initValidate() {
  18. const rules = { type: { required: true }, title: { required: true }, brief: { required: true }, limit: { required: true }, start_date: { required: true }, start_time: { required: true }, end_date: { required: true }, end_time: { required: true }, status: { required: true }, refund_hour: { required: true } }
  19. // 验证字段的提示信息,若不传则调用默认的信息
  20. const messages = { type: { required: '请输入课程类型', }, title: { required: '请输入课程标题', }, brief: { required: '请输入简介', }, limit: { required: '请输入人数上限', }, start_date: { required: '请选择开始日期', }, start_time: { required: '请选择开始时间', }, end_date: { required: '请选择结束日期', }, end_time: { required: '请选择结束时间', }, status: { required: '请选择状态', }, refund_hour: { 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. typeChange: function (e) {
  38. const that = this;
  39. let data = that.data.typeList[e.detail.value];
  40. if (data) {
  41. that.setData({ 'form.type': data.value });
  42. that.setData({ 'form.zhtype': data.label });
  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. refunddateChange: function (e) {
  67. const that = this;
  68. that.setData({ 'form.refund_date': e.detail.value })
  69. },
  70. // 退款期限时间
  71. refundtimeChange: function (e) {
  72. const that = this;
  73. that.setData({ 'form.refund_time': e.detail.value })
  74. },
  75. // 提交保存
  76. onSubmit: async function (e) {
  77. const that = this;
  78. const params = e.detail.value;
  79. const form = that.data.form;
  80. params.school_id = that.data.school_id;
  81. params.time_start = form.start_date + ' ' + form.start_time;
  82. params.time_end = form.end_date + ' ' + form.end_time;
  83. params.refund_hour = form.refund_date + ' ' + form.refund_time;
  84. if (!this.WxValidate.checkForm(params)) {
  85. const error = this.WxValidate.errorList[0];
  86. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  87. return false
  88. } else {
  89. let arr;
  90. if (form._id) { arr = await app.$post(`/lesson/${form._id}`, params); }
  91. else { arr = await app.$post(`/lesson`, params); }
  92. if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.back(); }
  93. else wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  94. }
  95. },
  96. /**
  97. * 生命周期函数--监听页面加载
  98. */
  99. onLoad: function (options) {
  100. const that = this;
  101. that.setData({ id: options.id || '' })
  102. //验证规则函数
  103. that.initValidate();
  104. // 监听用户是否登录
  105. that.watchLogin();
  106. },
  107. // 监听用户是否登录
  108. watchLogin: async function () {
  109. const that = this;
  110. wx.getStorage({
  111. key: 'user',
  112. success: async res => {
  113. that.setData({ school_id: res.data.info.id })
  114. const aee = await app.$get(`/dict`, { code: "lesson_status" });
  115. if (aee.errcode == '0' && aee.total > 0) that.setData({ statusList: aee.data[0].list });
  116. const acc = await app.$get(`/dict`, { code: "lesson_type" });
  117. if (acc.errcode == '0' && aee.total > 0) that.setData({ typeList: acc.data[0].list });
  118. if (that.data.id) {
  119. const arr = await app.$get(`/lesson/${that.data.id}`);
  120. if (arr.errcode == '0') {
  121. // 状态
  122. let status = that.data.statusList.find(i => i.value == arr.data.status)
  123. if (status) arr.data.zhstatus = status.label;
  124. // 类型
  125. let type = that.data.typeList.find(i => i.value == arr.data.type)
  126. if (type) arr.data.zhtype = type.label;
  127. // 开始时间
  128. arr.data.start_date = arr.data.time_start.slice(0, 10);
  129. arr.data.start_time = arr.data.time_start.slice(11, arr.data.time_start.length);
  130. // 结束时间
  131. arr.data.end_date = arr.data.time_end.slice(0, 10);
  132. arr.data.end_time = arr.data.time_end.slice(11, arr.data.time_end.length);
  133. // 退款期限
  134. arr.data.refund_date = arr.data.refund_hour.slice(0, 10);
  135. arr.data.refund_time = arr.data.refund_hour.slice(11, arr.data.refund_hour.length);
  136. that.setData({ form: arr.data });
  137. }
  138. }
  139. },
  140. fail: res => {
  141. wx.redirectTo({ url: '/pages/index/index', })
  142. }
  143. })
  144. },
  145. /**
  146. * 生命周期函数--监听页面初次渲染完成
  147. */
  148. onReady: function () {
  149. },
  150. /**
  151. * 生命周期函数--监听页面显示
  152. */
  153. onShow: function () {
  154. },
  155. /**
  156. * 生命周期函数--监听页面隐藏
  157. */
  158. onHide: function () {
  159. },
  160. /**
  161. * 生命周期函数--监听页面卸载
  162. */
  163. onUnload: function () {
  164. },
  165. /**
  166. * 页面相关事件处理函数--监听用户下拉动作
  167. */
  168. onPullDownRefresh: function () {
  169. },
  170. /**
  171. * 页面上拉触底事件的处理函数
  172. */
  173. onReachBottom: function () {
  174. },
  175. /**
  176. * 用户点击右上角分享
  177. */
  178. onShareAppMessage: function () {
  179. }
  180. })