This commit is contained in:
张良(004796)
2024-02-19 12:26:00 +08:00
parent db9b4e845b
commit 7cfc0583dd
5 changed files with 40 additions and 21 deletions

1
public/domain.txt Normal file
View File

@@ -0,0 +1 @@
https://baidu.com,https://jingdong.com

View File

@@ -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'
},
{

View File

@@ -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<RouteRecordRaw> = [
{
@@ -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;
}

View File

@@ -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()
// }
// }
}
})

View File

@@ -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}`;