From db9b4e845b3852b5797299a2ec0b39563bbcb2b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=89=AF=28004796=29?= Date: Mon, 19 Feb 2024 11:54:36 +0800 Subject: [PATCH] 123 --- src/api/login/index.ts | 15 ++++++++++++++ src/router/index.ts | 2 ++ src/store/modules/domain.ts | 37 +++++++++++++++++++++++++++++++++++ src/utils/http/axios/index.ts | 22 ++++++++++++++++----- 4 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 src/store/modules/domain.ts diff --git a/src/api/login/index.ts b/src/api/login/index.ts index 8f572d9..02487f0 100644 --- a/src/api/login/index.ts +++ b/src/api/login/index.ts @@ -1,6 +1,21 @@ 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, + method: 'GET' + }, + { + withToken: false, + }) +} + + /** * @description: sendSmsRegister */ diff --git a/src/router/index.ts b/src/router/index.ts index 3bb1dba..a5385f8 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -1,5 +1,6 @@ import {createRouter, createWebHashHistory, RouteRecordRaw} from 'vue-router'; import {useUserStore} from "@/store/modules/user"; +import {domainStoreWidthOut} from "@/store/modules/domain"; export const constantRouter: Array = [ { @@ -235,6 +236,7 @@ const router = createRouter({ // @ts-ignore router.beforeEach((to, from, next) => { + domainStoreWidthOut().refreshDomain(); 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 new file mode 100644 index 0000000..3d27a13 --- /dev/null +++ b/src/store/modules/domain.ts @@ -0,0 +1,37 @@ +import {defineStore} from 'pinia'; +import {store} from '@/store'; +import {getDomainList} from "@/api/login"; + +export interface IDomain{ + domain: string[] +} + +export const domainStore = defineStore({ + id: 'domain', + state: (): IDomain => ({ + domain: [] + }), + getters:{ + getDomain(): string[] { + return this.domain + } + }, + actions: { + setDomain() { + getDomainList().then(response => { + if(response && response.data){ + this.domain = response.data.split(',') + } + }) + }, + refreshDomain(){ + if(!this.domain || this.domain.length == 0){ + this.setDomain() + } + } + } +}) + +export function domainStoreWidthOut() { + return domainStore(store); +} diff --git a/src/utils/http/axios/index.ts b/src/utils/http/axios/index.ts index 33b3865..6c59f4b 100644 --- a/src/utils/http/axios/index.ts +++ b/src/utils/http/axios/index.ts @@ -14,6 +14,8 @@ import { setObjToUrlParams } from '@/utils/urlUtils'; 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 = ''; @@ -38,6 +40,10 @@ const transform: AxiosTransform = { isReturnNativeResponse, } = options; + if(res.request.responseURL.includes(domainList)){ + return res; + } + // 是否返回原生响应头 比如:需要获取响应头时使用该属性 if (isReturnNativeResponse) { return res; @@ -122,16 +128,22 @@ const transform: AxiosTransform = { // 请求之前处理config beforeRequestHook: (config, options) => { const { apiUrl, joinPrefix, joinParamsToUrl, formatDate, joinTime = true, urlPrefix } = options; - const isUrlStr = isUrl(config.url as string); - if (!isUrlStr && joinPrefix) { config.url = `${urlPrefix}${config.url}`; } - - if (!isUrlStr && apiUrl && isString(apiUrl)) { - config.url = `${apiUrl}${config.url}`; + if(!isUrlStr){ + let domain = domainStoreWidthOut().getDomain; + 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}`; + } } + // if (!isUrlStr && apiUrl && isString(apiUrl)) { + // config.url = `${apiUrl}${config.url}`; + // } const params = config.params || {}; const data = config.data || false; if (config.method?.toUpperCase() === RequestEnum.GET) {