@gx-design-vue/create-gx-cli 0.1.20 → 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.
Files changed (180) hide show
  1. package/README.md +1 -4
  2. package/package.json +5 -3
  3. package/template-mobile-vant-cli/.editorconfig +19 -0
  4. package/template-mobile-vant-cli/.env +3 -0
  5. package/template-mobile-vant-cli/.env.development +17 -0
  6. package/template-mobile-vant-cli/.env.pro +28 -0
  7. package/template-mobile-vant-cli/.env.production +31 -0
  8. package/template-mobile-vant-cli/.eslintignore +15 -0
  9. package/template-mobile-vant-cli/.prettierignore +9 -0
  10. package/template-mobile-vant-cli/README.md +1 -0
  11. package/template-mobile-vant-cli/commitlint.config.cjs +32 -0
  12. package/template-mobile-vant-cli/config/default/defaultSettings.ts +70 -0
  13. package/template-mobile-vant-cli/config/default/network.ts +10 -0
  14. package/template-mobile-vant-cli/config/default/proxy.ts +50 -0
  15. package/template-mobile-vant-cli/config/default/theme.ts +3 -0
  16. package/template-mobile-vant-cli/config/index.ts +11 -0
  17. package/template-mobile-vant-cli/eslint.config.js +58 -0
  18. package/template-mobile-vant-cli/index.html +25 -0
  19. package/template-mobile-vant-cli/internal/vite-config/generate/generateModifyVars.ts +7 -0
  20. package/template-mobile-vant-cli/internal/vite-config/rollupOptions/index.ts +22 -0
  21. package/template-mobile-vant-cli/internal/vite-config/util/hash.ts +17 -0
  22. package/template-mobile-vant-cli/internal/vite-config/util/index.ts +132 -0
  23. package/template-mobile-vant-cli/internal/vite-config/vite/cdn.ts +65 -0
  24. package/template-mobile-vant-cli/internal/vite-config/vite/optimizer.ts +26 -0
  25. package/template-mobile-vant-cli/internal/vite-config/vite/plugin/appConfig.ts +91 -0
  26. package/template-mobile-vant-cli/internal/vite-config/vite/plugin/autoImport.ts +28 -0
  27. package/template-mobile-vant-cli/internal/vite-config/vite/plugin/compress.ts +31 -0
  28. package/template-mobile-vant-cli/internal/vite-config/vite/plugin/html.ts +32 -0
  29. package/template-mobile-vant-cli/internal/vite-config/vite/plugin/index.ts +74 -0
  30. package/template-mobile-vant-cli/internal/vite-config/vite/plugin/visualizer.ts +14 -0
  31. package/template-mobile-vant-cli/internal/vite-config/vite/plugin/viteNotice.ts +40 -0
  32. package/template-mobile-vant-cli/mock/config/permissions.ts +15 -0
  33. package/template-mobile-vant-cli/mock/config/user/id.ts +5 -0
  34. package/template-mobile-vant-cli/mock/config/user/index.ts +96 -0
  35. package/template-mobile-vant-cli/mock/index.ts +55 -0
  36. package/template-mobile-vant-cli/mock/routers/table/index.fake.ts +22 -0
  37. package/template-mobile-vant-cli/mock/routers/user/account.fake.ts +52 -0
  38. package/template-mobile-vant-cli/mock/routers/user/index.fake.ts +46 -0
  39. package/template-mobile-vant-cli/mock/utils/crypto.ts +21 -0
  40. package/template-mobile-vant-cli/mock/utils/table.ts +96 -0
  41. package/template-mobile-vant-cli/mock/utils/util.ts +91 -0
  42. package/template-mobile-vant-cli/package.json +74 -0
  43. package/template-mobile-vant-cli/public/css/default.css +54 -0
  44. package/template-mobile-vant-cli/public/favicon.ico +0 -0
  45. package/template-mobile-vant-cli/src/App.vue +8 -0
  46. package/template-mobile-vant-cli/src/assets/logo.png +0 -0
  47. package/template-mobile-vant-cli/src/components/PageContainer/ProSkeleton.tsx +29 -0
  48. package/template-mobile-vant-cli/src/components/PageContainer/index.tsx +207 -0
  49. package/template-mobile-vant-cli/src/components/PageContainer/style.module.less +17 -0
  50. package/template-mobile-vant-cli/src/components/TabsMenu/index.vue +45 -0
  51. package/template-mobile-vant-cli/src/core/gx-design/index.ts +7 -0
  52. package/template-mobile-vant-cli/src/core/index.ts +5 -0
  53. package/template-mobile-vant-cli/src/design/color.less +1 -0
  54. package/template-mobile-vant-cli/src/design/config.less +5 -0
  55. package/template-mobile-vant-cli/src/design/index.less +3 -0
  56. package/template-mobile-vant-cli/src/design/mixin.less +65 -0
  57. package/template-mobile-vant-cli/src/design/reset.less +39 -0
  58. package/template-mobile-vant-cli/src/design/root.less +3 -0
  59. package/template-mobile-vant-cli/src/design/vant/index.less +0 -0
  60. package/template-mobile-vant-cli/src/hooks/web/index.ts +5 -0
  61. package/template-mobile-vant-cli/src/hooks/web/usePageLoading.ts +56 -0
  62. package/template-mobile-vant-cli/src/layout/BasicLayout/index.vue +18 -0
  63. package/template-mobile-vant-cli/src/layout/BasicLayout/style.less +3 -0
  64. package/template-mobile-vant-cli/src/layout/UserLayout/index.vue +18 -0
  65. package/template-mobile-vant-cli/src/layout/UserLayout/style.module.less +36 -0
  66. package/template-mobile-vant-cli/src/main.ts +34 -0
  67. package/template-mobile-vant-cli/src/pages/Account/details.vue +13 -0
  68. package/template-mobile-vant-cli/src/pages/Account/index.vue +102 -0
  69. package/template-mobile-vant-cli/src/pages/Home/index.vue +71 -0
  70. package/template-mobile-vant-cli/src/pages/user/login/index.vue +76 -0
  71. package/template-mobile-vant-cli/src/plugins/dayjs/index.ts +3 -0
  72. package/template-mobile-vant-cli/src/plugins/index.ts +2 -0
  73. package/template-mobile-vant-cli/src/router/guard/index.ts +18 -0
  74. package/template-mobile-vant-cli/src/router/guard/permissions.ts +41 -0
  75. package/template-mobile-vant-cli/src/router/guard/stateGuard.ts +10 -0
  76. package/template-mobile-vant-cli/src/router/index.ts +31 -0
  77. package/template-mobile-vant-cli/src/router/routes/index.ts +46 -0
  78. package/template-mobile-vant-cli/src/router/routes/modules/dataSource.ts +32 -0
  79. package/template-mobile-vant-cli/src/services/index.ts +31 -0
  80. package/template-mobile-vant-cli/src/services/userCenter/account.ts +42 -0
  81. package/template-mobile-vant-cli/src/services/userCenter/index.ts +28 -0
  82. package/template-mobile-vant-cli/src/store/index.ts +20 -0
  83. package/template-mobile-vant-cli/src/store/modules/global.ts +23 -0
  84. package/template-mobile-vant-cli/src/store/modules/user.ts +107 -0
  85. package/template-mobile-vant-cli/src/utils/accessToken.ts +97 -0
  86. package/template-mobile-vant-cli/src/utils/crypto/index.ts +56 -0
  87. package/template-mobile-vant-cli/src/utils/env.ts +50 -0
  88. package/template-mobile-vant-cli/src/utils/pageTitle.ts +20 -0
  89. package/template-mobile-vant-cli/src/utils/request/XHR.ts +137 -0
  90. package/template-mobile-vant-cli/src/utils/request/axiosCancel.ts +69 -0
  91. package/template-mobile-vant-cli/src/utils/request/checkStatus.ts +25 -0
  92. package/template-mobile-vant-cli/src/utils/request/index.ts +155 -0
  93. package/template-mobile-vant-cli/src/utils/request/typings.ts +171 -0
  94. package/template-mobile-vant-cli/src/utils/storage.ts +215 -0
  95. package/template-mobile-vant-cli/src/utils/util.ts +27 -0
  96. package/template-mobile-vant-cli/src/utils/validate.ts +211 -0
  97. package/template-mobile-vant-cli/tsconfig.json +50 -0
  98. package/template-mobile-vant-cli/types/auto-imports.d.ts +80 -0
  99. package/template-mobile-vant-cli/types/components.d.ts +7 -0
  100. package/template-mobile-vant-cli/types/config.d.ts +56 -0
  101. package/template-mobile-vant-cli/types/global.d.ts +80 -0
  102. package/template-mobile-vant-cli/types/mock.d.ts +34 -0
  103. package/template-mobile-vant-cli/types/module.d.ts +61 -0
  104. package/template-mobile-vant-cli/types/response.d.ts +17 -0
  105. package/template-mobile-vant-cli/types/system.d.ts +58 -0
  106. package/template-mobile-vant-cli/types/vant-import.d.ts +25 -0
  107. package/template-mobile-vant-cli/unocss.config.ts +161 -0
  108. package/template-mobile-vant-cli/vite.config.ts +142 -0
  109. package/template-mobile-vant-html/README.md +4 -0
  110. package/template-mobile-vant-html/css/global.css +293 -0
  111. package/template-mobile-vant-html/css/global.css.map +1 -0
  112. package/template-mobile-vant-html/css/global.less +293 -0
  113. package/template-mobile-vant-html/css/index.css +20 -0
  114. package/template-mobile-vant-html/css/index.css.map +1 -0
  115. package/template-mobile-vant-html/css/index.less +21 -0
  116. package/template-mobile-vant-html/image/disconnected.svg +60 -0
  117. package/template-mobile-vant-html/image/empty.svg +52 -0
  118. package/template-mobile-vant-html/image/error.svg +53 -0
  119. package/template-mobile-vant-html/image/loading.svg +1 -0
  120. package/template-mobile-vant-html/index.html +91 -0
  121. package/template-mobile-vant-html/js/index.js +29 -0
  122. package/template-mobile-vant-html/js/plugin/day.min.js +1 -0
  123. package/template-mobile-vant-html/js/plugin/fastclick.js +730 -0
  124. package/template-mobile-vant-html/js/plugin/jquery.min.js +2 -0
  125. package/template-mobile-vant-html/js/plugin/rem.js +28 -0
  126. package/template-mobile-vant-html/js/utils/appUtil.js +70 -0
  127. package/template-mobile-vant-html/js/utils/config.js +3 -0
  128. package/template-mobile-vant-html/js/utils/request.js +61 -0
  129. package/template-mobile-vant-html/js/utils/utils.js +65 -0
  130. package/template-mobile-vant-html/js/utils/validate.js +292 -0
  131. package/template-vite-project/.env +5 -0
  132. package/template-vite-project/.env.development +5 -0
  133. package/template-vite-project/.env.pro +5 -0
  134. package/template-vite-project/.env.production +5 -0
  135. package/template-vite-project/README.md +5 -0
  136. package/template-vite-project/_gitignore +24 -0
  137. package/template-vite-project/index.html +46 -0
  138. package/template-vite-project/internal/proxy/index.ts +47 -0
  139. package/template-vite-project/internal/vite/generate/generateModifyVars.ts +9 -0
  140. package/template-vite-project/internal/vite/rollupOptions/index.ts +18 -0
  141. package/template-vite-project/internal/vite/util/hash.ts +17 -0
  142. package/template-vite-project/internal/vite/util/index.ts +132 -0
  143. package/template-vite-project/internal/vite/vite/cdn.ts +65 -0
  144. package/template-vite-project/internal/vite/vite/plugin/appConfig.ts +91 -0
  145. package/template-vite-project/internal/vite/vite/plugin/autoImport.ts +28 -0
  146. package/template-vite-project/internal/vite/vite/plugin/compress.ts +31 -0
  147. package/template-vite-project/internal/vite/vite/plugin/html.ts +24 -0
  148. package/template-vite-project/internal/vite/vite/plugin/index.ts +72 -0
  149. package/template-vite-project/internal/vite/vite/plugin/visualizer.ts +14 -0
  150. package/template-vite-project/internal/vite/vite/plugin/viteNotice.ts +40 -0
  151. package/template-vite-project/node_modules/.bin/cross-env +21 -0
  152. package/template-vite-project/node_modules/.bin/cross-env-shell +21 -0
  153. package/template-vite-project/node_modules/.bin/esbuild +21 -0
  154. package/template-vite-project/node_modules/.bin/jiti +21 -0
  155. package/template-vite-project/node_modules/.bin/lessc +21 -0
  156. package/template-vite-project/node_modules/.bin/parser +21 -0
  157. package/template-vite-project/node_modules/.bin/rollup +21 -0
  158. package/template-vite-project/node_modules/.bin/terser +21 -0
  159. package/template-vite-project/node_modules/.bin/tsc +21 -0
  160. package/template-vite-project/node_modules/.bin/tsserver +21 -0
  161. package/template-vite-project/node_modules/.bin/vite +21 -0
  162. package/template-vite-project/node_modules/.bin/vue-tsc +21 -0
  163. package/template-vite-project/node_modules/.bin/yaml +21 -0
  164. package/template-vite-project/package.json +38 -0
  165. package/template-vite-project/public/css/index.css +89 -0
  166. package/template-vite-project/public/css/normalize.css +396 -0
  167. package/template-vite-project/public/vite.svg +1 -0
  168. package/template-vite-project/src/App.vue +30 -0
  169. package/template-vite-project/src/assets/vue.svg +1 -0
  170. package/template-vite-project/src/components/HelloWorld.vue +41 -0
  171. package/template-vite-project/src/design/config.less +0 -0
  172. package/template-vite-project/src/design/index.less +0 -0
  173. package/template-vite-project/src/main.ts +14 -0
  174. package/template-vite-project/src/utils/env.ts +50 -0
  175. package/template-vite-project/tsconfig.app.json +59 -0
  176. package/template-vite-project/tsconfig.json +7 -0
  177. package/template-vite-project/tsconfig.node.json +28 -0
  178. package/template-vite-project/types/global.d.ts +33 -0
  179. package/template-vite-project/unocss.config.ts +208 -0
  180. package/template-vite-project/vite.config.ts +88 -0
@@ -0,0 +1,69 @@
1
+ import type { Canceler } from 'axios'
2
+ import type { GAxiosOptions } from './typings'
3
+ import { isFunction } from '@gx-design-vue/pro-utils'
4
+ import axios from 'axios'
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,25 @@
1
+ import { router } from '@/router'
2
+ import { useStoreUser } from '@/store'
3
+ import { showToast } from 'vant'
4
+
5
+ /**
6
+ * @author gx12358 2539306317@qq.com
7
+ * @description 处理code异常
8
+ * @param {*} code
9
+ * @param {*} msg
10
+ */
11
+ export const handleCode = (code: number, msg: string) => {
12
+ const user = useStoreUser()
13
+ switch (code) {
14
+ case 401:
15
+ user.resetPermissions()
16
+ router.push({ path: '/user/login' })
17
+ break
18
+ default:
19
+ showToast({
20
+ type: 'fail',
21
+ message: msg || '后端接口异常'
22
+ })
23
+ break
24
+ }
25
+ }
@@ -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
+ }