add.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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: 'Bill', 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 = Math.floor((Number(rcsInfo.money || 0) + Number(params.money)) * 100) / 100;
  47. const aee = await app.$post(`/rcs/${rcsInfo._id}`, { money: money });
  48. if (aee.errcode == '0') {
  49. const att = await app.$post(`/payOrder/${arr.data.data._id}`, { status: '1' });
  50. if (att.errcode == '0') {
  51. wx.showToast({ title: `支付成功`, icon: 'success', duration: 2000 });
  52. that.back()
  53. }
  54. } else { wx.showToast({ title: `${aee.errmsg}`, icon: 'error', duration: 2000 }) }
  55. },
  56. "fail": function (res) {
  57. wx.showToast({ title: `支付未成功`, icon: 'error', duration: 2000 })
  58. that.back()
  59. },
  60. })
  61. }
  62. }
  63. },
  64. /**
  65. * 生命周期函数--监听页面加载
  66. */
  67. onLoad: async function (options) {
  68. const that = this;
  69. that.setData({ id: options.id || '62ff243908c3ce998df1602a' });
  70. //验证规则函数
  71. that.initValidate();
  72. // 监听用户是否登录
  73. await that.watchLogin();
  74. },
  75. // 监听用户是否登录
  76. watchLogin: async function () {
  77. const that = this;
  78. wx.getStorage({
  79. key: 'user',
  80. success: async res => {
  81. that.setData({ user: res.data })
  82. let arr;
  83. if (that.data.id) {
  84. // 关系信息
  85. arr = await app.$get(`/rcs/${that.data.id}`);
  86. if (arr.errcode == '0') that.setData({ rcsInfo: arr.data })
  87. else { wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 }) }
  88. }
  89. },
  90. fail: async 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. onHide: function () {
  108. },
  109. /**
  110. * 生命周期函数--监听页面卸载
  111. */
  112. onUnload: function () {
  113. },
  114. /**
  115. * 页面相关事件处理函数--监听用户下拉动作
  116. */
  117. onPullDownRefresh: function () {
  118. },
  119. /**
  120. * 页面上拉触底事件的处理函数
  121. */
  122. onReachBottom: function () {
  123. },
  124. /**
  125. * 用户点击右上角分享
  126. */
  127. onShareAppMessage: function () {
  128. }
  129. })