From da01fa58489bd13385c7926d1bc8363e20c04a2f Mon Sep 17 00:00:00 2001 From: wangxiang <1827945911@qq.com> Date: Sat, 16 Apr 2022 15:58:07 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=9F=20=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kicc-ui/src/api/sys/model/userModel.ts | 6 ++-- kicc-ui/src/hooks/web/useI18n.ts | 41 +++++++++++--------------- kicc-ui/src/locales/helper.ts | 4 +-- kicc-ui/src/locales/setupI18n.ts | 2 +- kicc-ui/src/locales/useLocale.ts | 17 +++-------- kicc-ui/src/store/modules/user.ts | 16 +++++----- kicc-ui/src/utils/cache/persistent.ts | 4 +-- kicc-ui/types/store.d.ts | 4 +-- 8 files changed, 38 insertions(+), 56 deletions(-) diff --git a/kicc-ui/src/api/sys/model/userModel.ts b/kicc-ui/src/api/sys/model/userModel.ts index 571a76c1..a98cc8ac 100644 --- a/kicc-ui/src/api/sys/model/userModel.ts +++ b/kicc-ui/src/api/sys/model/userModel.ts @@ -12,7 +12,7 @@ export interface LoginParams { export interface UserInfo { accountNonExpired: boolean; accountNonLocked: boolean; - authorities: Recordable[]; + authorities: string[]; credentialsNonExpired: boolean; deptId: string; enabled: boolean; @@ -58,9 +58,9 @@ export interface GetUserInfoModel { // 邮箱 email: string; // 菜单按钮权限 - permissions: Recordable[]; + permissions: string[]; // 角色ID权限 - roleIds: Recordable[]; + roleIds: string[]; // 手机号 phone: string; // 备注信息 diff --git a/kicc-ui/src/hooks/web/useI18n.ts b/kicc-ui/src/hooks/web/useI18n.ts index d6c0b10e..e8bcbac9 100644 --- a/kicc-ui/src/hooks/web/useI18n.ts +++ b/kicc-ui/src/hooks/web/useI18n.ts @@ -1,5 +1,11 @@ -import { i18n } from '/@/locales/setupI18n'; +/** + * @program: kicc-ui + * @description: 国际化工具 + * @author: entfrm开发团队-王翔 + * @create: 2022/4/10 + */ +import { i18n } from '/@/locales/setupI18n'; type I18nGlobalTranslation = { (key: string): string; (key: string, locale: string): string; @@ -8,41 +14,28 @@ type I18nGlobalTranslation = { (key: string, list: unknown[]): string; (key: string, named: Record): string; }; - type I18nTranslationRestParameters = [string, any]; function getKey(namespace: string | undefined, key: string) { - if (!namespace) { - return key; - } - if (key.startsWith(namespace)) { - return key; - } + if (!namespace) return key; + if (key.startsWith(namespace)) return key; return `${namespace}.${key}`; } -export function useI18n(namespace?: string): { - t: I18nGlobalTranslation; -} { +/** 国际化转换,提供i18n全部功能 */ +export function useI18n(namespace?: string): { t: I18nGlobalTranslation; } { const normalFn = { - t: (key: string) => { - return getKey(namespace, key); - }, + t: (key: string) => getKey(namespace, key) }; - - if (!i18n) { - return normalFn; - } - + if (!i18n) return normalFn; const { t, ...methods } = i18n.global; - const tFn: I18nGlobalTranslation = (key: string, ...arg: any[]) => { if (!key) return ''; if (!key.includes('.') && !namespace) return key; return t(getKey(namespace, key), ...(arg as I18nTranslationRestParameters)); }; - return { - ...methods, - t: tFn, - }; + return { ...methods, t: tFn }; } + +/** 国际化转换,只提供i18n简单转换功能 */ +export const t = (key: string): string => useI18n().t(key); diff --git a/kicc-ui/src/locales/helper.ts b/kicc-ui/src/locales/helper.ts index 0976027e..d6ad5645 100644 --- a/kicc-ui/src/locales/helper.ts +++ b/kicc-ui/src/locales/helper.ts @@ -34,9 +34,7 @@ export function genMessage(langs: Record>, prefix = if (objKey) { set(obj, moduleName, obj[moduleName] || {}); set(obj[moduleName], objKey, langFileModule); - } else { - set(obj, moduleName, langFileModule || {}); - } + } else set(obj, moduleName, langFileModule || {}); } }); return obj; diff --git a/kicc-ui/src/locales/setupI18n.ts b/kicc-ui/src/locales/setupI18n.ts index f63a8ed4..c5d0b682 100644 --- a/kicc-ui/src/locales/setupI18n.ts +++ b/kicc-ui/src/locales/setupI18n.ts @@ -42,7 +42,7 @@ async function createI18nOptions(): Promise { }; } -// 使用全局设置 i18n 实例 +/** 使用全局设置 i18n 实例 */ export async function setupI18n(app: App) { const options = await createI18nOptions(); i18n = createI18n(options) as I18n; diff --git a/kicc-ui/src/locales/useLocale.ts b/kicc-ui/src/locales/useLocale.ts index 99603dee..ccdf407b 100644 --- a/kicc-ui/src/locales/useLocale.ts +++ b/kicc-ui/src/locales/useLocale.ts @@ -20,12 +20,9 @@ interface LangModule { function setI18nLanguage(locale: LocaleType) { const localeStore = useLocaleStoreWithOut(); - if (i18n.mode === 'legacy') { i18n.global.locale = locale; - } else { - (i18n.global.locale as any).value = locale; - } + } else (i18n.global.locale as any).value = locale; localeStore.setLocaleInfo({ locale }); setHtmlPageLang(locale); } @@ -34,19 +31,13 @@ export function useLocale() { const localeStore = useLocaleStoreWithOut(); const getLocale = computed(() => localeStore.getLocale); const getShowLocalePicker = computed(() => localeStore.getShowPicker); + const getAntdLocale = computed((): any => i18n.global.getLocaleMessage(unref(getLocale))?.antdLocale ?? {}); - const getAntdLocale = computed((): any => { - return i18n.global.getLocaleMessage(unref(getLocale))?.antdLocale ?? {}; - }); - - // 切换语言会改变 useI18n 的语言环境 - // 并提交配置修改 + // 切换语言会改变useI18n的语言环境 async function changeLocale(locale: LocaleType) { const globalI18n = i18n.global; const currentLocale = unref(globalI18n.locale); - if (currentLocale === locale) { - return locale; - } + if (currentLocale === locale) return locale; if (loadLocalePool.includes(locale)) { setI18nLanguage(locale); return locale; diff --git a/kicc-ui/src/store/modules/user.ts b/kicc-ui/src/store/modules/user.ts index 0a379987..df50ce07 100644 --- a/kicc-ui/src/store/modules/user.ts +++ b/kicc-ui/src/store/modules/user.ts @@ -22,8 +22,8 @@ import {RouteRecordRaw} from 'vue-router'; interface UserState { userInfo: Nullable; sessionTimeout: boolean; - roleIds: Recordable[]; - permissions: Recordable[]; + roleIds: string[]; + permissions: string[]; access_token?: string; refresh_token?: string; } @@ -36,9 +36,9 @@ export const useUserStore = defineStore({ // 登录是否过期 sessionTimeout: false, // 角色ID用于权限校验 - roleIds: getAuthCache(ROLE_IDS_KEY), + roleIds: getAuthCache(ROLE_IDS_KEY), // 按钮权限标识用于权限校验 - permissions: getAuthCache(PERMISSIONS_KEY), + permissions: getAuthCache(PERMISSIONS_KEY), // 访问令牌 access_token: getAuthCache(ACCESS_TOKEN_KEY), // 刷新令牌 @@ -57,19 +57,19 @@ export const useUserStore = defineStore({ getSessionTimeout(): boolean { return !!this.sessionTimeout; }, - getRoleIds(): Recordable[] { + getRoleIds(): string[] { return this.roleIds; }, - getPermissions(): Recordable[] { + getPermissions(): string[] { return this.permissions; }, }, actions: { - setRoleIds(roleIds: Recordable[]) { + setRoleIds(roleIds: string[]) { this.roleIds = roleIds; setAuthCache(ROLE_IDS_KEY, roleIds); }, - setPermissions(permissions: Recordable[]) { + setPermissions(permissions: string[]) { this.permissions = permissions; }, setAccessToken(accessToken: string) { diff --git a/kicc-ui/src/utils/cache/persistent.ts b/kicc-ui/src/utils/cache/persistent.ts index cd0ae90c..610c3ce6 100644 --- a/kicc-ui/src/utils/cache/persistent.ts +++ b/kicc-ui/src/utils/cache/persistent.ts @@ -28,8 +28,8 @@ import { pick, omit } from 'lodash-es'; interface BasicStore { [ACCESS_TOKEN_KEY]: string | number | null | undefined; [REFRESH_TOKEN_KEY]: string | number | null | undefined; - [ROLE_IDS_KEY]: Recordable[]; - [PERMISSIONS_KEY]: Recordable[]; + [ROLE_IDS_KEY]: string[]; + [PERMISSIONS_KEY]: string[]; [USER_INFO_KEY]: UserInfo; [LOCK_INFO_KEY]: LockInfo; [PROJ_CFG_KEY]: ProjectConfig; diff --git a/kicc-ui/types/store.d.ts b/kicc-ui/types/store.d.ts index 63bf7058..780df5b0 100644 --- a/kicc-ui/types/store.d.ts +++ b/kicc-ui/types/store.d.ts @@ -31,9 +31,9 @@ export interface UserInfo { // 邮箱 email: string; // 菜单按钮权限 - permissions: Recordable[]; + permissions: string[]; // 角色ID权限 - roleIds: Recordable[]; + roleIds: string[]; // 手机号 phone: string; // 备注信息