更新:loading加载时机问题

This commit is contained in:
2025-11-28 16:16:22 +08:00
parent 8edf719431
commit 3d20683d76
4 changed files with 160 additions and 156 deletions

View File

@@ -6,6 +6,8 @@ import { createRequestClient } from '../request';
import { SERVICE_MAP } from '../config';
export const paymentClient = createRequestClient({
baseURL: SERVICE_MAP.PAYMENT,
baseURL: SERVICE_MAP.MAIN,
// baseURL: SERVICE_MAP.PAYMENT,
loading: false
});

View File

@@ -1,5 +1,6 @@
// api/modules/user.ts
import { mainClient } from '@/api/clients/main'
import { paymentClient } from '@/api/clients/payment'
import type { IApiResponse } from '@/api/types'
import type {
IUserInfo,
@@ -196,7 +197,7 @@ export async function submitFeedback(data: IFeedbackForm) {
*/
export async function verifyGooglePay(productId: number, purchaseToken: string, orderSn: string) {
console.log(productId, purchaseToken, orderSn);
const res = await mainClient.request<IApiResponse>({
const res = await paymentClient.request<IApiResponse>({
url: 'pay/googlepay/googleVerify',
method: 'POST',
data: { productId, purchaseToken, orderSn }
@@ -278,7 +279,7 @@ export async function getTransactionDetailsList(current : number, limit : number
*/
export async function getPlaceOrder(data: object) {
const res = await mainClient.request<IApiResponse>({
const res = await paymentClient.request<IApiResponse>({
url: '/book/buyOrder/placeOrder',
method: 'POST',
data: data

View File

@@ -23,7 +23,7 @@ export function createRequestClient(cfg: ICreateClientConfig) {
const intercepted = requestInterceptor(final as IRequestOptions);
// 全局处理请求 loading
const loading = !cfg.loading ? true : cfg.loading // 接口请求参数不传loading默认显示loading
const loading = cfg.loading ?? true // 接口请求参数不传loading默认显示loading
if (loading) {
uni.showLoading({ mask: true })
reqCount++

View File

@@ -117,6 +117,52 @@
const orderSn = ref('')
/**
* 获取使用环境
*/
const getDevName = () => {
if (uni.getSystemInfoSync().platform === "android") {
qudao.value = 'Google'
isAndroid.value = true;
console.log('运行Android上')
} else {
qudao.value = 'Google'
console.log('运行iOS上')
}
getData()
}
/**
* 获取充值列表数据
*/
const getData = async () => {
try {
rechargeList.value = await getBookBuyConfigList(type.value, qudao.value)
console.log(rechargeList.value.bookBuyConfigList, '充值列表');
// 默认选择第一个金额
aloneItem.value = rechargeList.value.bookBuyConfigList[0]
} catch (error) {
console.error('获取订单列表失败:', error)
}
}
/**
* 点击支付按钮
*/
const paymentButton = async () => {
if (!state.value) {
uni.showToast({
title: t('order.readAgreeServices'),
icon: 'none'
})
return
}
getPlaceOrderObj()
}
// 节流支付按钮
const handleRecharge = useThrottle(paymentButton);
/**
* 获取订单编号
*/
@@ -132,31 +178,17 @@
productId: priceTypeId.value // 商品id
}
try {
// uni.hideLoading()
const res = await getPlaceOrder(data)
orderSn.value = res.orderSn
console.log(orderSn.value, '获取订单号');
uni.showLoading({ title: '生成订单中...' })
getGooglePay()
} catch (error) {
console.error('获取订单号失败', error)
}
}
/**
* 获取使用环境
*/
const getDevName = () => {
if (uni.getSystemInfoSync().platform === "android") {
qudao.value = 'Google'
isAndroid.value = true;
console.log('运行Android上')
} else {
qudao.value = 'Google'
console.log('运行iOS上')
}
getData()
}
// 点击金额
const chosPric = (item : any) => {
console.log(item, '金额每项');
@@ -177,17 +209,115 @@
const showAgreement = () => {
agreemenState.value = true
}
/**
* 获取充值列表数据
* 初始化
*/
const getData = async () => {
const getGooglePay = () => {
googlePay.init({
}, (e : any) => {
console.log('init', e);
if (e.code == 0) {
isConnected.value = true;
console.log('init成功了');
getQuerySku()
// 初始化成功
} else {
console.log('init失败了');
// 初始化失败
isConnected.value = false;
}
});
}
/**
* 查询sku
*/
const getQuerySku = () => {
const id = aloneItem.value.priceTypeId
console.log(id, '获取每项');
googlePay.querySku(
{
inapp: [id], // 与subs二选一, 参数为商品ID字符串数组
},
(e : any) => {
if (e.code == 0) {
// 查询成功.
console.log('querySku查询成功', e);
getPayAll()
uni.hideLoading()
} else {
console.log('查询失败', e);
uni.showToast({
title: '请求失败,请检查您的网络',
icon: 'error'
})
}
}
)
}
/**
* 发起支付
*/
const getPayAll = () => {
console.log(aloneItem.value.priceTypeId, orderSn.value, '发起支付传入产品id,订单id');
googlePay.payAll(
{
productId: aloneItem.value.priceTypeId, // 产品id
accountId: orderSn.value // 订单编号
},
(e : any) => {
if (e.code == 0) {
purchaseToken.value = e.data[0].original.purchaseToken
// 支付成功
console.log(e, 'payAll方法成功返参');
getConsume()
} else {
uni.showToast({ title: '支付失败', icon: 'error' })
console.log(e, 'e');
// 支付失败
}
},
)
}
/**
* 消耗品 确认交易
*/
const getConsume = () => {
googlePay.consume(
{
purchaseToken: purchaseToken.value, // 来自支付结果的original.purchaseToken (或 original.token)
},
(e : any) => {
if (e.code == 0) {
console.log(e, '确认交易成功');
uni.showToast({ title: '即将返回我的页面', icon: 'none' })
// 确认成功
googleVerify()
} else {
console.log(e, '确认交易失败');
// 确认失败
}
},
);
}
/**
* 校验订单
*/
const googleVerify = async () => {
uni.hideLoading()
console.log(typeof aloneItem.value.priceTypeId, typeof purchaseToken.value, typeof orderSn.value);
try {
rechargeList.value = await getBookBuyConfigList(type.value, qudao.value)
console.log(rechargeList.value.bookBuyConfigList, '充值列表');
// 默认选择第一个金额
aloneItem.value = rechargeList.value.bookBuyConfigList[0]
const obj = await verifyGooglePay(aloneItem.value.priceTypeId, purchaseToken.value, orderSn.value)
uni.switchTab({
url: '/pages/user/index'
})
console.log(obj, '校验订单');
} catch (error) {
console.error('获取订单列表失败:', error)
console.error('校验订单失败:', error)
}
}
@@ -227,135 +357,6 @@
// payType.value = val;
}
/**
* 点击支付按钮
*/
const paymentButton = async () => {
if (!state.value) {
uni.showToast({
title: t('order.readAgreeServices'),
icon: 'none'
})
return
}
getPlaceOrderObj()
uni.showLoading({ title: '生成订单中...' })
}
// 节流支付按钮
const handleRecharge = useThrottle(paymentButton);
/**
* 初始化
*/
const getGooglePay = () => {
googlePay.init({
}, (e : any) => {
console.log('init', e);
if (e.code == 0) {
isConnected.value = true;
getQuerySku()
// 初始化成功
} else {
// 初始化失败
isConnected.value = false;
}
});
}
/**
* 查询sku
*/
const getQuerySku = () => {
const id = aloneItem.value.priceTypeId
console.log(id, '获取每项');
googlePay.querySku(
{
inapp: [id], // 与subs二选一, 参数为商品ID字符串数组
},
(e : any) => {
if (e.code == 0) {
// 查询成功.
console.log('querySku查询成功', e);
uni.hideLoading()
getPayAll()
} else {
console.log('查询失败', e);
uni.showToast({
title: 'No product found.',
icon: 'none',
duration: 2000
})
// 查询失败
}
}
)
}
/**
* 发起支付
*/
const getPayAll = () => {
console.log(aloneItem.value.priceTypeId, orderSn.value, '发起支付传入产品id,订单id');
googlePay.payAll(
{
productId: aloneItem.value.priceTypeId, // 产品id
accountId: orderSn.value // 订单编号
},
(e : any) => {
if (e.code == 0) {
purchaseToken.value = e.data[0].original.purchaseToken
// 支付成功
console.log(e, 'payAll方法成功返参');
getConsume()
} else {
uni.showToast({ title: '支付失败', icon: 'success' })
console.log(e, 'e');
// 支付失败
}
},
)
}
/**
* 消耗品 确认交易
*/
const getConsume = () => {
googlePay.consume(
{
purchaseToken: purchaseToken.value, // 来自支付结果的original.purchaseToken (或 original.token)
},
(e : any) => {
if (e.code == 0) {
console.log(e, '确认交易成功');
// 确认成功
googleVerify()
} else {
console.log(e, '确认交易失败');
// 确认失败
}
},
);
}
/**
* 校验订单
*/
const googleVerify = async () => {
console.log(typeof aloneItem.value.priceTypeId, typeof purchaseToken.value, typeof orderSn.value);
try {
const obj = await verifyGooglePay(aloneItem.value.priceTypeId, purchaseToken.value, orderSn.value)
uni.switchTab({
url: '/pages/user/index'
})
console.log(obj, '校验订单');
} catch (error) {
console.error('校验订单失败:', error)
}
}
onMounted(() => {
getDevName();
getActivityDescriptionData()