payment.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import request from "@/utils/request";
  2. const PAYMENT_BASE_URL = 'https://662993rq13tg.vicp.fun';
  3. const PAYMENT_TIMEOUT = 15000;
  4. const buildPaymentUrl = (pathOrUrl = '') => {
  5. const deviceInfo = uni.getStorageSync('deviceInfo') || {};
  6. const devId = deviceInfo.deviceId || '';
  7. const sbmc = deviceInfo.model || '';
  8. const hasProtocol = /^https?:\/\//.test(pathOrUrl);
  9. const baseUrl = hasProtocol ? pathOrUrl : `${PAYMENT_BASE_URL}${pathOrUrl}`;
  10. const separator = baseUrl.includes('?') ? '&' : '?';
  11. return `${baseUrl}${separator}devId=${encodeURIComponent(devId)}&sbmc=${encodeURIComponent(sbmc)}`;
  12. };
  13. const formatFormData = (data) => {
  14. if (!data || typeof data !== 'object') return data;
  15. return Object.keys(data)
  16. .map((key) => {
  17. const value = data[key];
  18. if (Array.isArray(value)) {
  19. return value.map((item) => `${encodeURIComponent(key)}=${encodeURIComponent(item)}`).join('&');
  20. }
  21. return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
  22. })
  23. .join('&');
  24. };
  25. const showPaymentLoading = (loadingOptions) => {
  26. if (loadingOptions === false) {
  27. return () => {};
  28. }
  29. const config = typeof loadingOptions === 'object' ? loadingOptions : {};
  30. const shouldShow = loadingOptions === undefined ? true : config.show !== false;
  31. if (!shouldShow) {
  32. return () => {};
  33. }
  34. const title = config.title || (typeof loadingOptions === 'string' ? loadingOptions : '加载中...');
  35. const mask = config.mask ?? true;
  36. const delay = config.delay ?? 300;
  37. const loadingManager = request.loadingManager;
  38. if (loadingManager) {
  39. loadingManager.show({ title, mask, delay });
  40. return () => loadingManager.hide();
  41. }
  42. uni.showLoading({ title, mask });
  43. return () => {
  44. try {
  45. uni.hideLoading();
  46. } catch (error) {
  47. console.warn('hideLoading failed:', error);
  48. }
  49. };
  50. };
  51. const paymentRequest = (pathOrUrl, {
  52. method = 'POST',
  53. data = {},
  54. loading,
  55. formData = false,
  56. timeout = PAYMENT_TIMEOUT,
  57. } = {}) => {
  58. const hideLoading = showPaymentLoading(loading);
  59. const requestMethod = String(method || 'POST').toUpperCase();
  60. let requestData = data;
  61. if (formData) {
  62. requestData = formatFormData(data);
  63. }
  64. const headers = {};
  65. headers['content-type'] = formData
  66. ? 'application/x-www-form-urlencoded'
  67. : 'application/json';
  68. const jsessionId = uni.getStorageSync('JSESSIONID');
  69. if (jsessionId) {
  70. headers['Cookie'] = `JSESSIONID=${jsessionId}`;
  71. }
  72. return new Promise((resolve, reject) => {
  73. uni.request({
  74. url: buildPaymentUrl(pathOrUrl),
  75. method: requestMethod,
  76. data: requestData,
  77. timeout,
  78. header: headers,
  79. success: (res) => {
  80. const resHeader = res.header || {};
  81. const setCookie = resHeader['set-cookie'] || resHeader['Set-Cookie'];
  82. if (setCookie) {
  83. const match = setCookie.match(/JSESSIONID=([^;]+)/);
  84. if (match && match[1]) {
  85. uni.setStorageSync('JSESSIONID', match[1]);
  86. }
  87. }
  88. if (res.statusCode === 200) {
  89. resolve({ data: res.data });
  90. } else {
  91. reject(res);
  92. }
  93. },
  94. fail: (err) => {
  95. uni.showToast({
  96. title: '网络请求失败',
  97. icon: 'none'
  98. });
  99. reject(err);
  100. },
  101. complete: () => {
  102. hideLoading();
  103. }
  104. });
  105. });
  106. };
  107. /**
  108. * 支付相关 API
  109. * 注意:接口地址需要根据后端实际提供的地址进行修改
  110. */
  111. export const paymentApi = {
  112. /**
  113. * 创建充值订单并获取支付参数
  114. * @param {Object} data - 订单信息
  115. * @param {Number} data.amount - 充值金额(单位:元)
  116. * @param {String} data.studentId - 学生ID(可选)
  117. * @param {String} data.cardNo - 卡号(可选)
  118. * @returns {Promise} 返回微信支付所需参数
  119. */
  120. createRechargeOrder(_data) {
  121. return paymentRequest('/order/payOrder', {
  122. method: 'POST',
  123. data: undefined,
  124. loading: {
  125. title: '正在创建订单...'
  126. }
  127. });
  128. },
  129. /**
  130. * 查询订单状态
  131. * @param {String} orderId - 订单ID
  132. * @returns {Promise}
  133. */
  134. queryOrderStatus(outTradeNo) {
  135. const url = outTradeNo
  136. ? `/order/getOrder?outTradeNo=${encodeURIComponent(outTradeNo)}`
  137. : '/order/getOrder'
  138. return paymentRequest(url, {
  139. method: 'GET',
  140. data: undefined,
  141. loading: false
  142. });
  143. },
  144. /**
  145. * 获取充值记录列表
  146. * @param {Object} params - 查询参数
  147. * @returns {Promise}
  148. */
  149. getRechargeHistory(params) {
  150. return paymentRequest('/api/payment/recharge/history', {
  151. data: params,
  152. loading: {
  153. title: '加载中...'
  154. }
  155. });
  156. }
  157. };