index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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/?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. console.log(res)
  45. util.Tips({ title: `获取OpenID失败:${data.msg || statusCode}` });
  46. }
  47. },
  48. fail: err => {
  49. util.Tips({ title: `获取OpenID错误:${err?.statusCode ? err?.statusCode : ''} ${err?.data?.error ? err?.data?.error : ''} ${err?.errMsg ? err?.errMsg : ''}` })
  50. },
  51. });
  52. },
  53. createOrder(orderNo, openId) {
  54. wx.request({
  55. url: `https://gbyy91.com/api/web/wechatMiniAppPay?orderNo=${orderNo}&openId=${openId}`,
  56. method: 'POST',
  57. success: res => {
  58. const { statusCode, data } = res;
  59. if (statusCode === 200 && data.code === 200) {
  60. let outputJSON;
  61. try {
  62. outputJSON = JSON.parse(data.data.outputJSON);
  63. } catch (e) {
  64. return util.Tips({ title: `解析响应失败:${data.msg}` });
  65. }
  66. const { returnCode, returnMsg } = outputJSON;
  67. if (returnCode === '0000') {
  68. this.cacheOrder(this.data.orderNo, outputJSON);
  69. this.setData({ wechatData: outputJSON });
  70. this.composeAndPay();
  71. } else {
  72. util.Tips({ title: `创建订单失败:${returnMsg}` });
  73. }
  74. } else {
  75. util.Tips({ title: `下单失败:${data.msg}` });
  76. }
  77. },
  78. fail: err => util.Tips({ title: `下单错误:${err}` }),
  79. });
  80. },
  81. cacheOrder(orderNo, data) {
  82. try {
  83. const key = `order_${orderNo}`;
  84. wx.setStorageSync(key, data);
  85. console.log('[Cache] 保存订单', key, data);
  86. } catch (e) {
  87. console.warn('缓存订单失败', e);
  88. }
  89. },
  90. clearOrderCache(orderNo) {
  91. try {
  92. const key = `order_${orderNo}`;
  93. wx.removeStorageSync(key);
  94. console.log('[Cache] 清除订单缓存', key);
  95. } catch (e) {
  96. console.warn('清除订单缓存失败', e);
  97. }
  98. },
  99. launchAppError(e) {
  100. wx.showToast({ title: '跳回 App 失败', icon: 'none' });
  101. },
  102. composeAndPay() {
  103. const payData = this.data.wechatData.wxJsapiParam;
  104. if (!payData) return util.Tips({ title: '支付参数缺失' });
  105. wx.showLoading({ title: '加载中' });
  106. wx.hideLoading();
  107. try {
  108. wx.requestPayment({
  109. timeStamp: payData.timeStamp,
  110. nonceStr: payData.nonceStr,
  111. package: payData.package,
  112. signType: payData.signType,
  113. paySign: payData.paySign,
  114. success: () => {
  115. this.setData({ msg: '0000', back: true, sandpay: 'success' });
  116. this.clearOrderCache(this.data.orderNo);
  117. },
  118. fail: err => {
  119. console.error('支付失败', err);
  120. this.setData({ msg: '9999', back: true, sandpay: 'fail' });
  121. },
  122. complete: () => {},
  123. });
  124. } catch {
  125. this.setData({ msg: redirectUrl, sandpay: 'fail' });
  126. }
  127. },
  128. });