mi-assessment/uniapp/App.vue
zpc be1fb78d6b
All checks were successful
continuous-integration/drone/push Build is passing
修复bug
2026-03-25 01:36:44 +08:00

73 lines
1.8 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<script>
import { useUserStore } from './store/user.js'
import { useAppStore } from './store/app.js'
import { setupRouteGuard } from './utils/routeGuard.js'
export default {
onLaunch: function(options) {
console.log('App Launch', options)
// 初始化路由守卫(未登录跳转登录页)
setupRouteGuard()
// 初始化系统信息
const appStore = useAppStore()
appStore.initSystemInfo()
// 恢复用户状态
const userStore = useUserStore()
userStore.restoreFromStorage()
// 解析邀请人参数(二维码扫码 scene 或分享链接 query
this.parseInviterFromLaunch(options)
// 已登录时从服务器刷新用户信息
if (userStore.isLoggedIn) {
userStore.fetchUserInfo()
}
},
onShow: function() {
console.log('App Show')
},
onHide: function() {
console.log('App Hide')
},
methods: {
/**
* 解析邀请人参数
* 来源1: 扫描小程序码 scene 参数,格式 inviter={userId}
* 来源2: 分享链接 query 参数 inviteUid={uid}
*/
parseInviterFromLaunch(options) {
if (!options) return
let inviterId = null
// 1. 扫码进入:解析 scene 参数inviter={userId}
if (options.scene) {
const scene = decodeURIComponent(options.scene)
console.log('扫码 scene:', scene)
const match = scene.match(/inviter=(\d+)/)
if (match) {
inviterId = match[1]
}
}
// 2. 分享链接进入:解析 inviterId 参数
if (!inviterId && options.inviterId) {
inviterId = options.inviterId
}
// 存储邀请人ID仅未登录用户需要绑定
if (inviterId) {
console.log('检测到邀请人ID:', inviterId)
uni.setStorageSync('inviterId', inviterId)
}
}
}
}
</script>
<style>
/*每个页面公共css */
</style>