73 lines
1.8 KiB
Vue
73 lines
1.8 KiB
Vue
<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>
|