261 lines
6.9 KiB
TypeScript
261 lines
6.9 KiB
TypeScript
/**
|
|
* 财运解析专用支付工具
|
|
* 处理财运解析相关的支付和解锁功能
|
|
*/
|
|
import { payWithCallbacks } from './payment';
|
|
import type { CreateOrderParams } from '@/api/types';
|
|
|
|
declare const uni: any;
|
|
|
|
/**
|
|
* 财运解析解锁类型
|
|
*/
|
|
export enum WealthUnlockType {
|
|
MONTHLY = 'monthly',
|
|
DAILY = 'daily',
|
|
FULL = 'full'
|
|
}
|
|
|
|
/**
|
|
* 解锁配置接口
|
|
*/
|
|
export interface WealthUnlockConfig {
|
|
reportId: number;
|
|
unlockType: WealthUnlockType;
|
|
title: string;
|
|
price: number;
|
|
description?: string;
|
|
}
|
|
|
|
/**
|
|
* 解锁结果接口
|
|
*/
|
|
export interface WealthUnlockResult {
|
|
success: boolean;
|
|
unlockType: WealthUnlockType;
|
|
reportId: number;
|
|
msg?: string;
|
|
outTradeNo?: string;
|
|
}
|
|
|
|
/**
|
|
* 财运解析月度详批支付解锁
|
|
* @param config 解锁配置
|
|
* @returns Promise<解锁结果>
|
|
*/
|
|
export const payForWealthUnlock = async (config: WealthUnlockConfig): Promise<WealthUnlockResult> => {
|
|
const { reportId, unlockType, title, price, description } = config;
|
|
|
|
// 构建支付参数
|
|
const paymentParams: CreateOrderParams = {
|
|
description: description || `财运解析-${title}`,
|
|
total_amount: Math.round(price * 100), // 转换为分
|
|
business_type: 'wealth_analysis',
|
|
business_id: reportId,
|
|
pay_type: 'jsapi'
|
|
};
|
|
|
|
return new Promise((resolve) => {
|
|
payWithCallbacks(paymentParams, {
|
|
onSuccess: async (result) => {
|
|
uni.showToast({ title: '支付成功,正在解锁...', icon: 'success' });
|
|
|
|
// 保存解锁状态到本地存储
|
|
const storageKey = getUnlockStorageKey(reportId, unlockType);
|
|
uni.setStorageSync(storageKey, true);
|
|
|
|
// 延迟显示解锁成功提示
|
|
setTimeout(() => {
|
|
uni.showToast({
|
|
title: '解锁成功!请查看详细内容',
|
|
icon: 'success',
|
|
duration: 2000
|
|
});
|
|
}, 500);
|
|
|
|
resolve({
|
|
success: true,
|
|
unlockType,
|
|
reportId,
|
|
msg: '解锁成功',
|
|
outTradeNo: result.outTradeNo
|
|
});
|
|
},
|
|
onFail: (result) => {
|
|
uni.showModal({
|
|
title: '支付失败',
|
|
content: result.msg || '支付失败,请重试',
|
|
showCancel: true,
|
|
confirmText: '重试',
|
|
cancelText: '取消',
|
|
success: (res: any) => {
|
|
if (res.confirm) {
|
|
// 重新发起支付
|
|
payForWealthUnlock(config).then(resolve);
|
|
} else {
|
|
resolve({
|
|
success: false,
|
|
unlockType,
|
|
reportId,
|
|
msg: result.msg || '支付失败'
|
|
});
|
|
}
|
|
}
|
|
});
|
|
},
|
|
onCancel: () => {
|
|
console.log('用户取消财运解析支付解锁');
|
|
resolve({
|
|
success: false,
|
|
unlockType,
|
|
reportId,
|
|
msg: '用户取消支付'
|
|
});
|
|
}
|
|
}, true); // 使用安全模式
|
|
});
|
|
};
|
|
|
|
/**
|
|
* 检查财运解析解锁状态
|
|
* @param reportId 报告ID
|
|
* @param unlockType 解锁类型
|
|
* @returns 是否已解锁
|
|
*/
|
|
export const checkWealthUnlockStatus = (reportId: number, unlockType: WealthUnlockType): boolean => {
|
|
const storageKey = getUnlockStorageKey(reportId, unlockType);
|
|
return !!uni.getStorageSync(storageKey);
|
|
};
|
|
|
|
/**
|
|
* 批量检查财运解析解锁状态
|
|
* @param reportId 报告ID
|
|
* @returns 解锁状态对象
|
|
*/
|
|
export const checkAllWealthUnlockStatus = (reportId: number) => {
|
|
return {
|
|
monthly: checkWealthUnlockStatus(reportId, WealthUnlockType.MONTHLY),
|
|
daily: checkWealthUnlockStatus(reportId, WealthUnlockType.DAILY),
|
|
full: checkWealthUnlockStatus(reportId, WealthUnlockType.FULL)
|
|
};
|
|
};
|
|
|
|
/**
|
|
* 清除财运解析解锁状态(用于测试或重置)
|
|
* @param reportId 报告ID
|
|
* @param unlockType 解锁类型,不传则清除所有
|
|
*/
|
|
export const clearWealthUnlockStatus = (reportId: number, unlockType?: WealthUnlockType) => {
|
|
if (unlockType) {
|
|
const storageKey = getUnlockStorageKey(reportId, unlockType);
|
|
uni.removeStorageSync(storageKey);
|
|
} else {
|
|
// 清除所有解锁状态
|
|
Object.values(WealthUnlockType).forEach(type => {
|
|
const storageKey = getUnlockStorageKey(reportId, type);
|
|
uni.removeStorageSync(storageKey);
|
|
});
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 获取解锁状态存储键
|
|
* @param reportId 报告ID
|
|
* @param unlockType 解锁类型
|
|
* @returns 存储键
|
|
*/
|
|
const getUnlockStorageKey = (reportId: number, unlockType: WealthUnlockType): string => {
|
|
return `wealth_unlock_${unlockType}_${reportId}`;
|
|
};
|
|
|
|
/**
|
|
* 财运解析月度详批快速支付
|
|
* @param reportId 报告ID
|
|
* @param price 价格(元)
|
|
* @returns Promise<解锁结果>
|
|
*/
|
|
export const payForMonthlyWealth = async (reportId: number, price: number): Promise<WealthUnlockResult> => {
|
|
return payForWealthUnlock({
|
|
reportId,
|
|
unlockType: WealthUnlockType.MONTHLY,
|
|
title: '12个月运势详批',
|
|
price,
|
|
description: '财运解析-12个月运势详批'
|
|
});
|
|
};
|
|
|
|
/**
|
|
* 财运解析每日运程快速支付
|
|
* @param reportId 报告ID
|
|
* @param price 价格(元)
|
|
* @returns Promise<解锁结果>
|
|
*/
|
|
export const payForDailyWealth = async (reportId: number, price: number): Promise<WealthUnlockResult> => {
|
|
return payForWealthUnlock({
|
|
reportId,
|
|
unlockType: WealthUnlockType.DAILY,
|
|
title: '365天每日吉凶指南',
|
|
price,
|
|
description: '财运解析-365天每日吉凶指南'
|
|
});
|
|
};
|
|
|
|
/**
|
|
* 财运解析完整版支付
|
|
* @param reportId 报告ID
|
|
* @param price 价格(元)
|
|
* @returns Promise<解锁结果>
|
|
*/
|
|
export const payForFullWealth = async (reportId: number, price: number): Promise<WealthUnlockResult> => {
|
|
return payForWealthUnlock({
|
|
reportId,
|
|
unlockType: WealthUnlockType.FULL,
|
|
title: '完整财运解析报告',
|
|
price,
|
|
description: '财运解析-完整版解析报告'
|
|
});
|
|
};
|
|
|
|
/**
|
|
* 财运解析支付工具类
|
|
*/
|
|
export const WealthPaymentHelper = {
|
|
/**
|
|
* 根据解锁类型获取默认标题
|
|
*/
|
|
getDefaultTitle: (unlockType: WealthUnlockType): string => {
|
|
const titles = {
|
|
[WealthUnlockType.MONTHLY]: '12个月运势详批',
|
|
[WealthUnlockType.DAILY]: '365天每日吉凶指南',
|
|
[WealthUnlockType.FULL]: '完整财运解析报告'
|
|
};
|
|
return titles[unlockType];
|
|
},
|
|
|
|
/**
|
|
* 根据解锁类型获取默认描述
|
|
*/
|
|
getDefaultDescription: (unlockType: WealthUnlockType): string => {
|
|
const descriptions = {
|
|
[WealthUnlockType.MONTHLY]: '财运解析-12个月运势详批',
|
|
[WealthUnlockType.DAILY]: '财运解析-365天每日吉凶指南',
|
|
[WealthUnlockType.FULL]: '财运解析-完整版解析报告'
|
|
};
|
|
return descriptions[unlockType];
|
|
},
|
|
|
|
/**
|
|
* 格式化价格显示
|
|
*/
|
|
formatPrice: (price: number): string => {
|
|
return `¥${price.toFixed(2)}`;
|
|
},
|
|
|
|
/**
|
|
* 验证解锁配置
|
|
*/
|
|
validateConfig: (config: WealthUnlockConfig): boolean => {
|
|
return !!(config.reportId && config.unlockType && config.title && config.price > 0);
|
|
}
|
|
};
|