Browse Source

🎟 优化

master
wangxiang 3 years ago
parent
commit
da01fa5848
  1. 6
      kicc-ui/src/api/sys/model/userModel.ts
  2. 41
      kicc-ui/src/hooks/web/useI18n.ts
  3. 4
      kicc-ui/src/locales/helper.ts
  4. 2
      kicc-ui/src/locales/setupI18n.ts
  5. 17
      kicc-ui/src/locales/useLocale.ts
  6. 16
      kicc-ui/src/store/modules/user.ts
  7. 4
      kicc-ui/src/utils/cache/persistent.ts
  8. 4
      kicc-ui/types/store.d.ts

6
kicc-ui/src/api/sys/model/userModel.ts

@ -12,7 +12,7 @@ export interface LoginParams {
export interface UserInfo { export interface UserInfo {
accountNonExpired: boolean; accountNonExpired: boolean;
accountNonLocked: boolean; accountNonLocked: boolean;
authorities: Recordable[]; authorities: string[];
credentialsNonExpired: boolean; credentialsNonExpired: boolean;
deptId: string; deptId: string;
enabled: boolean; enabled: boolean;
@ -58,9 +58,9 @@ export interface GetUserInfoModel {
// 邮箱 // 邮箱
email: string; email: string;
// 菜单按钮权限 // 菜单按钮权限
permissions: Recordable[]; permissions: string[];
// 角色ID权限 // 角色ID权限
roleIds: Recordable[]; roleIds: string[];
// 手机号 // 手机号
phone: string; phone: string;
// 备注信息 // 备注信息

41
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 = { type I18nGlobalTranslation = {
(key: string): string; (key: string): string;
(key: string, locale: string): string; (key: string, locale: string): string;
@ -8,41 +14,28 @@ type I18nGlobalTranslation = {
(key: string, list: unknown[]): string; (key: string, list: unknown[]): string;
(key: string, named: Record<string, unknown>): string; (key: string, named: Record<string, unknown>): string;
}; };
type I18nTranslationRestParameters = [string, any]; type I18nTranslationRestParameters = [string, any];
function getKey(namespace: string | undefined, key: string) { function getKey(namespace: string | undefined, key: string) {
if (!namespace) { if (!namespace) return key;
return key; if (key.startsWith(namespace)) return key;
}
if (key.startsWith(namespace)) {
return key;
}
return `${namespace}.${key}`; return `${namespace}.${key}`;
} }
export function useI18n(namespace?: string): { /** 国际化转换,提供i18n全部功能 */
t: I18nGlobalTranslation; export function useI18n(namespace?: string): { t: I18nGlobalTranslation; } {
} {
const normalFn = { const normalFn = {
t: (key: string) => { t: (key: string) => getKey(namespace, key)
return getKey(namespace, key);
},
}; };
if (!i18n) return normalFn;
if (!i18n) {
return normalFn;
}
const { t, ...methods } = i18n.global; const { t, ...methods } = i18n.global;
const tFn: I18nGlobalTranslation = (key: string, ...arg: any[]) => { const tFn: I18nGlobalTranslation = (key: string, ...arg: any[]) => {
if (!key) return ''; if (!key) return '';
if (!key.includes('.') && !namespace) return key; if (!key.includes('.') && !namespace) return key;
return t(getKey(namespace, key), ...(arg as I18nTranslationRestParameters)); return t(getKey(namespace, key), ...(arg as I18nTranslationRestParameters));
}; };
return { return { ...methods, t: tFn };
...methods,
t: tFn,
};
} }
/** 国际化转换,只提供i18n简单转换功能 */
export const t = (key: string): string => useI18n().t(key);

4
kicc-ui/src/locales/helper.ts

@ -34,9 +34,7 @@ export function genMessage(langs: Record<string, Record<string, any>>, prefix =
if (objKey) { if (objKey) {
set(obj, moduleName, obj[moduleName] || {}); set(obj, moduleName, obj[moduleName] || {});
set(obj[moduleName], objKey, langFileModule); set(obj[moduleName], objKey, langFileModule);
} else { } else set(obj, moduleName, langFileModule || {});
set(obj, moduleName, langFileModule || {});
}
} }
}); });
return obj; return obj;

2
kicc-ui/src/locales/setupI18n.ts

