refactor: vue3 axios api ...

This commit is contained in:
xingyu
2022-07-19 22:33:54 +08:00
parent ba96eef51a
commit 9e2e220b69
121 changed files with 1022 additions and 9700 deletions

View File

@ -1,29 +1,101 @@
import { useCache } from '@/hooks/web/useCache'
import { TokenType } from '@/api/login/types'
import { decrypt, encrypt } from '@/utils/jsencrypt'
const { wsCache } = useCache()
const AccessTokenKey = 'ACCESS_TOKEN'
const RefreshTokenKey = 'REFRESH_TOKEN'
// 获取token
export function getAccessToken() {
export const getAccessToken = () => {
// 此处与TokenKey相同此写法解决初始化时Cookies中不存在TokenKey报错
return wsCache.get('ACCESS_TOKEN')
}
// 刷新token
export function getRefreshToken() {
export const getRefreshToken = () => {
return wsCache.get(RefreshTokenKey)
}
// 设置token
export function setToken(token: TokenType) {
export const setToken = (token: TokenType) => {
wsCache.set(RefreshTokenKey, token.refreshToken, { exp: token.expiresTime })
wsCache.set(AccessTokenKey, token.accessToken)
}
// 删除token
export function removeToken() {
export const removeToken = () => {
wsCache.delete(AccessTokenKey)
wsCache.delete(RefreshTokenKey)
}
// ========== 账号相关 ==========
const UsernameKey = 'USERNAME'
const PasswordKey = 'PASSWORD'
const RememberMeKey = 'REMEMBER_ME'
export const getUsername = () => {
return wsCache.get(UsernameKey)
}
export const setUsername = (username: string) => {
wsCache.set(UsernameKey, username)
}
export const removeUsername = () => {
wsCache.delete(UsernameKey)
}
export const getPassword = () => {
const password = wsCache.get(PasswordKey)
return password ? decrypt(password) : undefined
}
export const setPassword = (password: string) => {
wsCache.set(PasswordKey, encrypt(password))
}
export const removePassword = () => {
wsCache.delete(PasswordKey)
}
export const getRememberMe = () => {
return wsCache.get(RememberMeKey) === 'true'
}
export const setRememberMe = (rememberMe: string) => {
wsCache.set(RememberMeKey, rememberMe)
}
export const removeRememberMe = () => {
wsCache.delete(RememberMeKey)
}
// ========== 租户相关 ==========
const TenantIdKey = 'TENANT_ID'
const TenantNameKey = 'TENANT_NAME'
export const getTenantName = () => {
return wsCache.get(TenantNameKey)
}
export const setTenantName = (username: string) => {
wsCache.set(TenantNameKey, username)
}
export const removeTenantName = () => {
wsCache.delete(TenantNameKey)
}
export const getTenantId = () => {
return wsCache.get(TenantIdKey)
}
export const setTenantId = (username: string) => {
wsCache.set(TenantIdKey, username)
}
export const removeTenantId = () => {
wsCache.delete(TenantIdKey)
}

View File

@ -1,130 +0,0 @@
import { required as requiredRule } from '@/utils/formRules'
import dayjs from 'dayjs'
import { useI18n } from '@/hooks/web/useI18n'
const { t } = useI18n() // 国际化
export class FormSchemaBuilder {
static input(label: string, field: string, required: Boolean = false): FormSchema {
return {
label,
field,
component: 'Input',
formItemProps: {
rules: required ? [requiredRule] : []
}
}
}
static inputNumber(
label: string,
field: string,
value: number,
required: Boolean = false
): FormSchema {
return {
label,
field,
value,
component: 'InputNumber',
formItemProps: {
rules: required ? [requiredRule] : []
}
}
}
static radioButton(
label: string,
field: string,
value: number,
options: ComponentOptions[],
required: Boolean = false
): FormSchema {
return {
label,
field,
component: 'RadioButton',
value,
formItemProps: {
rules: required ? [requiredRule] : []
},
componentProps: {
options
}
}
}
static select(
label: string,
field: string,
value: number | null,
options: ComponentOptions[],
required: Boolean = false
): FormSchema {
return {
label,
field,
component: 'Select',
value,
formItemProps: {
rules: required ? [requiredRule] : []
},
componentProps: {
options
}
}
}
static textarea(
label: string,
field: string,
rows: number,
span: number,
required: Boolean = false
): FormSchema {
return {
label,
field,
component: 'Input',
componentProps: {
type: 'textarea',
rows: rows
},
formItemProps: {
rules: required ? [requiredRule] : []
},
colProps: {
span: span
}
}
}
}
export class TableColumnBuilder {
static column(label: string, field: string): TableColumn {
return {
label,
field
}
}
static date(label: string, field: string, template?: string): TableColumn {
return {
label,
field,
formatter: (_: Recordable, __: TableColumn, cellValue: string) => {
return dayjs(cellValue).format(template || 'YYYY-MM-DD HH:mm:ss')
}
}
}
static action(width: number): TableColumn {
return {
label: t('table.action'),
field: 'action',
width: width + 'px'
}
}
}
export class ComponentOptionsBuilder {
static option(label: string, value: number): ComponentOptions {
return {
label,
value
}
}
}

View File

@ -0,0 +1,31 @@
import { JSEncrypt } from 'jsencrypt/bin/jsencrypt.min'
// 密钥对生成 http://web.chacuo.net/netrsakeypair
const publicKey =
'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdH\n' +
'nzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ=='
const privateKey =
'MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqhHyZfSsYourNxaY\n' +
'7Nt+PrgrxkiA50efORdI5U5lsW79MmFnusUA355oaSXcLhu5xxB38SMSyP2KvuKN\n' +
'PuH3owIDAQABAkAfoiLyL+Z4lf4Myxk6xUDgLaWGximj20CUf+5BKKnlrK+Ed8gA\n' +
'kM0HqoTt2UZwA5E2MzS4EI2gjfQhz5X28uqxAiEA3wNFxfrCZlSZHb0gn2zDpWow\n' +
'cSxQAgiCstxGUoOqlW8CIQDDOerGKH5OmCJ4Z21v+F25WaHYPxCFMvwxpcw99Ecv\n' +
'DQIgIdhDTIqD2jfYjPTY8Jj3EDGPbH2HHuffvflECt3Ek60CIQCFRlCkHpi7hthh\n' +
'YhovyloRYsM+IS9h/0BzlEAuO0ktMQIgSPT3aFAgJYwKpqRYKlLDVcflZFCKY7u3\n' +
'UP8iWi1Qw0Y='
// 加密
export const encrypt = (txt: string) => {
const encryptor = new JSEncrypt()
encryptor.setPublicKey(publicKey) // 设置公钥
return encryptor.encrypt(txt) // 对数据进行加密
}
// 解密
export const decrypt = (txt: string) => {
const encryptor = new JSEncrypt()
encryptor.setPrivateKey(privateKey) // 设置私钥
return encryptor.decrypt(txt) // 对数据进行解密
}

View File

@ -94,13 +94,13 @@ export const generateRoutes = (routes: AppCustomRouteRecordRaw[]): AppRouteRecor
}
return res
}
export const getRedirect = (parentPath: string, children: Array<Object>) => {
export const getRedirect = (parentPath: string, children: AppCustomRouteRecordRaw[]) => {
if (!children || children.length == 0) {
return parentPath
}
const path = generateRoutePath(parentPath, children[0]?.path)
const path = generateRoutePath(parentPath, children[0].path)
// 递归子节点
return getRedirect(path, children[0]?.children)
if (children[0].children) return getRedirect(path, children[0].children)
}
const generateRoutePath = (parentPath: string, path: string) => {
if (parentPath.endsWith('/')) {