@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.
Files changed (181) hide show
  1. package/README.md +1 -4
  2. package/dist/index.js +36 -48
  3. package/package.json +5 -3
  4. package/template-mobile-vant-cli/.editorconfig +19 -0
  5. package/template-mobile-vant-cli/.env +3 -0
  6. package/template-mobile-vant-cli/.env.development +17 -0
  7. package/template-mobile-vant-cli/.env.pro +28 -0
  8. package/template-mobile-vant-cli/.env.production +31 -0
  9. package/template-mobile-vant-cli/.eslintignore +15 -0
  10. package/template-mobile-vant-cli/.prettierignore +9 -0
  11. package/template-mobile-vant-cli/README.md +1 -0
  12. package/template-mobile-vant-cli/commitlint.config.cjs +32 -0
  13. package/template-mobile-vant-cli/config/default/defaultSettings.ts +70 -0
  14. package/template-mobile-vant-cli/config/default/network.ts +10 -0
  15. package/template-mobile-vant-cli/config/default/proxy.ts +50 -0
  16. package/template-mobile-vant-cli/config/default/theme.ts +3 -0
  17. package/template-mobile-vant-cli/config/index.ts +11 -0
  18. package/template-mobile-vant-cli/eslint.config.js +58 -0
  19. package/template-mobile-vant-cli/index.html +25 -0
  20. package/template-mobile-vant-cli/internal/vite-config/generate/generateModifyVars.ts +7 -0
  21. package/template-mobile-vant-cli/internal/vite-config/rollupOptions/index.ts +22 -0
  22. package/template-mobile-vant-cli/internal/vite-config/util/hash.ts +17 -0
  23. package/template-mobile-vant-cli/internal/vite-config/util/index.ts +132 -0
  24. package/template-mobile-vant-cli/internal/vite-config/vite/cdn.ts +65 -0
  25. package/template-mobile-vant-cli/internal/vite-config/vite/optimizer.ts +26 -0
  26. package/template-mobile-vant-cli/internal/vite-config/vite/plugin/appConfig.ts +91 -0
  27. package/template-mobile-vant-cli/internal/vite-config/vite/plugin/autoImport.ts +28 -0
  28. package/template-mobile-vant-cli/internal/vite-config/vite/plugin/compress.ts +31 -0
  29. package/template-mobile-vant-cli/internal/vite-config/vite/plugin/html.ts +32 -0
  30. package/template-mobile-vant-cli/internal/vite-config/vite/plugin/index.ts +74 -0
  31. package/template-mobile-vant-cli/internal/vite-config/vite/plugin/visualizer.ts +14 -0
  32. package/template-mobile-vant-cli/internal/vite-config/vite/plugin/viteNotice.ts +40 -0
  33. package/template-mobile-vant-cli/mock/config/permissions.ts +15 -0
  34. package/template-mobile-vant-cli/mock/config/user/id.ts +5 -0
  35. package/template-mobile-vant-cli/mock/config/user/index.ts +96 -0
  36. package/template-mobile-vant-cli/mock/index.ts +55 -0
  37. package/template-mobile-vant-cli/mock/routers/table/index.fake.ts +22 -0
  38. package/template-mobile-vant-cli/mock/routers/user/account.fake.ts +52 -0
  39. package/template-mobile-vant-cli/mock/routers/user/index.fake.ts +46 -0
  40. package/template-mobile-vant-cli/mock/utils/crypto.ts +21 -0
  41. package/template-mobile-vant-cli/mock/utils/table.ts +96 -0
  42. package/template-mobile-vant-cli/mock/utils/util.ts +91 -0
  43. package/template-mobile-vant-cli/package.json +74 -0
  44. package/template-mobile-vant-cli/public/css/default.css +54 -0
  45. package/template-mobile-vant-cli/public/favicon.ico +0 -0
  46. package/template-mobile-vant-cli/src/App.vue +8 -0
  47. package/template-mobile-vant-cli/src/assets/logo.png +0 -0
  48. package/template-mobile-vant-cli/src/components/PageContainer/ProSkeleton.tsx +29 -0
  49. package/template-mobile-vant-cli/src/components/PageContainer/index.tsx +207 -0
  50. package/template-mobile-vant-cli/src/components/PageContainer/style.module.less +17 -0
  51. package/template-mobile-vant-cli/src/components/TabsMenu/index.vue +45 -0
  52. package/template-mobile-vant-cli/src/core/gx-design/index.ts +7 -0
  53. package/template-mobile-vant-cli/src/core/index.ts +5 -0
  54. package/template-mobile-vant-cli/src/design/color.less +1 -0
  55. package/template-mobile-vant-cli/src/design/config.less +5 -0
  56. package/template-mobile-vant-cli/src/design/index.less +3 -0
  57. package/template-mobile-vant-cli/src/design/mixin.less +65 -0
  58. package/template-mobile-vant-cli/src/design/reset.less +39 -0
  59. package/template-mobile-vant-cli/src/design/root.less +3 -0
  60. package/template-mobile-vant-cli/src/design/vant/index.less +0 -0
  61. package/template-mobile-vant-cli/src/hooks/web/index.ts +5 -0
  62. package/template-mobile-vant-cli/src/hooks/web/usePageLoading.ts +56 -0
  63. package/template-mobile-vant-cli/src/layout/BasicLayout/index.vue +18 -0
  64. package/template-mobile-vant-cli/src/layout/BasicLayout/style.less +3 -0
  65. package/template-mobile-vant-cli/src/layout/UserLayout/index.vue +18 -0
  66. package/template-mobile-vant-cli/src/layout/UserLayout/style.module.less +36 -0
  67. package/template-mobile-vant-cli/src/main.ts +34 -0
  68. package/template-mobile-vant-cli/src/pages/Account/details.vue +13 -0
  69. package/template-mobile-vant-cli/src/pages/Account/index.vue +102 -0
  70. package/template-mobile-vant-cli/src/pages/Home/index.vue +71 -0
  71. package/template-mobile-vant-cli/src/pages/user/login/index.vue +76 -0
  72. package/template-mobile-vant-cli/src/plugins/dayjs/index.ts +3 -0
  73. package/template-mobile-vant-cli/src/plugins/index.ts +2 -0
  74. package/template-mobile-vant-cli/src/router/guard/index.ts +18 -0
  75. package/template-mobile-vant-cli/src/router/guard/permissions.ts +41 -0
  76. package/template-mobile-vant-cli/src/router/guard/stateGuard.ts +10 -0
  77. package/template-mobile-vant-cli/src/router/index.ts +31 -0
  78. package/template-mobile-vant-cli/src/router/routes/index.ts +46 -0
  79. package/template-mobile-vant-cli/src/router/routes/modules/dataSource.ts +32 -0
  80. package/template-mobile-vant-cli/src/services/index.ts +31 -0
  81. package/template-mobile-vant-cli/src/services/userCenter/account.ts +42 -0
  82. package/template-mobile-vant-cli/src/services/userCenter/index.ts +28 -0
  83. package/template-mobile-vant-cli/src/store/index.ts +20 -0
  84. package/template-mobile-vant-cli/src/store/modules/global.ts +23 -0
  85. package/template-mobile-vant-cli/src/store/modules/user.ts +107 -0
  86. package/template-mobile-vant-cli/src/utils/accessToken.ts +97 -0
  87. package/template-mobile-vant-cli/src/utils/crypto/index.ts +56 -0
  88. package/template-mobile-vant-cli/src/utils/env.ts +50 -0
  89. package/template-mobile-vant-cli/src/utils/pageTitle.ts +20 -0
  90. package/template-mobile-vant-cli/src/utils/request/XHR.ts +137 -0
  91. package/template-mobile-vant-cli/src/utils/request/axiosCancel.ts +69 -0
  92. package/template-mobile-vant-cli/src/utils/request/checkStatus.ts +25 -0
  93. package/template-mobile-vant-cli/src/utils/request/index.ts +155 -0
  94. package/template-mobile-vant-cli/src/utils/request/typings.ts +171 -0
  95. package/template-mobile-vant-cli/src/utils/storage.ts +215 -0
  96. package/template-mobile-vant-cli/src/utils/util.ts +27 -0
  97. package/template-mobile-vant-cli/src/utils/validate.ts +211 -0
  98. package/template-mobile-vant-cli/tsconfig.json +50 -0
  99. package/template-mobile-vant-cli/types/auto-imports.d.ts +80 -0
  100. package/template-mobile-vant-cli/types/components.d.ts +7 -0
  101. package/template-mobile-vant-cli/types/config.d.ts +56 -0
  102. package/template-mobile-vant-cli/types/global.d.ts +80 -0
  103. package/template-mobile-vant-cli/types/mock.d.ts +34 -0
  104. package/template-mobile-vant-cli/types/module.d.ts +61 -0
  105. package/template-mobile-vant-cli/types/response.d.ts +17 -0
  106. package/template-mobile-vant-cli/types/system.d.ts +58 -0
  107. package/template-mobile-vant-cli/types/vant-import.d.ts +25 -0
  108. package/template-mobile-vant-cli/unocss.config.ts +161 -0
  109. package/template-mobile-vant-cli/vite.config.ts +142 -0
  110. package/template-mobile-vant-html/README.md +4 -0
  111. package/template-mobile-vant-html/css/global.css +293 -0
  112. package/template-mobile-vant-html/css/global.css.map +1 -0
  113. package/template-mobile-vant-html/css/global.less +293 -0
  114. package/template-mobile-vant-html/css/index.css +20 -0
  115. package/template-mobile-vant-html/css/index.css.map +1 -0
  116. package/template-mobile-vant-html/css/index.less +21 -0
  117. package/template-mobile-vant-html/image/disconnected.svg +60 -0
  118. package/template-mobile-vant-html/image/empty.svg +52 -0
  119. package/template-mobile-vant-html/image/error.svg +53 -0
  120. package/template-mobile-vant-html/image/loading.svg +1 -0
  121. package/template-mobile-vant-html/index.html +91 -0
  122. package/template-mobile-vant-html/js/index.js +29 -0
  123. package/template-mobile-vant-html/js/plugin/day.min.js +1 -0
  124. package/template-mobile-vant-html/js/plugin/fastclick.js +730 -0
  125. package/template-mobile-vant-html/js/plugin/jquery.min.js +2 -0
  126. package/template-mobile-vant-html/js/plugin/rem.js +28 -0
  127. package/template-mobile-vant-html/js/utils/appUtil.js +70 -0
  128. package/template-mobile-vant-html/js/utils/config.js +3 -0
  129. package/template-mobile-vant-html/js/utils/request.js +61 -0
  130. package/template-mobile-vant-html/js/utils/utils.js +65 -0
  131. package/template-mobile-vant-html/js/utils/validate.js +292 -0
  132. package/template-vite-project/.env +5 -0
  133. package/template-vite-project/.env.development +5 -0
  134. package/template-vite-project/.env.pro +5 -0
  135. package/template-vite-project/.env.production +5 -0
  136. package/template-vite-project/README.md +5 -0
  137. package/template-vite-project/_gitignore +24 -0
  138. package/template-vite-project/index.html +46 -0
  139. package/template-vite-project/internal/proxy/index.ts +47 -0
  140. package/template-vite-project/internal/vite/generate/generateModifyVars.ts +9 -0
  141. package/template-vite-project/internal/vite/rollupOptions/index.ts +18 -0
  142. package/template-vite-project/internal/vite/util/hash.ts +17 -0
  143. package/template-vite-project/internal/vite/util/index.ts +132 -0
  144. package/template-vite-project/internal/vite/vite/cdn.ts +65 -0
  145. package/template-vite-project/internal/vite/vite/plugin/appConfig.ts +91 -0
  146. package/template-vite-project/internal/vite/vite/plugin/autoImport.ts +28 -0
  147. package/template-vite-project/internal/vite/vite/plugin/compress.ts +31 -0
  148. package/template-vite-project/internal/vite/vite/plugin/html.ts +24 -0
  149. package/template-vite-project/internal/vite/vite/plugin/index.ts +72 -0
  150. package/template-vite-project/internal/vite/vite/plugin/visualizer.ts +14 -0
  151. package/template-vite-project/internal/vite/vite/plugin/viteNotice.ts +40 -0
  152. package/template-vite-project/node_modules/.bin/cross-env +21 -0
  153. package/template-vite-project/node_modules/.bin/cross-env-shell +21 -0
  154. package/template-vite-project/node_modules/.bin/esbuild +21 -0
  155. package/template-vite-project/node_modules/.bin/jiti +21 -0
  156. package/template-vite-project/node_modules/.bin/lessc +21 -0
  157. package/template-vite-project/node_modules/.bin/parser +21 -0
  158. package/template-vite-project/node_modules/.bin/rollup +21 -0
  159. package/template-vite-project/node_modules/.bin/terser +21 -0
  160. package/template-vite-project/node_modules/.bin/tsc +21 -0
  161. package/template-vite-project/node_modules/.bin/tsserver +21 -0
  162. package/template-vite-project/node_modules/.bin/vite +21 -0
  163. package/template-vite-project/node_modules/.bin/vue-tsc +21 -0
  164. package/template-vite-project/node_modules/.bin/yaml +21 -0
  165. package/template-vite-project/package.json +38 -0
  166. package/template-vite-project/public/css/index.css +89 -0
  167. package/template-vite-project/public/css/normalize.css +396 -0
  168. package/template-vite-project/public/vite.svg +1 -0
  169. package/template-vite-project/src/App.vue +30 -0
  170. package/template-vite-project/src/assets/vue.svg +1 -0
  171. package/template-vite-project/src/components/HelloWorld.vue +41 -0
  172. package/template-vite-project/src/design/config.less +0 -0
  173. package/template-vite-project/src/design/index.less +0 -0
  174. package/template-vite-project/src/main.ts +14 -0
  175. package/template-vite-project/src/utils/env.ts +50 -0
  176. package/template-vite-project/tsconfig.app.json +59 -0
  177. package/template-vite-project/tsconfig.json +7 -0
  178. package/template-vite-project/tsconfig.node.json +28 -0
  179. package/template-vite-project/types/global.d.ts +33 -0
  180. package/template-vite-project/unocss.config.ts +208 -0
  181. package/template-vite-project/vite.config.ts +88 -0
