浏览代码

优化:优化邀请进入券中心参数获取方式

haiyang 1 周之前
父节点
当前提交
da048a05d8
共有 2 个文件被更改,包括 60 次插入52 次删除
  1. 15 13
      src/App.ku.vue
  2. 45 39
      src/store/token.ts

+ 15 - 13
src/App.ku.vue

@@ -10,18 +10,8 @@ const isCurrentPageTabbar = ref(true)
 const loadingStore = useLoadingStore()
 const tokenStore = useTokenStore()
 
-onShow(() => {
-    console.log('App.ku.vue onShow', currRoute())
-    const { path, query } = currRoute()
-    // “蜡笔小开心”提到本地是 '/pages/index/index',线上是 '/' 导致线上 tabbar 不见了
-    // 所以这里需要判断一下,如果是 '/' 就当做首页,也要显示 tabbar
-    isCurrentPageTabbar.value = path === '/' || isPageTabbar(path)
-    setTimeout(() => {
-        if (isCurrentPageTabbar.value) {
-            tabbarStore.setAutoCurIdx(path)
-        }
-    }, 0)
-
+onLoad((options) => {
+    console.log('App.ku.vue onLoad', options)
     // 判断是否是通过分享进入
     const paramKey = 'CCShareId'
     const keyName = 'shareRecordId'
@@ -39,10 +29,22 @@ onShow(() => {
         }
         return temp
     }
-    const queryParams = getSceneData(query)
+    const queryParams = getSceneData(options)
     if (!!queryParams && Object.keys(queryParams).includes(keyName)) {
         tokenStore.cacheShareParams(queryParams)
     }
+})
+
+onShow(() => {
+    const { path, query } = currRoute()
+    // “蜡笔小开心”提到本地是 '/pages/index/index',线上是 '/' 导致线上 tabbar 不见了
+    // 所以这里需要判断一下,如果是 '/' 就当做首页,也要显示 tabbar
+    isCurrentPageTabbar.value = path === '/' || isPageTabbar(path)
+    setTimeout(() => {
+        if (isCurrentPageTabbar.value) {
+            tabbarStore.setAutoCurIdx(path)
+        }
+    }, 0)
 
     loadingStore.hideLoading()
 })

+ 45 - 39
src/store/token.ts

@@ -61,8 +61,8 @@ export const useTokenStore = defineStore(
         }
 
         /**
-                                                                 * 判断token是否过期
-                                                                 */
+                                                                                 * 判断token是否过期
+                                                                                 */
         const isTokenExpired = computed(() => {
             if (!tokenInfo.value) {
                 return true
@@ -75,10 +75,18 @@ export const useTokenStore = defineStore(
                 return true
             return now >= expireTime
         })
+        // 分享邀请进入首页时,缓存分享参数
+        const cacheShareParams = (params: Record<string, string>) => {
+            uni.setStorageSync('shareParams', JSON.stringify(params))
+        }
+
+        const cleanShareParams = () => {
+            uni.removeStorageSync('shareParams')
+        }
 
         /**
-                                                                 * 判断refreshToken是否过期
-                                                                 */
+                                                                                 * 判断refreshToken是否过期
+                                                                                 */
         const isRefreshTokenExpired = computed(() => {
             if (!isDoubleTokenMode)
                 return true
@@ -92,9 +100,9 @@ export const useTokenStore = defineStore(
         })
 
         /**
-                                                                 * 登录成功后处理逻辑
-                                                                 * @param tokenInfo 登录返回的token信息
-                                                                 */
+                                                                                 * 登录成功后处理逻辑
+                                                                                 * @param tokenInfo 登录返回的token信息
+                                                                                 */
         async function _postLogin(tokenInfo: IAuthLoginRes) {
             setTokenInfo(tokenInfo)
             const userStore = useUserStore()
@@ -110,12 +118,12 @@ export const useTokenStore = defineStore(
         }
 
         /**
-                                                                 * 用户登录
-                                                                 * 有的时候后端会用一个接口返回token和用户信息,有的时候会分开2个接口,一个获取token,一个获取用户信息
-                                                                 * (各有利弊,看业务场景和系统复杂度),这里使用2个接口返回的来模拟
-                                                                 * @param loginForm 登录参数
-                                                                 * @returns 登录结果
-                                                                 */
+                                                                                 * 用户登录
+                                                                                 * 有的时候后端会用一个接口返回token和用户信息,有的时候会分开2个接口,一个获取token,一个获取用户信息
+                                                                                 * (各有利弊,看业务场景和系统复杂度),这里使用2个接口返回的来模拟
+                                                                                 * @param loginForm 登录参数
+                                                                                 * @returns 登录结果
+                                                                                 */
         const login = async (loginForm: ILoginForm) => {
             try {
                 const res = await _login(loginForm)
@@ -139,11 +147,11 @@ export const useTokenStore = defineStore(
         }
 
         /**
-                                                                                                                                                                                                             * 微信登录
-                                                                                                                                                                                                             * 有的时候后端会用一个接口返回token和用户信息,有的时候会分开2个接口,一个获取token,一个获取用户信息
-                                                                                                                                                                                                             * (各有利弊,看业务场景和系统复杂度),这里使用2个接口返回的来模拟
-                                                                                                                                                                                                             * @returns 登录结果
-                                                                                                                                                                                                             */
+                                                                                                                                                                                                                             * 微信登录
+                                                                                                                                                                                                                             * 有的时候后端会用一个接口返回token和用户信息,有的时候会分开2个接口,一个获取token,一个获取用户信息
+                                                                                                                                                                                                                             * (各有利弊,看业务场景和系统复杂度),这里使用2个接口返回的来模拟
+                                                                                                                                                                                                                             * @returns 登录结果
+                                                                                                                                                                                                                             */
         const wxLogin = async () => {
             try {
                 // 获取微信小程序登录的code
@@ -161,6 +169,7 @@ export const useTokenStore = defineStore(
                     title: '登录成功',
                     icon: 'success',
                 })
+                cleanShareParams()
                 return res
             }
             catch (error) {
@@ -174,8 +183,8 @@ export const useTokenStore = defineStore(
         }
 
         /**
-                                                                                                                                                                                                             * 退出登录 并 删除用户信息
-                                                                                                                                                                                                             */
+                                                                                                                                                                                                                             * 退出登录 并 删除用户信息
+                                                                                                                                                                                                                             */
         const logout = async () => {
             try {
                 // TODO 实现自己的退出登录逻辑
@@ -198,9 +207,9 @@ export const useTokenStore = defineStore(
         }
 
         /**
-                                                                                                                                                                                                             * 刷新token
-                                                                                                                                                                                                             * @returns 刷新结果
-                                                                                                                                                                                                             */
+                                                                                                                                                                                                                             * 刷新token
+                                                                                                                                                                                                                             * @returns 刷新结果
+                                                                                                                                                                                                                             */
         const refreshToken = async () => {
             if (!isDoubleTokenMode) {
                 console.error('单token模式不支持刷新token')
@@ -226,10 +235,10 @@ export const useTokenStore = defineStore(
         }
 
         /**
-                                                                                                                                                                                                             * 获取有效的token
-                                                                                                                                                                                                             * 注意:在computed中不直接调用异步函数,只做状态判断
-                                                                                                                                                                                                             * 实际的刷新操作应由调用方处理
-                                                                                                                                                                                                             */
+                                                                                                                                                                                                                             * 获取有效的token
+                                                                                                                                                                                                                             * 注意:在computed中不直接调用异步函数,只做状态判断
+                                                                                                                                                                                                                             * 实际的刷新操作应由调用方处理
+                                                                                                                                                                                                                             */
         const getValidToken = computed(() => {
             // token已过期,返回空
             if (isTokenExpired.value) {
@@ -245,8 +254,8 @@ export const useTokenStore = defineStore(
         })
 
         /**
-                                                                                                                                                                                                             * 检查是否有登录信息(不考虑token是否过期)
-                                                                                                                                                                                                             */
+                                                                                                                                                                                                                             * 检查是否有登录信息(不考虑token是否过期)
+                                                                                                                                                                                                                             */
         const hasLoginInfo = computed(() => {
             if (!tokenInfo.value) {
                 return false
@@ -260,17 +269,17 @@ export const useTokenStore = defineStore(
         })
 
         /**
-                                                                                                                                                             * 检查是否已登录且token有效
-                                                                                                                                                             */
+                                                                                                                                                                             * 检查是否已登录且token有效
+                                                                                                                                                                             */
         const hasValidLogin = computed(() => {
             console.log('hasValidLogin', hasLoginInfo.value && !isTokenExpired.value, hasLoginInfo.value, !isTokenExpired.value)
             return hasLoginInfo.value && !isTokenExpired.value
         })
 
         /**
-                                                                                                                                                         * 尝试获取有效的token,如果过期且可刷新,则刷新token
-                                                                                                                                                         * @returns 有效的token或空字符串
-                                                                                                                                                         */
+                                                                                                                                                                         * 尝试获取有效的token,如果过期且可刷新,则刷新token
+                                                                                                                                                                         * @returns 有效的token或空字符串
+                                                                                                                                                                         */
         const tryGetValidToken = async (): Promise<string> => {
             if (!getValidToken.value && isDoubleTokenMode && !isRefreshTokenExpired.value) {
                 try {
@@ -284,10 +293,6 @@ export const useTokenStore = defineStore(
             }
             return getValidToken.value
         }
-        // 分享进入首页时,缓存分享参数
-        const cacheShareParams = (params: Record<string, string>) => {
-            uni.setStorageSync('shareParams', JSON.stringify(params))
-        }
 
         return {
             // 核心API方法
@@ -307,7 +312,8 @@ export const useTokenStore = defineStore(
             tokenInfo,
             setTokenInfo,
             cleanToken,
-            cacheShareParams
+            cacheShareParams,
+            cleanShareParams
         }
     },
     {