From c159a511469556ff822fe02c07dd55e94ff2b094 Mon Sep 17 00:00:00 2001 From: IchliebedichZhu <54796446@qq.com> Date: Mon, 26 Feb 2024 17:58:41 +0000 Subject: [PATCH] feat: support typescript in common request files --- .eslintrc.js | 1 + .vscode/settings.json | 2 +- src/config.ts | 4 ++++ src/env.d.ts | 8 -------- src/types/env.d.ts | 5 +++-- src/utils/axios.ts | 42 ++++++++++++++++++++++++++---------------- src/views/Index.vue | 3 ++- 7 files changed, 37 insertions(+), 28 deletions(-) delete mode 100644 src/env.d.ts diff --git a/.eslintrc.js b/.eslintrc.js index eaec327..804817c 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -13,6 +13,7 @@ module.exports = { // 自定义你的规则 'vue/component-tags-order': ['off'], 'vue/no-multiple-template-root': ['off'], + 'max-params': ['off'], // 'no-undef': 'off', // 禁止使用未定义的变量,会把TS声明视为变量,暂时关闭 }, parserOptions: { diff --git a/.vscode/settings.json b/.vscode/settings.json index 24c30f1..d2284bc 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,7 +5,7 @@ "editor.defaultFormatter": "esbenp.prettier-vscode", "eslint.validate": ["javascript", "javascriptreact", "vue", "typescript", "typescriptreact"], "editor.codeActionsOnSave": { - "source.fixAll.eslint": true + "source.fixAll.eslint": "explicit" }, "css.validate": false, "less.validate": false, diff --git a/src/config.ts b/src/config.ts index 92c73f9..9a5862a 100644 --- a/src/config.ts +++ b/src/config.ts @@ -20,3 +20,7 @@ export default { QINIUYUN_PLUGIN: 'https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/qiniu-js/2.5.5/qiniu.min.js', supportSubFont: true, // 是否开启服务端字体压缩 } + +export const LocalStorageKey = { + tokenKey: "xp_token" +} diff --git a/src/env.d.ts b/src/env.d.ts deleted file mode 100644 index 32fd09e..0000000 --- a/src/env.d.ts +++ /dev/null @@ -1,8 +0,0 @@ -// / - -declare module '*.vue' { - import { DefineComponent } from 'vue'; - // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/ban-types - const component: DefineComponent<{}, {}, any>; - export default component; -} \ No newline at end of file diff --git a/src/types/env.d.ts b/src/types/env.d.ts index 7deacdb..641e18c 100644 --- a/src/types/env.d.ts +++ b/src/types/env.d.ts @@ -1,4 +1,4 @@ -/// +// / interface ImportMeta { url: string @@ -23,7 +23,8 @@ interface ImportMeta { on(event: string, cb: (...args: any[]) => void): void } - readonly env: ImportMetaEnv + // readonly env: ImportMetaEnv + glob(pattern: string): Record< string, diff --git a/src/utils/axios.ts b/src/utils/axios.ts index bf3f462..6f2d8a7 100644 --- a/src/utils/axios.ts +++ b/src/utils/axios.ts @@ -2,12 +2,12 @@ * @Author: ShawnPhang * @Date: 2021-07-13 02:48:38 * @Description: 本地测试项目请勿修改此文件 - * @LastEditors: ShawnPhang - * @LastEditTime: 2024-01-11 17:36:33 + * @LastEditors: Jeremy Yu + * @LastEditTime: 2024-02-26 17:54:00 */ -import axios from 'axios' +import axios, { AxiosRequestConfig, AxiosResponse, AxiosStatic } from 'axios' import store from '@/store' -import app_config from '@/config' +import app_config, { LocalStorageKey } from '@/config' axios.defaults.timeout = 30000 axios.defaults.headers.authorization = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MTAwMDEsImV4cCI6MTc4ODU3NDc1MDU4NX0.L_t6DFD48Dm6rUPfgIgOWJkz18En1m_-hhMHcpbxliY'; @@ -16,15 +16,15 @@ const baseUrl = app_config.API_URL // 请求拦截器 axios.interceptors.request.use( - (config: Type.Object) => { + (config: AxiosRequestConfig) => { // const access_token = store.state.currentUser.access_token; - const url = config.url + const url = config.url ?? "" const values = {} // values.access_token = access_token; // values.version = version; - if (url.indexOf('http://') !== 0 && url.indexOf('https://') !== 0) { - url.indexOf('/') === 0 ? (config.url = baseUrl + url) : (config.url = baseUrl + '/' + url) + if (!url.startsWith('http://') && !url.startsWith('https://')) { + config.url = url.startsWith('/') ? baseUrl + url : config.url = baseUrl + '/' + url } if (config.method === 'get') { @@ -44,10 +44,8 @@ axios.interceptors.request.use( ) // 响应拦截器 -axios.interceptors.response.use( - (res: Type.Object) => { +axios.interceptors.response.use((res: AxiosResponse) => { // store.dispatch('hideLoading'); - // 接口规则:只有正确code为200时返回result结果对象,错误返回整个结果对象 if (!res.data) { @@ -74,16 +72,28 @@ axios.interceptors.response.use( }, ) +type TFetchRequestConfigParams = AxiosRequestConfig & Record +type TFetchMethod = keyof Pick< + AxiosStatic, + "get" | "post" | "put" | "getUri" | "request" | "delete" | "head" | "options" | "patch" +> + // export default axios; -const fetch = (url: string, params: Type.Object, type: string | undefined = 'get', exheaders: Type.Object = {}, extra: any = {}) => { - if (params && params._noLoading) { +const fetch = ( + url: string, + params: TFetchRequestConfigParams, + type: TFetchMethod = 'get', + exheaders: Record = {}, + extra: Record = {} +) => { + if (params?._noLoading) { delete params._noLoading } else { // store.commit('loading', '加载中..'); } - const token = localStorage.getItem('xp_token') - const headerObject: Type.Object = { } + const token = localStorage.getItem(LocalStorageKey.tokenKey) + const headerObject: Record = {} token && (headerObject.authorization = token) if (type === 'get') { @@ -93,7 +103,7 @@ const fetch = (url: string, params: Type.Object, type: string | undefined = 'get ...extra, }) } else { - return (axios as Type.Object)[type](url, params, { + return axios[type](url, params, { headers: Object.assign(headerObject, exheaders), ...extra, }) diff --git a/src/views/Index.vue b/src/views/Index.vue index 065b784..f2e4db4 100644 --- a/src/views/Index.vue +++ b/src/views/Index.vue @@ -1,8 +1,9 @@