f
This commit is contained in:
4
.env
4
.env
@@ -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
|
||||||
|
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ export default defineManifestConfig({
|
|||||||
'quickapp': {},
|
'quickapp': {},
|
||||||
/* 小程序特有相关 */
|
/* 小程序特有相关 */
|
||||||
'mp-weixin': {
|
'mp-weixin': {
|
||||||
appid: '',
|
appid: 'wx5bacc94add2da981',
|
||||||
setting: {
|
setting: {
|
||||||
urlCheck: false,
|
urlCheck: false,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
},
|
},
|
||||||
"compileType": "miniprogram",
|
"compileType": "miniprogram",
|
||||||
"libVersion": "",
|
"libVersion": "",
|
||||||
"appid": "touristappid",
|
"appid": "wx5bacc94add2da981",
|
||||||
"projectname": "qnc-uniapp",
|
"projectname": "qnc-uniapp",
|
||||||
"miniprogramRoot": "dist/dev/mp-weixin/",
|
"miniprogramRoot": "dist/dev/mp-weixin/",
|
||||||
"condition": {}
|
"condition": {}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { QNC_API } from '@/config/api'
|
|||||||
const AUTH_FALLBACK_PAGE = '/pages/login'
|
const AUTH_FALLBACK_PAGE = '/pages/login'
|
||||||
|
|
||||||
const TOKEN_KEY = 'token'
|
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
|
let loadingCount = 0
|
||||||
|
|
||||||
|
|||||||
@@ -5,3 +5,9 @@ export async function postPayPayment(body, requestConfig) {
|
|||||||
const res = await http.post('/pay/payment', body, requestConfig)
|
const res = await http.post('/pay/payment', body, requestConfig)
|
||||||
return res.data
|
return res.data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 查询支付状态(xpay 轮询兜底) */
|
||||||
|
export async function postPayCheck(body, requestConfig) {
|
||||||
|
const res = await http.post('/pay/check', body, requestConfig)
|
||||||
|
return res.data
|
||||||
|
}
|
||||||
|
|||||||
@@ -48,7 +48,7 @@
|
|||||||
},
|
},
|
||||||
"quickapp": {},
|
"quickapp": {},
|
||||||
"mp-weixin": {
|
"mp-weixin": {
|
||||||
"appid": "",
|
"appid": "wx5bacc94add2da981",
|
||||||
"setting": {
|
"setting": {
|
||||||
"urlCheck": false
|
"urlCheck": false
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { onLoad, onReady } from '@dcloudio/uni-app'
|
import { onLoad, onReady } from '@dcloudio/uni-app'
|
||||||
import { computed, onUnmounted, ref } from 'vue'
|
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 { productHasSmsCode, useInquireForm } from '@/composables/useInquireForm'
|
||||||
import { useBindMobile } from '@/composables/useBindMobile'
|
import { useBindMobile } from '@/composables/useBindMobile'
|
||||||
import { aesEncrypt, QUERY_PAYLOAD_AES_HEX_KEY } from '@/utils/crypto.js'
|
import { aesEncrypt, QUERY_PAYLOAD_AES_HEX_KEY } from '@/utils/crypto.js'
|
||||||
@@ -471,11 +472,20 @@ async function onConfirmPay() {
|
|||||||
|
|
||||||
paying.value = true
|
paying.value = true
|
||||||
try {
|
try {
|
||||||
const payRes = await postPayPayment({
|
const payBody = {
|
||||||
id: queryId.value,
|
id: queryId.value,
|
||||||
pay_method: 'wechat',
|
pay_method: 'wechat',
|
||||||
pay_type: 'query',
|
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) {
|
if (payRes?.code !== 200 || !payRes.data) {
|
||||||
return
|
return
|
||||||
@@ -500,15 +510,7 @@ async function onConfirmPay() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
await new Promise((resolve, reject) => {
|
function afterPaySuccess() {
|
||||||
uni.requestPayment({
|
|
||||||
provider: 'wxpay',
|
|
||||||
timeStamp: String(prepay.timeStamp ?? prepay.timestamp ?? ''),
|
|
||||||
nonceStr: String(prepay.nonceStr ?? prepay.nonce_str ?? ''),
|
|
||||||
package: String(prepay.package ?? ''),
|
|
||||||
signType: String(prepay.signType ?? prepay.sign_type ?? 'RSA'),
|
|
||||||
paySign: String(prepay.paySign ?? prepay.pay_sign ?? ''),
|
|
||||||
success() {
|
|
||||||
uni.showToast({ title: '支付成功', icon: 'none' })
|
uni.showToast({ title: '支付成功', icon: 'none' })
|
||||||
showPaySheet.value = false
|
showPaySheet.value = false
|
||||||
try {
|
try {
|
||||||
@@ -521,6 +523,47 @@ async function onConfirmPay() {
|
|||||||
uni.redirectTo({
|
uni.redirectTo({
|
||||||
url: `/pages/report/detail?orderNo=${encodeURIComponent(orderNo)}`,
|
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',
|
||||||
|
timeStamp: String(prepay.timeStamp ?? prepay.timestamp ?? ''),
|
||||||
|
nonceStr: String(prepay.nonceStr ?? prepay.nonce_str ?? ''),
|
||||||
|
package: String(prepay.package ?? ''),
|
||||||
|
signType: String(prepay.signType ?? prepay.sign_type ?? 'RSA'),
|
||||||
|
paySign: String(prepay.paySign ?? prepay.pay_sign ?? ''),
|
||||||
|
success() {
|
||||||
|
afterPaySuccess()
|
||||||
resolve()
|
resolve()
|
||||||
},
|
},
|
||||||
fail(err) {
|
fail(err) {
|
||||||
@@ -531,8 +574,10 @@ async function onConfirmPay() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
catch {
|
catch (e) {
|
||||||
/* Toast 已提示 */
|
const msg = e?.errMsg || e?.message || ''
|
||||||
|
if (msg && !/cancel/i.test(msg))
|
||||||
|
uni.showToast({ title: String(msg).replace('requestVirtualPayment:fail ', ''), icon: 'none' })
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
paying.value = false
|
paying.value = false
|
||||||
|
|||||||
@@ -1,8 +1,47 @@
|
|||||||
import { postUserWxMiniAuth } from '@/api/user'
|
import { postUserWxMiniAuth } from '@/api/user'
|
||||||
import { saveAuthSession } from '@/utils/session'
|
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 启动静默登录)
|
* @param {{ silent?: boolean }} [opts] silent=true:不触发全局 loading、不弹业务/网络失败 Toast(适合 App 启动静默登录)
|
||||||
* @returns {Promise<boolean>} 是否登录成功
|
* @returns {Promise<boolean>} 是否登录成功
|
||||||
*/
|
*/
|
||||||
@@ -13,15 +52,26 @@ export async function tryWxMiniProgramAuth(opts = {}) {
|
|||||||
: undefined
|
: undefined
|
||||||
|
|
||||||
const loginRes = await new Promise((resolve, reject) => {
|
const loginRes = await new Promise((resolve, reject) => {
|
||||||
uni.login({
|
// #ifdef MP-WEIXIN
|
||||||
provider: 'weixin',
|
wx.login({
|
||||||
success: resolve,
|
success: resolve,
|
||||||
fail: reject,
|
fail: reject,
|
||||||
})
|
})
|
||||||
|
// #endif
|
||||||
|
// #ifndef MP-WEIXIN
|
||||||
|
reject(new Error('仅支持微信小程序登录'))
|
||||||
|
// #endif
|
||||||
})
|
})
|
||||||
if (!loginRes?.code) {
|
if (!isValidWxLoginCode(loginRes?.code)) {
|
||||||
if (!silent)
|
const runtimeAppId = getRuntimeWxAppId()
|
||||||
uni.showToast({ title: '未获取到微信登录凭证', icon: 'none' })
|
console.warn('[wxMiniAuth] 无效登录凭证', {
|
||||||
|
code: loginRes?.code,
|
||||||
|
runtimeAppId,
|
||||||
|
errMsg: loginRes?.errMsg,
|
||||||
|
})
|
||||||
|
if (!silent) {
|
||||||
|
uni.showToast({ title: getWxLoginFailureTip(loginRes?.code), icon: 'none', duration: 3500 })
|
||||||
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
36
src/utils/xpayPay.js
Normal file
36
src/utils/xpayPay.js
Normal 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 */
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user