Bladeren bron

更改浏览器参数获取方式

zhuf 1 jaar geleden
bovenliggende
commit
e89bdbb919
4 gewijzigde bestanden met toevoegingen van 16 en 6 verwijderingen
  1. 3 1
      src/main.ts
  2. 1 1
      src/request/request.ts
  3. 5 2
      src/router/index.ts
  4. 7 2
      src/utils/utils.js

+ 3 - 1
src/main.ts

@@ -10,9 +10,11 @@ import 'vant/es/dialog/style';
 import 'vant/es/notify/style';
 import 'vant/es/image-preview/style';
 
+import { urlParams } from '~/utils/utils'
+
 import VConsole from 'vconsole'
 const search = new URLSearchParams(location.search)
-if(search.get('debug')){
+if (search.get('debug') || urlParams?.debug) {
   new VConsole()
 }
 

+ 1 - 1
src/request/request.ts

@@ -2,7 +2,7 @@ import type { AxiosRequestConfig } from 'axios'
 import axios from 'axios'
 import { showFailToast } from 'vant'
 import { user } from '~/store/user'
-import router from '../router/index'
+// import router from '../router/index'
 
 const token = user.value?.token
 export interface Response {

+ 5 - 2
src/router/index.ts

@@ -1,7 +1,8 @@
 import routes from 'virtual:generated-pages'
 import { createRouter, createWebHashHistory } from 'vue-router'
-
 import { user } from '~/store/user'
+import { urlParams } from '~/utils/utils'
+import { showFailToast } from 'vant'
 
 console.log('routes : ', routes)
 const router = createRouter({
@@ -23,11 +24,13 @@ export default router
 
 
 
-const token = getUrlParams(window.location.href).token;
+const token = urlParams?.token;
 if (token) {
   sessionStorage.setItem('token', token)
   const { code, data } = await request({ api: 'openapi', url: '/user/main/detail', data: { token } });
   if (code == 1) {
     user.value = { token, ...data.one_info }
   }
+}else{
+  showFailToast('登录信息已过期,请重新扫码使用')
 }

+ 7 - 2
src/utils/utils.js

@@ -1,4 +1,7 @@
-export function getUrlParams(url) {
+// 针对错误的地址的妥协方案
+// https://aimooc.bozedu.net/mobile/#/home/caiji?token=ed1b4nIOoqi5K4K8l83Dau0Ju71JtiZT8N3Tk7LBgxl6v_aPip7eBdzC0BrOVCPw2Y8rWhjBx3gEkakGcFoBKFWZgJaY
+// 这种地址query参数是在#后面的,所以需要通过字符串截取的方式获取参数,而不是通过window.location.search
+function getUrlParams(url) {
   // 通过 ? 分割获取后面的参数字符串
   const urlStr = decodeURIComponent(url).split('?')[1]
   if (urlStr != undefined) {
@@ -16,4 +19,6 @@ export function getUrlParams(url) {
   else {
     return {}
   }
-}
+}
+
+export const urlParams = getUrlParams(window.location.href)