useRequestPayment.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import {
  2. showSuccess,
  3. showToast
  4. } from "@/core/app"
  5. import {
  6. ref
  7. } from "vue";
  8. import {
  9. getPayList,
  10. prePayment
  11. } from "@/api/pay.js";
  12. /**
  13. * 支付列表以及拉起支付
  14. * @param {boolean} isGetPaymentList:false 是否立即执行获取列表
  15. */
  16. // 支付列表是全局的
  17. const payList = ref([])
  18. export const useRequestPayment = (isGetList = true) => {
  19. // 获取支付列表
  20. const getPayListData = async (type = "wechat_applet") => {
  21. let res = await getPayList({
  22. type
  23. })
  24. if (res) {
  25. payList.value = res.data.map(item => {
  26. if (item.type == 'wechat_applet') item.type = 'wxpay'
  27. return item
  28. })
  29. return res.data
  30. }
  31. }
  32. if (isGetList) getPayListData()
  33. // 获取支付列表中指定的支付方式
  34. const getPayItem = (type, key) => {
  35. let res
  36. if (payList.value && payList.value.length > 0) res = payList.value.find(item => item.type == type)
  37. if (res[key]) res = res[key]
  38. return res
  39. }
  40. /**
  41. * 预支付,获取拉起支付的数据
  42. * @param {object} {payment_code:'',order_no:''}
  43. */
  44. const getPrePaymentData = async (parmas) => {
  45. let res = await prePayment(parmas)
  46. if (res) {
  47. return res.data
  48. }
  49. }
  50. /**
  51. * @description 拉起微信支付和支付宝支付
  52. * @param {String} provider:'wxpay'|'alipay'
  53. * @param {Object|String} orderInfo:{}|''
  54. */
  55. const toPay = (provider, orderInfo = {}) => {
  56. return new Promise((resolve, reject) => {
  57. // #ifdef APP
  58. uni.requestPayment({
  59. provider,
  60. orderInfo,
  61. success: res => {
  62. return resolve(true)
  63. },
  64. fail: res => {
  65. return reject(false)
  66. },
  67. complete: (e) => {
  68. console.log(e);
  69. }
  70. })
  71. // #endif
  72. // #ifdef MP-WEIXIN
  73. uni.requestPayment({
  74. provider: provider,
  75. timeStamp: orderInfo.timeStamp,
  76. nonceStr: orderInfo.nonceStr,
  77. package: orderInfo.package,
  78. signType: 'MD5',
  79. paySign: orderInfo.paySign,
  80. success: res => resolve(true),
  81. fail: res => reject(false)
  82. })
  83. // #endif
  84. })
  85. }
  86. return {
  87. payList,
  88. getPayListData,
  89. getPayItem,
  90. getPrePaymentData,
  91. toPay
  92. }
  93. }