consumer-app/App.vue

173 lines
5.6 KiB
Vue
Raw Normal View History

2025-12-19 12:27:55 +00:00
<script>
2026-03-04 08:58:40 +00:00
import { updateLuOrderPayStatus } from "@/api/service";
2025-12-19 12:27:55 +00:00
export default {
globalData: {
// 用于从首页跳转到服务页面时传递需要高亮的分类
2026-03-09 03:37:41 +00:00
serviceCategory: null,
// 扫码进入时携带的邀请码(小程序码 scene格式邀请类型-用户id如 '0-123'
inviteCode: null
2025-12-19 12:27:55 +00:00
},
2026-03-09 03:37:41 +00:00
onLaunch: function(options) {
2025-12-19 12:27:55 +00:00
console.log('App Launch')
2026-03-09 03:37:41 +00:00
// 扫码进入:小程序码的 scene 即邀请码(格式 邀请类型-用户id如 0-123、1-123
const scene = (options && options.query && options.query.scene) || (options && options.scene)
const sceneStr = scene != null ? String(scene) : ''
if (sceneStr && /^\d+-\d+$/.test(sceneStr)) {
this.globalData.inviteCode = sceneStr
uni.setStorageSync('inviteCode', sceneStr)
// 未登录时直接进入登录注册页,便于邀请流程
if (!uni.getStorageSync('token')) {
setTimeout(() => {
uni.reLaunch({ url: '/pages/login/login' })
}, 50)
return
}
}
2025-12-19 12:27:55 +00:00
// 检查登录状态
this.checkLoginStatus()
},
onShow: function() {
console.log('App Show')
2026-02-27 08:48:28 +00:00
// 1. uni-app 中通过 uni.getEnterOptionsSync() 调用和微信小程序API一致只是前缀改为uni
const options = uni.getEnterOptionsSync();
console.log("查询到的返回数据")
console.log(options)
// 2. 注意scene 返回的是数字类型,不是字符串!原生代码中写 '1038' 会导致判断失败
2026-03-09 08:53:29 +00:00
if (options.scene === 1038 &&
options.referrerInfo?.appId === 'wxef277996acc166c3') {
2026-02-27 08:48:28 +00:00
2026-03-09 08:53:29 +00:00
// 从收银台小程序返回的逻辑
const extraData = options.referrerInfo.extraData;
console.log("extraData",extraData)
2026-02-27 08:48:28 +00:00
2026-03-09 08:53:29 +00:00
if (!extraData) {
uni.showToast({
title: '当前通过物理按键返回,未接收到返参,建议自行查询交易结果',
icon: 'none',
duration: 3000
});
} else {
if (extraData.code === 'success') {
// 有返回成功标记后,启动轮询查询订单状态
// 1. 优先使用收银台回传的订单号
const orderNumberFromExtra =
extraData.orderNumber || extraData.reqsn || extraData.orderNo;
// 2. 如果对方没有回传,则使用我们在跳转前自己缓存到本地的订单号
const storedOrderNumber = uni.getStorageSync("lastOrderNumber");
const orderNumber = orderNumberFromExtra || storedOrderNumber;
2026-03-04 08:58:40 +00:00
2026-03-09 08:53:29 +00:00
if (orderNumber) {
this.startOrderStatusPolling(orderNumber);
} else {
uni.showToast({
2026-03-27 01:54:40 +00:00
title: '结算返回缺少订单号,请稍后在服务记录中查看',
2026-03-09 08:53:29 +00:00
icon: 'none',
duration: 3000
});
}
} else if (extraData.code === 'cancel') {
uni.showToast({
2026-03-27 01:54:40 +00:00
title: '已取消',
2026-03-09 08:53:29 +00:00
icon: 'none'
});
} else {
uni.showToast({
2026-03-27 01:54:40 +00:00
title: `结算失败:${extraData.errmsg || '未知错误'}`,
2026-03-09 08:53:29 +00:00
icon: 'none',
duration: 3000
});
}
}
}
2025-12-19 12:27:55 +00:00
},
onHide: function() {
console.log('App Hide')
2026-03-04 08:58:40 +00:00
// 页面整体隐藏时,清理轮询定时器
if (this._orderStatusTimer) {
clearInterval(this._orderStatusTimer);
this._orderStatusTimer = null;
}
2025-12-19 12:27:55 +00:00
},
methods: {
// 检查登录状态
checkLoginStatus() {
const token = uni.getStorageSync('token')
2026-02-27 08:48:28 +00:00
2025-12-19 12:27:55 +00:00
// 如果有token且当前在登录页则跳转到首页
if (token) {
// 延迟一下,确保页面已经初始化
setTimeout(() => {
const pages = getCurrentPages()
const currentPage = pages[pages.length - 1]
const currentRoute = currentPage ? currentPage.route : ''
2026-02-27 08:48:28 +00:00
2025-12-19 12:27:55 +00:00
// 如果当前在登录页,跳转到首页
if (currentRoute === 'pages/login/login') {
uni.switchTab({
url: '/pages/index/index',
fail: () => {
// 如果switchTab失败可能不在tabBar页面使用reLaunch
uni.reLaunch({
url: '/pages/index/index'
})
}
})
}
}, 100)
}
2026-03-04 08:58:40 +00:00
},
2026-03-27 01:54:40 +00:00
// 启动轮询状态:回到小程序立马查一次,失败则每隔 3 秒再查,最多再查 3 次(共 4 次)
2026-03-04 08:58:40 +00:00
startOrderStatusPolling(orderNumber) {
if (this._orderStatusTimer) {
clearInterval(this._orderStatusTimer);
this._orderStatusTimer = null;
}
let times = 0;
2026-03-05 11:04:23 +00:00
const maxTimes = 4; // 立即 1 次 + 最多再查 3 次
2026-03-04 08:58:40 +00:00
2026-03-05 11:04:23 +00:00
const doCheck = async () => {
2026-03-04 08:58:40 +00:00
times++;
try {
const res = await updateLuOrderPayStatus({ orderNumber });
if (res) {
clearInterval(this._orderStatusTimer);
this._orderStatusTimer = null;
uni.removeStorageSync("lastOrderNumber");
2026-03-27 01:54:40 +00:00
uni.showToast({ title: '结算成功', icon: 'success' });
2026-03-04 08:58:40 +00:00
uni.navigateTo({
url: '/pages/profileSub/serviceRecords?tab=pending_verification'
});
2026-03-05 11:04:23 +00:00
return;
}
if (times >= maxTimes) {
2026-03-04 08:58:40 +00:00
clearInterval(this._orderStatusTimer);
this._orderStatusTimer = null;
uni.showToast({
2026-03-27 01:54:40 +00:00
title: '状态确认超时,请稍后在服务记录中查看',
2026-03-04 08:58:40 +00:00
icon: 'none',
duration: 3000
});
}
} catch (error) {
2026-03-27 01:54:40 +00:00
console.error('查询订单状态失败:', error);
2026-03-04 08:58:40 +00:00
if (times >= maxTimes) {
clearInterval(this._orderStatusTimer);
this._orderStatusTimer = null;
}
}
2026-03-05 11:04:23 +00:00
};
doCheck(); // 回到小程序立马查一次
this._orderStatusTimer = setInterval(doCheck, 3000); // 失败则每 3 秒再查,最多再查 3 次
2025-12-19 12:27:55 +00:00
}
}
}
</script>
<style>
/*每个页面公共css */
2026-03-27 01:54:40 +00:00
</style>