coachadd.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. lesson_id: '',
  11. school_id: '',
  12. form: {},
  13. coachList: [],
  14. },
  15. initValidate() {
  16. const rules = { coach_id: { required: true }, money: { required: true }, }
  17. // 验证字段的提示信息,若不传则调用默认的信息
  18. const messages = { coach_id: { required: '请选择教练', }, money: { required: '请输入金额', } };
  19. this.WxValidate = new WxValidate(rules, messages)
  20. },
  21. // 返回
  22. back: function () {
  23. wx.navigateBack({ delta: 1 })
  24. },
  25. // 选择教练
  26. coachChange: function (e) {
  27. const that = this;
  28. let data = that.data.coachList[e.detail.value];
  29. if (data) {
  30. that.setData({ 'form.coach_id': data.coach_id });
  31. that.setData({ 'form.zhcoach': data.coach_id_name });
  32. }
  33. },
  34. // 提交保存
  35. onSubmit: async function (e) {
  36. const that = this;
  37. const params = e.detail.value;
  38. const form = that.data.form;
  39. params.school_id = that.data.school_id;
  40. if (!this.WxValidate.checkForm(params)) {
  41. const error = this.WxValidate.errorList[0];
  42. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  43. return false
  44. } else {
  45. let arr;
  46. if (form._id) {
  47. arr = await app.$post(`/lessonCoach/${form._id}`, params);
  48. }
  49. else {
  50. params.lesson_id = that.data.lesson_id;
  51. arr = await app.$post(`/lessonCoach`, params);
  52. }
  53. if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.back(); }
  54. else wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  55. }
  56. },
  57. /**
  58. * 生命周期函数--监听页面加载
  59. */
  60. onLoad: function (options) {
  61. const that = this;
  62. that.setData({ lesson_id: options.lesson_id || '' })
  63. that.setData({ id: options.id || '' })
  64. //验证规则函数
  65. that.initValidate();
  66. // 监听用户是否登录
  67. that.watchLogin();
  68. },
  69. // 监听用户是否登录
  70. watchLogin: async function () {
  71. const that = this;
  72. wx.getStorage({
  73. key: 'user',
  74. success: async res => {
  75. that.setData({ school_id: res.data.info.id })
  76. const abb = await app.$get(`/rcs`, { school_id: res.data.info.id });
  77. if (abb.errcode == '0' && abb.total > 0) {
  78. that.setData({ coachList: abb.data })
  79. }
  80. if (that.data.id) {
  81. const arr = await app.$get(`/lessonCoach/${that.data.id}`);
  82. if (arr.errcode == '0') {
  83. // 教练
  84. let coach_id = that.data.coachList.find(i => i.coach_id == arr.data.coach_id)
  85. if (coach_id) arr.data.zhcoach = coach_id.coach_id_name;
  86. that.setData({ form: arr.data });
  87. }
  88. }
  89. },
  90. fail: res => {
  91. wx.redirectTo({ url: '/pages/index/index', })
  92. }
  93. })
  94. },
  95. /**
  96. * 生命周期函数--监听页面初次渲染完成
  97. */
  98. onReady: function () {
  99. },
  100. /**
  101. * 生命周期函数--监听页面显示
  102. */
  103. onShow: function () {
  104. },
  105. /**
  106. * 生命周期函数--监听页面隐藏
  107. */
  108. onHide: function () {
  109. },
  110. /**
  111. * 生命周期函数--监听页面卸载
  112. */
  113. onUnload: function () {
  114. },
  115. /**
  116. * 页面相关事件处理函数--监听用户下拉动作
  117. */
  118. onPullDownRefresh: function () {
  119. },
  120. /**
  121. * 页面上拉触底事件的处理函数
  122. */
  123. onReachBottom: function () {
  124. },
  125. /**
  126. * 用户点击右上角分享
  127. */
  128. onShareAppMessage: function () {
  129. }
  130. })