feat: 完成主题切换,语言切换的本地存储

This commit is contained in:
MTrun
2021-12-17 11:55:42 +08:00
parent 7f67f482da
commit 557ddd6ee5
27 changed files with 446 additions and 102 deletions
View File
+61 -9
View File
@@ -1,5 +1,5 @@
import { h } from 'vue';
import { NIcon } from 'naive-ui';
import { h } from 'vue'
import { NIcon } from 'naive-ui'
/**
* * 生成一个用不重复的ID
@@ -7,24 +7,76 @@ import { NIcon } from 'naive-ui';
*/
export function getUUID(randomLength: number) {
return Number(
Math.random()
.toString()
.substr(2, randomLength) + Date.now()
).toString(36);
Math.random().toString().substr(2, randomLength) + Date.now()
).toString(36)
}
/**
* * render 图标
*/
export const renderIcon = (icon: typeof NIcon) => {
return () => h(NIcon, null, { default: () => h(icon) });
return () => h(NIcon, null, { default: () => h(icon) })
}
/**
* * 处理 vite 中无法使用 require 的问题
* @param name
* @returns
* @param name
* @returns url
*/
export const requireUrl = (path: string, name: string) => {
return new URL(`${path}/${name}`, import.meta.url).href
}
/**
* * 存储本地会话数据
* @param k 键名
* @param v 键值
* @returns RemovableRef
*/
export const setLocalStorage = <T>(k: string, v: T) => {
try {
window.localStorage.setItem(k, JSON.stringify(v))
} catch (error) {
return false
}
}
/**
* * 获取本地会话数据
* @returns any
*/
export const getLocalStorage: (k: string) => any = (k: string) => {
const item = window.localStorage.getItem(k)
try {
return item ? JSON.parse(item) : item
} catch (err) {
return item
}
}
/**
* * 存储临时会话数据
* @param k 键名
* @param v 键值
* @returns RemovableRef
*/
export const setSessionStorage = <T>(k: string, v: T) => {
try {
window.sessionStorage.setItem(k, JSON.stringify(v))
} catch (error) {
return false
}
}
/**
* * 获取临时会话数据
* @returns any
*/
export const getSessionStorage: (k: string) => any = (k: string) => {
const item = window.sessionStorage.getItem(k)
try {
return item ? JSON.parse(item) : item
} catch (err) {
return item
}
}
+16 -10
View File
@@ -1,25 +1,31 @@
import { ResultEnum } from "@/enums/httpEnum"
import { ErrorPageNameMap } from "@/enums/pageEnum"
import router from '@/router';
import { ResultEnum } from '@/enums/httpEnum'
import { ErrorPageNameMap } from '@/enums/pageEnum'
import router from '@/router'
/**
* * 错误页重定向
* @param icon
* @returns
* @param icon
* @returns
*/
export const redirectErrorPage = (code: ResultEnum) => {
if(!code) return false
if (!code) return false
const pageName = ErrorPageNameMap.get(code)
if(!pageName) return false
if (!pageName) return false
routerTurnByName(pageName)
}
/**
* * 根据名字跳转路由
* @param pageName
* @param pageName
*/
export const routerTurnByName = (pageName: string) => {
export const routerTurnByName = (pageName: string, isReplace?: boolean) => {
if (isReplace) {
router.replace({
name: pageName
})
return
}
router.push({
name: pageName
})
}
}
+3 -4
View File
@@ -1,12 +1,11 @@
import { useDesignStore } from '@/store/modules/designStore/designStore'
import { theme as themeEnum } from '@/settings/designSetting'
export const setHtmlTheme = (themeName?: string) => {
const e = window.document.documentElement
if (themeName) {
e.setAttribute("data-theme", themeName);
e.setAttribute('data-theme', themeName)
return
}
const designStore = useDesignStore()
e.setAttribute("data-theme", designStore.getDarkTheme ? themeEnum.darkThemeName : themeEnum.lightThemeName);
}
e.setAttribute('data-theme', designStore.themeName)
}