agilebuilder-ui 1.0.71-tmp3 → 1.0.71-tmp6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agilebuilder-ui",
3
- "version": "1.0.71tmp3",
3
+ "version": "1.0.71tmp6",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "main": "./lib/super-ui.js",
@@ -34,6 +34,7 @@
34
34
  "vue": "3.3.4",
35
35
  "vue-i18n": "^9.5.0",
36
36
  "vue-router": "^4.2.5",
37
- "vuex": "^4.1.0"
37
+ "vuex": "^4.1.0",
38
+ "axios": "^1.5.1"
38
39
  }
39
40
  }
@@ -3,7 +3,7 @@ import authApi from './auth-api'
3
3
  import { getToken, getLanguage, getAllLanguages, setAllLanguages } from './auth'
4
4
  import { v4 as uuidv4 } from 'uuid'
5
5
  import { getCookieCache } from './auth'
6
-
6
+ import i18nUtil from './i18n-util'
7
7
  /**
8
8
  * 获得相对地址
9
9
  */
@@ -312,15 +312,7 @@ export function isMobileBrowser() {
312
312
  }
313
313
 
314
314
  export function getLocaleByLang(lang) {
315
- let locale = 'cn'
316
- if (lang && lang.indexOf('_') > 0) {
317
- const language = lang.substring(0, lang.indexOf('_'))
318
- locale = language
319
- }
320
- if (locale === 'zh') {
321
- locale = 'cn'
322
- }
323
- return locale
315
+ return i18nUtil.getLocaleByLang(lang)
324
316
  }
325
317
 
