From ee65ec9f72cb9ba19ca69681a698d8ebe956ff07 Mon Sep 17 00:00:00 2001 From: xiaoxian521 <1923740402@qq.com> Date: Sun, 11 Sep 2022 16:46:21 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BD=BF=E7=94=A8`/**=20*/`=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2`//`=E6=B3=A8=E9=87=8A=EF=BC=8C=E5=AF=B9=E7=BC=96?= =?UTF-8?q?=E8=BE=91=E5=99=A8=E7=9A=84=E6=99=BA=E8=83=BD=E6=8F=90=E7=A4=BA?= =?UTF-8?q?=E6=9B=B4=E5=8F=8B=E5=A5=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/index.ts | 8 ++++---- src/api/user.ts | 6 +++--- src/config/index.ts | 2 +- src/plugins/i18n.ts | 2 +- src/router/index.ts | 14 +++++++------- src/router/utils.ts | 20 ++++++++++---------- src/store/modules/epTheme.ts | 2 +- src/store/modules/permission.ts | 4 ++-- src/store/modules/user.ts | 6 +++--- src/utils/auth.ts | 7 +++---- src/utils/http/index.ts | 14 +++++++------- vite.config.ts | 6 +++--- 12 files changed, 45 insertions(+), 46 deletions(-) diff --git a/build/index.ts b/build/index.ts index c6df2e0..192bb7b 100644 --- a/build/index.ts +++ b/build/index.ts @@ -1,6 +1,6 @@ -// 处理环境变量 +/** 处理环境变量 */ const warpperEnv = (envConf: Recordable): ViteEnv => { - // 此处为默认值,无需修改 + /** 此处为默认值,无需修改 */ const ret: ViteEnv = { VITE_PORT: 8848, VITE_PUBLIC_PATH: "", @@ -28,12 +28,12 @@ const warpperEnv = (envConf: Recordable): ViteEnv => { return ret; }; -// 跨域代理重写 +/** 跨域代理重写 */ const regExps = (value: string, reg: string): string => { return value.replace(new RegExp(`^${reg}`, "g"), ""); }; -// 环境变量 +/** 环境变量 */ const loadEnv = (): ViteEnv => { return import.meta.env; }; diff --git a/src/api/user.ts b/src/api/user.ts index 5492f3d..3965ab5 100644 --- a/src/api/user.ts +++ b/src/api/user.ts @@ -6,17 +6,17 @@ type Result = { info?: object; }; -// 获取验证码 +/** 获取验证码 */ export const getVerify = () => { return http.request("get", "/captcha"); }; -// 登录 +/** 登录 */ export const getLogin = (data: object) => { return http.request("post", "/login", { data }); }; -// 刷新token +/** 刷新token */ export const refreshToken = (data: object) => { return http.request("post", "/refreshToken", { data }); }; diff --git a/src/config/index.ts b/src/config/index.ts index 7b0426a..8365018 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -27,7 +27,7 @@ const getConfig = (key?: string): ServerConfigs => { return config; }; -// 获取项目动态全局配置 +/** 获取项目动态全局配置 */ export const getServerConfig = async (app: App): Promise => { app.config.globalProperties.$config = getConfig(); return axios({ diff --git a/src/plugins/i18n.ts b/src/plugins/i18n.ts index c1fedda..8266d08 100644 --- a/src/plugins/i18n.ts +++ b/src/plugins/i18n.ts @@ -58,7 +58,7 @@ export function transformI18n(message: any = "") { } } -// 此函数只是配合i18n Ally插件来进行国际化智能提示,并无实际意义(只对提示起作用),如果不需要国际化可删除 +/** 此函数只是配合i18n Ally插件来进行国际化智能提示,并无实际意义(只对提示起作用),如果不需要国际化可删除 */ export const $t = (key: string) => key; export const i18n: I18n = createI18n({ diff --git a/src/router/index.ts b/src/router/index.ts index a260b1e..998a736 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -32,25 +32,25 @@ import homeRouter from "./modules/home"; import errorRouter from "./modules/error"; import remainingRouter from "./modules/remaining"; -// 原始静态路由(未做任何处理) +/** 原始静态路由(未做任何处理) */ const routes = [homeRouter, errorRouter]; -// 导出处理后的静态路由(三级及以上的路由全部拍成二级) +/** 导出处理后的静态路由(三级及以上的路由全部拍成二级) */ export const constantRoutes: Array = formatTwoStageRoutes( formatFlatteningRoutes(buildHierarchyTree(ascending(routes))) ); -// 用于渲染菜单,保持原始层级 +/** 用于渲染菜单,保持原始层级 */ export const constantMenus: Array = ascending(routes).concat( ...remainingRouter ); -// 不参与菜单的路由 +/** 不参与菜单的路由 */ export const remainingPaths = Object.keys(remainingRouter).map(v => { return remainingRouter[v].path; }); -// 创建路由实例 +/** 创建路由实例 */ export const router: Router = createRouter({ history: getHistoryMode(), routes: constantRoutes.concat(...(remainingRouter as any)), @@ -70,7 +70,7 @@ export const router: Router = createRouter({ } }); -// 重置路由 +/** 重置路由 */ export function resetRouter() { router.getRoutes().forEach(route => { const { name, meta } = route; @@ -84,7 +84,7 @@ export function resetRouter() { usePermissionStoreHook().clearAllCachePage(); } -// 路由白名单 +/** 路由白名单 */ const whiteList = ["/login"]; router.beforeEach((to: toRouteType, _from, next) => { diff --git a/src/router/utils.ts b/src/router/utils.ts index 62d6bb0..c23b98e 100644 --- a/src/router/utils.ts +++ b/src/router/utils.ts @@ -21,7 +21,7 @@ const modulesRoutes = import.meta.glob("/src/views/**/*.{vue,tsx}"); // 动态路由 import { getAsyncRoutes } from "/@/api/routes"; -// 按照路由中meta下的rank等级升序来排序路由 +/** 按照路由中meta下的rank等级升序来排序路由 */ function ascending(arr: any[]) { arr.forEach(v => { if (v?.meta?.rank === null) v.meta.rank = undefined; @@ -38,7 +38,7 @@ function ascending(arr: any[]) { ); } -// 过滤meta中showLink为false的路由 +/** 过滤meta中showLink为false的路由 */ function filterTree(data: RouteComponent[]) { const newTree = cloneDeep(data).filter( (v: { meta: { showLink: boolean } }) => v.meta?.showLink !== false @@ -49,7 +49,7 @@ function filterTree(data: RouteComponent[]) { return newTree; } -// 批量删除缓存路由(keepalive) +/** 批量删除缓存路由(keepalive) */ function delAliveRoutes(delAliveRouteList: Array) { delAliveRouteList.forEach(route => { usePermissionStoreHook().cacheOperate({ @@ -59,7 +59,7 @@ function delAliveRoutes(delAliveRouteList: Array) { }); } -// 通过path获取父级路径 +/** 通过path获取父级路径 */ function getParentPaths(path: string, routes: RouteRecordRaw[]) { // 深度遍历查找 function dfs(routes: RouteRecordRaw[], path: string, parents: string[]) { @@ -83,7 +83,7 @@ function getParentPaths(path: string, routes: RouteRecordRaw[]) { return dfs(routes, path, []); } -// 查找对应path的路由信息 +/** 查找对应path的路由信息 */ function findRouteByPath(path: string, routes: RouteRecordRaw[]) { let res = routes.find((item: { path: string }) => item.path == path); if (res) { @@ -114,7 +114,7 @@ function addPathMatch() { } } -// 初始化路由 +/** 初始化路由 */ function initRouter(name: string) { return new Promise(resolve => { getAsyncRoutes({ name }).then(({ info }) => { @@ -195,7 +195,7 @@ function formatTwoStageRoutes(routesList: RouteRecordRaw[]) { return newRoutesList; } -// 处理缓存路由(添加、删除、刷新) +/** 处理缓存路由(添加、删除、刷新) */ function handleAliveRoute(matched: RouteRecordNormalized[], mode?: string) { switch (mode) { case "add": @@ -222,7 +222,7 @@ function handleAliveRoute(matched: RouteRecordNormalized[], mode?: string) { } } -// 过滤后端传来的动态路由 重新生成规范路由 +/** 过滤后端传来的动态路由 重新生成规范路由 */ function addAsyncRoutes(arrRoutes: Array) { if (!arrRoutes || !arrRoutes.length) return; const modulesRoutesKeys = Object.keys(modulesRoutes); @@ -251,7 +251,7 @@ function addAsyncRoutes(arrRoutes: Array) { return arrRoutes; } -// 获取路由历史模式 https://next.router.vuejs.org/zh/guide/essentials/history-mode.html +/** 获取路由历史模式 https://next.router.vuejs.org/zh/guide/essentials/history-mode.html */ function getHistoryMode(): RouterHistory { const routerHistory = loadEnv().VITE_ROUTER_HISTORY; // len为1 代表只有历史模式 为2 代表历史模式中存在base参数 https://next.router.vuejs.org/zh/api/#%E5%8F%82%E6%95%B0-1 @@ -275,7 +275,7 @@ function getHistoryMode(): RouterHistory { } } -// 是否有权限 +/** 是否有权限 */ function hasPermissions(value: Array): boolean { if (value && value instanceof Array && value.length > 0) { const roles = usePermissionStoreHook().buttonAuth; diff --git a/src/store/modules/epTheme.ts b/src/store/modules/epTheme.ts index fa75493..4d14aec 100644 --- a/src/store/modules/epTheme.ts +++ b/src/store/modules/epTheme.ts @@ -18,7 +18,7 @@ export const useEpThemeStore = defineStore({ getEpThemeColor() { return this.epThemeColor; }, - // 用于mix导航模式下hamburger-svg的fill属性 + /** 用于mix导航模式下hamburger-svg的fill属性 */ fill() { if (this.epTheme === "light") { return "#409eff"; diff --git a/src/store/modules/permission.ts b/src/store/modules/permission.ts index 27c5dae..ed32425 100644 --- a/src/store/modules/permission.ts +++ b/src/store/modules/permission.ts @@ -20,7 +20,7 @@ export const usePermissionStore = defineStore({ cachePageList: [] }), actions: { - // 获取异步路由菜单 + /** 获取异步路由菜单 */ asyncActionRoutes(routes) { if (this.wholeMenus.length > 0) return; this.wholeMenus = filterTree( @@ -61,7 +61,7 @@ export const usePermissionStore = defineStore({ break; } }, - // 清空缓存页面 + /** 清空缓存页面 */ clearAllCachePage() { this.wholeMenus = []; this.menusTree = []; diff --git a/src/store/modules/user.ts b/src/store/modules/user.ts index 5849011..5c8192e 100644 --- a/src/store/modules/user.ts +++ b/src/store/modules/user.ts @@ -32,7 +32,7 @@ export const useUserStore = defineStore({ SET_NAME(name) { this.name = name; }, - // 登入 + /** 登入 */ async loginByUsername(data) { return new Promise((resolve, reject) => { getLogin(data) @@ -47,7 +47,7 @@ export const useUserStore = defineStore({ }); }); }, - // 登出 清空缓存 + /** 登出 清空缓存 */ logOut() { this.token = ""; this.name = ""; @@ -56,7 +56,7 @@ export const useUserStore = defineStore({ useMultiTagsStoreHook().handleTags("equal", routerArrays); router.push("/login"); }, - // 刷新token + /** 刷新token */ async refreshToken(data) { removeToken(); return refreshToken(data).then(data => { diff --git a/src/utils/auth.ts b/src/utils/auth.ts index ea02bce..b592b1d 100644 --- a/src/utils/auth.ts +++ b/src/utils/auth.ts @@ -9,14 +9,13 @@ type paramsMapType = { accessToken: string; }; -// 获取token +/** 获取token */ export function getToken() { // 此处与TokenKey相同,此写法解决初始化时Cookies中不存在TokenKey报错 return Cookies.get("authorized-token"); } -// 设置token以及过期时间(cookies、sessionStorage各一份) -// 后端需要将用户信息和token以及过期时间都返回给前端,过期时间主要用于刷新token +/** 设置token以及过期时间(cookies、sessionStorage各一份),后端需要将用户信息和token以及过期时间都返回给前端,过期时间主要用于刷新token */ export function setToken(data) { const { accessToken, expires, name } = data; // 提取关键信息进行存储 @@ -36,7 +35,7 @@ export function setToken(data) { sessionStorage.setItem(TokenKey, dataString); } -// 删除token +/** 删除token */ export function removeToken() { Cookies.remove(TokenKey); sessionStorage.removeItem(TokenKey); diff --git a/src/utils/http/index.ts b/src/utils/http/index.ts index 59d5bd8..6eab82c 100644 --- a/src/utils/http/index.ts +++ b/src/utils/http/index.ts @@ -38,13 +38,13 @@ class PureHttp { this.httpInterceptorsRequest(); this.httpInterceptorsResponse(); } - // 初始化配置对象 + /** 初始化配置对象 */ private static initConfig: PureHttpRequestConfig = {}; - // 保存当前Axios实例对象 + /** 保存当前Axios实例对象 */ private static axiosInstance: AxiosInstance = Axios.create(defaultConfig); - // 请求拦截 + /** 请求拦截 */ private httpInterceptorsRequest(): void { PureHttp.axiosInstance.interceptors.request.use( (config: PureHttpRequestConfig) => { @@ -87,7 +87,7 @@ class PureHttp { ); } - // 响应拦截 + /** 响应拦截 */ private httpInterceptorsResponse(): void { const instance = PureHttp.axiosInstance; instance.interceptors.response.use( @@ -117,7 +117,7 @@ class PureHttp { ); } - // 通用请求工具函数 + /** 通用请求工具函数 */ public request( method: RequestMethods, url: string, @@ -144,7 +144,7 @@ class PureHttp { }); } - // 单独抽离的post工具函数 + /** 单独抽离的post工具函数 */ public post( url: string, params?: T, @@ -153,7 +153,7 @@ class PureHttp { return this.request

("post", url, params, config); } - // 单独抽离的get工具函数 + /** 单独抽离的get工具函数 */ public get( url: string, params?: T, diff --git a/vite.config.ts b/vite.config.ts index 5244c03..46e3270 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -5,15 +5,15 @@ import { warpperEnv, regExps } from "./build"; import { getPluginsList } from "./build/plugins"; import { UserConfigExport, ConfigEnv, loadEnv } from "vite"; -// 当前执行node命令时文件夹的地址(工作目录) +/** 当前执行node命令时文件夹的地址(工作目录) */ const root: string = process.cwd(); -// 路径查找 +/** 路径查找 */ const pathResolve = (dir: string): string => { return resolve(__dirname, ".", dir); }; -// 设置别名 +/** 设置别名 */ const alias: Record = { "/@": pathResolve("src"), "@build": pathResolve("build")