From 7cfc0583ddf5945fae56dcd7a423d7050208ef62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=89=AF=28004796=29?= Date: Mon, 19 Feb 2024 12:26:00 +0800 Subject: [PATCH] 123 --- public/domain.txt | 1 + src/api/login/index.ts | 5 +---- src/router/index.ts | 12 ++++++++++-- src/store/modules/domain.ts | 30 ++++++++++++++++++------------ src/utils/http/axios/index.ts | 13 ++++++++++--- 5 files changed, 40 insertions(+), 21 deletions(-) create mode 100644 public/domain.txt diff --git a/public/domain.txt b/public/domain.txt new file mode 100644 index 0000000..47c4cea --- /dev/null +++ b/public/domain.txt @@ -0,0 +1 @@ +https://baidu.com,https://jingdong.com diff --git a/src/api/login/index.ts b/src/api/login/index.ts index 02487f0..e33176e 100644 --- a/src/api/login/index.ts +++ b/src/api/login/index.ts @@ -2,12 +2,9 @@ import { http } from '@/utils/http/axios'; const baseUrl = '/api' -export const domainList = 'https://dk-1257812345.cos.ap-nanjing.myqcloud.com/domain.txt'; - - export function getDomainList(){ return http.request({ - url: domainList, + url: 'domain.txt', method: 'GET' }, { diff --git a/src/router/index.ts b/src/router/index.ts index a5385f8..69e6538 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -1,6 +1,7 @@ import {createRouter, createWebHashHistory, RouteRecordRaw} from 'vue-router'; import {useUserStore} from "@/store/modules/user"; import {domainStoreWidthOut} from "@/store/modules/domain"; +import {getDomainList} from "@/api/login"; export const constantRouter: Array = [ { @@ -235,8 +236,15 @@ const router = createRouter({ // @ts-ignore -router.beforeEach((to, from, next) => { - domainStoreWidthOut().refreshDomain(); +router.beforeEach(async (to, from, next) => { + const dsw = domainStoreWidthOut() + if (!dsw.getDomain || dsw.getDomain.length == 0) { + const response = await getDomainList() + if (response && response.data) { + const domain = response.data.split(',') + dsw.setDomain(domain) + } + } if (to.meta.title) { // 判断是否有标题 document.title = to.meta.title as string; } diff --git a/src/store/modules/domain.ts b/src/store/modules/domain.ts index 3d27a13..ed598eb 100644 --- a/src/store/modules/domain.ts +++ b/src/store/modules/domain.ts @@ -1,6 +1,5 @@ import {defineStore} from 'pinia'; import {store} from '@/store'; -import {getDomainList} from "@/api/login"; export interface IDomain{ domain: string[] @@ -17,18 +16,25 @@ export const domainStore = defineStore({ } }, actions: { - setDomain() { - getDomainList().then(response => { - if(response && response.data){ - this.domain = response.data.split(',') - } - }) + // async setDomain() { + // let response = await getDomainList(); + // if (response && response.data) { + // this.domain = response.data.split(',') + // } + // // getDomainList().then(response => { + // // if(response && response.data){ + // // this.domain = response.data.split(',') + // // } + // // }) + // }, + setDomain(domain: string[]): void { + this.domain = domain }, - refreshDomain(){ - if(!this.domain || this.domain.length == 0){ - this.setDomain() - } - } + // refreshDomain(){ + // if(!this.domain || this.domain.length == 0){ + // this.setDomain().then() + // } + // } } }) diff --git a/src/utils/http/axios/index.ts b/src/utils/http/axios/index.ts index 6c59f4b..52c1430 100644 --- a/src/utils/http/axios/index.ts +++ b/src/utils/http/axios/index.ts @@ -15,7 +15,6 @@ import { RequestOptions, Result, CreateAxiosOptions } from './types'; import { useUserStoreWidthOut } from '@/store/modules/user'; import {domainStoreWidthOut} from "@/store/modules/domain"; -import {domainList} from "@/api/login"; const urlPrefix = ''; @@ -40,7 +39,9 @@ const transform: AxiosTransform = { isReturnNativeResponse, } = options; - if(res.request.responseURL.includes(domainList)){ + console.log(res.request.responseURL.endsWith('domain.txt')) + console.log(res.request.responseURL) + if(res.request.responseURL.includes('domain.txt')){ return res; } @@ -134,12 +135,18 @@ const transform: AxiosTransform = { } if(!isUrlStr){ let domain = domainStoreWidthOut().getDomain; - if(domain && domain.length > 0){ + if(config.url && config.url.endsWith("domain.txt")){ + return config + }else if(domain && domain.length > 0){ let domainUrl = domain[Math.floor((Math.random()*domain.length))]; config.url = domainUrl + config.url; }else if(apiUrl && isString(apiUrl)){ config.url = `${apiUrl}${config.url}`; } + }else{ + if(config.url && config.url.endsWith("domain.txt")){ + return config + } } // if (!isUrlStr && apiUrl && isString(apiUrl)) { // config.url = `${apiUrl}${config.url}`;