add.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. const app = getApp()
  2. import WxValidate from '../../../utils/wxValidate';
  3. const moment = require("../../../utils/moment.min")
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. frameStyle: { useTop: true, name: '账号充值', leftArrow: true, useBar: false },
  10. user: {},
  11. // 关系信息
  12. id: '',
  13. rcsInfo: {},
  14. form: {}
  15. },
  16. initValidate() {
  17. const rules = { money: { required: true } }
  18. // 验证字段的提示信息,若不传则调用默认的信息
  19. const messages = { money: { required: '充值金额' } };
  20. this.WxValidate = new WxValidate(rules, messages)
  21. },
  22. // 返回
  23. back: function () {
  24. wx.navigateBack({ delta: 1 })
  25. },
  26. onSubmit: async function (e) {
  27. const that = this;
  28. const params = e.detail.value;
  29. const rcsInfo = that.data.rcsInfo;
  30. const user = that.data.user;
  31. if (!this.WxValidate.checkForm(params)) {
  32. const error = this.WxValidate.errorList[0];
  33. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  34. return false
  35. } else {
  36. let obj = { school_id: rcsInfo.school_id, openid: user.openid, payer_id: user.info.id, payer_role: 'Coach', pay_for: '充值', money: params.money, time: moment().format('YYYY-MM-DD HH:mm:ss') }
  37. const arr = await app.$post(`/payOrder`, obj)
  38. if (arr.errcode == '0') {
  39. wx.requestPayment({
  40. "timeStamp": arr.data.wxSign.timestamp,
  41. "nonceStr": arr.data.wxSign.nonceStr,
  42. "package": `prepay_id=${arr.data.wxSign.prepay_id}`,
  43. "signType": arr.data.wxSign.signType,
  44. "paySign": arr.data.wxSign.paySign,
  45. "success": async function (res) {
  46. let money = parseInt(rcsInfo.money || 0) + parseFloat(params.money);
  47. const aee = await app.$post(`/rcs/${rcsInfo._id}`, { money: money });
  48. if (aee.errcode == '0') {
  49. wx.showToast({ title: `支付成功`, icon: 'success', duration: 2000 });
  50. that.back()
  51. } else { wx.showToast({ title: `${aee.errmsg}`, icon: 'error', duration: 2000 }) }
  52. },
  53. "fail": function (res) {
  54. wx.showToast({ title: `支付未成功`, icon: 'error', duration: 2000 })
  55. that.back()
  56. },
  57. })
  58. }
  59. }
  60. },
  61. /**
  62. * 生命周期函数--监听页面加载
  63. */
  64. onLoad: async function (options) {
  65. const that = this;
  66. that.setData({ id: options.id || '62ff243908c3ce998df1602a' });
  67. //验证规则函数
  68. that.initValidate();
  69. // 监听用户是否登录
  70. await that.watchLogin();
  71. },
  72. // 监听用户是否登录
  73. watchLogin: async function () {
  74. const that = this;
  75. wx.getStorage({
  76. key: 'user',
  77. success: async res => {
  78. that.setData({ user: res.data })
  79. let arr;
  80. if (that.data.id) {
  81. // 关系信息
  82. arr = await app.$get(`/rcs/${that.data.id}`);
  83. if (arr.errcode == '0') that.setData({ rcsInfo: arr.data })
  84. else { wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 }) }
  85. }
  86. },
  87. fail: async res => {
  88. wx.redirectTo({ url: '/pages/index/index' })
  89. }
  90. })
  91. },
  92. /**
  93. * 生命周期函数--监听页面初次渲染完成
  94. */
  95. onReady: function () {
  96. },
  97. /**
  98. * 生命周期函数--监听页面显示
  99. */
  100. onShow: function () { },
  101. /**
  102. * 生命周期函数--监听页面隐藏
  103. */
  104. onHide: function () {
  105. },
  106. /**
  107. * 生命周期函数--监听页面卸载
  108. */
  109. onUnload: function () {
  110. },
  111. /**
  112. * 页面相关事件处理函数--监听用户下拉动作
  113. */
  114. onPullDownRefresh: function () {
  115. },
  116. /**
  117. * 页面上拉触底事件的处理函数
  118. */
  119. onReachBottom: function () {
  120. },
  121. /**
  122. * 用户点击右上角分享
  123. */
  124. onShareAppMessage: function () {
  125. }
  126. })