index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { APPID, USERCODE, USE_PRIVATEKEY, MYpublicKey } from '../../config_sand.js';
  2. import util from '../../utils_sand/util.js';
  3. Page({
  4. data: {
  5. wechatData: null,
  6. orderNo: '',
  7. msg: '',
  8. sandpay: '',
  9. backing: true,
  10. },
  11. onLoad(options) {
  12. const { orderNo, back } = options;
  13. this.setData({ orderNo: orderNo || '' });
  14. if (back) this.setData({ backing: false });
  15. // Try cached order
  16. const cacheKey = `order_${orderNo}`;
  17. const cached = wx.getStorageSync(cacheKey);
  18. if (cached) {
  19. console.log('[Cache] Found cached payment data for', orderNo, cached);
  20. this.setData({ wechatData: cached });
  21. this.composeAndPay();
  22. } else if (orderNo) {
  23. this.initiateLogin(orderNo);
  24. } else {
  25. util.Tips({ title: '无效的订单号' });
  26. }
  27. },
  28. initiateLogin(orderNo) {
  29. wx.login({
  30. success: ({ code }) => this.fetchOpenId(code, orderNo),
  31. fail: () => util.Tips({ title: '微信登录失败' }),
  32. });
  33. },
  34. fetchOpenId(code, orderNo) {
  35. if (!orderNo) return util.Tips({ title: '订单号缺失' });
  36. wx.request({
  37. url: `https://gbyy91.com/api/efps/wechat/miniapp/auth?code=${code}`,
  38. method: 'POST',
  39. success: res => {
  40. const { statusCode, data } = res;
  41. if (statusCode === 200 && data.success) {
  42. this.createOrder(orderNo, data.openid);
  43. } else {
  44. util.Tips({ title: `获取OpenID失败:${data.msg || statusCode}` });
  45. }
  46. },
  47. fail: err => util.Tips({ title: `获取OpenID错误:${err}` }),
  48. });
  49. },
  50. createOrder(orderNo, openId) {
  51. wx.request({
  52. url: `https://gbyy91.com/api/web/wechatMiniAppPay?orderNo=${orderNo}&openId=${openId}`,
  53. method: 'POST',
  54. success: res => {
  55. const { statusCode, data } = res;
  56. if (statusCode === 200 && data.code === 200) {
  57. let outputJSON;
  58. try {
  59. outputJSON = JSON.parse(data.data.outputJSON);
  60. } catch (e) {
  61. return util.Tips({ title: `解析响应失败:${data.msg}` });
  62. }
  63. const { returnCode, returnMsg } = outputJSON;
  64. if (returnCode === '0000') {
  65. this.cacheOrder(this.data.orderNo, outputJSON);
  66. this.setData({ wechatData: outputJSON });
  67. this.composeAndPay();
  68. } else {
  69. util.Tips({ title: `创建订单失败:${returnMsg}` });
  70. }
  71. } else {
  72. util.Tips({ title: `下单失败:${data.msg}` });
  73. }
  74. },
  75. fail: err => util.Tips({ title: `下单错误:${err}` }),
  76. });
  77. },
  78. cacheOrder(orderNo, data) {
  79. try {
  80. const key = `order_${orderNo}`;
  81. wx.setStorageSync(key, data);
  82. console.log('[Cache] 保存订单', key, data);
  83. } catch (e) {
  84. console.warn('缓存订单失败', e);
  85. }
  86. },
  87. clearOrderCache(orderNo) {
  88. try {
  89. const key = `order_${orderNo}`;
  90. wx.removeStorageSync(key);
  91. console.log('[Cache] 清除订单缓存', key);
  92. } catch (e) {
  93. console.warn('清除订单缓存失败', e);
  94. }
  95. },
  96. launchAppError(e) {
  97. wx.showToast({ title: '跳回 App 失败', icon: 'none' });
  98. },
  99. composeAndPay() {
  100. const payData = this.data.wechatData.wxJsapiParam;
  101. if (!payData) return util.Tips({ title: '支付参数缺失' });
  102. wx.showLoading({ title: '加载中' });
  103. wx.hideLoading();
  104. try {
  105. wx.requestPayment({
  106. timeStamp: payData.timeStamp,
  107. nonceStr: payData.nonceStr,
  108. package: payData.package,
  109. signType: payData.signType,
  110. paySign: payData.paySign,
  111. success: () => {
  112. this.setData({ msg: '0000', back: true, sandpay: 'success' });
  113. this.clearOrderCache(this.data.orderNo);
  114. },
  115. fail: err => {
  116. console.error('支付失败', err);
  117. this.setData({ msg: '9999', back: true, sandpay: 'fail' });
  118. },
  119. complete: () => {},
  120. });
  121. } catch {
  122. this.setData({ msg: redirectUrl, sandpay: 'fail' });
  123. }
  124. },
  125. });