Skip to content

Commit

Permalink
feature: 封装单独的获取cookie方法 (#1930)
Browse files Browse the repository at this point in the history
  • Loading branch information
lhzzforever authored Apr 21, 2023
1 parent 9b088e0 commit 426d897
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
6 changes: 3 additions & 3 deletions frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@
</div>
</template>
<script>
import Cookie from 'js-cookie';
// import Cookie from 'js-cookie';
import HeaderNav from '@/components/header-nav/index.vue';
import theHeader from '@/components/header/index.vue';
import theNav from '@/components/nav/index.vue';
import IamGuide from '@/components/iam-guide/index.vue';
import { existValue } from '@/common/util';
import { existValue, getCookie } from '@/common/util';
import { bus } from '@/common/bus';
import { mapGetters } from 'vuex';
import { afterEach } from '@/router';
Expand Down Expand Up @@ -123,7 +123,7 @@
},
created () {
const platform = window.navigator.platform.toLowerCase();
window.CUR_LANGUAGE = Cookie.get('blueking_language') || 'zh-cn';
window.CUR_LANGUAGE = getCookie('blueking_language') || 'zh-cn';
this.$i18n.locale = window.CUR_LANGUAGE;
if (platform.indexOf('win') === 0) {
this.systemCls = 'win';
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -581,3 +581,13 @@ export function renderLabelWidth (payload) {
};
return typeMap[payload]();
}

// 获取cookie
export function getCookie (name) {
const data = document.cookie.split(';');
const params = {};
for (let i = 0; i < data.length; i++) {
params[data[i].split('=')[0].replace(/\s/, '')] = data[i].split('=')[1];
}
return params[name];
}
7 changes: 4 additions & 3 deletions frontend/src/components/header-nav/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
import { il8n, language } from '@/language';
import { bus } from '@/common/bus';
import { buildURLParams } from '@/common/url';
import { getCookie } from '@/common/util';
import SystemLog from '../system-log';
import { getRouterDiff, getNavRouterDiff } from '@/common/router-handle';
import Cookie from 'js-cookie';
Expand Down Expand Up @@ -562,9 +563,9 @@
'zh-cn': lang.zhCN,
en: lang.enUS
};
locale.use(magicBoxLanguageMap[targetLocale]);
window.CUR_LANGUAGE = targetLocale;
this.$i18n.locale = targetLocale;
locale.use(magicBoxLanguageMap[getCookie('blueking_language')]);
window.CUR_LANGUAGE = getCookie('blueking_language');
this.$i18n.locale = getCookie('blueking_language');
window.location.reload();
},
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/language/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import en from './lang/en';
import cn from './lang/zh';
import magicbox from 'bk-magic-vue';
import Cookie from 'js-cookie';
import { getCookie } from '@/common/util';

const { lang } = magicbox;
const messages = {
Expand All @@ -40,7 +40,7 @@ const messages = {
}
};

const language = Cookie.get('blueking_language') || 'zh-cn';
const language = getCookie('blueking_language') || 'zh-cn';

// 检测漏掉的翻译
// const cnLan = cn.language;
Expand Down

0 comments on commit 426d897

Please sign in to comment.