init: 初始系统模板
This commit is contained in:
26
src/hooks/useI18n.ts
Normal file
26
src/hooks/useI18n.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { computed } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import { localSet } from '@/utils/cache-local-utils';
|
||||
import { CACHE_LOCAL_I18N } from '@/constants/cache-keys-constants';
|
||||
|
||||
export default function useLocale() {
|
||||
//实例化i18n
|
||||
const i18n = useI18n();
|
||||
|
||||
// 获取当前语言设置
|
||||
const currentLocale = computed(() => {
|
||||
return i18n.locale.value;
|
||||
});
|
||||
|
||||
// 切换语言
|
||||
const changeLocale = (value: string) => {
|
||||
i18n.locale.value = value;
|
||||
localSet(CACHE_LOCAL_I18N, value);
|
||||
};
|
||||
|
||||
return {
|
||||
currentLocale,
|
||||
changeLocale,
|
||||
t: i18n.t,
|
||||
};
|
||||
}
|
||||
16
src/hooks/useLoading.ts
Normal file
16
src/hooks/useLoading.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { ref, inject, provide } from 'vue';
|
||||
|
||||
const INJECT_LOADING_KEY = Symbol('loading_store');
|
||||
|
||||
export const createLoading = (v = false) => {
|
||||
const loading = ref<boolean>(v);
|
||||
const change = (bool: boolean) => {
|
||||
loading.value = bool;
|
||||
};
|
||||
provide(INJECT_LOADING_KEY, loading);
|
||||
return [loading, change];
|
||||
};
|
||||
|
||||
export const useLoading = () => {
|
||||
return inject(INJECT_LOADING_KEY);
|
||||
};
|
||||
58
src/hooks/useTheme.ts
Normal file
58
src/hooks/useTheme.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { onBeforeMount } from 'vue';
|
||||
import { ConfigProvider } from 'ant-design-vue/lib';
|
||||
import { CACHE_LOCAL_PRIMARY_COLOR } from '@/constants/cache-keys-constants';
|
||||
import { localGet, localSet } from '@/utils/cache-local-utils';
|
||||
|
||||
/**
|
||||
* 初始主题色
|
||||
*/
|
||||
export const usePrimaryColor = () => {
|
||||
onBeforeMount(() => {
|
||||
changePrimaryColor(getLocalColor());
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* 改变主题色
|
||||
* @param color 颜色
|
||||
*/
|
||||
export function changePrimaryColor(color?: string) {
|
||||
if (!color) {
|
||||
color = getRandomColor();
|
||||
}
|
||||
ConfigProvider.config({
|
||||
theme: {
|
||||
primaryColor: color,
|
||||
},
|
||||
});
|
||||
localSet(CACHE_LOCAL_PRIMARY_COLOR, color);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取主题色-本地缓存
|
||||
* @returns 颜色
|
||||
*/
|
||||
export function getLocalColor() {
|
||||
return localGet(CACHE_LOCAL_PRIMARY_COLOR) || '#1890ff';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取随机颜色范围
|
||||
* @returns 颜色
|
||||
*/
|
||||
function getRandomColor(): string {
|
||||
const colors: string[] = [
|
||||
'#f5222d',
|
||||
'#fa541c',
|
||||
'#fa8c16',
|
||||
'#a0d911',
|
||||
'#13c2c2',
|
||||
'#1890ff',
|
||||
'#722ed1',
|
||||
'#eb2f96',
|
||||
'#faad14',
|
||||
'#52c41a',
|
||||
];
|
||||
const i = Math.floor(Math.random() * 10);
|
||||
return colors[i];
|
||||
}
|
||||
Reference in New Issue
Block a user