@gx-design-vue/create-gx-cli 0.1.19 → 0.1.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -4
- package/dist/index.js +36 -48
- package/package.json +5 -3
- package/template-mobile-vant-cli/.editorconfig +19 -0
- package/template-mobile-vant-cli/.env +3 -0
- package/template-mobile-vant-cli/.env.development +17 -0
- package/template-mobile-vant-cli/.env.pro +28 -0
- package/template-mobile-vant-cli/.env.production +31 -0
- package/template-mobile-vant-cli/.eslintignore +15 -0
- package/template-mobile-vant-cli/.prettierignore +9 -0
- package/template-mobile-vant-cli/README.md +1 -0
- package/template-mobile-vant-cli/commitlint.config.cjs +32 -0
- package/template-mobile-vant-cli/config/default/defaultSettings.ts +70 -0
- package/template-mobile-vant-cli/config/default/network.ts +10 -0
- package/template-mobile-vant-cli/config/default/proxy.ts +50 -0
- package/template-mobile-vant-cli/config/default/theme.ts +3 -0
- package/template-mobile-vant-cli/config/index.ts +11 -0
- package/template-mobile-vant-cli/eslint.config.js +58 -0
- package/template-mobile-vant-cli/index.html +25 -0
- package/template-mobile-vant-cli/internal/vite-config/generate/generateModifyVars.ts +7 -0
- package/template-mobile-vant-cli/internal/vite-config/rollupOptions/index.ts +22 -0
- package/template-mobile-vant-cli/internal/vite-config/util/hash.ts +17 -0
- package/template-mobile-vant-cli/internal/vite-config/util/index.ts +132 -0
- package/template-mobile-vant-cli/internal/vite-config/vite/cdn.ts +65 -0
- package/template-mobile-vant-cli/internal/vite-config/vite/optimizer.ts +26 -0
- package/template-mobile-vant-cli/internal/vite-config/vite/plugin/appConfig.ts +91 -0
- package/template-mobile-vant-cli/internal/vite-config/vite/plugin/autoImport.ts +28 -0
- package/template-mobile-vant-cli/internal/vite-config/vite/plugin/compress.ts +31 -0
- package/template-mobile-vant-cli/internal/vite-config/vite/plugin/html.ts +32 -0
- package/template-mobile-vant-cli/internal/vite-config/vite/plugin/index.ts +74 -0
- package/template-mobile-vant-cli/internal/vite-config/vite/plugin/visualizer.ts +14 -0
- package/template-mobile-vant-cli/internal/vite-config/vite/plugin/viteNotice.ts +40 -0
- package/template-mobile-vant-cli/mock/config/permissions.ts +15 -0
- package/template-mobile-vant-cli/mock/config/user/id.ts +5 -0
- package/template-mobile-vant-cli/mock/config/user/index.ts +96 -0
- package/template-mobile-vant-cli/mock/index.ts +55 -0
- package/template-mobile-vant-cli/mock/routers/table/index.fake.ts +22 -0
- package/template-mobile-vant-cli/mock/routers/user/account.fake.ts +52 -0
- package/template-mobile-vant-cli/mock/routers/user/index.fake.ts +46 -0
- package/template-mobile-vant-cli/mock/utils/crypto.ts +21 -0
- package/template-mobile-vant-cli/mock/utils/table.ts +96 -0
- package/template-mobile-vant-cli/mock/utils/util.ts +91 -0
- package/template-mobile-vant-cli/package.json +74 -0
- package/template-mobile-vant-cli/public/css/default.css +54 -0
- package/template-mobile-vant-cli/public/favicon.ico +0 -0
- package/template-mobile-vant-cli/src/App.vue +8 -0
- package/template-mobile-vant-cli/src/assets/logo.png +0 -0
- package/template-mobile-vant-cli/src/components/PageContainer/ProSkeleton.tsx +29 -0
- package/template-mobile-vant-cli/src/components/PageContainer/index.tsx +207 -0
- package/template-mobile-vant-cli/src/components/PageContainer/style.module.less +17 -0
- package/template-mobile-vant-cli/src/components/TabsMenu/index.vue +45 -0
- package/template-mobile-vant-cli/src/core/gx-design/index.ts +7 -0
- package/template-mobile-vant-cli/src/core/index.ts +5 -0
- package/template-mobile-vant-cli/src/design/color.less +1 -0
- package/template-mobile-vant-cli/src/design/config.less +5 -0
- package/template-mobile-vant-cli/src/design/index.less +3 -0
- package/template-mobile-vant-cli/src/design/mixin.less +65 -0
- package/template-mobile-vant-cli/src/design/reset.less +39 -0
- package/template-mobile-vant-cli/src/design/root.less +3 -0
- package/template-mobile-vant-cli/src/design/vant/index.less +0 -0
- package/template-mobile-vant-cli/src/hooks/web/index.ts +5 -0
- package/template-mobile-vant-cli/src/hooks/web/usePageLoading.ts +56 -0
- package/template-mobile-vant-cli/src/layout/BasicLayout/index.vue +18 -0
- package/template-mobile-vant-cli/src/layout/BasicLayout/style.less +3 -0
- package/template-mobile-vant-cli/src/layout/UserLayout/index.vue +18 -0
- package/template-mobile-vant-cli/src/layout/UserLayout/style.module.less +36 -0
- package/template-mobile-vant-cli/src/main.ts +34 -0
- package/template-mobile-vant-cli/src/pages/Account/details.vue +13 -0
- package/template-mobile-vant-cli/src/pages/Account/index.vue +102 -0
- package/template-mobile-vant-cli/src/pages/Home/index.vue +71 -0
- package/template-mobile-vant-cli/src/pages/user/login/index.vue +76 -0
- package/template-mobile-vant-cli/src/plugins/dayjs/index.ts +3 -0
- package/template-mobile-vant-cli/src/plugins/index.ts +2 -0
- package/template-mobile-vant-cli/src/router/guard/index.ts +18 -0
- package/template-mobile-vant-cli/src/router/guard/permissions.ts +41 -0
- package/template-mobile-vant-cli/src/router/guard/stateGuard.ts +10 -0
- package/template-mobile-vant-cli/src/router/index.ts +31 -0
- package/template-mobile-vant-cli/src/router/routes/index.ts +46 -0
- package/template-mobile-vant-cli/src/router/routes/modules/dataSource.ts +32 -0
- package/template-mobile-vant-cli/src/services/index.ts +31 -0
- package/template-mobile-vant-cli/src/services/userCenter/account.ts +42 -0
- package/template-mobile-vant-cli/src/services/userCenter/index.ts +28 -0
- package/template-mobile-vant-cli/src/store/index.ts +20 -0
- package/template-mobile-vant-cli/src/store/modules/global.ts +23 -0
- package/template-mobile-vant-cli/src/store/modules/user.ts +107 -0
- package/template-mobile-vant-cli/src/utils/accessToken.ts +97 -0
- package/template-mobile-vant-cli/src/utils/crypto/index.ts +56 -0
- package/template-mobile-vant-cli/src/utils/env.ts +50 -0
- package/template-mobile-vant-cli/src/utils/pageTitle.ts +20 -0
- package/template-mobile-vant-cli/src/utils/request/XHR.ts +137 -0
- package/template-mobile-vant-cli/src/utils/request/axiosCancel.ts +69 -0
- package/template-mobile-vant-cli/src/utils/request/checkStatus.ts +25 -0
- package/template-mobile-vant-cli/src/utils/request/index.ts +155 -0
- package/template-mobile-vant-cli/src/utils/request/typings.ts +171 -0
- package/template-mobile-vant-cli/src/utils/storage.ts +215 -0
- package/template-mobile-vant-cli/src/utils/util.ts +27 -0
- package/template-mobile-vant-cli/src/utils/validate.ts +211 -0
- package/template-mobile-vant-cli/tsconfig.json +50 -0
- package/template-mobile-vant-cli/types/auto-imports.d.ts +80 -0
- package/template-mobile-vant-cli/types/components.d.ts +7 -0
- package/template-mobile-vant-cli/types/config.d.ts +56 -0
- package/template-mobile-vant-cli/types/global.d.ts +80 -0
- package/template-mobile-vant-cli/types/mock.d.ts +34 -0
- package/template-mobile-vant-cli/types/module.d.ts +61 -0
- package/template-mobile-vant-cli/types/response.d.ts +17 -0
- package/template-mobile-vant-cli/types/system.d.ts +58 -0
- package/template-mobile-vant-cli/types/vant-import.d.ts +25 -0
- package/template-mobile-vant-cli/unocss.config.ts +161 -0
- package/template-mobile-vant-cli/vite.config.ts +142 -0
- package/template-mobile-vant-html/README.md +4 -0
- package/template-mobile-vant-html/css/global.css +293 -0
- package/template-mobile-vant-html/css/global.css.map +1 -0
- package/template-mobile-vant-html/css/global.less +293 -0
- package/template-mobile-vant-html/css/index.css +20 -0
- package/template-mobile-vant-html/css/index.css.map +1 -0
- package/template-mobile-vant-html/css/index.less +21 -0
- package/template-mobile-vant-html/image/disconnected.svg +60 -0
- package/template-mobile-vant-html/image/empty.svg +52 -0
- package/template-mobile-vant-html/image/error.svg +53 -0
- package/template-mobile-vant-html/image/loading.svg +1 -0
- package/template-mobile-vant-html/index.html +91 -0
- package/template-mobile-vant-html/js/index.js +29 -0
- package/template-mobile-vant-html/js/plugin/day.min.js +1 -0
- package/template-mobile-vant-html/js/plugin/fastclick.js +730 -0
- package/template-mobile-vant-html/js/plugin/jquery.min.js +2 -0
- package/template-mobile-vant-html/js/plugin/rem.js +28 -0
- package/template-mobile-vant-html/js/utils/appUtil.js +70 -0
- package/template-mobile-vant-html/js/utils/config.js +3 -0
- package/template-mobile-vant-html/js/utils/request.js +61 -0
- package/template-mobile-vant-html/js/utils/utils.js +65 -0
- package/template-mobile-vant-html/js/utils/validate.js +292 -0
- package/template-vite-project/.env +5 -0
- package/template-vite-project/.env.development +5 -0
- package/template-vite-project/.env.pro +5 -0
- package/template-vite-project/.env.production +5 -0
- package/template-vite-project/README.md +5 -0
- package/template-vite-project/_gitignore +24 -0
- package/template-vite-project/index.html +46 -0
- package/template-vite-project/internal/proxy/index.ts +47 -0
- package/template-vite-project/internal/vite/generate/generateModifyVars.ts +9 -0
- package/template-vite-project/internal/vite/rollupOptions/index.ts +18 -0
- package/template-vite-project/internal/vite/util/hash.ts +17 -0
- package/template-vite-project/internal/vite/util/index.ts +132 -0
- package/template-vite-project/internal/vite/vite/cdn.ts +65 -0
- package/template-vite-project/internal/vite/vite/plugin/appConfig.ts +91 -0
- package/template-vite-project/internal/vite/vite/plugin/autoImport.ts +28 -0
- package/template-vite-project/internal/vite/vite/plugin/compress.ts +31 -0
- package/template-vite-project/internal/vite/vite/plugin/html.ts +24 -0
- package/template-vite-project/internal/vite/vite/plugin/index.ts +72 -0
- package/template-vite-project/internal/vite/vite/plugin/visualizer.ts +14 -0
- package/template-vite-project/internal/vite/vite/plugin/viteNotice.ts +40 -0
- package/template-vite-project/node_modules/.bin/cross-env +21 -0
- package/template-vite-project/node_modules/.bin/cross-env-shell +21 -0
- package/template-vite-project/node_modules/.bin/esbuild +21 -0
- package/template-vite-project/node_modules/.bin/jiti +21 -0
- package/template-vite-project/node_modules/.bin/lessc +21 -0
- package/template-vite-project/node_modules/.bin/parser +21 -0
- package/template-vite-project/node_modules/.bin/rollup +21 -0
- package/template-vite-project/node_modules/.bin/terser +21 -0
- package/template-vite-project/node_modules/.bin/tsc +21 -0
- package/template-vite-project/node_modules/.bin/tsserver +21 -0
- package/template-vite-project/node_modules/.bin/vite +21 -0
- package/template-vite-project/node_modules/.bin/vue-tsc +21 -0
- package/template-vite-project/node_modules/.bin/yaml +21 -0
- package/template-vite-project/package.json +38 -0
- package/template-vite-project/public/css/index.css +89 -0
- package/template-vite-project/public/css/normalize.css +396 -0
- package/template-vite-project/public/vite.svg +1 -0
- package/template-vite-project/src/App.vue +30 -0
- package/template-vite-project/src/assets/vue.svg +1 -0
- package/template-vite-project/src/components/HelloWorld.vue +41 -0
- package/template-vite-project/src/design/config.less +0 -0
- package/template-vite-project/src/design/index.less +0 -0
- package/template-vite-project/src/main.ts +14 -0
- package/template-vite-project/src/utils/env.ts +50 -0
- package/template-vite-project/tsconfig.app.json +59 -0
- package/template-vite-project/tsconfig.json +7 -0
- package/template-vite-project/tsconfig.node.json +28 -0
- package/template-vite-project/types/global.d.ts +33 -0
- package/template-vite-project/unocss.config.ts +208 -0
- package/template-vite-project/vite.config.ts +88 -0
@@ -0,0 +1,155 @@
|
|
1
|
+
import type { GAxiosOptions, XhtInstance } from './typings'
|
2
|
+
import { useStoreUser } from '@/store'
|
3
|
+
import { isDev, typeViteEnv } from '@/utils/env'
|
4
|
+
import { tansParams } from '@/utils/util'
|
5
|
+
import { checkURL } from '@/utils/validate'
|
6
|
+
import { defaultSettings, network } from '@gx-config'
|
7
|
+
import { isBoolean } from '@gx-design-vue/pro-utils'
|
8
|
+
import { showToast } from 'vant'
|
9
|
+
import { handleCode } from './checkStatus'
|
10
|
+
import { RequestEnum } from './typings'
|
11
|
+
import { GAxios } from './XHR'
|
12
|
+
|
13
|
+
const { token, mock } = defaultSettings
|
14
|
+
|
15
|
+
const { requestTimeout, successCode } = network
|
16
|
+
|
17
|
+
const xhtInstance: XhtInstance = {
|
18
|
+
/**
|
19
|
+
* @description: 处理响应数据。如果数据不是预期格式,可直接抛出错误
|
20
|
+
*/
|
21
|
+
transformResponseHook: (res, options) => {
|
22
|
+
const { customize, isReturnNativeResponse } = options
|
23
|
+
// 是否返回原生响应头 比如:需要获取响应头时使用该属性
|
24
|
+
if (isReturnNativeResponse) {
|
25
|
+
return res
|
26
|
+
}
|
27
|
+
// 不进行任何处理,直接返回
|
28
|
+
// 用于页面代码可能需要直接获取code,data,message这些信息时开启
|
29
|
+
if (customize) {
|
30
|
+
return res.data
|
31
|
+
}
|
32
|
+
// 错误的时候返回
|
33
|
+
|
34
|
+
const { data }: { data: ResponseResult } = res
|
35
|
+
if (!data) {
|
36
|
+
throw new Error('请求出错,请稍候重试')
|
37
|
+
}
|
38
|
+
// 这里 code,result,message为 后台统一的字段,需要在 types.ts内修改为项目自己的接口返回格式
|
39
|
+
const { code, message = '' } = data
|
40
|
+
|
41
|
+
const codeVerificationArray = successCode
|
42
|
+
|
43
|
+
// 这里逻辑可以根据项目进行修改
|
44
|
+
const hasSuccess = codeVerificationArray.includes(code)
|
45
|
+
if (hasSuccess) {
|
46
|
+
return data
|
47
|
+
}
|
48
|
+
|
49
|
+
// 在此处根据自己项目的实际情况对不同的code执行不同的操作
|
50
|
+
// 如果不希望中断当前请求,请return数据,否则直接抛出异常即可
|
51
|
+
handleCode(code, message)
|
52
|
+
|
53
|
+
return Promise.resolve(false)
|
54
|
+
},
|
55
|
+
|
56
|
+
// 请求之前处理config
|
57
|
+
beforeRequestHook: (config) => {
|
58
|
+
// get请求映射params参数
|
59
|
+
if (config.method?.toUpperCase() === RequestEnum.GET && config.params) {
|
60
|
+
let url = config.url + '?' + tansParams(config.params)
|
61
|
+
url = url.slice(0, -1)
|
62
|
+
config.params = {}
|
63
|
+
config.url = url
|
64
|
+
}
|
65
|
+
|
66
|
+
if (!checkURL(config.url)) {
|
67
|
+
const isMock = isBoolean(typeViteEnv('VITE_IS_MOCK')) ? typeViteEnv('VITE_IS_MOCK') : config.isMock
|
68
|
+
const prefix = isDev() && !isMock ? typeViteEnv('VITE_PROXY_PREFIX') : ''
|
69
|
+
const baseUrl = isMock ? mock.prefix : typeViteEnv('VITE_BASE_URL')
|
70
|
+
config.url = `${prefix}${baseUrl}${config.url}`
|
71
|
+
}
|
72
|
+
|
73
|
+
return config
|
74
|
+
},
|
75
|
+
|
76
|
+
/**
|
77
|
+
* @description: 请求拦截器处理
|
78
|
+
*/
|
79
|
+
requestInterceptors: (config) => {
|
80
|
+
const { name } = token
|
81
|
+
const user = useStoreUser()
|
82
|
+
const carryToken = isBoolean(config.carryToken) ? config.carryToken : true
|
83
|
+
if (user.accessToken && carryToken) {
|
84
|
+
if (config.headers) {
|
85
|
+
config.headers[name] = user.accessToken
|
86
|
+
} else {
|
87
|
+
config.headers = {
|
88
|
+
[name]: user.accessToken
|
89
|
+
}
|
90
|
+
}
|
91
|
+
}
|
92
|
+
return config
|
93
|
+
},
|
94
|
+
|
95
|
+
/**
|
96
|
+
* @description: 响应拦截器处理
|
97
|
+
*/
|
98
|
+
responseInterceptors: (res) => {
|
99
|
+
return res
|
100
|
+
},
|
101
|
+
|
102
|
+
/**
|
103
|
+
* @description: 响应错误处理
|
104
|
+
*/
|
105
|
+
responseInterceptorsCatch: (_, error) => {
|
106
|
+
const { response } = error
|
107
|
+
let errorMessage = error.message || ''
|
108
|
+
if (error.response && error.response.data) {
|
109
|
+
const { status = 404 } = response || {}
|
110
|
+
handleCode(status, errorMessage)
|
111
|
+
return Promise.resolve(false)
|
112
|
+
} else {
|
113
|
+
if (errorMessage === 'Network Error') {
|
114
|
+
errorMessage = '后端接口连接异常'
|
115
|
+
}
|
116
|
+
if (errorMessage.includes('timeout')) {
|
117
|
+
errorMessage = '后端接口请求超时'
|
118
|
+
}
|
119
|
+
if (errorMessage.includes('Request failed with status code')) {
|
120
|
+
const code = errorMessage.substr(errorMessage.length - 3)
|
121
|
+
errorMessage = '后端接口' + code || '' + '异常'
|
122
|
+
}
|
123
|
+
showToast({
|
124
|
+
type: 'fail',
|
125
|
+
message: errorMessage || '后端接口未知异常'
|
126
|
+
})
|
127
|
+
return Promise.resolve(false)
|
128
|
+
}
|
129
|
+
},
|
130
|
+
/**
|
131
|
+
* @description: 处理响应错误数据
|
132
|
+
*/
|
133
|
+
requestCatchHook: () => false
|
134
|
+
}
|
135
|
+
|
136
|
+
function createXhr(opt?: Partial<GAxiosOptions>) {
|
137
|
+
return new GAxios({
|
138
|
+
method: 'get',
|
139
|
+
timeout: requestTimeout,
|
140
|
+
headers: {
|
141
|
+
'Content-Type': 'application/json;charset=UTF-8'
|
142
|
+
},
|
143
|
+
// 忽略重复请求
|
144
|
+
ignoreCancelToken: true,
|
145
|
+
// 是否携带token
|
146
|
+
carryToken: true,
|
147
|
+
...xhtInstance,
|
148
|
+
...opt
|
149
|
+
})
|
150
|
+
}
|
151
|
+
|
152
|
+
const request: <T = any, R = undefined>(opt?: GAxiosOptions) => Promise<ResponseResult<T, R>> = opt => createXhr()
|
153
|
+
.request(opt)
|
154
|
+
|
155
|
+
export default request
|
@@ -0,0 +1,171 @@
|
|
1
|
+
import type {
|
2
|
+
AxiosError,
|
3
|
+
AxiosInstance,
|
4
|
+
AxiosInterceptorManager,
|
5
|
+
AxiosInterceptorOptions,
|
6
|
+
AxiosPromise,
|
7
|
+
AxiosRequestConfig,
|
8
|
+
AxiosResponse,
|
9
|
+
InternalAxiosRequestConfig,
|
10
|
+
Method
|
11
|
+
} from 'axios'
|
12
|
+
|
13
|
+
export interface GAxiosResponse<T = any> extends Omit<AxiosResponse, 'data' | 'config'> {
|
14
|
+
data: T;
|
15
|
+
config: Omit<GAxiosOptions, 'headers'>;
|
16
|
+
}
|
17
|
+
|
18
|
+
export interface CancelOptions {
|
19
|
+
cancel: (key?: string) => void,
|
20
|
+
cancelAll: () => void;
|
21
|
+
reset: () => void
|
22
|
+
}
|
23
|
+
|
24
|
+
export interface XhtInstance {
|
25
|
+
/**
|
26
|
+
* @Author gx12358
|
27
|
+
* @DateTime 2023/1/6
|
28
|
+
* @lastTime 2023/1/6
|
29
|
+
* @description 请求之前处理config
|
30
|
+
*/
|
31
|
+
beforeRequestHook?: (config: GAxiosOptions) => GAxiosOptions;
|
32
|
+
|
33
|
+
/**
|
34
|
+
* @Author gx12358
|
35
|
+
* @DateTime 2023/11/3
|
36
|
+
* @lastTime 2023/11/3
|
37
|
+
* @description 请求之前返回cancel函数
|
38
|
+
*/
|
39
|
+
cancelCallBackHook?: ({ cancel, cancelAll, reset }: CancelOptions) => void;
|
40
|
+
|
41
|
+
/**
|
42
|
+
* @description: 处理响应数据
|
43
|
+
*/
|
44
|
+
transformResponseHook?: (res: GAxiosResponse, options: Partial<GAxiosOptions>) => any;
|
45
|
+
|
46
|
+
/**
|
47
|
+
* @description: 请求失败处理
|
48
|
+
*/
|
49
|
+
requestCatchHook?: (e: Error) => boolean;
|
50
|
+
|
51
|
+
/**
|
52
|
+
* @description: 请求之前的拦截器
|
53
|
+
*/
|
54
|
+
requestInterceptors?: (config: GAxiosOptions) => GAxiosOptions;
|
55
|
+
|
56
|
+
/**
|
57
|
+
* @description: 请求之后的拦截器
|
58
|
+
*/
|
59
|
+
responseInterceptors?: (res: GAxiosResponse) => GAxiosResponse;
|
60
|
+
|
61
|
+
/**
|
62
|
+
* @description: 请求之前的拦截器错误处理
|
63
|
+
*/
|
64
|
+
requestInterceptorsCatch?: (error: AxiosError) => void;
|
65
|
+
|
66
|
+
/**
|
67
|
+
* @description: 请求之后的拦截器错误处理
|
68
|
+
*/
|
69
|
+
responseInterceptorsCatch?: (axiosInstance: GAxiosInstance, error: AxiosError) => void;
|
70
|
+
}
|
71
|
+
|
72
|
+
export interface GAxiosOptions extends Omit<AxiosRequestConfig, 'headers' | 'method'> {
|
73
|
+
headers?: Record<string, any>;
|
74
|
+
method: Method;
|
75
|
+
isMock?: boolean; // 是否是mock
|
76
|
+
retry?: boolean; // 是否是重试接口
|
77
|
+
isReturnNativeResponse?: boolean; // 直接返回response,不作任何处理(包含响应值等基本信息)
|
78
|
+
customize?: boolean; // 直接返回response.data(接口返回值),错误不做统一提示
|
79
|
+
carryToken?: boolean; // 是否携带token
|
80
|
+
prefix?: string; // 接口自定义前缀
|
81
|
+
ignoreCancelToken?: boolean; // 忽略重复请求
|
82
|
+
cancelKey?: string; // 取消请求key(用来需要请求)
|
83
|
+
/**
|
84
|
+
* @Author gx12358
|
85
|
+
* @DateTime 2023/1/6
|
86
|
+
* @lastTime 2023/1/6
|
87
|
+
* @description 请求之前处理config
|
88
|
+
*/
|
89
|
+
beforeRequestHook?: XhtInstance['beforeRequestHook'];
|
90
|
+
|
91
|
+
/**
|
92
|
+
* @Author gx12358
|
93
|
+
* @DateTime 2023/11/3
|
94
|
+
* @lastTime 2023/11/3
|
95
|
+
* @description 请求之前返回cancel函数
|
96
|
+
*/
|
97
|
+
cancelCallBackHook?: XhtInstance['cancelCallBackHook'];
|
98
|
+
|
99
|
+
/**
|
100
|
+
* @description: 处理响应数据
|
101
|
+
*/
|
102
|
+
transformResponseHook?: XhtInstance['transformResponseHook'];
|
103
|
+
|
104
|
+
/**
|
105
|
+
* @description: 请求失败处理
|
106
|
+
*/
|
107
|
+
requestCatchHook?: XhtInstance['requestCatchHook'];
|
108
|
+
|
109
|
+
/**
|
110
|
+
* @description: 请求之前的拦截器
|
111
|
+
*/
|
112
|
+
requestInterceptors?: XhtInstance['requestInterceptors'];
|
113
|
+
|
114
|
+
/**
|
115
|
+
* @description: 请求之后的拦截器
|
116
|
+
*/
|
117
|
+
responseInterceptors?: XhtInstance['responseInterceptors'];
|
118
|
+
|
119
|
+
/**
|
120
|
+
* @description: 请求之前的拦截器错误处理
|
121
|
+
*/
|
122
|
+
requestInterceptorsCatch?: XhtInstance['requestInterceptorsCatch'];
|
123
|
+
|
124
|
+
/**
|
125
|
+
* @description: 请求之后的拦截器错误处理
|
126
|
+
*/
|
127
|
+
responseInterceptorsCatch?: XhtInstance['responseInterceptorsCatch'];
|
128
|
+
}
|
129
|
+
|
130
|
+
export interface GAxiosInstance extends Omit<AxiosInstance, 'interceptors'> {
|
131
|
+
(config: GAxiosOptions): AxiosPromise<ResponseResult>;
|
132
|
+
|
133
|
+
(url: string, config?: GAxiosOptions): AxiosPromise<ResponseResult>;
|
134
|
+
|
135
|
+
interceptors: {
|
136
|
+
request: Omit<AxiosInterceptorManager<InternalAxiosRequestConfig>, 'use'> & {
|
137
|
+
use<V>(
|
138
|
+
onFulfilled?: ((value: V) => V | Promise<V>) | null,
|
139
|
+
onRejected?: ((error: any) => any) | null,
|
140
|
+
options?: AxiosInterceptorOptions
|
141
|
+
): number;
|
142
|
+
};
|
143
|
+
response: Omit<AxiosInterceptorManager<AxiosResponse>, 'use'> & {
|
144
|
+
use<V>(
|
145
|
+
onFulfilled?: ((value: V) => V | Promise<V>) | null,
|
146
|
+
onRejected?: ((error: any) => any) | null,
|
147
|
+
options?: AxiosInterceptorOptions
|
148
|
+
): number;
|
149
|
+
};
|
150
|
+
}
|
151
|
+
}
|
152
|
+
|
153
|
+
/**
|
154
|
+
* @description: request method
|
155
|
+
*/
|
156
|
+
export enum RequestEnum {
|
157
|
+
GET = 'GET',
|
158
|
+
POST = 'POST',
|
159
|
+
PUT = 'PUT',
|
160
|
+
DELETE = 'DELETE',
|
161
|
+
}
|
162
|
+
|
163
|
+
/**
|
164
|
+
* @description: contentType
|
165
|
+
*/
|
166
|
+
export enum ContentTypeEnum {
|
167
|
+
// json
|
168
|
+
JSON = 'application/json;charset=UTF-8',
|
169
|
+
// form-data qs
|
170
|
+
FORM_URLENCODED = 'application/x-www-form-urlencoded;charset=UTF-8',
|
171
|
+
}
|
@@ -0,0 +1,215 @@
|
|
1
|
+
import { isJSONStr, isNumber, isObject, isString } from '@gx-design-vue/pro-utils'
|
2
|
+
import dayjs from 'dayjs'
|
3
|
+
import { Decrypt, Encrypt } from './crypto'
|
4
|
+
import { isPro, typeViteEnv } from './env'
|
5
|
+
|
6
|
+
function isEncryption(status: boolean) {
|
7
|
+
return isPro() ? status : false
|
8
|
+
}
|
9
|
+
|
10
|
+
function handleStorageValue(value: string) {
|
11
|
+
if (isJSONStr(value))
|
12
|
+
return JSON.parse(value)
|
13
|
+
return value
|
14
|
+
}
|
15
|
+
|
16
|
+
/**
|
17
|
+
* @Author gx12358
|
18
|
+
* @DateTime 2019/12/3
|
19
|
+
* @lastTime 2019/12/3
|
20
|
+
* @description 设置Local-key的规则
|
21
|
+
*/
|
22
|
+
export function getStorageKey(key: string, originKey?: boolean) {
|
23
|
+
const { pkg } = __APP_INFO__
|
24
|
+
return originKey ? key : `${pkg.name}_${pkg.version}_${typeViteEnv('VITE_APP_ENV') === 'dev'
|
25
|
+
? 'development'
|
26
|
+
: typeViteEnv('VITE_USE_MODE')}_${key}`
|
27
|
+
}
|
28
|
+
|
29
|
+
/**
|
30
|
+
* @Author gx12358
|
31
|
+
* @DateTime 2019/12/3
|
32
|
+
* @lastTime 2019/12/3
|
33
|
+
* @description 获取Storage
|
34
|
+
*/
|
35
|
+
export function getStorage({
|
36
|
+
key,
|
37
|
+
originKey,
|
38
|
+
type = 'local',
|
39
|
+
encryption = true
|
40
|
+
}: {
|
41
|
+
key: string;
|
42
|
+
encryption?: boolean;
|
43
|
+
type?: 'local' | 'cookie' | 'session';
|
44
|
+
originKey?: boolean;
|
45
|
+
}) {
|
46
|
+
const storageValue = type === 'local'
|
47
|
+
? localStorage.getItem(getStorageKey(key, originKey))
|
48
|
+
: type === 'session' ? sessionStorage.getItem(getStorageKey(key, originKey)) : getCookie(
|
49
|
+
getStorageKey(
|
50
|
+
key,
|
51
|
+
originKey
|
52
|
+
))
|
53
|
+
const result: string | LocalResult = storageValue
|
54
|
+
? isEncryption(encryption) ? Decrypt(storageValue) : handleStorageValue(storageValue)
|
55
|
+
: ''
|
56
|
+
if (result && isObject(result)) {
|
57
|
+
if (result.expired) {
|
58
|
+
const expiredStatus = dayjs().diff(dayjs(result.time)) >= result.expired
|
59
|
+
if (expiredStatus) {
|
60
|
+
removeStorage({ key, originKey, type })
|
61
|
+
return ''
|
62
|
+
}
|
63
|
+
}
|
64
|
+
} else if (result && isString(result)) {
|
65
|
+
return isJSONStr(result) ? JSON.parse(result) : result
|
66
|
+
}
|
67
|
+
return typeof result === 'string' ? result : result?.['value'] || result || ''
|
68
|
+
}
|
69
|
+
|
70
|
+
/**
|
71
|
+
* @Author gx12358
|
72
|
+
* @DateTime 2019/12/3
|
73
|
+
* @lastTime 2019/12/3
|
74
|
+
* @description 设置Storage
|
75
|
+
*/
|
76
|
+
export function setStorage({
|
77
|
+
key,
|
78
|
+
value,
|
79
|
+
expired,
|
80
|
+
originKey,
|
81
|
+
type = 'local',
|
82
|
+
encryption = true
|
83
|
+
}: {
|
84
|
+
key: string;
|
85
|
+
value: any;
|
86
|
+
originKey?: boolean;
|
87
|
+
expired?: number;
|
88
|
+
encryption?: boolean;
|
89
|
+
type?: 'local' | 'cookie' | 'session';
|
90
|
+
}) {
|
91
|
+
const result: LocalResult = originKey ? value : {
|
92
|
+
value,
|
93
|
+
time: dayjs().format('YYYY-MM-DD HH:mm:ss'),
|
94
|
+
expired: expired || 0
|
95
|
+
}
|
96
|
+
const storageValue = isEncryption(encryption)
|
97
|
+
? Encrypt(JSON.stringify(result))
|
98
|
+
: isString(result) || isNumber(result) ? result : JSON.stringify(result)
|
99
|
+
if (type === 'local') localStorage.setItem(getStorageKey(key, originKey), storageValue)
|
100
|
+
else if (type === 'cookie') setCookie(getStorageKey(key, originKey), storageValue)
|
101
|
+
else sessionStorage.setItem(getStorageKey(key, originKey), storageValue)
|
102
|
+
}
|
103
|
+
|
104
|
+
/**
|
105
|
+
* @Author gx12358
|
106
|
+
* @DateTime 2019/12/3
|
107
|
+
* @lastTime 2019/12/3
|
108
|
+
* @description 删除Storage
|
109
|
+
*/
|
110
|
+
export function removeStorage({
|
111
|
+
key,
|
112
|
+
originKey,
|
113
|
+
type = 'local'
|
114
|
+
}: {
|
115
|
+
key: string;
|
116
|
+
originKey?: boolean;
|
117
|
+
type?: 'local' | 'cookie' | 'session';
|
118
|
+
}) {
|
119
|
+
if (type === 'local') localStorage.removeItem(getStorageKey(key, originKey))
|
120
|
+
else if (type === 'cookie') delCookie(getStorageKey(key, originKey))
|
121
|
+
else sessionStorage.removeItem(getStorageKey(key, originKey))
|
122
|
+
}
|
123
|
+
|
124
|
+
/**
|
125
|
+
* @Author gx12358
|
126
|
+
* @DateTime 2019-09-24
|
127
|
+
* @lastTime 2019-09-24
|
128
|
+
* @description 获取Cookie-name
|
129
|
+
*/
|
130
|
+
function getCookies(cname: string) {
|
131
|
+
const name = `${cname}=`
|
132
|
+
const decodedCookie = decodeURIComponent(document.cookie)
|
133
|
+
const ca = decodedCookie.split(';')
|
134
|
+
for (let i = 0; i < ca.length; i += 1) {
|
135
|
+
let c = ca[i]
|
136
|
+
while (c.charAt(0) === ' ') {
|
137
|
+
c = c.substring(1)
|
138
|
+
}
|
139
|
+
if (c.indexOf(name) === 0) {
|
140
|
+
return decodeURIComponent(c.substring(name.length, c.length))
|
141
|
+
}
|
142
|
+
}
|
143
|
+
return ''
|
144
|
+
}
|
145
|
+
|
146
|
+
/**
|
147
|
+
* @Author gx12358
|
148
|
+
* @DateTime 2019-09-24
|
149
|
+
* @lastTime 2019-09-24
|
150
|
+
* @description 获取Cookiedomin
|
151
|
+
*/
|
152
|
+
function GetCookieDomain() {
|
153
|
+
let host = location.hostname
|
154
|
+
const ip = /^(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
|
155
|
+
if (ip.test(host) === true || host === 'localhost')
|
156
|
+
return host
|
157
|
+
// eslint-disable-next-line regexp/optimal-quantifier-concatenation
|
158
|
+
const regex = /([\s\S]*).*/
|
159
|
+
const match = host.match(regex)
|
160
|
+
if (typeof match !== 'undefined' && match !== null) {
|
161
|
+
const someIndex = 1
|
162
|
+
host = match[someIndex]
|
163
|
+
}
|
164
|
+
if (typeof host !== 'undefined' && host !== null) {
|
165
|
+
const strAry = host.split('.')
|
166
|
+
if (strAry.length > 1) {
|
167
|
+
host = `${strAry[strAry.length - 2]}.${strAry[strAry.length - 1]}`
|
168
|
+
}
|
169
|
+
}
|
170
|
+
return `.${host}`
|
171
|
+
}
|
172
|
+
|
173
|
+
/**
|
174
|
+
* @Author gx12358
|
175
|
+
* @DateTime 2019-09-24
|
176
|
+
* @lastTime 2019-09-24
|
177
|
+
* @description 设置Cookie
|
178
|
+
*/
|
179
|
+
export function setCookie(cname: string, cvalue: string, exdays?: number) {
|
180
|
+
const d = new Date()
|
181
|
+
d.setTime(d.getTime() + (exdays || 365) * 24 * 60 * 60 * 1000)
|
182
|
+
const expires = `=${d.toUTCString()}`
|
183
|
+
document.cookie = `${cname}=${encodeURIComponent(
|
184
|
+
cvalue
|
185
|
+
)}; expires=${expires}; domain=${GetCookieDomain()}; path=/`
|
186
|
+
}
|
187
|
+
|
188
|
+
/**
|
189
|
+
* @Author gx12358
|
190
|
+
* @DateTime 2019-09-24
|
191
|
+
* @lastTime 2019-09-24
|
192
|
+
* @description 获取Cookie
|
193
|
+
*/
|
194
|
+
export function getCookie(cname: string) {
|
195
|
+
const result = getCookies(cname)
|
196
|
+
if (result === '') {
|
197
|
+
return ''
|
198
|
+
}
|
199
|
+
return decodeURIComponent(result)
|
200
|
+
}
|
201
|
+
|
202
|
+
/**
|
203
|
+
* @Author gx12358
|
204
|
+
* @DateTime 2019-09-24
|
205
|
+
* @lastTime 2019-09-24
|
206
|
+
* @description 删除Cookie
|
207
|
+
*/
|
208
|
+
export function delCookie(name: string) {
|
209
|
+
const exp = new Date()
|
210
|
+
exp.setTime(exp.getTime() - 1)
|
211
|
+
const cval = getCookies(name)
|
212
|
+
if (cval !== null) {
|
213
|
+
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; domain=${GetCookieDomain()}; path=/`
|
214
|
+
}
|
215
|
+
}
|
@@ -0,0 +1,27 @@
|
|
1
|
+
/**
|
2
|
+
* @Author gx12358
|
3
|
+
* @DateTime 2021/11/3
|
4
|
+
* @lastTime 2021/11/3
|
5
|
+
* @description get参数处理
|
6
|
+
*/
|
7
|
+
export function tansParams(params) {
|
8
|
+
let result = ''
|
9
|
+
for (const propName of Object.keys(params)) {
|
10
|
+
const value = params[propName]
|
11
|
+
const part = encodeURIComponent(propName) + '='
|
12
|
+
if (value !== null && typeof (value) !== 'undefined' && value !== '') {
|
13
|
+
if (typeof value === 'object') {
|
14
|
+
for (const key of Object.keys(value)) {
|
15
|
+
if (value[key] !== null && typeof (value[key]) !== 'undefined') {
|
16
|
+
const params = propName + '[' + key + ']'
|
17
|
+
const subPart = encodeURIComponent(params) + '='
|
18
|
+
result += subPart + encodeURIComponent(value[key]) + '&'
|
19
|
+
}
|
20
|
+
}
|
21
|
+
} else {
|
22
|
+
result += part + encodeURIComponent(value) + '&'
|
23
|
+
}
|
24
|
+
}
|
25
|
+
}
|
26
|
+
return result
|
27
|
+
}
|