326
318
  export function cacheLangs(langs) {
@@ -439,28 +431,71 @@ export function cacheCurrentLanguageUtil(http) {
439
431
  const token = getToken()
440
432
  if (token) {
441
433
  const currentLanguage = getLanguage()
442
- debugger
443
- if (
444
- currentLanguage &&
445
- currentLanguage !== 'undefined' &&
446
- getLangFileUrl(window.$vueApp.config.globalProperties.currentSystem, currentLanguage)
447
- ) {
448
- resolve(currentLanguage)
434
+ const systemCode = getCurrentSystemCode()
435
+ if (isPlateSys(systemCode)) {
436
+ // 平台系统不需要加载国际化文件
437
+ if (currentLanguage && currentLanguage !== 'undefined') {
438
+ resolve(currentLanguage)
439
+ } else {
440
+ // 如果没有缓存当前用户的语言,则需要获取
441
+ const params = { systemCode: systemCode }
442
+ http
443
+ .post(window.$vueApp.config.globalProperties.baseAPI + '/acs/user/i18n-languages', params)
444
+ .then((langInfo) => {
445
+ resolve(langInfo.language ? langInfo.language : 'zh_CN')
446
+ })
447
+ .catch(() => {
448
+ resolve('zh_CN')
449
+ })
450
+ }
449
451
  } else {
450
- // http.get(window.$vueApp.config.globalProperties.baseAPI + '/acs/user-languages').then((currentLanguage) => {
451
- http
452
- .get(
453
- window.$vueApp.config.globalProperties.baseAPI +
454
- '/acs/user/i18n-languages?systemCode=' +
455
- window.$vueApp.config.globalProperties.currentSystem
456
- )
457
- .then((langInfo) => {
458
- debugger
459
- if (langInfo.i18nFileUrls && langInfo.i18nFileUrls[langInfo.language]) {
460
- window.$vueApp.config.globalProperties.$loadScript(langInfo.i18nFileUrls[langInfo.language])
461
- }
462
- resolve(langInfo.language)
463
- })
452
+ // 业务系统是否启用了国际化
453
+ const enableI18n = i18nUtil.getEnableI18nState(systemCode)
454
+ if (!enableI18n && currentLanguage && currentLanguage !== 'undefined') {
455
+ // 如果没有开启国际化, 并且已经缓存了当前用户的语言,则直接返回
456
+ resolve(currentLanguage)
457
+ } else {
458
+ // 启用了国际化并且当前已经加载过了国际化json文件,则需要检查是否是最新的国际化文件
459
+ // 没有加载过国际化文件,则不用传递checkLastestI18nFile参数,后台会返回最新的国际化文件路径和用户当前语言
460
+ const url = i18nUtil.getLangFileUrl(systemCode, currentLanguage)
461
+ const params = { systemCode: systemCode }
462
+ if (url && currentLanguage && currentLanguage !== 'undefined' && enableI18n) {
463
+ params.checkLastestI18nFile = true
464
+ }
465
+ http
466
+ .post(window.$vueApp.config.globalProperties.baseAPI + '/acs/user/i18n-languages', params)
467
+ .then((langInfo) => {
468
+ if (langInfo.isLastestI18nFile) {
469
+ // 表示是最新的国际化文件,可以直接加载使用
470
+ i18nUtil
471
+ .loadLangFile(systemCode, url, langInfo.language)
472
+ .then(() => {
473
+ resolve(langInfo.language)
474
+ })
475
+ .catch(() => {
476
+ resolve(langInfo.language ? langInfo.language : 'zh_CN')
477
+ })
478
+ } else {
479
+ // 不是最新的国际化文件或者没有加载过国际化文件,则后端会返回最新的国际化文件路径和用户当前语言
480
+ i18nUtil.setEnableI18nState(systemCode, langInfo.enableI18n ? true : false)
481
+ if (langInfo.enableI18n && langInfo.i18nFileUrls[langInfo.language]) {
482
+ const url = langInfo.i18nFileUrls[langInfo.language] + '?_t_=' + new Date().getTime()
483
+ i18nUtil.setLangFileUrl(systemCode, langInfo.language, url)
484
+ i18nUtil
485
+ .loadLangFile(systemCode, url, langInfo.language)
486
+ .then(() => {
487
+ resolve(langInfo.language)
488
+ })
489
+ .catch(() => {
490
+ resolve(langInfo.language ? langInfo.language : 'zh_CN')
491
+ })
492
+ } else {
493
+ // 如果没有开启国际化
494
+ resolve(langInfo.language)
495
+ }
496
+ }
497
+ })
498
+ }
464
499
  }
465
500
  } else {
466
501
  // 默认是中文
@@ -583,10 +618,11 @@ export function formatFileName(fileName) {
583
618
  }
584
619
  return fileName
585
620
  }
586
- export function setLangFileUrl(systemCode, lang, url) {
587
- window.sessionStorage.setItem('langFileUrl_' + systemCode + '_' + lang, url)
588
- }
589
621
 
590
- export function getLangFileUrl(systemCode, lang) {
591
- return window.sessionStorage.getItem('langFileUrl_' + systemCode + '_' + lang)
622
+ export function getCurrentSystemCode() {
623
+ if (window.$vueApp.config.globalProperties.currentSystem) {
624
+ return window.$vueApp.config.globalProperties.currentSystem
625
+ } else {
626
+ return window.$vueApp.config.globalProperties.systemCode
627
+ }
592
628
  }
@@ -0,0 +1,127 @@
1
+ import axios from 'axios'
2
+ export function setLangFileUrl(systemCode, lang, url) {
3
+ let i18nFileUrlsItem = getLocalStorageJson('i18nFileUrls')
4
+ if (!i18nFileUrlsItem) {
5
+ i18nFileUrlsItem = {}
6
+ }
7
+ i18nFileUrlsItem[systemCode + '_' + lang] = url
8
+ window.localStorage.setItem('i18nFileUrls', JSON.stringify(i18nFileUrlsItem))
9
+ }
10
+
11
+ export function getLangFileUrl(systemCode, lang) {
12
+ const i18nFileUrlsItem = getLocalStorageJson('i18nFileUrls')
13
+ if (i18nFileUrlsItem) {
14
+ return i18nFileUrlsItem[systemCode + '_' + lang]
15
+ }
16
+ return null
17
+ }
18
+
19
+ export function removeLangFileUrl(systemCode, lang) {
20
+ const i18nFileUrlsItem = getLocalStorageJson('i18nFileUrls')
21
+ if (i18nFileUrlsItem) {
22
+ delete i18nFileUrlsItem[systemCode + '_' + lang]
23
+ window.localStorage.setItem('i18nFileUrls', JSON.stringify(i18nFileUrlsItem))
24
+ }
25
+ }
26
+
27
+ export function removeAllLangFileUrl(systemCode, lang) {
28
+ window.localStorage.removeItem('i18nFileUrls')
29
+ }
30
+
31
+ export function setEnableI18nState(systemCode, enableI18n) {
32
+ let allStateObj = {}
33
+ const allState = getLocalStorageJson('enableI18nSystem')
34
+ if (allState) {
35
+ allStateObj = allState
36
+ }
37
+ allStateObj[systemCode] = enableI18n ? true : false
38
+ window.localStorage.setItem('enableI18nSystem', JSON.stringify(allStateObj))
39
+ }
40
+
41
+ export function getEnableI18nState(systemCode) {
42
+ const allState = getLocalStorageJson('enableI18nSystem')
43
+ if (allState) {
44
+ return allState[systemCode]
45
+ }
46
+ return null
47
+ }
48
+
49
+ export function removeEnableI18nState() {
50
+ window.localStorage.removeItem('enableI18nSystem')
51
+ }
52
+
53
+ export function removeEnableI18nWidthSystemCode() {
54
+ const allState = getLocalStorageJson('enableI18nSystem')
55
+ if (allState) {
56
+ delete allState[getCurrentSystemCode()]
57
+ window.localStorage.setItem('enableI18nSystem', JSON.stringify(allState))
58
+ }
59
+ }
60
+
61
+ function getLocalStorageJson(key) {
62
+ const item = window.localStorage.getItem(key)
63
+ if (item) {
64
+ return JSON.parse(item)
65
+ }
66
+ return null
67
+ }
68
+
69
+ function loadLangFile(systemCode, url, language) {
70
+ console.log('loadLangFile', systemCode, url, language)
71
+ return new Promise((resolve, reject) => {
72
+ if (window.$i18n) {
73
+ axios
74
+ .get(url, {
75
+ withCredential: false
76
+ })
77
+ .then((response) => {
78
+ const lang = getLocaleByLang(language)
79
+ if (response && response.data) {
80
+ // 获取当前的国际化消息
81
+ const currentMessages = window.$i18n.global.getLocaleMessage(lang)
82
+ // 新的国际化消息
83
+ if (currentMessages[systemCode]) {
84
+ Object.assign(currentMessages[systemCode], response.data)
85
+ } else {
86
+ currentMessages[systemCode] = response.data
87
+ }
88
+ // 设置合并后的国际化消息
89
+ window.$i18n.global.setLocaleMessage(lang, currentMessages)
90
+ }
91
+ resolve()
92
+ })
93
+ .catch((error) => {
94
+ reject(error)
95
+ })
96
+ }
97
+ resolve()
98
+ })
99
+ }
100
+ export function getLocaleByLang(lang) {
101
+ let locale = 'cn'
102
+ if (lang && lang.indexOf('_') > 0) {
103
+ const language = lang.substring(0, lang.indexOf('_'))
104
+ locale = language
105
+ }
106
+ if (locale === 'zh') {
107
+ locale = 'cn'
108
+ }
109
+ return locale
110
+ }
111
+
112
+ function clearI18nCache(lang) {
113
+ removeEnableI18nState()
114
+ removeAllLangFileUrl()
115
+ }
116
+ export default {
117
+ setLangFileUrl,
118
+ getLangFileUrl,
119
+ removeLangFileUrl,
120
+ setEnableI18nState,
121
+ getEnableI18nState,
122
+ removeEnableI18nState,
123
+ removeEnableI18nWidthSystemCode,
124
+ getLocalStorageJson,
125
+ loadLangFile,
126
+ getLocaleByLang
127
+ }