@@ -0,0 +1,107 @@
1
+ import { getUserInfo, login, logout } from '@/services/userCenter'
2
+ import { getAccessToken, removeAccessToken, setAccessToken } from '@/utils/accessToken'
3
+ import { defaultSettings } from '@gx-config'
4
+ import { useReactiveState } from '@gx-design-vue/pro-hooks'
5
+ import { isArray, isNumber, isObject } from '@gx-design-vue/pro-utils'
6
+ import { defineStore } from 'pinia'
7
+
8
+ const { loginInterception } = defaultSettings.system
9
+
10
+ export interface UserState {
11
+ accessToken: string;
12
+ userInfo: UserDetails;
13
+ }
14
+
15
+ // 0 返回登录页 1 成功 2 返回注册页
16
+ export type CheckUserStatus = 0 | 1 | 2
17
+
18
+ export const useStoreUser = defineStore('user', () => {
19
+ const [ state, setValue ] = useReactiveState<UserState>({
20
+ accessToken: getAccessToken(),
21
+ userInfo: {} as UserDetails
22
+ }, { omitNil: false, omitEmpty: false })
23
+
24
+ /**
25
+ * @Author gx12358
26
+ * @DateTime 2022/1/11
27
+ * @lastTime 2022/1/11
28
+ * @description 登录拦截放行时,设置虚拟角色
29
+ */
30
+ const setVirtualUserInfo = (): CheckUserStatus => {
31
+ return 1
32
+ }
33
+
34
+ /**
35
+ * @Author gx12358
36
+ * @DateTime 2022/1/11
37
+ * @lastTime 2022/1/11
38
+ * @description 登录
39
+ */
40
+ const userLogin = async (params: any): Promise<boolean> => {
41
+ const response: ResponseResult<{ token: string; expiresIn: number }> = await login(params)
42
+ if (response) {
43
+ const accessToken = response.data?.token
44
+ if (accessToken) {
45
+ setValue({ accessToken })
46
+ setAccessToken(accessToken, response.data?.expiresIn ? response.data?.expiresIn * 60 * 1000 : 0)
47
+ return true
48
+ }
49
+ }
50
+
51
+ return false
52
+ }
53
+
54
+ const updateUserInfo = async (): Promise<CheckUserStatus> => {
55
+ let status: CheckUserStatus = 0
56
+ const response: ResponseResult<null, UserInfo> = await getUserInfo()
57
+ const { user, roles } = response || {} as UserInfo
58
+ if (response && user && isObject(user)) {
59
+ if (isNumber(user.userId) && roles && isArray(roles)) {
60
+ status = 1
61
+ setValue({ userInfo: { ...user } })
62
+ }
63
+ }
64
+ return status
65
+ }
66
+
67
+ /**
68
+ * @Author gx12358
69
+ * @DateTime 2022/1/11
70
+ * @lastTime 2022/1/11
71
+ * @description 获取用户信息
72
+ */
73
+ const checkUserPremission = async (): Promise<CheckUserStatus> => {
74
+ let status: CheckUserStatus = 0
75
+ if (loginInterception) {
76
+ status = await updateUserInfo()
77
+ } else {
78
+ status = setVirtualUserInfo()
79
+ }
80
+ return status
81
+ }
82
+
83
+ const resetPermissions = () => {
84
+ removeAccessToken()
85
+ setValue({ accessToken: '', userInfo: {} })
86
+ }
87
+
88
+ /**
89
+ * @Author gx12358
90
+ * @DateTime 2022/5/15
91
+ * @lastTime 2022/5/15
92
+ * @description 用户退出登录
93
+ */
94
+ const userLogut = async () => {
95
+ await logout()
96
+ resetPermissions()
97
+ }
98
+
99
+ return {
100
+ ...state,
101
+ userLogin,
102
+ userLogut,
103
+ setValue,
104
+ resetPermissions,
105
+ checkUserPremission
106
+ }
107
+ })
@@ -0,0 +1,97 @@
1
+ import {
2
+ delCookie,
3
+ getCookie,
4
+ getStorage,
5
+ removeStorage,
6
+ setCookie,
7
+ setStorage
8
+ } from '@/utils/storage'
9
+ import { defaultSettings } from '@gx-config'
10
+
11
+ const { storage, storageName } = defaultSettings.token
12
+
13
+ /**
14
+ * @Author gx12358
15
+ * @DateTime 2021/12/27
16
+ * @lastTime 2021/12/27
17
+ * @description 获取accessToken
18
+ */
19
+ export function getAccessToken() {
20
+ if (storage) {
21
+ if (storage === 'localStorage') {
22
+ return getStorage({ key: storageName, originKey: true })
23
+ } else if (storage === 'sessionStorage') {
24
+ return getStorage({ key: storageName, type: 'session', originKey: true })
25
+ } else if (storage === 'cookie') {
26
+ return getCookie(storageName)
27
+ } else {
28
+ return getStorage({ key: storageName, originKey: true })
29
+ }
30
+ } else {
31
+ return getStorage({ key: storageName, originKey: true })
32
+ }
33
+ }
34
+
35
+ /**
36
+ * @author gx12358 2539306317@qq.com
37
+ * @description 存储accessToken
38
+ * @param accessToken
39
+ * @returns {void|*}
40
+ */
41
+ export function setAccessToken(accessToken: string, expired?: number) {
42
+ if (storage) {
43
+ if (storage === 'localStorage') {
44
+ return setStorage({
45
+ key: storageName,
46
+ originKey: true,
47
+ value: accessToken,
48
+ expired
49
+ })
50
+ } else if (storage === 'sessionStorage') {
51
+ return setStorage({
52
+ key: storageName,
53
+ originKey: true,
54
+ value: accessToken,
55
+ expired,
56
+ type: 'session'
57
+ })
58
+ } else if (storage === 'cookie') {
59
+ return setCookie(storageName, accessToken, expired)
60
+ } else {
61
+ return setStorage({
62
+ key: storageName,
63
+ originKey: true,
64
+ value: accessToken,
65
+ expired
66
+ })
67
+ }
68
+ } else {
69
+ return setStorage({
70
+ key: storageName,
71
+ originKey: true,
72
+ value: accessToken,
73
+ expired
74
+ })
75
+ }
76
+ }
77
+
78
+ /**
79
+ * @author gx12358 2539306317@qq.com
80
+ * @description 移除accessToken
81
+ * @returns {void|Promise<void>}
82
+ */
83
+ export function removeAccessToken() {
84
+ if (storage) {
85
+ if (storage === 'localStorage') {
86
+ return removeStorage({ key: storageName, originKey: true })
87
+ } else if (storage === 'sessionStorage') {
88
+ return removeStorage({ key: storageName, type: 'session', originKey: true })
89
+ } else if (storage === 'cookie') {
90
+ return delCookie(storageName)
91
+ } else {
92
+ return removeStorage({ key: storageName, originKey: true })
93
+ }
94
+ } else {
95
+ return removeStorage({ key: storageName, originKey: true })
96
+ }
97
+ }
@@ -0,0 +1,56 @@
1
+ import { isArray, isJSONStr, isObject } from '@gx-design-vue/pro-utils'
2
+ import CryptoProJS from 'crypto-js'
3
+ import * as CryptoJS from 'crypto-js/core'
4
+ import 'crypto-js/enc-utf8'
5
+ import 'crypto-js/tripledes'
6
+ import 'crypto-js/sha1'
7
+
8
+ const key = '1234123412ABCDEF' // 十六位十六进制数作为密钥
9
+
10
+ /**
11
+ * 登录密码加密
12
+ * @param password
13
+ * @returns {string}
14
+ */
15
+ export const encodePassword = (password: string) => {
16
+ const key = CryptoProJS.enc.Utf8.parse('8QONwyJtHesysWpM')
17
+ const passwordENC = CryptoProJS.AES.encrypt(password, key, {
18
+ mode: CryptoProJS.mode.ECB,
19
+ padding: CryptoProJS.pad.Pkcs7
20
+ })
21
+ const encodePW = passwordENC.ciphertext.toString()
22
+ return encodePW
23
+ }
24
+
25
+ // 加密方法
26
+ export function Encrypt(word) {
27
+ let str: string | object = word
28
+ if (isObject(word) || isArray(word)) {
29
+ str = JSON.stringify(word)
30
+ }
31
+ const keyHex = CryptoJS.enc.Utf8.parse(key)
32
+ const ivHex = CryptoJS.enc.Utf8.parse(key)
33
+ const encrypted = CryptoJS.DES.encrypt(str, keyHex, {
34
+ iv: ivHex,
35
+ mode: CryptoJS.mode.CBC,
36
+ padding: CryptoJS.pad.Pkcs7
37
+ })
38
+ return CryptoJS.enc.Base64.stringify(encrypted.ciphertext)
39
+ }
40
+
41
+ // 解密方法
42
+ export function Decrypt(word) {
43
+ const keyHex = CryptoJS.enc.Utf8.parse(key)
44
+ const ivHex = CryptoJS.enc.Utf8.parse(key)
45
+ const decrypted = CryptoJS.DES.decrypt({
46
+ ciphertext: CryptoJS.enc.Base64.parse(word)
47
+ }, keyHex, {
48
+ iv: ivHex,
49
+ mode: CryptoJS.mode.CBC,
50
+ padding: CryptoJS.pad.Pkcs7
51
+ })
52
+ const decryptedStr = decrypted.toString(CryptoJS.enc.Utf8)
53
+ return isJSONStr(decryptedStr.toString())
54
+ ? JSON.parse(decryptedStr.toString())
55
+ : decryptedStr.toString()
56
+ }
@@ -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_APP_ENV') !== 'dev'
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,20 @@
1
+ import { defaultSettings } from '@gx-config'
2
+
3
+ const { title, titleReverse, titleSeparator } = defaultSettings.system
4
+
5
+ /**
6
+ * @Author gx12358
7
+ * @DateTime 2024/9/20
8
+ * @lastTime 2024/9/20
9
+ * @description 设置标题
10
+ */
11
+ export default function getPageTitle(pageTitle: string) {
12
+ let newTitles: string[] = []
13
+ if (pageTitle)
14
+ newTitles.push(pageTitle)
15
+ if (title)
16
+ newTitles.push(title)
17
+ if (titleReverse)
18
+ newTitles = newTitles.reverse()
19
+ return newTitles.join(titleSeparator)
20
+ }
@@ -0,0 +1,137 @@
1
+ import type { AxiosError } from 'axios'
2
+ import type { GAxiosInstance, GAxiosOptions, GAxiosResponse } from './typings'
3
+ import { isFunction } from '@gx-design-vue/pro-utils'
4
+ import axios from 'axios'
5
+ import { cloneDeep } from 'lodash-es'
6
+ import qs from 'qs'
7
+ import { AxiosCanceler } from './axiosCancel'
8
+ import { ContentTypeEnum, RequestEnum } from './typings'
9
+
10
+ /**
11
+ * @Author gx12358
12
+ * @DateTime 2023/1/6
13
+ * @lastTime 2023/1/6
14
+ * @description 重新定义axios
15
+ */
16
+ export class GAxios {
17
+ private axiosInstance: GAxiosInstance
18
+ private readonly options: GAxiosOptions
19
+
20
+ constructor(options: GAxiosOptions) {
21
+ this.options = options
22
+ this.axiosInstance = axios.create(options) as GAxiosInstance
23
+ this.setupInterceptors()
24
+ }
25
+
26
+ /**
27
+ * @description: Interceptor configuration 拦截器配置
28
+ */
29
+ private setupInterceptors() {
30
+ const {
31
+ requestInterceptors,
32
+ requestInterceptorsCatch,
33
+ responseInterceptors,
34
+ responseInterceptorsCatch
35
+ } = this.options
36
+
37
+ const axiosCanceler = new AxiosCanceler(this.options.ignoreCancelToken)
38
+
39
+ this.axiosInstance.interceptors.request.use<GAxiosOptions>((config) => {
40
+ const { cancelCallBackHook } = config
41
+ axiosCanceler.addPending(config)
42
+ if (cancelCallBackHook) {
43
+ cancelCallBackHook({
44
+ reset: axiosCanceler.reset,
45
+ cancel: (key?: string) => axiosCanceler.removePending(config, key),
46
+ cancelAll: axiosCanceler.removeAllPending
47
+ })
48
+ }
49
+ if (requestInterceptors && isFunction(requestInterceptors)) {
50
+ config = requestInterceptors(config)
51
+ }
52
+ return config
53
+ }, undefined)
54
+
55
+ // Request interceptor error capture
56
+ requestInterceptorsCatch && isFunction(requestInterceptorsCatch) && this.axiosInstance.interceptors.request.use(
57
+ undefined,
58
+ requestInterceptorsCatch
59
+ )
60
+
61
+ // Response result interceptor processing
62
+ this.axiosInstance.interceptors.response.use((res: GAxiosResponse) => {
63
+ res && axiosCanceler.removePending(res.config)
64
+ if (responseInterceptors && isFunction(responseInterceptors)) {
65
+ return responseInterceptors(res)
66
+ }
67
+ return res
68
+ }, undefined)
69
+
70
+ // Response result interceptor error capture
71
+ responseInterceptorsCatch && isFunction(responseInterceptorsCatch) && this.axiosInstance.interceptors.response.use(
72
+ undefined,
73
+ (error) => {
74
+ axiosCanceler.removePending(error.config)
75
+
76
+ return responseInterceptorsCatch(this.axiosInstance, error)
77
+ }
78
+ )
79
+ }
80
+
81
+ // support form-data
82
+ supportFormData(config: GAxiosOptions) {
83
+ const headers = config.headers || this.options.headers
84
+ const contentType = headers?.['Content-Type'] || headers?.['content-type']
85
+
86
+ if (contentType !== ContentTypeEnum.FORM_URLENCODED || !Reflect.has(
87
+ config,
88
+ 'data'
89
+ ) || config.method?.toUpperCase() === RequestEnum.GET) {
90
+ return config
91
+ }
92
+
93
+ return {
94
+ ...config,
95
+ data: qs.stringify(config.data, { arrayFormat: 'brackets' })
96
+ }
97
+ }
98
+
99
+ request<T = ResponseResult | boolean>(config?: GAxiosOptions): Promise<T> {
100
+ let conf = cloneDeep(config || {} as GAxiosOptions)
101
+
102
+ const opt: GAxiosOptions = Object.assign({}, this.options, conf)
103
+
104
+ const { beforeRequestHook, requestCatchHook, transformResponseHook } = opt
105
+ if (beforeRequestHook && isFunction(beforeRequestHook)) {
106
+ conf = beforeRequestHook(opt)
107
+ }
108
+
109
+ conf = this.supportFormData(opt)
110
+
111
+ return new Promise((resolve) => {
112
+ this.axiosInstance
113
+ .request<any, GAxiosResponse>(conf)
114
+ .then((res: GAxiosResponse) => {
115
+ if (transformResponseHook && isFunction(transformResponseHook)) {
116
+ try {
117
+ const ret = transformResponseHook(res, config || {})
118
+ resolve(ret)
119
+ } catch (error) {
120
+ console.error('request-error', error)
121
+ resolve(false as unknown as Promise<T>)
122
+ return
123
+ }
124
+ return
125
+ }
126
+ resolve(res as unknown as Promise<T>)
127
+ })
128
+ .catch((e: Error | AxiosError) => {
129
+ if (requestCatchHook && isFunction(requestCatchHook)) {
130
+ resolve(requestCatchHook(e) as any)
131
+ return
132
+ }
133
+ resolve(false as any)
134
+ })
135
+ })
136
+ }
137
+ }
@@ -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
+ }