This commit is contained in:
Mrx
2026-06-06 17:03:15 +08:00
parent b7e0abb898
commit c57d37f601
9 changed files with 166 additions and 29 deletions

4
.env
View File

@@ -11,8 +11,8 @@
# ──────────────────────────────────────────────
# 想用线上接口时取消下面这行注释:
VITE_API_BASE_URL=https://www.quannengcha.com/api/v1
# VITE_API_BASE_URL=https://www.quannengcha.com/api/v1
# 想用本地接口时注释掉上面那行,取消下面这行注释:
# VITE_API_BASE_URL=http://127.0.0.1:8888/api/v1
VITE_API_BASE_URL=http://127.0.0.1:8888/api/v1

View File

@@ -52,7 +52,7 @@ export default defineManifestConfig({
'quickapp': {},
/* 小程序特有相关 */
'mp-weixin': {
appid: '',
appid: 'wx5bacc94add2da981',
setting: {
urlCheck: false,
},

View File

@@ -13,7 +13,7 @@
},
"compileType": "miniprogram",
"libVersion": "",
"appid": "touristappid",
"appid": "wx5bacc94add2da981",
"projectname": "qnc-uniapp",
"miniprogramRoot": "dist/dev/mp-weixin/",
"condition": {}

View File

@@ -5,7 +5,7 @@ import { QNC_API } from '@/config/api'
const AUTH_FALLBACK_PAGE = '/pages/login'
const TOKEN_KEY = 'token'
const SILENT_TOAST_CODES = new Set([200002, 200003, 200004, 100009])
const SILENT_TOAST_CODES = new Set([200002, 200003, 200004, 100009, 100007])
let loadingCount = 0

View File

@@ -5,3 +5,9 @@ export async function postPayPayment(body, requestConfig) {
const res = await http.post('/pay/payment', body, requestConfig)
return res.data
}
/** 查询支付状态xpay 轮询兜底) */
export async function postPayCheck(body, requestConfig) {
const res = await http.post('/pay/check', body, requestConfig)
return res.data
}

View File

@@ -48,7 +48,7 @@
},
"quickapp": {},
"mp-weixin": {
"appid": "",
"appid": "wx5bacc94add2da981",
"setting": {
"urlCheck": false
},

View File

@@ -1,7 +1,8 @@
<script setup>
import { onLoad, onReady } from '@dcloudio/uni-app'
import { computed, onUnmounted, ref } from 'vue'
import { getProductByEn, getUserDetail, postAuthSendSmsQuery, postPayPayment, postQueryService, postUploadImage } from '@/api'
import { getProductByEn, getUserDetail, postAuthSendSmsQuery, postPayCheck, postPayPayment, postQueryService, postUploadImage } from '@/api'
import { invokeWxVirtualPayment, prepareNativePayUI } from '@/utils/xpayPay'
import { productHasSmsCode, useInquireForm } from '@/composables/useInquireForm'
import { useBindMobile } from '@/composables/useBindMobile'
import { aesEncrypt, QUERY_PAYLOAD_AES_HEX_KEY } from '@/utils/crypto.js'
@@ -471,11 +472,20 @@ async function onConfirmPay() {
paying.value = true
try {
const payRes = await postPayPayment({
const payBody = {
id: queryId.value,
pay_method: 'wechat',
pay_type: 'query',
})
}
const payReqExtra = { skipLoading: true }
let payRes = await postPayPayment(payBody, payReqExtra)
if (payRes?.code === 100007) {
const { tryWxMiniProgramAuth } = await import('@/utils/wxMiniAuth')
const reloginOk = await tryWxMiniProgramAuth({ silent: true })
if (reloginOk)
payRes = await postPayPayment(payBody, payReqExtra)
}
if (payRes?.code !== 200 || !payRes.data) {
return
@@ -500,6 +510,50 @@ async function onConfirmPay() {
return
}
function afterPaySuccess() {
uni.showToast({ title: '支付成功', icon: 'none' })
showPaySheet.value = false
try {
if (orderNo)
uni.setStorageSync('lastPaidOrderNo', orderNo)
}
catch {
/* ignore */
}
uni.redirectTo({
url: `/pages/report/detail?orderNo=${encodeURIComponent(orderNo)}`,
})
}
// #ifdef MP-WEIXIN
if (prepay.provider === 'xpay') {
// 先关掉自定义支付蒙层和 loading避免挡住微信原生虚拟支付弹窗
showPaySheet.value = false
prepareNativePayUI()
await new Promise(r => setTimeout(r, 120))
await invokeWxVirtualPayment(prepay)
for (let i = 0; i < 8; i++) {
const checkRes = await postPayCheck({ order_no: orderNo }, { skipLoading: true })
const status = checkRes?.data?.status
if (status === 'paid') {
afterPaySuccess()
return
}
if (status === 'closed' || status === 'failed') {
uni.showToast({ title: '订单已关闭', icon: 'none' })
return
}
await new Promise(r => setTimeout(r, 1500))
}
uni.showToast({ title: '确认中,请稍后在报告中查看', icon: 'none' })
return
}
// #endif
showPaySheet.value = false
prepareNativePayUI()
await new Promise((resolve, reject) => {
uni.requestPayment({
provider: 'wxpay',
@@ -509,18 +563,7 @@ async function onConfirmPay() {
signType: String(prepay.signType ?? prepay.sign_type ?? 'RSA'),
paySign: String(prepay.paySign ?? prepay.pay_sign ?? ''),
success() {
uni.showToast({ title: '支付成功', icon: 'none' })
showPaySheet.value = false
try {
if (orderNo)
uni.setStorageSync('lastPaidOrderNo', orderNo)
}
catch {
/* ignore */
}
uni.redirectTo({
url: `/pages/report/detail?orderNo=${encodeURIComponent(orderNo)}`,
})
afterPaySuccess()
resolve()
},
fail(err) {
@@ -531,8 +574,10 @@ async function onConfirmPay() {
})
})
}
catch {
/* Toast 已提示 */
catch (e) {
const msg = e?.errMsg || e?.message || ''
if (msg && !/cancel/i.test(msg))
uni.showToast({ title: String(msg).replace('requestVirtualPayment:fail ', ''), icon: 'none' })
}
finally {
paying.value = false

View File

@@ -1,8 +1,47 @@
import { postUserWxMiniAuth } from '@/api/user'
import { saveAuthSession } from '@/utils/session'
/** 微信开发者工具在游客模式/未登录等场景下返回的占位 code不能用于 code2Session */
const WX_MOCK_LOGIN_CODE = 'the code is a mock one'
function isValidWxLoginCode(code) {
return typeof code === 'string'
&& code.length > 0
&& code !== WX_MOCK_LOGIN_CODE
&& !/\s/.test(code)
}
/** 读取当前运行环境的小程序 AppID以开发者工具/真机实际生效为准) */
function getRuntimeWxAppId() {
// #ifdef MP-WEIXIN
try {
return wx.getAccountInfoSync?.()?.miniProgram?.appId || ''
}
catch {
return ''
}
// #endif
// #ifndef MP-WEIXIN
return ''
// #endif
}
function getWxLoginFailureTip(code) {
const runtimeAppId = getRuntimeWxAppId()
if (runtimeAppId === 'touristappid') {
return '微信开发者工具处于游客模式,请切换为真实 AppID'
}
if (code === WX_MOCK_LOGIN_CODE) {
if (!runtimeAppId) {
return '请用微信开发者工具打开 dist/dev/mp-weixin 目录'
}
return '开发者工具未返回真实 code请确认已登录微信且非游客模式'
}
return '未获取到微信登录凭证'
}
/**
* 微信小程序:`uni.login` → `/user/wxMiniAuth` → 写入 token
* 微信小程序:`wx.login` → `/user/wxMiniAuth` → 写入 token
* @param {{ silent?: boolean }} [opts] silent=true不触发全局 loading、不弹业务/网络失败 Toast适合 App 启动静默登录)
* @returns {Promise<boolean>} 是否登录成功
*/
@@ -13,15 +52,26 @@ export async function tryWxMiniProgramAuth(opts = {}) {
: undefined
const loginRes = await new Promise((resolve, reject) => {
uni.login({
provider: 'weixin',
// #ifdef MP-WEIXIN
wx.login({
success: resolve,
fail: reject,
})
// #endif
// #ifndef MP-WEIXIN
reject(new Error('仅支持微信小程序登录'))
// #endif
})
if (!loginRes?.code) {
if (!silent)
uni.showToast({ title: '未获取到微信登录凭证', icon: 'none' })
if (!isValidWxLoginCode(loginRes?.code)) {
const runtimeAppId = getRuntimeWxAppId()
console.warn('[wxMiniAuth] 无效登录凭证', {
code: loginRes?.code,
runtimeAppId,
errMsg: loginRes?.errMsg,
})
if (!silent) {
uni.showToast({ title: getWxLoginFailureTip(loginRes?.code), icon: 'none', duration: 3500 })
}
return false
}

36
src/utils/xpayPay.js Normal file
View File

@@ -0,0 +1,36 @@
/**
* 调起微信小程序虚拟支付xpay
* @param {{ mode: string, signData: string, paySig: string, signature: string }} prepay
*/
export function invokeWxVirtualPayment(prepay) {
return new Promise((resolve, reject) => {
// #ifdef MP-WEIXIN
if (typeof wx?.requestVirtualPayment !== 'function') {
reject(new Error('当前环境不支持虚拟支付,请使用微信真机预览'))
return
}
wx.requestVirtualPayment({
mode: prepay.mode,
signData: prepay.signData,
paySig: prepay.paySig,
signature: prepay.signature,
success: resolve,
fail: reject,
})
console.log('invokeWxVirtualPayment', prepay)
// #endif
// #ifndef MP-WEIXIN
reject(new Error('仅支持微信小程序虚拟支付'))
// #endif
})
}
/** 关闭可能遮挡原生支付弹窗的 loading / 自定义蒙层 */
export function prepareNativePayUI() {
try {
uni.hideLoading()
}
catch {
/* ignore */
}
}