@gx-design-vue/create-gx-cli 0.1.3 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +5 -12
- package/package.json +3 -2
- package/template-mobile-vant-cli/.editorconfig +19 -0
- package/template-mobile-vant-cli/.env +0 -0
- package/template-mobile-vant-cli/.env.development +19 -0
- package/template-mobile-vant-cli/.env.pro +19 -0
- package/template-mobile-vant-cli/.env.production +19 -0
- package/template-mobile-vant-cli/.eslintignore +15 -0
- package/template-mobile-vant-cli/.prettierignore +9 -0
- package/template-mobile-vant-cli/build/cdn.ts +5 -0
- package/template-mobile-vant-cli/build/generateModifyVars.ts +7 -0
- package/template-mobile-vant-cli/build/optimizer.ts +18 -0
- package/template-mobile-vant-cli/build/plugin/autoImport.ts +24 -0
- package/template-mobile-vant-cli/build/plugin/html.ts +26 -0
- package/template-mobile-vant-cli/build/plugin/index.ts +42 -0
- package/template-mobile-vant-cli/build/plugin/mock.ts +14 -0
- package/template-mobile-vant-cli/build/plugin/viteMock/client.ts +88 -0
- package/template-mobile-vant-cli/build/plugin/viteMock/createMockServer.ts +271 -0
- package/template-mobile-vant-cli/build/plugin/viteMock/index.ts +69 -0
- package/template-mobile-vant-cli/build/plugin/viteMock/types.ts +48 -0
- package/template-mobile-vant-cli/build/plugin/viteMock/utils.ts +48 -0
- package/template-mobile-vant-cli/build/script/postBuild.ts +14 -0
- package/template-mobile-vant-cli/eslint.config.js +49 -0
- package/template-mobile-vant-cli/index.html +24 -0
- package/template-mobile-vant-cli/mock/_createProductionServer.ts +19 -0
- package/template-mobile-vant-cli/mock/datasSource/api/index.ts +0 -0
- package/template-mobile-vant-cli/mock/utils.ts +9 -0
- package/template-mobile-vant-cli/package.json +57 -0
- package/template-mobile-vant-cli/postcss.config.cjs +8 -0
- package/template-mobile-vant-cli/prettier.config.cjs +18 -0
- package/template-mobile-vant-cli/public/favicon.ico +0 -0
- package/template-mobile-vant-cli/public/js/flexible.js +44 -0
- package/template-mobile-vant-cli/src/App.vue +5 -0
- package/template-mobile-vant-cli/src/components/PageContainer/ProSkeleton.tsx +29 -0
- package/template-mobile-vant-cli/src/components/PageContainer/index.tsx +115 -0
- package/template-mobile-vant-cli/src/components/PageContainer/style.module.less +14 -0
- package/template-mobile-vant-cli/src/core/gx-design/index.ts +7 -0
- package/template-mobile-vant-cli/src/core/index.ts +12 -0
- package/template-mobile-vant-cli/src/core/vant-design/index.ts +4 -0
- package/template-mobile-vant-cli/src/design/config.less +0 -0
- package/template-mobile-vant-cli/src/design/index.less +3 -0
- package/template-mobile-vant-cli/src/design/reset.less +155 -0
- package/template-mobile-vant-cli/src/design/root.less +3 -0
- package/template-mobile-vant-cli/src/design/vant.less +2 -0
- package/template-mobile-vant-cli/src/global.less +1 -0
- package/template-mobile-vant-cli/src/hooks/web/index.ts +5 -0
- package/template-mobile-vant-cli/src/hooks/web/usePageLoading.ts +58 -0
- package/template-mobile-vant-cli/src/layout/BasicLayout.vue +16 -0
- package/template-mobile-vant-cli/src/layout/basicLayout.less +11 -0
- package/template-mobile-vant-cli/src/main.ts +27 -0
- package/template-mobile-vant-cli/src/pages/home.vue +71 -0
- package/template-mobile-vant-cli/src/router/index.ts +25 -0
- package/template-mobile-vant-cli/src/router/routes.ts +20 -0
- package/template-mobile-vant-cli/src/router/typings.ts +8 -0
- package/template-mobile-vant-cli/src/services/index.ts +31 -0
- package/template-mobile-vant-cli/src/settings/index.ts +10 -0
- package/template-mobile-vant-cli/src/store/index.ts +17 -0
- package/template-mobile-vant-cli/src/store/modules/global.ts +30 -0
- package/template-mobile-vant-cli/src/utils/crypto/base64.ts +101 -0
- package/template-mobile-vant-cli/src/utils/crypto/index.ts +57 -0
- package/template-mobile-vant-cli/src/utils/env.ts +50 -0
- package/template-mobile-vant-cli/src/utils/pageTitle.ts +14 -0
- package/template-mobile-vant-cli/src/utils/request/XHR.ts +139 -0
- package/template-mobile-vant-cli/src/utils/request/axiosCancel.ts +69 -0
- package/template-mobile-vant-cli/src/utils/request/checkStatus.ts +9 -0
- package/template-mobile-vant-cli/src/utils/request/index.ts +142 -0
- package/template-mobile-vant-cli/src/utils/request/typings.ts +171 -0
- package/template-mobile-vant-cli/src/utils/storage.ts +198 -0
- package/template-mobile-vant-cli/src/utils/util.ts +27 -0
- package/template-mobile-vant-cli/src/utils/validate.ts +216 -0
- package/template-mobile-vant-cli/tsconfig.json +42 -0
- package/template-mobile-vant-cli/types/ant-design-import.d.ts +13 -0
- package/template-mobile-vant-cli/types/auto-imports.d.ts +64 -0
- package/template-mobile-vant-cli/types/components.d.ts +12 -0
- package/template-mobile-vant-cli/types/global.d.ts +39 -0
- package/template-mobile-vant-cli/types/module.d.ts +20 -0
- package/template-mobile-vant-cli/types/plugins-auto-import.d.ts +14 -0
- package/template-mobile-vant-cli/types/response.d.ts +15 -0
- package/template-mobile-vant-cli/types/vant-import.d.ts +13 -0
- package/template-mobile-vant-cli/unocss.config.ts +101 -0
- package/template-mobile-vant-cli/vite.config.ts +145 -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
@@ -0,0 +1,50 @@
|
|
1
|
+
/**
|
2
|
+
* @Author gx12358
|
3
|
+
* @DateTime 2022/4/1
|
4
|
+
* @lastTime 2022/4/1
|
5
|
+
* @description 环境:本地开发环境
|
6
|
+
*/
|
7
|
+
export function isDev(): boolean {
|
8
|
+
return typeViteEnv('VITE_APP_ENV') === 'dev'
|
9
|
+
}
|
10
|
+
|
11
|
+
/**
|
12
|
+
* @Author gx12358
|
13
|
+
* @DateTime 2022/4/1
|
14
|
+
* @lastTime 2022/4/1
|
15
|
+
* @description 环境:正式
|
16
|
+
*/
|
17
|
+
export function isPro(): boolean {
|
18
|
+
return typeViteEnv('VITE_USE_MODE') === 'pro'
|
19
|
+
}
|
20
|
+
|
21
|
+
/**
|
22
|
+
* @Author gx12358
|
23
|
+
* @DateTime 2022/4/1
|
24
|
+
* @lastTime 2022/4/1
|
25
|
+
* @description 环境:非本地开发环境
|
26
|
+
*/
|
27
|
+
export function isBuild(): boolean {
|
28
|
+
return typeViteEnv('VITE_NODE_ENV') === 'production'
|
29
|
+
}
|
30
|
+
|
31
|
+
/**
|
32
|
+
* @Author gx12358
|
33
|
+
* @DateTime 2022/4/1
|
34
|
+
* @lastTime 2022/4/1
|
35
|
+
* @description 当前联调环境
|
36
|
+
*/
|
37
|
+
export function currentMode() {
|
38
|
+
return typeViteEnv('VITE_USE_MODE')
|
39
|
+
}
|
40
|
+
|
41
|
+
export function typeViteEnv<T extends keyof ViteEnv>(key: T): ViteEnv[T] | undefined {
|
42
|
+
let value: any = (import.meta as any).env[key]
|
43
|
+
|
44
|
+
if (value && typeof value === 'string') {
|
45
|
+
value = value.replace(/\\n/g, '\n') as ViteEnv[T]
|
46
|
+
value = value === 'true' ? true : value === 'false' ? false : value
|
47
|
+
}
|
48
|
+
|
49
|
+
return value
|
50
|
+
}
|
@@ -0,0 +1,14 @@
|
|
1
|
+
/**
|
2
|
+
* @author gx12358 2539306317@qq.com
|
3
|
+
* @description 设置标题
|
4
|
+
* @param pageTitle
|
5
|
+
* @returns {string}
|
6
|
+
*/
|
7
|
+
export default function getPageTitle(title: string, pageTitle: string) {
|
8
|
+
const newTitles: string[] = []
|
9
|
+
if (pageTitle)
|
10
|
+
newTitles.push(pageTitle)
|
11
|
+
if (title)
|
12
|
+
newTitles.push(title)
|
13
|
+
return newTitles.join('-')
|
14
|
+
}
|
@@ -0,0 +1,139 @@
|
|
1
|
+
import type { AxiosError } from 'axios'
|
2
|
+
import axios from 'axios'
|
3
|
+
import { cloneDeep } from 'lodash-es'
|
4
|
+
import qs from 'qs'
|
5
|
+
import { isFunction } from '@gx-design-vue/pro-utils'
|
6
|
+
import type { GAxiosInstance, GAxiosOptions, GAxiosResponse } from './typings'
|
7
|
+
import { ContentTypeEnum, RequestEnum } from './typings'
|
8
|
+
import { AxiosCanceler } from './axiosCancel'
|
9
|
+
|
10
|
+
export const getPendingUrl = (config: GAxiosOptions) => config.cancelKey || [ config.method, config.url ].join(
|
11
|
+
'&')
|
12
|
+
|
13
|
+
/**
|
14
|
+
* @Author gx12358
|
15
|
+
* @DateTime 2023/1/6
|
16
|
+
* @lastTime 2023/1/6
|
17
|
+
* @description 重新定义axios
|
18
|
+
*/
|
19
|
+
export class GAxios {
|
20
|
+
private axiosInstance: GAxiosInstance
|
21
|
+
private readonly options: GAxiosOptions
|
22
|
+
|
23
|
+
constructor(options: GAxiosOptions) {
|
24
|
+
this.options = options
|
25
|
+
this.axiosInstance = axios.create(options) as GAxiosInstance
|
26
|
+
this.setupInterceptors()
|
27
|
+
}
|
28
|
+
|
29
|
+
/**
|
30
|
+
* @description: Interceptor configuration 拦截器配置
|
31
|
+
*/
|
32
|
+
private setupInterceptors() {
|
33
|
+
const {
|
34
|
+
requestInterceptors,
|
35
|
+
requestInterceptorsCatch,
|
36
|
+
responseInterceptors,
|
37
|
+
responseInterceptorsCatch
|
38
|
+
} = this.options
|
39
|
+
|
40
|
+
const axiosCanceler = new AxiosCanceler(this.options.ignoreCancelToken)
|
41
|
+
|
42
|
+
this.axiosInstance.interceptors.request.use<GAxiosOptions>((config) => {
|
43
|
+
const { cancelCallBackHook } = config
|
44
|
+
axiosCanceler.addPending(config)
|
45
|
+
if (cancelCallBackHook) {
|
46
|
+
cancelCallBackHook({
|
47
|
+
reset: axiosCanceler.reset,
|
48
|
+
cancel: (key?: string) => axiosCanceler.removePending(config, key),
|
49
|
+
cancelAll: axiosCanceler.removeAllPending
|
50
|
+
})
|
51
|
+
}
|
52
|
+
if (requestInterceptors && isFunction(requestInterceptors)) {
|
53
|
+
config = requestInterceptors(config)
|
54
|
+
}
|
55
|
+
return config
|
56
|
+
}, undefined)
|
57
|
+
|
58
|
+
// Request interceptor error capture
|
59
|
+
requestInterceptorsCatch && isFunction(requestInterceptorsCatch) && this.axiosInstance.interceptors.request.use(
|
60
|
+
undefined,
|
61
|
+
requestInterceptorsCatch
|
62
|
+
)
|
63
|
+
|
64
|
+
// Response result interceptor processing
|
65
|
+
this.axiosInstance.interceptors.response.use((res: GAxiosResponse) => {
|
66
|
+
res && axiosCanceler.removePending(res.config)
|
67
|
+
if (responseInterceptors && isFunction(responseInterceptors)) {
|
68
|
+
return responseInterceptors(res)
|
69
|
+
}
|
70
|
+
return res
|
71
|
+
}, undefined)
|
72
|
+
|
73
|
+
// Response result interceptor error capture
|
74
|
+
responseInterceptorsCatch && isFunction(responseInterceptorsCatch) && this.axiosInstance.interceptors.response.use(
|
75
|
+
undefined,
|
76
|
+
(error) => {
|
77
|
+
axiosCanceler.removePending(error.config)
|
78
|
+
|
79
|
+
return responseInterceptorsCatch(this.axiosInstance, error)
|
80
|
+
}
|
81
|
+
)
|
82
|
+
}
|
83
|
+
|
84
|
+
// support form-data
|
85
|
+
supportFormData(config: GAxiosOptions) {
|
86
|
+
const headers = config.headers || this.options.headers
|
87
|
+
const contentType = headers?.['Content-Type'] || headers?.['content-type']
|
88
|
+
|
89
|
+
if (contentType !== ContentTypeEnum.FORM_URLENCODED || !Reflect.has(
|
90
|
+
config,
|
91
|
+
'data'
|
92
|
+
) || config.method?.toUpperCase() === RequestEnum.GET) {
|
93
|
+
return config
|
94
|
+
}
|
95
|
+
|
96
|
+
return {
|
97
|
+
...config,
|
98
|
+
data: qs.stringify(config.data, { arrayFormat: 'brackets' })
|
99
|
+
}
|
100
|
+
}
|
101
|
+
|
102
|
+
request<T = ResponseResult | boolean>(config: GAxiosOptions): Promise<T> {
|
103
|
+
let conf: GAxiosOptions = cloneDeep(config)
|
104
|
+
|
105
|
+
const opt: GAxiosOptions = Object.assign({}, this.options, conf)
|
106
|
+
|
107
|
+
const { beforeRequestHook, requestCatchHook, transformResponseHook } = opt
|
108
|
+
if (beforeRequestHook && isFunction(beforeRequestHook)) {
|
109
|
+
conf = beforeRequestHook(opt)
|
110
|
+
}
|
111
|
+
|
112
|
+
conf = this.supportFormData(opt)
|
113
|
+
|
114
|
+
return new Promise((resolve) => {
|
115
|
+
this.axiosInstance
|
116
|
+
.request<any, GAxiosResponse>(conf)
|
117
|
+
.then((res: GAxiosResponse) => {
|
118
|
+
if (transformResponseHook && isFunction(transformResponseHook)) {
|
119
|
+
try {
|
120
|
+
const ret = transformResponseHook(res, config)
|
121
|
+
resolve(ret)
|
122
|
+
} catch (err) {
|
123
|
+
resolve(false as any)
|
124
|
+
return
|
125
|
+
}
|
126
|
+
return
|
127
|
+
}
|
128
|
+
resolve(res as unknown as Promise<T>)
|
129
|
+
})
|
130
|
+
.catch((e: Error | AxiosError) => {
|
131
|
+
if (requestCatchHook && isFunction(requestCatchHook)) {
|
132
|
+
resolve(requestCatchHook(e) as any)
|
133
|
+
return
|
134
|
+
}
|
135
|
+
resolve(false as any)
|
136
|
+
})
|
137
|
+
})
|
138
|
+
}
|
139
|
+
}
|
@@ -0,0 +1,69 @@
|
|
1
|
+
import type { Canceler } from 'axios'
|
2
|
+
import axios from 'axios'
|
3
|
+
import { isFunction } from '@gx-design-vue/pro-utils'
|
4
|
+
import type { GAxiosOptions } from './typings'
|
5
|
+
|
6
|
+
export const getPendingUrl = (
|
7
|
+
config: GAxiosOptions,
|
8
|
+
key?: string
|
9
|
+
) => key || config.cancelKey || [ config.method, config.url ].join('&')
|
10
|
+
|
11
|
+
export class AxiosCanceler {
|
12
|
+
ignoreCancelToken: boolean
|
13
|
+
pendingMap: Map<string, Canceler>
|
14
|
+
|
15
|
+
constructor(ignoreCancel?: boolean) {
|
16
|
+
this.pendingMap = new Map<string, Canceler>()
|
17
|
+
|
18
|
+
this.ignoreCancelToken = ignoreCancel
|
19
|
+
}
|
20
|
+
|
21
|
+
/**
|
22
|
+
* Add request
|
23
|
+
* @param {Object} config
|
24
|
+
*/
|
25
|
+
addPending(config: GAxiosOptions) {
|
26
|
+
!this.ignoreCancelToken && this.removePending(config)
|
27
|
+
|
28
|
+
const url = getPendingUrl(config)
|
29
|
+
config.cancelToken = config.cancelToken || new axios.CancelToken((cancel) => {
|
30
|
+
if (!this.pendingMap.has(url)) {
|
31
|
+
// If there is no current request in pending, add it
|
32
|
+
this.pendingMap.set(url, cancel)
|
33
|
+
}
|
34
|
+
})
|
35
|
+
}
|
36
|
+
|
37
|
+
/**
|
38
|
+
* @description: Clear all pending
|
39
|
+
*/
|
40
|
+
removeAllPending() {
|
41
|
+
this.pendingMap.forEach((cancel) => {
|
42
|
+
cancel && isFunction(cancel) && cancel()
|
43
|
+
})
|
44
|
+
this.pendingMap.clear()
|
45
|
+
}
|
46
|
+
|
47
|
+
/**
|
48
|
+
* Removal request
|
49
|
+
* @param {Object} config
|
50
|
+
*/
|
51
|
+
removePending(config: GAxiosOptions, key?: string) {
|
52
|
+
const url = getPendingUrl(config, key)
|
53
|
+
|
54
|
+
if (this.pendingMap.has(url)) {
|
55
|
+
// If there is a current request identifier in pending,
|
56
|
+
// the current request needs to be cancelled and removed
|
57
|
+
const cancel = this.pendingMap.get(url)
|
58
|
+
cancel && cancel(url)
|
59
|
+
this.pendingMap.delete(url)
|
60
|
+
}
|
61
|
+
}
|
62
|
+
|
63
|
+
/**
|
64
|
+
* @description: reset
|
65
|
+
*/
|
66
|
+
reset(): void {
|
67
|
+
this.pendingMap = new Map<string, Canceler>()
|
68
|
+
}
|
69
|
+
}
|
@@ -0,0 +1,142 @@
|
|
1
|
+
import { showFailToast } from 'vant'
|
2
|
+
import settings from '@/settings'
|
3
|
+
import { useStoreGlobal } from '@/store'
|
4
|
+
import { isDev } from '@/utils/env'
|
5
|
+
import { tansParams } from '@/utils/util'
|
6
|
+
import { checkURL } from '@/utils/validate'
|
7
|
+
import { isBoolean } from '@gx-design-vue/pro-utils'
|
8
|
+
import { GAxios } from './XHR'
|
9
|
+
import type { GAxiosOptions, XhtInstance } from './typings'
|
10
|
+
import { RequestEnum } from './typings'
|
11
|
+
import { handleCode } from './checkStatus'
|
12
|
+
|
13
|
+
const { requestPrefix, successCode, requestTimeout, tokenName, contentType } = settings
|
14
|
+
|
15
|
+
const xhtInstance: XhtInstance = {
|
16
|
+
/**
|
17
|
+
* @description: 处理响应数据。如果数据不是预期格式,可直接抛出错误
|
18
|
+
*/
|
19
|
+
transformResponseHook: (res, options) => {
|
20
|
+
const { customize, isReturnNativeResponse } = options
|
21
|
+
// 是否返回原生响应头 比如:需要获取响应头时使用该属性
|
22
|
+
if (isReturnNativeResponse) {
|
23
|
+
return res
|
24
|
+
}
|
25
|
+
// 不进行任何处理,直接返回
|
26
|
+
// 用于页面代码可能需要直接获取code,data,message这些信息时开启
|
27
|
+
if (customize) {
|
28
|
+
return res.data
|
29
|
+
}
|
30
|
+
// 错误的时候返回
|
31
|
+
|
32
|
+
const { data }: { data: ResponseResult } = res
|
33
|
+
if (!data) {
|
34
|
+
throw new Error('请求出错,请稍候重试')
|
35
|
+
}
|
36
|
+
// 这里 code,result,message为 后台统一的字段,需要在 types.ts内修改为项目自己的接口返回格式
|
37
|
+
const { code, msg = '', message = '' } = data
|
38
|
+
|
39
|
+
const codeVerificationArray = successCode
|
40
|
+
|
41
|
+
// 这里逻辑可以根据项目进行修改
|
42
|
+
const hasSuccess = codeVerificationArray.includes(code)
|
43
|
+
if (hasSuccess) {
|
44
|
+
return data
|
45
|
+
}
|
46
|
+
|
47
|
+
// 在此处根据自己项目的实际情况对不同的code执行不同的操作
|
48
|
+
// 如果不希望中断当前请求,请return数据,否则直接抛出异常即可
|
49
|
+
handleCode(code, message || msg)
|
50
|
+
|
51
|
+
return Promise.resolve(false)
|
52
|
+
},
|
53
|
+
|
54
|
+
// 请求之前处理config
|
55
|
+
beforeRequestHook: (config) => {
|
56
|
+
// get请求映射params参数
|
57
|
+
if (config.method?.toUpperCase() === RequestEnum.GET && config.params) {
|
58
|
+
let url = config.url + '?' + tansParams(config.params)
|
59
|
+
url = url.slice(0, -1)
|
60
|
+
config.params = {}
|
61
|
+
config.url = url
|
62
|
+
}
|
63
|
+
|
64
|
+
if (!checkURL(config.url)) {
|
65
|
+
if (config.isMock) {
|
66
|
+
config.url = `/mock-server${config.url}`
|
67
|
+
} else {
|
68
|
+
config.url = `${import.meta.env.VITE_BASE_URL}${isDev ? requestPrefix || '' : ''}${config.url}`
|
69
|
+
}
|
70
|
+
}
|
71
|
+
|
72
|
+
return config
|
73
|
+
},
|
74
|
+
|
75
|
+
/**
|
76
|
+
* @description: 请求拦截器处理
|
77
|
+
*/
|
78
|
+
requestInterceptors: (config) => {
|
79
|
+
const global = useStoreGlobal()
|
80
|
+
const carryToken = isBoolean(config.carryToken) ? config.carryToken : true
|
81
|
+
if (global.token && carryToken)
|
82
|
+
config.headers[tokenName] = global.token
|
83
|
+
return config
|
84
|
+
},
|
85
|
+
|
86
|
+
/**
|
87
|
+
* @description: 响应拦截器处理
|
88
|
+
*/
|
89
|
+
responseInterceptors: (res) => {
|
90
|
+
return res
|
91
|
+
},
|
92
|
+
|
93
|
+
/**
|
94
|
+
* @description: 响应错误处理
|
95
|
+
*/
|
96
|
+
responseInterceptorsCatch: (error: any) => {
|
97
|
+
const { response } = error
|
98
|
+
let errorMessage = error.message || ''
|
99
|
+
if (error.response && error.response.data) {
|
100
|
+
const { status } = response
|
101
|
+
handleCode(status, errorMessage)
|
102
|
+
return Promise.resolve(false)
|
103
|
+
} else {
|
104
|
+
if (errorMessage === 'Network Error') {
|
105
|
+
errorMessage = '后端接口连接异常'
|
106
|
+
}
|
107
|
+
if (errorMessage.includes('timeout')) {
|
108
|
+
errorMessage = '后端接口请求超时'
|
109
|
+
}
|
110
|
+
if (errorMessage.includes('Request failed with status code')) {
|
111
|
+
const code = errorMessage.substr(errorMessage.length - 3)
|
112
|
+
errorMessage = '后端接口' + code || '' + '异常'
|
113
|
+
}
|
114
|
+
showFailToast({ message: errorMessage || '服务器出错!' })
|
115
|
+
return Promise.resolve(false)
|
116
|
+
}
|
117
|
+
},
|
118
|
+
/**
|
119
|
+
* @description: 处理响应错误数据
|
120
|
+
*/
|
121
|
+
requestCatchHook: () => false
|
122
|
+
}
|
123
|
+
|
124
|
+
function createXhr(opt?: Partial<GAxiosOptions>) {
|
125
|
+
return new GAxios({
|
126
|
+
method: 'get',
|
127
|
+
timeout: requestTimeout,
|
128
|
+
headers: {
|
129
|
+
'Content-Type': contentType
|
130
|
+
},
|
131
|
+
// 忽略重复请求
|
132
|
+
ignoreCancelToken: true,
|
133
|
+
// 是否携带token
|
134
|
+
carryToken: true,
|
135
|
+
...xhtInstance,
|
136
|
+
...opt
|
137
|
+
})
|
138
|
+
}
|
139
|
+
|
140
|
+
const request: <T = any, R = undefined>(opt?: GAxiosOptions) => Promise<ResponseResult<T, R>> = opt => createXhr().request(opt)
|
141
|
+
|
142
|
+
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
|
+
}
|