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 1/3] 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 @@
From 420f29a272aa66bbb8f16933d8633b52d657ca56 Mon Sep 17 00:00:00 2001
From: IchliebedichZhu <54796446@qq.com>
Date: Mon, 26 Feb 2024 19:35:43 +0000
Subject: [PATCH 2/3] feat: support typescript in util files
---
src/main.ts | 6 +++---
src/utils/index.ts | 3 ++-
src/utils/utils.ts | 15 ++++++++++-----
src/utils/widgets/elementConfig.ts | 2 +-
4 files changed, 16 insertions(+), 10 deletions(-)
diff --git a/src/main.ts b/src/main.ts
index 1e92767..5ef707c 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -2,8 +2,8 @@
* @Author: ShawnPhang
* @Date: 2022-03-03 14:13:16
* @Description:
- * @LastEditors: ShawnPhang
- * @LastEditTime: 2023-06-29 15:11:46
+ * @LastEditors: Jeremy Yu
+ * @LastEditTime: 2024-02-26 17:54:00
*/
import { createApp } from 'vue'
import App from './App.vue'
@@ -20,7 +20,7 @@ elementConfig.components.forEach((component) => {
app.component(component.name, component)
})
-elementConfig.plugins.forEach((plugin: any) => {
+elementConfig.plugins.forEach((plugin) => {
app.use(plugin)
})
diff --git a/src/utils/index.ts b/src/utils/index.ts
index 1ac267f..c274639 100644
--- a/src/utils/index.ts
+++ b/src/utils/index.ts
@@ -11,12 +11,13 @@ import * as utils from './utils'
import _config from '@/config'
import modules from './plugins/modules'
import cssLoader from './plugins/cssLoader'
+import type {App} from 'vue'
/**
* 全局组件方法
*/
export default {
- install(myVue: Type.Object) {
+ install(myVue: App) {
/** 全局组件注册 */
modules(myVue)
/** iconfont 注入 */
diff --git a/src/utils/utils.ts b/src/utils/utils.ts
index d320598..d2de8de 100644
--- a/src/utils/utils.ts
+++ b/src/utils/utils.ts
@@ -1,6 +1,8 @@
import app_config from '@/config'
export const config = app_config
+type TComObj = Record
+
/**
* 星期换算
* @param {String} 'YYYY-MM-DD'
@@ -47,21 +49,24 @@ export const isInArray = (arr: Type.Object[], value: any) => {
}
return false
}
+
/** 删除多个对象元素 */
-export const deleteSome = (obj: Type.Object, arr: string[]) => {
+export const deleteSome = (obj: T, arr: string[]) => {
arr.forEach((key) => {
delete obj[key]
})
- return obj
+ return obj as R extends T ? R : Partial
}
+
/** 拾取对象元素 */
-export const pickSome = (obj: Type.Object, arr: string[]) => {
- const newObj: Type.Object = {}
+export const pickSome = (obj: T, arr: string[]) => {
+ const newObj: Record = {}
arr.forEach((key) => {
newObj[key] = obj[key]
})
- return newObj
+ return newObj as R extends T ? R : Partial
}
+
/** String长度 */
// export const getBLen = (str: string | any) => {
// if (str === null) {
diff --git a/src/utils/widgets/elementConfig.ts b/src/utils/widgets/elementConfig.ts
index 14dff5a..147a654 100644
--- a/src/utils/widgets/elementConfig.ts
+++ b/src/utils/widgets/elementConfig.ts
@@ -169,7 +169,7 @@ const components = [
// ElUpload,
]
-const plugins: any = [
+const plugins = [
ElInfiniteScroll,
ElLoading,
// ElMessage,
From 9dad9fa28c883c8ae3213e1600ae813977eaba40 Mon Sep 17 00:00:00 2001
From: IchliebedichZhu <54796446@qq.com>
Date: Thu, 29 Feb 2024 13:10:32 +0000
Subject: [PATCH 3/3] feat: add common fetch type
---
src/api/home.ts | 35 +++++++++++++++++++++++++++++--
src/api/material.ts | 30 +++++++++++++++++++++++++-
src/main.ts | 2 +-
src/types/global.d.ts | 8 +++++++
src/utils/axios.ts | 8 +++----
src/utils/index.ts | 2 +-
src/utils/utils.ts | 10 ++++-----
src/utils/widgets/loadFontRule.ts | 4 ++--
8 files changed, 82 insertions(+), 17 deletions(-)
create mode 100644 src/types/global.d.ts
diff --git a/src/api/home.ts b/src/api/home.ts
index 1b0aa9d..3df9c66 100644
--- a/src/api/home.ts
+++ b/src/api/home.ts
@@ -11,10 +11,41 @@ import _config from '@/config'
// const screenshot_url = window.location.protocol + '//' + window.location.host + '/draw'
export const download = (params: Type.Object = {}) => `${_config.SCREEN_URL}/api/screenshots?id=${params.id}&width=${params.width}&height=${params.height}`
+type IGetTempListParam = {
+ search: string
+ page: number
+ pageSize: number
+ cate:number
+}
+type IGetTempListData = {
+ cover: string
+ height: number
+ id: number
+ state: number
+ title: string
+ width: number
+}
+type IGetTempListResult = TCommResResult
+
// 获取模板列表
-export const getTempList = (params: Type.Object = {}) => fetch('design/list', params, 'get')
+export const getTempList = (params: IGetTempListParam) => fetch('design/list', params, 'get')
+
export const getTempDetail = (params: Type.Object = {}) => fetch('design/temp', params, 'get')
-export const getCategories = (params: Type.Object = {}) => fetch('design/cate', params, 'get')
+
+type TGetCategoriesParams = {
+ type?: number
+}
+export type TGetCategoriesData = {
+ id: number
+ name: string
+ pid: number
+ type: number
+}
+type TgetCategoriesResult = TCommResResult
+
+export const getCategories = (params: TGetCategoriesParams) => fetch('design/cate', params, 'get')
+
+
// 保存模板
export const saveTemp = (params: Type.Object = {}) => fetch('design/edit', params, 'post')
// export const delTemp = (params: Type.Object = {}) => fetch('/api/template/temp_del', params)
diff --git a/src/api/material.ts b/src/api/material.ts
index 21a0e8c..ec5fdb3 100644
--- a/src/api/material.ts
+++ b/src/api/material.ts
@@ -10,8 +10,36 @@ import fetch from '@/utils/axios'
// 获取素材分类:
export const getKinds = (params: Type.Object = {}) => fetch('design/cate', params)
+type TGetListParam = {
+ cate: number
+ pageSize: number
+}
+
+export type TGetListData = {
+ category: number
+ created_time: string
+ height: number
+ id: number
+ model: string
+ original: string
+ state: number
+ thumb: string
+ title: string
+ type: string
+ updated_time: string
+ url: string
+ width: number
+}
+
+type TGetListResult = TCommResResult<{
+ list: TGetListData
+ total: number
+}>
+
+
+
// 获取素材列表:
-export const getList = (params: Type.Object = {}) => fetch('design/material', params)
+export const getList = (params: TGetListParam) => fetch('design/material', params)
// 获取字体
export const getFonts = (params: Type.Object = {}) => fetch('design/fonts', params)
diff --git a/src/main.ts b/src/main.ts
index 5ef707c..d960b07 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -2,7 +2,7 @@
* @Author: ShawnPhang
* @Date: 2022-03-03 14:13:16
* @Description:
- * @LastEditors: Jeremy Yu
+ * @LastEditors: ShawnPhang , Jeremy Yu
* @LastEditTime: 2024-02-26 17:54:00
*/
import { createApp } from 'vue'
diff --git a/src/types/global.d.ts b/src/types/global.d.ts
new file mode 100644
index 0000000..789280e
--- /dev/null
+++ b/src/types/global.d.ts
@@ -0,0 +1,8 @@
+
+/** 公共API返回结果 */
+type TCommResResult = {
+ code: number
+ msg: string
+ result: T
+}
+
diff --git a/src/utils/axios.ts b/src/utils/axios.ts
index 6f2d8a7..68a49af 100644
--- a/src/utils/axios.ts
+++ b/src/utils/axios.ts
@@ -2,7 +2,7 @@
* @Author: ShawnPhang
* @Date: 2021-07-13 02:48:38
* @Description: 本地测试项目请勿修改此文件
- * @LastEditors: Jeremy Yu
+ * @LastEditors: ShawnPhang , Jeremy Yu
* @LastEditTime: 2024-02-26 17:54:00
*/
import axios, { AxiosRequestConfig, AxiosResponse, AxiosStatic } from 'axios'
@@ -79,13 +79,13 @@ type TFetchMethod = keyof Pick<
>
// export default axios;
-const fetch = (
+const fetch = (
url: string,
params: TFetchRequestConfigParams,
type: TFetchMethod = 'get',
exheaders: Record = {},
extra: Record = {}
-) => {
+): Promise> => {
if (params?._noLoading) {
delete params._noLoading
} else {
@@ -106,7 +106,7 @@ const fetch = (
return axios[type](url, params, {
headers: Object.assign(headerObject, exheaders),
...extra,
- })
+ }) as Promise>
}
}
diff --git a/src/utils/index.ts b/src/utils/index.ts
index c274639..146ddb4 100644
--- a/src/utils/index.ts
+++ b/src/utils/index.ts
@@ -2,7 +2,7 @@
* @Author: ShawnPhang
* @Date: 2021-07-13 02:48:38
* @Description:
- * @LastEditors: ShawnPhang
+ * @LastEditors: ShawnPhang , Jeremy Yu
* @LastEditTime: 2022-03-07 20:25:54
*/
// import store from '../store'
diff --git a/src/utils/utils.ts b/src/utils/utils.ts
index d2de8de..30b9d07 100644
--- a/src/utils/utils.ts
+++ b/src/utils/utils.ts
@@ -40,12 +40,10 @@ type TComObj = Record
// }
// }
// 判断是否在数组中并返回下标
-export const isInArray = (arr: Type.Object[], value: any) => {
- if (arr.indexOf && typeof arr.indexOf === 'function') {
- const index = arr.indexOf(value)
- if (index >= 0) {
- return index
- }
+export const isInArray = (arr: (string | number)[], value: (string | number)) => {
+ const index = arr.indexOf(value)
+ if (index >= 0) {
+ return index
}
return false
}
diff --git a/src/utils/widgets/loadFontRule.ts b/src/utils/widgets/loadFontRule.ts
index 6556a72..a35d0bb 100644
--- a/src/utils/widgets/loadFontRule.ts
+++ b/src/utils/widgets/loadFontRule.ts
@@ -2,8 +2,8 @@
* @Author: ShawnPhang
* @Date: 2023-08-23 17:37:16
* @Description: 提取字体子集
- * @LastEditors: ShawnPhang
- * @LastEditTime: 2023-10-14 18:31:29
+ * @LastEditors: ShawnPhang , Jeremy Yu
+ * @LastEditTime: 2024-02-27 10:32:00
*/
/**
* 只有ttf/otf这种原始字体支持提取,如果服务端不支持该功能请设置false,以保证页面能加载字体。