first commit
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import axios from 'axios'
|
||||
import qs from 'qs'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { getToken } from './token'
|
||||
|
||||
// axios实例
|
||||
const service = axios.create({
|
||||
baseURL: import.meta.env.VITE_BASE_API as any,
|
||||
timeout: 60000,
|
||||
headers: { 'Content-Type': 'application/json;charset=UTF-8' }
|
||||
})
|
||||
|
||||
// 请求拦截器
|
||||
service.interceptors.request.use(
|
||||
(config: any) => {
|
||||
// 追加时间戳,防止GET请求缓存
|
||||
if (config.method?.toUpperCase() === 'GET') {
|
||||
config.params = { ...config.params, t: new Date().getTime() }
|
||||
}
|
||||
|
||||
config.headers.token = config.headers.token || getToken()
|
||||
|
||||
if (Object.values(config.headers).includes('application/x-www-form-urlencoded')) {
|
||||
config.data = qs.stringify(config.data)
|
||||
}
|
||||
|
||||
return config
|
||||
},
|
||||
error => {
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
// 响应拦截器
|
||||
service.interceptors.response.use(
|
||||
response => {
|
||||
if (response.status !== 200) {
|
||||
return Promise.reject(new Error(response.statusText || 'Error'))
|
||||
}
|
||||
|
||||
const res = response.data
|
||||
// 响应成功
|
||||
if (res.code === 200) {
|
||||
return res
|
||||
}
|
||||
|
||||
// 错误提示
|
||||
ElMessage.error(res.msg)
|
||||
|
||||
return Promise.reject(new Error(res.msg || 'Error'))
|
||||
},
|
||||
error => {
|
||||
ElMessage.error(error.message)
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
// 导出 axios 实例
|
||||
export default service
|
||||
@@ -0,0 +1,30 @@
|
||||
const TOKEN_CODE = 'access_token'
|
||||
|
||||
export function getToken() {
|
||||
return 'dev'
|
||||
return localStorage.getItem(TOKEN_CODE)
|
||||
}
|
||||
|
||||
export function setToken(token) {
|
||||
localStorage.setItem(TOKEN_CODE, token)
|
||||
}
|
||||
|
||||
export function removeToken() {
|
||||
localStorage.removeItem(TOKEN_CODE)
|
||||
}
|
||||
|
||||
// export async function refreshAccessToken() {
|
||||
// const tokenItem = lStorage.getItem(TOKEN_CODE)
|
||||
// if (!tokenItem) {
|
||||
// return
|
||||
// }
|
||||
// const { time } = tokenItem
|
||||
// // token生成或者刷新后30分钟内不执行刷新
|
||||
// if (new Date().getTime() - time <= 1000 * 60 * 30) return
|
||||
// try {
|
||||
// const res = await api.refreshToken()
|
||||
// setToken(res.data.token)
|
||||
// } catch (error) {
|
||||
// console.error(error)
|
||||
// }
|
||||
// }
|
||||
@@ -0,0 +1,45 @@
|
||||
import type { App, Plugin } from 'vue'
|
||||
import { AES, lib, enc, mode, pad } from 'crypto-js'
|
||||
// 全局组件安装
|
||||
export const withInstall = <T>(component: T, alias?: string) => {
|
||||
const comp = component as any
|
||||
comp.install = (app: App) => {
|
||||
app.component(comp.__name || comp.displayName, component)
|
||||
if (alias) {
|
||||
app.config.globalProperties[alias] = component
|
||||
}
|
||||
}
|
||||
return component as T & Plugin
|
||||
}
|
||||
|
||||
// 密钥
|
||||
const ENCRYPT_KEY = 'makulowcodeyyds1'
|
||||
|
||||
export const decrypt = (ciphertext: string): string => {
|
||||
// 将密文转换为CipherParams对象
|
||||
const cipherParams = lib.CipherParams.create({
|
||||
ciphertext: enc.Base64.parse(ciphertext)
|
||||
})
|
||||
|
||||
// 使用密钥解密CipherParams对象
|
||||
const decrypted = AES.decrypt(cipherParams, enc.Utf8.parse(ENCRYPT_KEY), {
|
||||
mode: mode.ECB,
|
||||
padding: pad.Pkcs7
|
||||
})
|
||||
|
||||
// 获取明文
|
||||
return decrypted.toString(enc.Utf8)
|
||||
}
|
||||
|
||||
export const encrypt = (plaintext: string): string => {
|
||||
// 将明文转换为要加密的格式
|
||||
const message = enc.Utf8.parse(plaintext)
|
||||
|
||||
// 使用密钥加密明文
|
||||
const encrypted = AES.encrypt(message, enc.Utf8.parse(ENCRYPT_KEY), {
|
||||
mode: mode.ECB,
|
||||
padding: pad.Pkcs7
|
||||
})
|
||||
|
||||
return encrypted.toString()
|
||||
}
|
||||
Reference in New Issue
Block a user