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. 15
      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 { @@ -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 { @@ -58,9 +58,9 @@ export interface GetUserInfoModel {
// 邮箱
email: string;
// 菜单按钮权限
permissions: Recordable[];
permissions: string[];
// 角色ID权限
roleIds: Recordable[];
roleIds: string[];
// 手机号
phone: string;
// 备注信息

41
kicc-ui/src/hooks/web/useI18n.ts

@ -1,5 +1,11 @@ @@ -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 = { @@ -8,41 +14,28 @@ type I18nGlobalTranslation = {
(key: string, list: unknown[]): string;
(key: string, named: Record<string, unknown>): 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);

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

@ -34,9 +34,7 @@ export function genMessage(langs: Record<string, Record<string, any>>, prefix = @@ -34,9 +34,7 @@ export function genMessage(langs: Record<string, Record<string, any>>, 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;

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

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

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

@ -20,12 +20,9 @@ interface LangModule { @@ -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() { @@ -34,19 +31,13 @@ export function useLocale() {
const localeStore = useLocaleStoreWithOut();
const getLocale = computed(() => localeStore.getLocale);
const getShowLocalePicker = computed(() => localeStore.getShowPicker);
const getAntdLocale = computed((): any => {
return i18n.global.getLocaleMessage(unref(getLocale))?.antdLocale ?? {};
});
const getAntdLocale = computed((): any => i18n.global.getLocaleMessage(unref(getLocale))?.antdLocale ?? {});
// 切换语言会改变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;

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

@ -22,8 +22,8 @@ import {RouteRecordRaw} from 'vue-router'; @@ -22,8 +22,8 @@ import {RouteRecordRaw} from 'vue-router';
interface UserState {
userInfo: Nullable<UserInfo>;
sessionTimeout: boolean;
roleIds: Recordable[];
permissions: Recordable[];
roleIds: string[];
permissions: string[];
access_token?: string;
refresh_token?: string;
}
@ -36,9 +36,9 @@ export const useUserStore = defineStore({ @@ -36,9 +36,9 @@ export const useUserStore = defineStore({
// 登录是否过期
sessionTimeout: false,
// 角色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),
// 刷新令牌
@ -57,19 +57,19 @@ export const useUserStore = defineStore({ @@ -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) {

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

@ -28,8 +28,8 @@ import { pick, omit } from 'lodash-es'; @@ -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;

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

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

Loading…
Cancel
Save