Sfoglia il codice sorgente

feat: 增加登录校验的判断

liangan 3 settimane fa
parent
commit
52c14f0361
3 ha cambiato i file con 40 aggiunte e 8 eliminazioni
  1. 33 2
      src/hooks/usePageAuth.ts
  2. 4 3
      src/pages/productDetail/productDetail.vue
  3. 3 3
      src/store/user.ts

+ 33 - 2
src/hooks/usePageAuth.ts

@@ -4,10 +4,41 @@ import { needLoginPages as _needLoginPages, getNeedLoginPages } from '@/utils'
 
 const loginRoute = import.meta.env.VITE_LOGIN_URL
 const isDev = import.meta.env.DEV
-function isLogined() {
+
+/**
+ * 判断是否已登录
+ */
+export function isLoggedIn() {
   const userStore = useUserStore()
   return !!userStore.token
 }
+
+/**
+ * 获取用户信息
+ */
+export function getUserInfoHook() {
+  const userStore = useUserStore()
+  return userStore.userInfo
+}
+
+/**
+ * 检查登录状态,未登录则跳转到登录页
+ * @param redirectUrl 登录成功后的跳转地址,默认为当前页面地址及参数
+ * @returns 是否已登录
+ */
+export function requireLogin(redirectUrl?: string) {
+  if (isLoggedIn()) {
+    return true
+  }
+
+  // 构建登录页面URL,包含重定向参数
+  const loginUrl = redirectUrl ? `/pages/login/login?redirect=${encodeURIComponent(redirectUrl)}` : `/pages/login/login`
+
+  // 跳转到登录页
+  uni.navigateTo({ url: loginUrl })
+  return false
+}
+
 // 检查当前页面是否需要登录
 export function usePageAuth() {
   onLoad((options) => {
@@ -31,7 +62,7 @@ export function usePageAuth() {
       return
     }
 
-    const hasLogin = isLogined()
+    const hasLogin = isLoggedIn()
     if (hasLogin) {
       return true
     }

+ 4 - 3
src/pages/productDetail/productDetail.vue

@@ -14,7 +14,6 @@ import { onPageScroll, onReachBottom } from '@dcloudio/uni-app'
 import useZPaging from 'z-paging/components/z-paging/js/hooks/useZPaging.js'
 import { preOrder as _preOrder } from '@/api/order'
 import { getDetail, pinkList } from '@/api/product'
-import { useUserStore } from '@/store/user'
 import { formatNumber } from '@/utils/index'
 import { getPageParams, goBack, toPage } from '@/utils/page'
 import CustomTooltip from './components/CustomTooltip.vue'
@@ -23,9 +22,8 @@ import NotificationCarousel from './components/NotificationCarousel.vue'
 defineOptions({
   name: 'ProductDetail', // 商品详情
 })
-const userStore = useUserStore()
 const userInfo = computed(() => {
-  return userStore.userInfo
+  return getUserInfoHook()
 })
 // 获取屏幕边界到安全区域距离
 const systemInfo = uni.getSystemInfoSync()
@@ -249,6 +247,9 @@ function setDefaultSpecs() {
 
 // 预下单
 async function preOrder() {
+  if (!requireLogin()) {
+    return
+  }
   const data = {
     orderDetails: {
       attrValueId: matchedAttrValue.value.id,

+ 3 - 3
src/store/user.ts

@@ -1,7 +1,7 @@
 import { defineStore } from 'pinia'
 import { ref } from 'vue'
 import { getUserInfo as _getUserInfo, login as _login } from '@/api/login'
-import { toPage } from '@/utils/page'
+import { goBack, toPage } from '@/utils/page'
 import { toast } from '@/utils/toast'
 
 // 初始化状态
@@ -70,11 +70,11 @@ export const useUserStore = defineStore(
         setTimeout(() => {
           if (redirectUrl) {
             // 使用 toPage 方法,自动判断是否为 tabBar 页面
-            toPage(redirectUrl, {}, true)
+            toPage(decodeURIComponent(redirectUrl), {}, true)
           }
           else {
             // 默认跳转到首页
-            toPage('/pages/index/index')
+            goBack()
           }
         }, 1500)