1.解决api接口问题

sws 2024-08-16
v1.0.0
sws 2024-08-16 17:16:25 +08:00
parent 35e1809b82
commit 51b8af20eb
2 changed files with 69 additions and 18 deletions

View File

@ -1,4 +1,4 @@
import { isEmpty } from "lodash";
import { isEmpty } from 'lodash';
/**
*
*
@ -41,13 +41,13 @@ export function gradient_handle(color_list: color_list[], direction: string) {
new_color_list.forEach((item: any, index: number) => {
container_common_styles += `${item.color ? item.color : 'rgb(255 255 255 / 0%)'}`;
if (color_list.length == 1) {
container_common_styles += ` ${ item.color_percentage || 0 }%, ${ item.color } 100%`;
container_common_styles += ` ${item.color_percentage || 0}%, ${item.color} 100%`;
} else {
if (!isEmpty(item.color_percentage)) {
if (index == color_list.length - 1) {
container_common_styles += ` ${ item.color_percentage }%`;
container_common_styles += ` ${item.color_percentage}%`;
} else {
container_common_styles += ` ${ item.color_percentage }%,`;
container_common_styles += ` ${item.color_percentage}%,`;
}
} else {
if (index == color_list.length - 1) {
@ -58,7 +58,6 @@ export function gradient_handle(color_list: color_list[], direction: string) {
container_common_styles += ` ${(100 / color_list.length) * index}%,`;
}
}
}
});
container_common_styles += `);`;
@ -200,7 +199,6 @@ export const ext_name = (name: string) => {
return '';
};
/**
*
*
@ -209,9 +207,9 @@ export const ext_name = (name: string) => {
* @returns 4
*/
export const percentage_count = (num: number, container_size: number) => {
const marks = num / container_size * 100;
return marks.toFixed(4)+ '%';
}
const marks = (num / container_size) * 100;
return marks.toFixed(4) + '%';
};
/**
*
@ -232,4 +230,53 @@ export const location_compute = (size: number, location: number, container_size:
} else {
return location;
}
}
};
/**
* cookie
* @param name cookie
* @returns cookie
*/
export const get_cookie = (name: string) => {
// 初始化cookie值为空字符串
var cookievalue = '';
// 定义要搜索的cookie名称字符串
var search = name + '=';
// 检查是否存在cookie
if (document.cookie.length > 0) {
// 尝试查找cookie名称的位置
let offset = document.cookie.indexOf(search);
// 如果找到了cookie名称
if (offset != -1) {
// 跳过cookie名称的长度
offset += search.length;
// 查找cookie值的结束位置可能是分号或者字符串末尾
let end = document.cookie.indexOf(';', offset);
if (end == -1) end = document.cookie.length;
// 提取并解码cookie值
cookievalue = decodeURIComponent(document.cookie.substring(offset, end));
}
}
// 返回获取到的cookie值
return cookievalue;
};
/**
* cookie
* cookiecookie
* @param name {string} - cookie
* @param value {string} - cookie
* @param expire_time {number} - cookie
*/
export const set_cookie = (name: string, value: string, expire_time?: number) => {
// 构造cookie字符串
var cookie_str = name + '=' + encodeURIComponent(value);
if (expire_time) {
// 获取当前时间
var now = new Date();
// 计算过期时间
var expire_date = new Date(now.getTime() + expire_time * 86400);
cookie_str += ';expires=' + expire_date.toUTCString();
// 将新增的cookie储存到cookie中可以存储多个而不是替换
document.cookie = cookie_str;
}
};

View File

@ -1,8 +1,11 @@
import axios, { InternalAxiosRequestConfig, AxiosResponse } from 'axios';
import { ElMessage, ElMessageBox } from 'element-plus';
import { get_cookie } from './index';
// 创建 axios 实例
const index = window.location.href.lastIndexOf('?');
const pro_url = window.location.href.substring(0, index);
const service = axios.create({
baseURL: import.meta.env.VITE_APP_BASE_API == '/dev-api' ? import.meta.env.VITE_APP_BASE_API : window.location.origin,
baseURL: import.meta.env.VITE_APP_BASE_API == '/dev-api' ? import.meta.env.VITE_APP_BASE_API : pro_url,
timeout: 50000,
headers: { 'Content-Type': 'application/json;charset=utf-8' },
});
@ -10,13 +13,14 @@ const service = axios.create({
// 请求拦截器
service.interceptors.request.use(
(config: InternalAxiosRequestConfig) => {
// const userStore = useUserStoreHook();
// const accessToken = localStorage.getItem(TOKEN_KEY);
const accessToken = { token: 'b84c6cac0dacd1bc624393cd522dec37' };
if (accessToken.token) {
// config.headers.Authorization = accessToken.token;
// config.data = { ...config.data, token: accessToken.token };
config.url = config.url + '?token=' + accessToken.token;
// 如果是本地则使用静态tonken如果是线上则使用cookie的token
const cookie = get_cookie('admin_info');
let token_key = '';
if (cookie) {
token_key = import.meta.env.VITE_APP_BASE_API == '/dev-api' ? '68f4eba5f67a758a972cca831885dfda' : JSON.parse(cookie);
}
if (token_key) {
config.url = config.url + '?token=' + token_key;
}
return config;
},