tpStudentadd.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. const app = getApp()
  2. import WxValidate from '../../../utils/wxValidate'
  3. import QRCode from '../../../utils/weapp-qrcode.js';
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. frameStyle: { useTop: true, name: '信息管理', leftArrow: true, useBar: false },
  10. user: {},
  11. lesson_id: '',
  12. lessonInfo: {},
  13. id: '',
  14. form: { file: [] },
  15. studentlist: [],
  16. payUrl: '',
  17. // dialog弹框
  18. dialog: { title: '二维码支付', show: false, type: '1' },
  19. payUrl: ''
  20. },
  21. initValidate() {
  22. const rules = { name: { required: true }, phone: { required: true }, file: { required: true } }
  23. // 验证字段的提示信息,若不传则调用默认的信息
  24. const messages = { name: { required: '学员姓名', }, phone: { required: '联系方式', }, file: { required: '图片', } };
  25. this.WxValidate = new WxValidate(rules, messages)
  26. },
  27. // 跳转菜单
  28. back(e) {
  29. wx.navigateBack({ delta: 1 })
  30. },
  31. // 选择学员
  32. stuChange(e) {
  33. const that = this;
  34. let data = that.data.studentlist[e.detail.value];
  35. if (data) that.setData({ 'form.student_id': data.student_id, 'form.name': data.student_id_name, 'form.phone': data.student_id_phone, })
  36. },
  37. // 上传照片
  38. imgUpl: function (e) {
  39. const that = this;
  40. let data = that.data.form.file;
  41. data.push(e.detail)
  42. that.setData({ 'form.file': data })
  43. },
  44. // 删除图片
  45. imgDel: function (e) {
  46. const that = this;
  47. let list = that.data.form.file;
  48. let arr = list.filter((i, index) => index != e.detail.index)
  49. that.setData({ 'form.file': arr })
  50. },
  51. // 提交
  52. onSubmit: async function (e) {
  53. const that = this;
  54. const params = e.detail.value;
  55. const form = that.data.form;
  56. params.file = form.file;
  57. if (!this.WxValidate.checkForm(params)) {
  58. const error = this.WxValidate.errorList[0];
  59. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  60. return false
  61. } else {
  62. let res = await app.$post(`/tempLessonApply`, params);
  63. if (res.errcode == '0') {
  64. if (res.data == 'ok') {
  65. wx.showToast({ title: '临时报名上课成功', icon: 'none' })
  66. that.back()
  67. } else {
  68. let data = JSON.parse(res.data);
  69. that.createPayUrl(data.code_url);
  70. that.setData({ dialog: { title: '二维码支付', show: true, type: '1' } })
  71. }
  72. } else {
  73. wx.showToast({ title: res.errmsg, icon: 'none' })
  74. }
  75. }
  76. },
  77. createPayUrl(e) {
  78. // 生成二维码
  79. const that = this;
  80. const url = `${e}`;
  81. var qrcode = new QRCode(`myQrcode`, {
  82. text: url,
  83. width: 110,
  84. height: 110,
  85. padding: 3,
  86. colorDark: "#000000",
  87. colorLight: "#ffffff",
  88. correctLevel: QRCode.CorrectLevel.L,
  89. });
  90. },
  91. // 关闭弹框
  92. toClose: function () {
  93. const that = this;
  94. that.setData({ dialog: { title: '二维码支付', show: false, type: '1' } })
  95. },
  96. // 付款成功
  97. paySuccess() {
  98. const that = this;
  99. that.setData({ form: {} })
  100. that.toClose();
  101. that.back();
  102. },
  103. /**
  104. * 生命周期函数--监听页面加载
  105. */
  106. onLoad: function (options) {
  107. const that = this;
  108. that.setData({ lesson_id: options.lesson_id || '63563bbd8018a39220eca774', id: options.id || '' });
  109. //验证规则函数
  110. that.initValidate();
  111. // 监听用户是否登录
  112. that.watchLogin();
  113. },
  114. // 监听用户是否登录
  115. watchLogin: async function () {
  116. const that = this;
  117. wx.getStorage({
  118. key: 'user',
  119. success: async res => {
  120. that.setData({ user: res.data });
  121. await that.searchLesson();
  122. await that.search();
  123. await that.searchStu();
  124. },
  125. fail: res => {
  126. wx.redirectTo({ url: '/pages/index/index', })
  127. }
  128. })
  129. },
  130. async searchLesson() {
  131. const that = this;
  132. let lesson_id = that.data.lesson_id;
  133. let res = await app.$get(`/lesson/${lesson_id}`);
  134. if (res.errcode == '0') {
  135. that.setData({ lessonInfo: res.data });
  136. }
  137. else { wx.showToast({ title: res.errmsg, icon: 'none' }) }
  138. },
  139. async search() {
  140. const that = this;
  141. let lessonInfo = that.data.lessonInfo;
  142. let user = that.data.user;
  143. let id = that.data.id;
  144. let form = { school_id: lessonInfo.school_id, coach_id: user.info.id, lesson_id: lessonInfo.id, money: lessonInfo.money, file: [] };
  145. let res;
  146. if (id) {
  147. res = await app.$get(`/tempLessonApply/${id}`);
  148. if (res.errcode == '0') form = res.data
  149. else { wx.showToast({ title: res.errmsg, icon: 'none' }) }
  150. }
  151. that.setData({ form })
  152. },
  153. async searchStu() {
  154. const that = this;
  155. let lessonInfo = that.data.lessonInfo;
  156. let res = await app.$get(`/rss`, { school_id: lessonInfo.school_id });
  157. if (res.errcode == '0') {
  158. that.setData({ studentlist: res.data })
  159. } else { wx.showToast({ title: res.errmsg, icon: 'none' }) }
  160. },
  161. /**
  162. * 生命周期函数--监听页面初次渲染完成
  163. */
  164. onReady: function () {
  165. },
  166. /**
  167. * 生命周期函数--监听页面显示
  168. */
  169. onShow: function () {
  170. },
  171. /**
  172. * 生命周期函数--监听页面隐藏
  173. */
  174. onHide: function () {
  175. },
  176. /**
  177. * 生命周期函数--监听页面卸载
  178. */
  179. onUnload: function () {
  180. },
  181. /**
  182. * 页面相关事件处理函数--监听用户下拉动作
  183. */
  184. onPullDownRefresh: function () {
  185. },
  186. /**
  187. * 页面上拉触底事件的处理函数
  188. */
  189. onReachBottom: function () {
  190. },
  191. /**
  192. * 用户点击右上角分享
  193. */
  194. onShareAppMessage: function () {
  195. }
  196. })