+
@@ -58,17 +71,17 @@ import { useRoute, useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Check, SuccessFilled, CircleCloseFilled, Loading } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'
-import api from '@/utils/api'
+import api, { paymentAPI } from '@/utils/api'
const route = useRoute()
const router = useRouter()
const userStore = useUserStore()
// 响应式数据
-const selectedMethod = ref('wechat')
+const selectedMethod = ref('wechat_h5') // 默认选择微信支付
const paymentLoading = ref(false)
const paymentStatus = ref('')
-const paymentAmount = ref(99) // 注册费用
+const paymentAmount = ref(399) // 注册费用
const userInfo = reactive({
userId: '',
})
@@ -90,7 +103,8 @@ const getUserInfo = async () => {
// 处理支付
const handlePayment = async () => {
- if (!selectedMethod.value) {
+ // 验证支付方式
+ if (!['wechat_h5', 'alipay_wap'].includes(selectedMethod.value)) {
ElMessage.error('请选择支付方式')
return
}
@@ -113,25 +127,28 @@ const handlePayment = async () => {
throw new Error('无法获取用户信息,请重新登录')
}
- // 创建支付订单
- const response = await api.post('/wechat-pay/create-registration-order', {
+ // 创建统一支付订单
+ const response = await paymentAPI.createOrder({
userId: userId,
amount: paymentAmount.value,
- description: '用户注册激活费用'
- }, token ? {
- headers: {
- Authorization: `Bearer ${token}`
- }
- } : {})
+ description: '用户注册激活费用',
+ paymentMethod: selectedMethod.value
+ })
if (response.data.success) {
- const { h5_url, out_trade_no } = response.data.data
+ const { outTradeNo, payUrl } = response.data.data
- // 跳转到微信H5支付页面
- window.location.href = h5_url
+ // 根据支付方式跳转到对应支付页面
+ if (selectedMethod.value === 'wechat_h5') {
+ // 微信支付 - 跳转到支付页面
+ window.location.href = payUrl
+ } else if (selectedMethod.value === 'alipay_wap') {
+ // 支付宝支付 - 跳转到支付页面
+ window.location.href = payUrl
+ }
// 开始轮询支付状态
- startPaymentStatusPolling(out_trade_no, token)
+ startPaymentStatusPolling(outTradeNo, token)
} else {
throw new Error(response.data.message || '创建支付订单失败')
}
@@ -148,16 +165,22 @@ const handlePayment = async () => {
const startPaymentStatusPolling = (outTradeNo, token) => {
const pollInterval = setInterval(async () => {
try {
- const response = await api.get(`/wechat-pay/query-status/${outTradeNo}`, token ? {
- headers: {
- Authorization: `Bearer ${token}`
- }
- } : {})
-
- if (response.data.success) {
- const status = response.data.data.trade_state
+ const response = await paymentAPI.queryStatus(outTradeNo, token)
- if (status === 'SUCCESS') {
+ if (response.data.success) {
+ // 处理支付状态(微信和支付宝)
+ const data = response.data.data;
+ let isPaid = false;
+
+ if (selectedMethod.value === 'wechat_h5') {
+ // 微信支付状态判断
+ isPaid = data.trade_state === 'SUCCESS';
+ } else if (selectedMethod.value === 'alipay_wap') {
+ // 支付宝支付状态判断
+ isPaid = data.trade_status === 'TRADE_SUCCESS';
+ }
+
+ if (isPaid) {
clearInterval(pollInterval)
paymentStatus.value = 'success'
ElMessage.success('支付成功!账户已激活')
@@ -174,10 +197,23 @@ const startPaymentStatusPolling = (outTradeNo, token) => {
router.push('/login')
}, 2000)
}
- } else if (status === 'CLOSED' || status === 'REVOKED' || status === 'PAYERROR') {
- clearInterval(pollInterval)
- paymentStatus.value = 'failed'
- ElMessage.error('支付失败,请重试')
+ } else {
+ // 检查支付失败状态
+ let isFailed = false;
+
+ if (selectedMethod.value === 'wechat_h5') {
+ // 微信支付失败状态
+ isFailed = data.trade_state === 'CLOSED' || data.trade_state === 'REVOKED' || data.trade_state === 'PAYERROR';
+ } else if (selectedMethod.value === 'alipay_wap') {
+ // 支付宝支付失败状态
+ isFailed = data.trade_status === 'TRADE_CLOSED';
+ }
+
+ if (isFailed) {
+ clearInterval(pollInterval)
+ paymentStatus.value = 'failed'
+ ElMessage.error('支付失败,请重试')
+ }
}
}
} catch (error) {
diff --git a/src/views/Register.vue b/src/views/Register.vue
index e5fc346..a9330ef 100644
--- a/src/views/Register.vue
+++ b/src/views/Register.vue
@@ -467,10 +467,6 @@ const handleRegister = async () => {
// 跳转到支付页面
router.push({
path: '/payment',
- query: {
- token: result.token,
- userId: result.user.id
- }
})
} else {
ElMessage.success('注册成功!请登录')
diff --git a/vite.config.js b/vite.config.js
index cd1c385..4ce0d23 100644
--- a/vite.config.js
+++ b/vite.config.js
@@ -11,6 +11,7 @@ export default defineConfig({
}
},
server: {
+ host: '0.0.0.0', // 添加这一行
port: 5173,
proxy: {
'/api': {