/** * 跨平台支付工具类 * 支持唤起微信/支付宝并打开通联小程序支付 */ class PaymentUtils { // 微信URL Scheme前缀 static WECHAT_SCHEME_PREFIX = 'weixin://dl/business/?t=' // 支付宝URL Scheme前缀 static ALIPAY_SCHEME_PREFIX = 'alipay://platformapi/startapp?appId=' /** * 构建微信跳转URL Scheme */ static buildWechatURLScheme(config) { const { appId, // 通联小程序AppID path = '', // 小程序页面路径 type = 'release', // 跳转类型 extraData = {} // 额外参数 } = config let urlScheme = this.WECHAT_SCHEME_PREFIX + Date.now() urlScheme += '&appid=' + encodeURIComponent(appId) if (path) { urlScheme += '&path=' + encodeURIComponent(path) } if (type) { urlScheme += '&miniProgramType=' + type } if (Object.keys(extraData).length > 0) { urlScheme += '&query=' + encodeURIComponent(JSON.stringify(extraData)) } return urlScheme } /** * 构建支付宝跳转URL Scheme */ static buildAlipayURLScheme(config) { const { appId, page = '', query = {} } = config let urlScheme = this.ALIPAY_SCHEME_PREFIX + appId if (page) { urlScheme += '&page=' + encodeURIComponent(page) } if (Object.keys(query).length > 0) { urlScheme += '&query=' + encodeURIComponent(JSON.stringify(query)) } return urlScheme } /** * 执行跳转 */ static launchApp(urlScheme) { return new Promise((resolve, reject) => { // #ifdef APP-PLUS plus.runtime.openURL(urlScheme, (success) => { if (success) { resolve({ success: true }) } else { reject(new Error('唤起应用失败')) } }) // #endif }) } /** * 检查应用是否安装 */ static checkAppInstalled(appType) { return new Promise((resolve) => { // #ifdef APP-PLUS if (appType === 'wechat') { if (plus.runtime.isApplicationExist({ pname: 'com.tencent.mm', action: 'weixin://' })) { resolve(true) } else { resolve(false) } } // #endif }) } } export default PaymentUtils