index.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <template>
  2. <view class="content">
  3. <web-view :src="durl" :update-title="false" @message="handleMessage"></web-view>
  4. </view>
  5. </template>
  6. <script>
  7. const wxMerchantTransfer = uni.requireNativePlugin('wxMerchantTransfer');
  8. export default {
  9. data() {
  10. return {
  11. durl:"",
  12. pageTitle: "" // 存储初始标题
  13. }
  14. },
  15. onLoad(options) {
  16. this.durl = options.url;
  17. if(options.name){
  18. this.pageTitle = options.name; // 保存标题到变量
  19. uni.setNavigationBarTitle({ title: this.pageTitle });
  20. }
  21. },
  22. onShow() {
  23. // 每次页面显示时重置标题
  24. if (this.pageTitle) {
  25. uni.setNavigationBarTitle({ title: this.pageTitle });
  26. }
  27. },
  28. methods: {
  29. handleMessage(event) {
  30. const info = event.detail.data[0]
  31. // #ifdef APP-PLUS
  32. if(info.withdrawal==1) {
  33. this.confirmReceipt(info.withdrawalInfo)
  34. return
  35. }
  36. if(info&&info.pagesUrl) {
  37. uni.navigateTo({
  38. url: info.pagesUrl
  39. })
  40. }
  41. // #endif
  42. },
  43. // 确认收款(拉起微信收款确认页面)
  44. confirmReceipt(item) {
  45. if (!item) {
  46. uni.showToast({
  47. title: '信息不完整',
  48. icon: 'none'
  49. });
  50. return;
  51. }
  52. const appId = item.appId;
  53. const mchId = item.mchId;
  54. const packageValue = item.packageInfo;
  55. if (!appId || !mchId || !packageValue) {
  56. uni.showToast({
  57. title: '缺少必要参数,无法确认收款',
  58. icon: 'none'
  59. });
  60. return;
  61. }
  62. // #ifdef APP-PLUS
  63. const params = {
  64. appId: String(appId),
  65. mchId: String(mchId),
  66. package: String(packageValue)
  67. };
  68. if (typeof wxMerchantTransfer !== 'undefined' && typeof wxMerchantTransfer.open === 'function') {
  69. console.log('拉起微信收款确认页面,参数:', params);
  70. wxMerchantTransfer.open(params, (res) => {
  71. console.log('确认收款回调结果:', res);
  72. if (res.code === 0) {
  73. // 确认收款成功,重新查询订单状态
  74. console.warn('用户拉起微信');
  75. // if (item.outBatchNo) {
  76. // setTimeout(() => {
  77. // uni.navigateTo({
  78. // url: '/pages/user/wallet/success?orderCode=' + item.outBatchNo
  79. // });
  80. // }, 1000);
  81. // }
  82. } else if (res.code === -3) {
  83. console.warn('用户微信版本过低');
  84. } else {
  85. uni.showModal({
  86. title: '确认收款失败',
  87. content: res.message || '请稍后重试',
  88. showCancel: false
  89. });
  90. }
  91. });
  92. } else {
  93. console.error('wxMerchantTransfer 插件未正确加载');
  94. uni.showModal({
  95. title: '功能不可用',
  96. content: '微信转账功能未正确初始化,请检查插件配置并重新编译应用。',
  97. showCancel: false
  98. });
  99. }
  100. // #endif
  101. // #ifndef APP-PLUS
  102. uni.showToast({
  103. icon: 'none',
  104. title: '此功能仅在 APP 中可用',
  105. });
  106. // #endif
  107. }
  108. }
  109. }
  110. </script>
  111. <style scoped lang="scss">
  112. page{
  113. height: 100%;
  114. background: #ffffff;
  115. }
  116. .content{
  117. width: 100%;
  118. height: 100%;
  119. display: flex;
  120. align-items: center;
  121. justify-content: center;
  122. }
  123. </style>