sign.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. wx.showShareMenu({
  60. menus: ['shareTimeline', 'shareAppMessage'],
  61. })
  62. } else {
  63. wx.showToast({ title: `${arr.errmsg}`, icon: 'fail', duration: 2000 });
  64. }
  65. }
  66. },
  67. async shareFinish() {
  68. const enroll = this.data.enrollInfo;
  69. if (!enroll) {
  70. wx.showToast({
  71. title: '未找到报名信息',
  72. icon: 'error'
  73. })
  74. return;
  75. }
  76. const { _id } = enroll;
  77. const res = await app.$post(`/newCourt/api/enroll/${_id}`, { has_turn: '1' })
  78. if (app.$checkRes(res)) {
  79. console.log('已转发')
  80. console.log(res)
  81. }
  82. },
  83. toForward: function () { },
  84. // 支付
  85. toPay: async function () {
  86. const that = this;
  87. const enroll = that.data.enrollInfo;
  88. wx.getStorage({
  89. key: 'user',
  90. success: async (res) => {
  91. let obj = { openid: res.data.openid, money: 50, enroll_id: enroll._id, type: '报名' }
  92. const arr = await app.$post(`/newCourt/api/payOrder`, obj)
  93. if (arr.errcode == '0') {
  94. wx.requestPayment({
  95. "timeStamp": arr.data.wxSign.timestamp,
  96. "nonceStr": arr.data.wxSign.nonceStr,
  97. "package": `prepay_id=${arr.data.wxSign.prepay_id}`,
  98. "signType": arr.data.wxSign.signType,
  99. "paySign": arr.data.wxSign.paySign,
  100. "success": async function (res) {
  101. wx.showToast({ title: `支付成功`, icon: 'success', duration: 2000 });
  102. const aee = await app.$post(`/newCourt/api/payOrder/${arr.data.data._id}`, { status: '1' });
  103. if (aee.errcode == '0') { that.watchLogin(); that.toClose(); that.back() }
  104. },
  105. "fail": function (res) {
  106. wx.showToast({ title: `支付未成功`, icon: 'error', duration: 2000 })
  107. that.watchLogin()
  108. },
  109. })
  110. }
  111. },
  112. fail: async (res) => {
  113. wx.redirectTo({ url: '/pages/index/index' });
  114. },
  115. });
  116. },
  117. // 关闭弹框
  118. toClose: function () {
  119. console.log('in')
  120. const that = this;
  121. that.setData({ dialog: { title: '支付报名费', show: false, type: '1' } })
  122. },
  123. /**
  124. * 生命周期函数--监听页面加载
  125. */
  126. onLoad: function (options) {
  127. const that = this;
  128. that.setData({ id: options.id || '62e23cd3bb22e03d0b49d73b' });
  129. //验证规则函数
  130. that.initValidate();
  131. // 监听用户是否登录
  132. that.watchLogin();
  133. // 先禁用分享
  134. wx.hideShareMenu()
  135. },
  136. watchLogin: function () {
  137. const that = this;
  138. wx.getStorage({
  139. key: 'user',
  140. success: async (res) => {
  141. if (that.data.id) {
  142. const arr = await app.$get(`/newCourt/api/match/${that.data.id}`);
  143. if (arr.errcode == '0') {
  144. that.setData({ info: arr.data })
  145. let data = { openid: res.data.openid, match_id: arr.data._id, match_name: arr.data.name }
  146. that.setData({ form: data })
  147. } else { wx.showToast({ title: `${res.errMsg}`, icon: 'fail', duration: 2000 }); }
  148. }
  149. },
  150. fail: async (res) => {
  151. wx.redirectTo({ url: '/pages/index/index' });
  152. },
  153. });
  154. },
  155. /**
  156. * 生命周期函数--监听页面初次渲染完成
  157. */
  158. onReady: function () {
  159. },
  160. /**
  161. * 生命周期函数--监听页面显示
  162. */
  163. onShow: function () {
  164. },
  165. /**
  166. * 生命周期函数--监听页面隐藏
  167. */
  168. onHide: function () {
  169. },
  170. /**
  171. * 生命周期函数--监听页面卸载
  172. */
  173. onUnload: function () {
  174. },
  175. /**
  176. * 页面相关事件处理函数--监听用户下拉动作
  177. */
  178. onPullDownRefresh: function () {
  179. },
  180. /**
  181. * 页面上拉触底事件的处理函数
  182. */
  183. onReachBottom: function () {
  184. },
  185. /**
  186. * 用户点击右上角分享
  187. */
  188. onShareAppMessage: function () {
  189. },
  190. onShareTimeline() {
  191. this.shareFinish();
  192. return {
  193. title: '羽毛球线上比赛',
  194. }
  195. }
  196. })