|
|
@@ -1,211 +1 @@
|
|
|
-/**
|
|
|
- * 支付相关 API
|
|
|
- */
|
|
|
-
|
|
|
-import https from '@/plugins/luch-request_0.0.7/request'
|
|
|
-
|
|
|
-/**
|
|
|
- * 创建支付订单
|
|
|
- * @param {Object} orderData 订单数据
|
|
|
- * @param {number} orderData.amount 支付金额
|
|
|
- * @param {string} orderData.paymentMethod 支付方式
|
|
|
- * @param {string} orderData.orderId 订单ID
|
|
|
- * @param {string} orderData.description 订单描述
|
|
|
- */
|
|
|
-export const createPaymentOrder = (orderData) => {
|
|
|
- return https.post('/api/payment/create', {
|
|
|
- amount: orderData.amount,
|
|
|
- paymentMethod: orderData.paymentMethod,
|
|
|
- orderId: orderData.orderId,
|
|
|
- description: orderData.description,
|
|
|
- timestamp: Date.now()
|
|
|
- }).then(response => {
|
|
|
- // 确保返回格式与原来一致
|
|
|
- if (response.statusCode === 200) {
|
|
|
- return {
|
|
|
- success: true,
|
|
|
- data: response.data.data || response.data,
|
|
|
- message: '订单创建成功'
|
|
|
- };
|
|
|
- } else {
|
|
|
- return {
|
|
|
- success: false,
|
|
|
- message: response.data.message || '订单创建失败'
|
|
|
- };
|
|
|
- }
|
|
|
- }).catch(error => {
|
|
|
- return {
|
|
|
- success: false,
|
|
|
- message: error.message || '网络错误,请重试'
|
|
|
- };
|
|
|
- });
|
|
|
-};
|
|
|
-
|
|
|
-/**
|
|
|
- * 发起支付
|
|
|
- * @param {Object} paymentData 支付数据
|
|
|
- * @param {string} paymentData.orderId 订单ID
|
|
|
- * @param {string} paymentData.paymentMethod 支付方式
|
|
|
- */
|
|
|
-export const requestPayment = (paymentData) => {
|
|
|
- return new Promise((resolve) => {
|
|
|
- // 只支持支付宝支付
|
|
|
- if (paymentData.paymentMethod === 'alipay') {
|
|
|
- alipayPay(paymentData).then(resolve).catch((error) => {
|
|
|
- resolve({
|
|
|
- success: false,
|
|
|
- message: error.message || '支付失败,请重试'
|
|
|
- });
|
|
|
- });
|
|
|
- } else {
|
|
|
- resolve({
|
|
|
- success: false,
|
|
|
- message: '不支持的支付方式'
|
|
|
- });
|
|
|
- }
|
|
|
- });
|
|
|
-};
|
|
|
-
|
|
|
-/**
|
|
|
- * 支付宝支付
|
|
|
- * @param {Object} paymentData 支付数据
|
|
|
- */
|
|
|
-const alipayPay = (paymentData) => {
|
|
|
- return new Promise((resolve) => {
|
|
|
- // 模拟支付宝支付流程
|
|
|
- uni.showLoading({
|
|
|
- title: '正在调起支付宝...'
|
|
|
- });
|
|
|
-
|
|
|
- setTimeout(() => {
|
|
|
- uni.hideLoading();
|
|
|
-
|
|
|
- // 模拟支付结果(实际项目中需要调用真实的支付宝API)
|
|
|
- const success = Math.random() > 0.1; // 90%成功率
|
|
|
-
|
|
|
- if (success) {
|
|
|
- // 支付成功,更新订单状态
|
|
|
- updateOrderStatus(paymentData.orderId, 'paid');
|
|
|
-
|
|
|
- resolve({
|
|
|
- success: true,
|
|
|
- message: '支付成功',
|
|
|
- transactionId: generateTransactionId()
|
|
|
- });
|
|
|
- } else {
|
|
|
- resolve({
|
|
|
- success: false,
|
|
|
- message: '支付失败,请重试'
|
|
|
- });
|
|
|
- }
|
|
|
- }, 2000);
|
|
|
- });
|
|
|
-};
|
|
|
-
|
|
|
-/**
|
|
|
- * 更新订单状态
|
|
|
- * @param {string} orderId 订单ID
|
|
|
- * @param {string} status 订单状态
|
|
|
- */
|
|
|
-const updateOrderStatus = (orderId, status) => {
|
|
|
- https.post('/api/order/update-status', {
|
|
|
- orderId,
|
|
|
- status,
|
|
|
- timestamp: Date.now()
|
|
|
- }).catch((error) => {
|
|
|
- console.error('更新订单状态失败:', error);
|
|
|
- });
|
|
|
-};
|
|
|
-
|
|
|
-/**
|
|
|
- * 查询支付结果
|
|
|
- * @param {string} orderId 订单ID
|
|
|
- */
|
|
|
-export const queryPaymentResult = (orderId) => {
|
|
|
- return https.get('/api/payment/query', { orderId }).then(response => {
|
|
|
- if (response.statusCode === 200) {
|
|
|
- return {
|
|
|
- success: true,
|
|
|
- data: response.data.data || response.data
|
|
|
- };
|
|
|
- } else {
|
|
|
- return {
|
|
|
- success: false,
|
|
|
- message: '查询支付结果失败'
|
|
|
- };
|
|
|
- }
|
|
|
- }).catch(error => {
|
|
|
- return {
|
|
|
- success: false,
|
|
|
- message: error.message
|
|
|
- };
|
|
|
- });
|
|
|
-};
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-/**
|
|
|
- * 生成交易ID
|
|
|
- */
|
|
|
-const generateTransactionId = () => {
|
|
|
- const timestamp = Date.now();
|
|
|
- const random = Math.floor(Math.random() * 10000);
|
|
|
- return `TXN${timestamp}${random}`;
|
|
|
-};
|
|
|
-
|
|
|
-/**
|
|
|
- * 格式化金额
|
|
|
- * @param {number} amount 金额
|
|
|
- * @param {number} decimals 小数位数
|
|
|
- */
|
|
|
-export const formatAmount = (amount, decimals = 2) => {
|
|
|
- return parseFloat(amount).toFixed(decimals);
|
|
|
-};
|
|
|
-
|
|
|
-/**
|
|
|
- * 验证金额格式
|
|
|
- * @param {string} amount 金额字符串
|
|
|
- */
|
|
|
-export const validateAmount = (amount) => {
|
|
|
- const amountRegex = /^\d+(\.\d{1,2})?$/;
|
|
|
- const numAmount = parseFloat(amount);
|
|
|
-
|
|
|
- return amountRegex.test(amount) && numAmount > 0 && numAmount <= 999999.99;
|
|
|
-};
|
|
|
-
|
|
|
-/**
|
|
|
- * 获取支付方式配置
|
|
|
- */
|
|
|
-export const getPaymentMethods = () => {
|
|
|
- return [
|
|
|
- {
|
|
|
- id: 'alipay',
|
|
|
- name: '支付宝',
|
|
|
- description: '安全便捷的支付方式',
|
|
|
- icon: 'alipay',
|
|
|
- enabled: true
|
|
|
- }
|
|
|
- ];
|
|
|
-};
|
|
|
-
|
|
|
-/**
|
|
|
- * 处理扫码结果
|
|
|
- * @param {string} scanResult 扫码结果
|
|
|
- */
|
|
|
-export const parseScanResult = (scanResult) => {
|
|
|
- try {
|
|
|
- // 解析扫码结果,提取订单信息
|
|
|
- const url = new URL(scanResult);
|
|
|
- const params = new URLSearchParams(url.search);
|
|
|
-
|
|
|
- return {
|
|
|
- orderId: params.get('orderId'),
|
|
|
- amount: params.get('amount'),
|
|
|
- description: params.get('description'),
|
|
|
- merchantId: params.get('merchantId')
|
|
|
- };
|
|
|
- } catch (error) {
|
|
|
- console.error('解析扫码结果失败:', error);
|
|
|
- return null;
|
|
|
- }
|
|
|
-};
|
|
|
+import https from '@/plugins/luch-request_0.0.7/request'
|