upload project source code
This commit is contained in:
285
前端源码/uni-app/utils/payment-examples.ts
Normal file
285
前端源码/uni-app/utils/payment-examples.ts
Normal file
@@ -0,0 +1,285 @@
|
||||
/**
|
||||
* 微信支付集成使用示例
|
||||
* 展示如何在不同场景下使用新的微信支付功能
|
||||
*/
|
||||
import { payWithCallbacks, quickPay, securePay } from './payment';
|
||||
import { quickWechatPay, secureWechatPay, handlePaymentResult } from './wechat-payment';
|
||||
import type { CreateOrderParams } from '@/api/types';
|
||||
|
||||
declare const uni: any;
|
||||
|
||||
/**
|
||||
* 示例1: 财运解析解锁支付
|
||||
*/
|
||||
export const payForWealthAnalysisUnlock = async (reportId: number, price: number) => {
|
||||
const params: CreateOrderParams = {
|
||||
description: '财运解析详细报告解锁',
|
||||
total_amount: Math.round(price * 100), // 转换为分
|
||||
business_type: 'wealth_analysis',
|
||||
business_id: reportId,
|
||||
pay_type: 'jsapi'
|
||||
};
|
||||
|
||||
return await payWithCallbacks(params, {
|
||||
onSuccess: (result) => {
|
||||
console.log('财运解析支付成功:', result);
|
||||
uni.showToast({ title: '解锁成功,正在刷新数据...', icon: 'success' });
|
||||
// 这里可以触发数据刷新或页面跳转
|
||||
},
|
||||
onFail: (result) => {
|
||||
console.error('财运解析支付失败:', result);
|
||||
uni.showModal({
|
||||
title: '支付失败',
|
||||
content: result.msg || '支付失败,请重试',
|
||||
showCancel: false
|
||||
});
|
||||
},
|
||||
onCancel: (result) => {
|
||||
console.log('用户取消财运解析支付:', result);
|
||||
// 用户取消支付,可以不做任何提示
|
||||
}
|
||||
}, true); // 使用安全模式
|
||||
};
|
||||
|
||||
/**
|
||||
* 示例2: 企业起名服务支付
|
||||
*/
|
||||
export const payForCompanyNaming = async (companyInfo: any, serviceLevel: 'basic' | 'premium') => {
|
||||
const prices = {
|
||||
basic: 99,
|
||||
premium: 299
|
||||
};
|
||||
|
||||
const descriptions = {
|
||||
basic: '企业起名基础服务',
|
||||
premium: '企业起名高级服务'
|
||||
};
|
||||
|
||||
const params: CreateOrderParams = {
|
||||
description: descriptions[serviceLevel],
|
||||
total_amount: prices[serviceLevel] * 100, // 转换为分
|
||||
business_type: 'company_naming',
|
||||
business_id: companyInfo.id,
|
||||
pay_type: 'jsapi'
|
||||
};
|
||||
|
||||
try {
|
||||
const result = await secureWechatPay(params);
|
||||
|
||||
handlePaymentResult(result, {
|
||||
onSuccess: (res) => {
|
||||
uni.showToast({ title: '支付成功,正在生成方案...', icon: 'success' });
|
||||
// 跳转到结果页面或刷新数据
|
||||
uni.navigateTo({
|
||||
url: `/pages/naming-result/index?orderId=${res.outTradeNo}`
|
||||
});
|
||||
},
|
||||
onFail: (res) => {
|
||||
uni.showModal({
|
||||
title: '支付失败',
|
||||
content: '支付过程中出现问题,请重试或联系客服',
|
||||
showCancel: true,
|
||||
confirmText: '重试',
|
||||
cancelText: '取消',
|
||||
success: (modalRes) => {
|
||||
if (modalRes.confirm) {
|
||||
// 重新发起支付
|
||||
payForCompanyNaming(companyInfo, serviceLevel);
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
onCancel: () => {
|
||||
// 用户取消支付,返回上一页或停留在当前页
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.error('企业起名支付异常:', error);
|
||||
uni.showToast({ title: '支付异常,请重试', icon: 'none' });
|
||||
return { success: false, msg: '支付异常' };
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 示例3: 个人测名服务支付
|
||||
*/
|
||||
export const payForPersonalNameAnalysis = async (nameInfo: any) => {
|
||||
const params: CreateOrderParams = {
|
||||
description: `个人测名服务-${nameInfo.name}`,
|
||||
total_amount: 1980, // 19.8元
|
||||
business_type: 'personal_name_analysis',
|
||||
business_id: nameInfo.id,
|
||||
pay_type: 'jsapi'
|
||||
};
|
||||
|
||||
// 使用快速支付模式(不验证支付状态)
|
||||
const result = await quickPay(params);
|
||||
|
||||
if (result.success) {
|
||||
// 支付成功后的处理
|
||||
uni.showToast({ title: '支付成功', icon: 'success' });
|
||||
|
||||
// 可以立即显示结果或跳转
|
||||
setTimeout(() => {
|
||||
uni.navigateTo({
|
||||
url: `/pages/name-analysis-result/index?orderId=${result.outTradeNo}`
|
||||
});
|
||||
}, 1500);
|
||||
} else {
|
||||
// 支付失败的处理
|
||||
if (result.msg && !result.msg.includes('取消')) {
|
||||
uni.showToast({ title: result.msg, icon: 'none' });
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
/**
|
||||
* 示例4: 批量支付处理(多个服务一起购买)
|
||||
*/
|
||||
export const payForMultipleServices = async (services: Array<{
|
||||
type: string;
|
||||
description: string;
|
||||
amount: number;
|
||||
businessId: number;
|
||||
}>) => {
|
||||
const totalAmount = services.reduce((sum, service) => sum + service.amount, 0);
|
||||
const description = services.map(s => s.description).join('、');
|
||||
|
||||
const params: CreateOrderParams = {
|
||||
description: `套餐服务:${description}`,
|
||||
total_amount: totalAmount * 100, // 转换为分
|
||||
business_type: 'company_naming', // 主要业务类型
|
||||
business_id: services[0].businessId, // 使用第一个服务的ID
|
||||
pay_type: 'jsapi'
|
||||
};
|
||||
|
||||
return await payWithCallbacks(params, {
|
||||
onSuccess: (result) => {
|
||||
uni.showModal({
|
||||
title: '支付成功',
|
||||
content: '您的套餐服务已购买成功,正在为您生成报告...',
|
||||
showCancel: false,
|
||||
confirmText: '查看详情',
|
||||
success: () => {
|
||||
uni.navigateTo({
|
||||
url: `/pages/service-package-result/index?orderId=${result.outTradeNo}`
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
onFail: (result) => {
|
||||
uni.showModal({
|
||||
title: '支付失败',
|
||||
content: `支付失败:${result.msg}`,
|
||||
showCancel: true,
|
||||
confirmText: '重试',
|
||||
success: (modalRes) => {
|
||||
if (modalRes.confirm) {
|
||||
payForMultipleServices(services);
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
onCancel: () => {
|
||||
console.log('用户取消套餐支付');
|
||||
}
|
||||
}, true); // 使用安全模式验证支付
|
||||
};
|
||||
|
||||
/**
|
||||
* 示例5: 支付状态查询和处理
|
||||
*/
|
||||
export const checkAndHandlePaymentStatus = async (outTradeNo: string) => {
|
||||
try {
|
||||
uni.showLoading({ title: '查询支付状态...' });
|
||||
|
||||
const { queryOrderStatus } = await import('./payment');
|
||||
const orderInfo = await queryOrderStatus(outTradeNo);
|
||||
|
||||
uni.hideLoading();
|
||||
|
||||
switch (orderInfo.status) {
|
||||
case 'paid':
|
||||
uni.showToast({ title: '支付已完成', icon: 'success' });
|
||||
return true;
|
||||
case 'pending':
|
||||
uni.showModal({
|
||||
title: '支付处理中',
|
||||
content: '您的支付正在处理中,请稍后查看',
|
||||
showCancel: false
|
||||
});
|
||||
return false;
|
||||
case 'cancelled':
|
||||
uni.showToast({ title: '订单已取消', icon: 'none' });
|
||||
return false;
|
||||
case 'refunded':
|
||||
uni.showToast({ title: '订单已退款', icon: 'none' });
|
||||
return false;
|
||||
default:
|
||||
uni.showToast({ title: '支付状态异常', icon: 'none' });
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
uni.hideLoading();
|
||||
console.error('查询支付状态失败:', error);
|
||||
uni.showToast({ title: '查询失败,请重试', icon: 'none' });
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 通用支付工具函数
|
||||
*/
|
||||
export const PaymentHelper = {
|
||||
/**
|
||||
* 格式化金额显示(分转元)
|
||||
*/
|
||||
formatAmount: (amountInCents: number): string => {
|
||||
return (amountInCents / 100).toFixed(2);
|
||||
},
|
||||
|
||||
/**
|
||||
* 元转分
|
||||
*/
|
||||
yuanToCents: (yuan: number): number => {
|
||||
return Math.round(yuan * 100);
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取业务类型的中文名称
|
||||
*/
|
||||
getBusinessTypeName: (type: string): string => {
|
||||
const typeNames: Record<string, string> = {
|
||||
'company_naming': '企业起名',
|
||||
'company_renaming': '企业改名',
|
||||
'company_name_analysis': '企业测名',
|
||||
'personal_naming': '个人起名',
|
||||
'personal_renaming': '个人改名',
|
||||
'personal_name_analysis': '个人测名',
|
||||
'wealth_analysis': '财运解析'
|
||||
};
|
||||
return typeNames[type] || '未知服务';
|
||||
},
|
||||
|
||||
/**
|
||||
* 创建标准的支付参数
|
||||
*/
|
||||
createPaymentParams: (
|
||||
businessType: string,
|
||||
businessId: number,
|
||||
description: string,
|
||||
amountInYuan: number
|
||||
): CreateOrderParams => {
|
||||
return {
|
||||
description,
|
||||
total_amount: PaymentHelper.yuanToCents(amountInYuan),
|
||||
business_type: businessType as any,
|
||||
business_id: businessId,
|
||||
pay_type: 'jsapi'
|
||||
};
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user