sign.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. info: {},
  11. form: {},
  12. projectList: [],
  13. dialog: { title: '支付报名费', show: false, type: '1' },
  14. enrollInfo: {}
  15. },
  16. initValidate() {
  17. const rules = { grouping_id: { required: true }, project_id: { required: true } }
  18. // 验证字段的提示信息,若不传则调用默认的信息
  19. const messages = { grouping_id: { required: '请选择赛事分组' }, project_id: { required: '请选择组内项目' } };
  20. this.WxValidate = new WxValidate(rules, messages)
  21. },
  22. // 跳转菜单
  23. back(e) {
  24. wx.navigateBack({ delta: 1 })
  25. },
  26. // 赛事分组
  27. groupingChange: async function (e) {
  28. const that = this;
  29. let data = that.data.info.grouping[e.detail.value];
  30. that.setData({ 'form.grouping_id': data._id });
  31. that.setData({ 'form.grouping_name': data.name });
  32. let project = [];
  33. for (const val of data.project) {
  34. const arr = await app.$get(`/newCourt/api/matchProject/${val}`);
  35. if (arr.errcode == '0') { project.push(arr.data) }
  36. }
  37. that.setData({ projectList: project });
  38. },
  39. // 选择组内项目
  40. projectChange: function (e) {
  41. const that = this;
  42. let data = that.data.projectList[e.detail.value];
  43. that.setData({ 'form.project_id': data._id });
  44. that.setData({ 'form.project_name': data.name });
  45. },
  46. onSubmit: async function (e) {
  47. const that = this;
  48. const params = e.detail.value;
  49. if (!this.WxValidate.checkForm(params)) {
  50. const error = this.WxValidate.errorList[0];
  51. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  52. return false
  53. } else {
  54. const arr = await app.$post(`/newCourt/api/enroll`, params);
  55. if (arr.errcode == '0') {
  56. wx.showToast({ title: `报名成功`, icon: 'success', duration: 2000 });
  57. that.setData({ enrollInfo: arr.data })
  58. that.setData({ dialog: { title: '支付报名费', show: true, type: '1' } })
  59. } else {
  60. wx.showToast({ title: `${res.errMsg}`, icon: 'fail', duration: 2000 });
  61. }
  62. }
  63. },
  64. toForward: function () { },
  65. // 支付
  66. toPay: async function () {
  67. const that = this;
  68. const enroll = that.data.enrollInfo;
  69. wx.getStorage({
  70. key: 'user',
  71. success: async (res) => {
  72. let obj = { openid: res.data.openid, money: 0.1, enroll_id: enroll._id, type: '报名' }
  73. const arr = await app.$post(`/newCourt/api/payOrder`, obj)
  74. if (arr.errcode == '0') {
  75. wx.requestPayment({
  76. "timeStamp": arr.data.wxSign.timestamp,
  77. "nonceStr": arr.data.wxSign.nonceStr,
  78. "package": `prepay_id=${arr.data.wxSign.prepay_id}`,
  79. "signType": arr.data.wxSign.signType,
  80. "paySign": arr.data.wxSign.paySign,
  81. "success": async function (res) {
  82. wx.showToast({ title: `支付成功`, icon: 'success', duration: 2000 });
  83. const aee = await app.$post(`/newCourt/api/payOrder/${arr.data.data._id}`, { status: '1' });
  84. if (aee.errcode == '0') { that.watchLogin(); that.toclose(); that.back() }
  85. },
  86. "fail": function (res) {
  87. wx.showToast({ title: `支付未成功`, icon: 'error', duration: 2000 })
  88. that.watchLogin()
  89. },
  90. })
  91. }
  92. },
  93. fail: async (res) => {
  94. wx.redirectTo({ url: '/pages/index/index' });
  95. },
  96. });
  97. },
  98. // 关闭弹框
  99. toclose: function () {
  100. const that = this;
  101. that.setData({ dialog: { title: '支付报名费', show: false, type: '1' } })
  102. },
  103. /**
  104. * 生命周期函数--监听页面加载
  105. */
  106. onLoad: function (options) {
  107. const that = this;
  108. that.setData({ id: options.id || '62e23cd3bb22e03d0b49d73b' });
  109. //验证规则函数
  110. that.initValidate();
  111. // 监听用户是否登录
  112. that.watchLogin();
  113. },
  114. watchLogin: function () {
  115. const that = this;
  116. wx.getStorage({
  117. key: 'user',
  118. success: async (res) => {
  119. if (that.data.id) {
  120. const arr = await app.$get(`/newCourt/api/match/${that.data.id}`);
  121. if (arr.errcode == '0') {
  122. that.setData({ info: arr.data })
  123. let data = { openid: res.data.openid, match_id: arr.data._id, match_name: arr.data.name }
  124. that.setData({ form: data })
  125. } else { wx.showToast({ title: `${res.errMsg}`, icon: 'fail', duration: 2000 }); }
  126. }
  127. },
  128. fail: async (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. })