Selaa lähdekoodia

支付宝h5支付

mht 1 kuukausi sitten
vanhempi
commit
fe23be0d08
4 muutettua tiedostoa jossa 40 lisäystä ja 16 poistoa
  1. 14 0
      src/api/pay.js
  2. 1 1
      src/utils/config.js
  3. 5 2
      src/utils/request.js
  4. 20 13
      src/views/Recharge.vue

+ 14 - 0
src/api/pay.js

@@ -28,6 +28,20 @@ export function aliPayCoin(data, platform) {
   })
 }
 
+// 支付宝H5支付接口
+export function aliPayH5(data, platform) {
+  return request({
+    url: '/pay/alipay/h5',
+    method: 'post',
+    headers: {
+      'Content-Type': 'application/json',
+      'platform': platform
+    },
+    responseType: 'text',  // 指定响应类型为文本
+    data
+  })
+}
+
 export const ahtuCodeApi = (code) =>{
   return requestPay({
     url : `/efps/wechat/officialaccount/auth?code=${code}&state=1`,

+ 1 - 1
src/utils/config.js

@@ -3,7 +3,7 @@ const isDev = process.env.NODE_ENV === 'development'
 
 // API基础URL配置
 export const API_BASE_URL = isDev 
-  ? 'http://localhost:8082'
+  ? 'https://gbyy91.com/api'
   : 'https://gbyy91.com/api' // 生产环境 
 
 // 支付方式ID

+ 5 - 2
src/utils/request.js

@@ -24,14 +24,17 @@ service.interceptors.request.use(
 // 响应拦截器
 service.interceptors.response.use(
   response => {
+    console.log(response)
     const res = response.data
     // 根据后端约定的状态码判断请求是否成功
     if (res.code !== 200) {
-      Toast.fail(res.message || '请求失败')
-      return Promise.reject(new Error(res.message || '请求失败'))
+      //Toast.fail(res.message || '请求失败')
+     // return Promise.reject(new Error(res.message || '请求失败'))
+     return res
     } else {
       return res
     }
+   // return res
   },
   error => {
     console.log('请求错误:', error)

+ 20 - 13
src/views/Recharge.vue

@@ -114,7 +114,7 @@
 // eslint-disable-next-line no-undef
 import { getProductList } from '@/api/product'
 import { getUserInfo } from '@/api/user'
-import { aliPayCoin } from '@/api/pay'
+import { aliPayCoin, aliPayH5 } from '@/api/pay'
 import { getDevicePayWayId, getPlatformType } from '@/utils/config'
 
 export default {
@@ -290,22 +290,29 @@ export default {
           productId: product.id
         }
         
-        // 调用支付宝支付接口
-        const res = await aliPayCoin(payData, platform)
+        // 调用支付宝H5支付接口
+        const res = await aliPayH5(payData, platform)
         this.$toast.clear()
         this.isLoading = false
         
-        // 处理支付结果
-        if (res.code === 200) {
-          // 支付宝支付成功,通常会返回一个支付链接或表单
-          if (res.data && res.data.payUrl) {
-            // 跳转到支付宝支付页面
-            window.location.href = res.data.payUrl
-          } else {
-            this.$toast.fail('获取支付链接失败')
-          }
+        // 处理HTML表单响应
+        if (typeof res === 'string' && res.includes('<form')) {
+          // 创建一个div来放置表单
+          const div = document.createElement('div')
+          div.innerHTML = res
+          document.body.appendChild(div)
+          
+          // 自动提交表单
+          setTimeout(() => {
+            const form = div.querySelector('form')
+            if (form) {
+              form.submit()
+            } else {
+              this.$toast.fail('支付表单加载失败')
+            }
+          }, 100)
         } else {
-          this.$toast.fail(res.message || '支付请求失败')
+          this.$toast.fail('获取支付表单失败')
         }
       } catch (error) {
         this.$toast.clear()