| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- /**
- * 跨平台支付工具类
- * 支持唤起微信/支付宝并打开通联小程序支付
- */
- 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
|