add.js 5.4 KB

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