payment.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * 跨平台支付工具类
  3. * 支持唤起微信/支付宝并打开通联小程序支付
  4. */
  5. class PaymentUtils {
  6. // 微信URL Scheme前缀
  7. static WECHAT_SCHEME_PREFIX = 'weixin://dl/business/?t='
  8. // 支付宝URL Scheme前缀
  9. static ALIPAY_SCHEME_PREFIX = 'alipay://platformapi/startapp?appId='
  10. /**
  11. * 构建微信跳转URL Scheme
  12. */
  13. static buildWechatURLScheme(config) {
  14. const {
  15. appId, // 通联小程序AppID
  16. path = '', // 小程序页面路径
  17. type = 'release', // 跳转类型
  18. extraData = {} // 额外参数
  19. } = config
  20. let urlScheme = this.WECHAT_SCHEME_PREFIX + Date.now()
  21. urlScheme += '&appid=' + encodeURIComponent(appId)
  22. if (path) {
  23. urlScheme += '&path=' + encodeURIComponent(path)
  24. }
  25. if (type) {
  26. urlScheme += '&miniProgramType=' + type
  27. }
  28. if (Object.keys(extraData).length > 0) {
  29. urlScheme += '&query=' + encodeURIComponent(JSON.stringify(extraData))
  30. }
  31. return urlScheme
  32. }
  33. /**
  34. * 构建支付宝跳转URL Scheme
  35. */
  36. static buildAlipayURLScheme(config) {
  37. const {
  38. appId,
  39. page = '',
  40. query = {}
  41. } = config
  42. let urlScheme = this.ALIPAY_SCHEME_PREFIX + appId
  43. if (page) {
  44. urlScheme += '&page=' + encodeURIComponent(page)
  45. }
  46. if (Object.keys(query).length > 0) {
  47. urlScheme += '&query=' + encodeURIComponent(JSON.stringify(query))
  48. }
  49. return urlScheme
  50. }
  51. /**
  52. * 执行跳转
  53. */
  54. static launchApp(urlScheme) {
  55. return new Promise((resolve, reject) => {
  56. // #ifdef APP-PLUS
  57. plus.runtime.openURL(urlScheme, (success) => {
  58. if (success) {
  59. resolve({
  60. success: true
  61. })
  62. } else {
  63. reject(new Error('唤起应用失败'))
  64. }
  65. })
  66. // #endif
  67. })
  68. }
  69. /**
  70. * 检查应用是否安装
  71. */
  72. static checkAppInstalled(appType) {
  73. return new Promise((resolve) => {
  74. // #ifdef APP-PLUS
  75. if (appType === 'wechat') {
  76. if (plus.runtime.isApplicationExist({
  77. pname: 'com.tencent.mm',
  78. action: 'weixin://'
  79. })) {
  80. resolve(true)
  81. } else {
  82. resolve(false)
  83. }
  84. }
  85. // #endif
  86. })
  87. }
  88. }
  89. export default PaymentUtils