@ -42,7 +42,7 @@ async function createI18nOptions(): Promise<I18nOptions> {
}; };
} }
// 使用全局设置 i18n 实例 /** 使用全局设置 i18n 实例 */
export async function setupI18n(app: App) { export async function setupI18n(app: App) {
const options = await createI18nOptions(); const options = await createI18nOptions();
i18n = createI18n(options) as I18n; i18n = createI18n(options) as I18n;

17
kicc-ui/src/locales/useLocale.ts

@ -20,12 +20,9 @@ interface LangModule {
function setI18nLanguage(locale: LocaleType) { function setI18nLanguage(locale: LocaleType) {
const localeStore = useLocaleStoreWithOut(); const localeStore = useLocaleStoreWithOut();
if (i18n.mode === 'legacy') { if (i18n.mode === 'legacy') {
i18n.global.locale = locale; i18n.global.locale = locale;
} else { } else (i18n.global.locale as any).value = locale;
(i18n.global.locale as any).value = locale;
}
localeStore.setLocaleInfo({ locale }); localeStore.setLocaleInfo({ locale });
setHtmlPageLang(locale); setHtmlPageLang(locale);
} }
@ -34,19 +31,13 @@ export function useLocale() {
const localeStore = useLocaleStoreWithOut(); const localeStore = useLocaleStoreWithOut();
const getLocale = computed(() => localeStore.getLocale); const getLocale = computed(() => localeStore.getLocale);
const getShowLocalePicker = computed(() => localeStore.getShowPicker); const getShowLocalePicker = computed(() => localeStore.getShowPicker);
const getAntdLocale = computed((): any => i18n.global.getLocaleMessage(unref(getLocale))?.antdLocale ?? {});
const getAntdLocale = computed((): any => { // 切换语言会改变useI18n的语言环境
return i18n.global.getLocaleMessage(unref(getLocale))?.antdLocale ?? {};
});
// 切换语言会改变 useI18n 的语言环境
// 并提交配置修改
async function changeLocale(locale: LocaleType) { async function changeLocale(locale: LocaleType) {
const globalI18n = i18n.global; const globalI18n = i18n.global;
const currentLocale = unref(globalI18n.locale); const currentLocale = unref(globalI18n.locale);
if (currentLocale === locale) { if (currentLocale === locale) return locale;
return locale;
}
if (loadLocalePool.includes(locale)) { if (loadLocalePool.includes(locale)) {
setI18nLanguage(locale); setI18nLanguage(locale);
return locale; return locale;

16
kicc-ui/src/store/modules/user.ts

@ -22,8 +22,8 @@ import {RouteRecordRaw} from 'vue-router';
interface UserState { interface UserState {
userInfo: Nullable<UserInfo>; userInfo: Nullable<UserInfo>;
sessionTimeout: boolean; sessionTimeout: boolean;
roleIds: Recordable[]; roleIds: string[];
permissions: Recordable[]; permissions: string[];
access_token?: string; access_token?: string;
refresh_token?: string; refresh_token?: string;
} }
@ -36,9 +36,9 @@ export const useUserStore = defineStore({
// 登录是否过期 // 登录是否过期
sessionTimeout: false, sessionTimeout: false,
// 角色ID用于权限校验 // 角色ID用于权限校验
roleIds: getAuthCache<Recordable[]>(ROLE_IDS_KEY), roleIds: getAuthCache<string[]>(ROLE_IDS_KEY),
// 按钮权限标识用于权限校验 // 按钮权限标识用于权限校验
permissions: getAuthCache<Recordable[]>(PERMISSIONS_KEY), permissions: getAuthCache<string[]>(PERMISSIONS_KEY),
// 访问令牌 // 访问令牌
access_token: getAuthCache<string>(ACCESS_TOKEN_KEY), access_token: getAuthCache<string>(ACCESS_TOKEN_KEY),
// 刷新令牌 // 刷新令牌
@ -57,19 +57,19 @@ export const useUserStore = defineStore({
getSessionTimeout(): boolean { getSessionTimeout(): boolean {
return !!this.sessionTimeout; return !!this.sessionTimeout;
}, },
getRoleIds(): Recordable[] { getRoleIds(): string[] {
return this.roleIds; return this.roleIds;
}, },
getPermissions(): Recordable[] { getPermissions(): string[] {
return this.permissions; return this.permissions;
}, },
}, },
actions: { actions: {
setRoleIds(roleIds: Recordable[]) { setRoleIds(roleIds: string[]) {
this.roleIds = roleIds; this.roleIds = roleIds;
setAuthCache(ROLE_IDS_KEY, roleIds); setAuthCache(ROLE_IDS_KEY, roleIds);
}, },
setPermissions(permissions: Recordable[]) { setPermissions(permissions: string[]) {
this.permissions = permissions; this.permissions = permissions;
}, },
setAccessToken(accessToken: string) { setAccessToken(accessToken: string) {

4
kicc-ui/src/utils/cache/persistent.ts vendored

@ -28,8 +28,8 @@ import { pick, omit } from 'lodash-es';
interface BasicStore { interface BasicStore {
[ACCESS_TOKEN_KEY]: string | number | null | undefined; [ACCESS_TOKEN_KEY]: string | number | null | undefined;
[REFRESH_TOKEN_KEY]: string | number | null | undefined; [REFRESH_TOKEN_KEY]: string | number | null | undefined;
[ROLE_IDS_KEY]: Recordable[]; [ROLE_IDS_KEY]: string[];
[PERMISSIONS_KEY]: Recordable[]; [PERMISSIONS_KEY]: string[];
[USER_INFO_KEY]: UserInfo; [USER_INFO_KEY]: UserInfo;
[LOCK_INFO_KEY]: LockInfo; [LOCK_INFO_KEY]: LockInfo;
[PROJ_CFG_KEY]: ProjectConfig; [PROJ_CFG_KEY]: ProjectConfig;

4
kicc-ui/types/store.d.ts vendored

@ -31,9 +31,9 @@ export interface UserInfo {
// 邮箱 // 邮箱
email: string; email: string;
// 菜单按钮权限 // 菜单按钮权限
permissions: Recordable[]; permissions: string[];
// 角色ID权限 // 角色ID权限
roleIds: Recordable[]; roleIds: string[];
// 手机号 // 手机号
phone: string; phone: string;
// 备注信息 // 备注信息

Loading…
Cancel
Save