Skip to content

Commit

Permalink
feat: 新增useStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
kailong321200875 committed Aug 5, 2023
1 parent 57a5fa7 commit dfea91c
Show file tree
Hide file tree
Showing 17 changed files with 86 additions and 68 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"i": "pnpm install",
"dev": "vite --mode base",
"ts:check": "vue-tsc --noEmit",
"ts:check": "vue-tsc --noEmit --skipLibCheck",
"build:pro": "vite build --mode pro",
"build:gitee": "vite build --mode gitee",
"build:dev": "vite build --mode dev",
Expand Down
8 changes: 4 additions & 4 deletions src/App.vue
Expand Up @@ -4,7 +4,7 @@ import { useAppStore } from '@/store/modules/app'
import { ConfigGlobal } from '@/components/ConfigGlobal'
import { isDark } from '@/utils/is'
import { useDesign } from '@/hooks/web/useDesign'
import { useCache } from '@/hooks/web/useCache'
import { useStorage } from '@/hooks/web/useStorage'
const { getPrefixCls } = useDesign()
Expand All @@ -16,12 +16,12 @@ const currentSize = computed(() => appStore.getCurrentSize)
const greyMode = computed(() => appStore.getGreyMode)
const { wsCache } = useCache()
const { getStorage } = useStorage()
// 根据浏览器当前主题设置系统主题色
const setDefaultTheme = () => {
if (wsCache.get('isDark') !== null) {
appStore.setIsDark(wsCache.get('isDark'))
if (getStorage('isDark') !== null) {
appStore.setIsDark(getStorage('isDark'))
return
}
const isDarkTheme = isDark()
Expand Down
11 changes: 6 additions & 5 deletions src/components/Setting/src/Setting.vue
Expand Up @@ -10,10 +10,12 @@ import { trim, setCssVar } from '@/utils'
import ColorRadioPicker from './components/ColorRadioPicker.vue'
import InterfaceDisplay from './components/InterfaceDisplay.vue'
import LayoutRadioPicker from './components/LayoutRadioPicker.vue'
import { useCache } from '@/hooks/web/useCache'
import { useStorage } from '@/hooks/web/useStorage'
import { useClipboard } from '@vueuse/core'
import { useDesign } from '@/hooks/web/useDesign'
const { removeStorage } = useStorage()
const { getPrefixCls } = useDesign()
const prefixCls = getPrefixCls('setting')
Expand Down Expand Up @@ -186,10 +188,9 @@ const copyConfig = async () => {
// 清空缓存
const clear = () => {
const { wsCache } = useCache()
wsCache.delete('layout')
wsCache.delete('theme')
wsCache.delete('isDark')
removeStorage('layout')
removeStorage('theme')
removeStorage('isDark')
window.location.reload()
}
</script>
Expand Down
6 changes: 3 additions & 3 deletions src/components/UserInfo/src/UserInfo.vue
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { ElDropdown, ElDropdownMenu, ElDropdownItem, ElMessageBox } from 'element-plus'
import { useI18n } from '@/hooks/web/useI18n'
import { useCache } from '@/hooks/web/useCache'
import { useStorage } from '@/hooks/web/useStorage'
import { resetRouter } from '@/router'
import { useRouter } from 'vue-router'
import { loginOutApi } from '@/api/login'
Expand All @@ -24,7 +24,7 @@ const prefixCls = getPrefixCls('user-info')
const { t } = useI18n()
const { wsCache } = useCache()
const { clear } = useStorage()
const { replace } = useRouter()
Expand All @@ -37,7 +37,7 @@ const loginOut = () => {
.then(async () => {
const res = await loginOutApi().catch(() => {})
if (res) {
wsCache.clear()
clear()
tagsViewStore.delAllViews()
resetRouter() // 重置静态路由表
replace('/login')
Expand Down
6 changes: 3 additions & 3 deletions src/components/UserInfo/src/components/LockPage.vue
Expand Up @@ -3,7 +3,7 @@ import { ref } from 'vue'
import { ElInput, ElButton } from 'element-plus'
import { resetRouter } from '@/router'
import { useRouter } from 'vue-router'
import { useCache } from '@/hooks/web/useCache'
import { useStorage } from '@/hooks/web/useStorage'
import { useLockStore } from '@/store/modules/lock'
import { useI18n } from '@/hooks/web/useI18n'
import { useNow } from '@/hooks/web/useNow'
Expand All @@ -14,7 +14,7 @@ import { useTagsViewStore } from '@/store/modules/tagsView'
const tagsViewStore = useTagsViewStore()
const { wsCache } = useCache()
const { clear } = useStorage()
const { replace } = useRouter()
Expand Down Expand Up @@ -51,7 +51,7 @@ async function unLock() {
async function goLogin() {
const res = await loginOutApi().catch(() => {})
if (res) {
wsCache.clear()
clear()
tagsViewStore.delAllViews()
resetRouter() // 重置静态路由表
lockStore.resetLockInfo()
Expand Down
6 changes: 3 additions & 3 deletions src/directives/permission/hasPermi.ts
@@ -1,18 +1,18 @@
import type { App, Directive, DirectiveBinding } from 'vue'
import { useI18n } from '@/hooks/web/useI18n'
import { useCache } from '@/hooks/web/useCache'
import { useStorage } from '@/hooks/web/useStorage'
import { intersection } from 'lodash-es'
import { isArray } from '@/utils/is'
import { useAppStoreWithOut } from '@/store/modules/app'

const { t } = useI18n()
const { wsCache } = useCache()
const { getStorage } = useStorage()
const appStore = useAppStoreWithOut()

// 全部权限
const all_permission = ['*.*.*']
const hasPermission = (value: string | string[]): boolean => {
const permissions = wsCache.get(appStore.getUserInfo).permissions as string[]
const permissions = getStorage(appStore.getUserInfo).permissions as string[]
if (!value) {
throw new Error(t('permission.hasPermission'))
}
Expand Down
File renamed without changes.
17 changes: 0 additions & 17 deletions src/hooks/web/useCache.ts

This file was deleted.

31 changes: 31 additions & 0 deletions src/hooks/web/useStorage.ts
@@ -0,0 +1,31 @@
import { isArray, isObject } from '@/utils/is'

export const useStorage = (type: 'sessionStorage' | 'localStorage' = 'sessionStorage') => {
const setStorage = (key: string, value: any) => {
window[type].setItem(key, isArray(value) || isObject(value) ? JSON.stringify(value) : value)
}

const getStorage = (key: string) => {
const value = window[type].getItem(key)
try {
return JSON.parse(value || '')
} catch (error) {
return value
}
}

const removeStorage = (key: string) => {
window[type].removeItem(key)
}

const clear = () => {
window[type].clear()
}

return {
setStorage,
getStorage,
removeStorage,
clear
}
}
10 changes: 5 additions & 5 deletions src/permission.ts
@@ -1,6 +1,6 @@
import router from './router'
import { useAppStoreWithOut } from '@/store/modules/app'
import { useCache } from '@/hooks/web/useCache'
import { useStorage } from '@/hooks/web/useStorage'
import type { RouteRecordRaw } from 'vue-router'
import { useTitle } from '@/hooks/web/useTitle'
import { useNProgress } from '@/hooks/web/useNProgress'
Expand All @@ -15,7 +15,7 @@ const appStore = useAppStoreWithOut()

const dictStore = useDictStoreWithOut()

const { wsCache } = useCache()
const { getStorage } = useStorage()

const { start, done } = useNProgress()

Expand All @@ -26,7 +26,7 @@ const whiteList = ['/login'] // 不重定向白名单
router.beforeEach(async (to, from, next) => {
start()
loadStart()
if (wsCache.get(appStore.getUserInfo)) {
if (getStorage(appStore.getUserInfo)) {
if (to.path === '/login') {
next({ path: '/' })
} else {
Expand All @@ -44,8 +44,8 @@ router.beforeEach(async (to, from, next) => {
}

// 开发者可根据实际情况进行修改
const roleRouters = wsCache.get('roleRouters') || []
const userInfo = wsCache.get(appStore.getUserInfo)
const roleRouters = getStorage('roleRouters') || []
const userInfo = getStorage(appStore.getUserInfo)

// 是否使用动态路由
if (appStore.getDynamicRouter) {
Expand Down
28 changes: 14 additions & 14 deletions src/store/modules/app.ts
Expand Up @@ -2,9 +2,9 @@ import { defineStore } from 'pinia'
import { store } from '../index'
import { setCssVar, humpToUnderline } from '@/utils'
import { ElMessage, ComponentSize } from 'element-plus'
import { useCache } from '@/hooks/web/useCache'
import { useStorage } from '@/hooks/web/useStorage'

const { wsCache } = useCache()
const { getStorage, setStorage } = useStorage()

interface AppState {
breadcrumb: boolean
Expand Down Expand Up @@ -57,13 +57,13 @@ export const useAppStore = defineStore('app', {
fixedHeader: true, // 固定toolheader
footer: true, // 显示页脚
greyMode: false, // 是否开始灰色模式,用于特殊悼念日
dynamicRouter: wsCache.get('dynamicRouter') || false, // 是否动态路由
fixedMenu: wsCache.get('fixedMenu') || false, // 是否固定菜单
dynamicRouter: getStorage('dynamicRouter') || false, // 是否动态路由
fixedMenu: getStorage('fixedMenu') || false, // 是否固定菜单

layout: wsCache.get('layout') || 'classic', // layout布局
isDark: wsCache.get('isDark') || false, // 是否是暗黑模式
currentSize: wsCache.get('default') || 'default', // 组件尺寸
theme: wsCache.get('theme') || {
layout: getStorage('layout') || 'classic', // layout布局
isDark: getStorage('isDark') || false, // 是否是暗黑模式
currentSize: getStorage('default') || 'default', // 组件尺寸
theme: getStorage('theme') || {
// 主题色
elColorPrimary: '#409eff',
// 左侧菜单边框颜色
Expand Down Expand Up @@ -213,11 +213,11 @@ export const useAppStore = defineStore('app', {
this.greyMode = greyMode
},
setDynamicRouter(dynamicRouter: boolean) {
wsCache.set('dynamicRouter', dynamicRouter)
setStorage('dynamicRouter', dynamicRouter)
this.dynamicRouter = dynamicRouter
},
setFixedMenu(fixedMenu: boolean) {
wsCache.set('fixedMenu', fixedMenu)
setStorage('fixedMenu', fixedMenu)
this.fixedMenu = fixedMenu
},
setPageLoading(pageLoading: boolean) {
Expand All @@ -229,7 +229,7 @@ export const useAppStore = defineStore('app', {
return
}
this.layout = layout
wsCache.set('layout', this.layout)
setStorage('layout', this.layout)
},
setTitle(title: string) {
this.title = title
Expand All @@ -243,18 +243,18 @@ export const useAppStore = defineStore('app', {
document.documentElement.classList.add('light')
document.documentElement.classList.remove('dark')
}
wsCache.set('isDark', this.isDark)
setStorage('isDark', this.isDark)
},
setCurrentSize(currentSize: ComponentSize) {
this.currentSize = currentSize
wsCache.set('currentSize', this.currentSize)
setStorage('currentSize', this.currentSize)
},
setMobile(mobile: boolean) {
this.mobile = mobile
},
setTheme(theme: ThemeTypes) {
this.theme = Object.assign(this.theme, theme)
wsCache.set('theme', this.theme)
setStorage('theme', this.theme)
},
setCssVarTheme() {
for (const key in this.theme) {
Expand Down
10 changes: 5 additions & 5 deletions src/store/modules/locale.ts
Expand Up @@ -2,10 +2,10 @@ import { defineStore } from 'pinia'
import { store } from '../index'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import en from 'element-plus/es/locale/lang/en'
import { useCache } from '@/hooks/web/useCache'
import { useStorage } from '@/hooks/web/useStorage'
import { LocaleDropdownType } from '@/components/LocaleDropdown'

const { wsCache } = useCache()
const { getStorage, setStorage } = useStorage()

const elLocaleMap = {
'zh-CN': zhCn,
Expand All @@ -20,8 +20,8 @@ export const useLocaleStore = defineStore('locales', {
state: (): LocaleState => {
return {
currentLocale: {
lang: wsCache.get('lang') || 'zh-CN',
elLocale: elLocaleMap[wsCache.get('lang') || 'zh-CN']
lang: getStorage('lang') || 'zh-CN',
elLocale: elLocaleMap[getStorage('lang') || 'zh-CN']
},
// 多语言
localeMap: [
Expand Down Expand Up @@ -49,7 +49,7 @@ export const useLocaleStore = defineStore('locales', {
// this.locale = Object.assign(this.locale, localeMap)
this.currentLocale.lang = localeMap?.lang
this.currentLocale.elLocale = elLocaleMap[localeMap?.lang]
wsCache.set('lang', localeMap?.lang)
setStorage('lang', localeMap?.lang)
}
}
})
Expand Down
3 changes: 2 additions & 1 deletion src/views/Example/Page/ExampleAdd.vue
Expand Up @@ -6,7 +6,7 @@ import { ElButton } from 'element-plus'
import { useI18n } from '@/hooks/web/useI18n'
import { useRouter } from 'vue-router'
import { saveTableApi } from '@/api/table'
import { useEmitt } from '@/hooks/web/useEmitt'
import { useEmitt } from '@/hooks/event/useEmitt'
const { emitter } = useEmitt()
Expand Down Expand Up @@ -50,3 +50,4 @@ const save = async () => {
</template>
</ContentDetailWrap>
</template>
@/hooks/event/useEmitt
3 changes: 2 additions & 1 deletion src/views/Example/Page/ExampleEdit.vue
Expand Up @@ -7,7 +7,7 @@ import { useI18n } from '@/hooks/web/useI18n'
import { useRouter, useRoute } from 'vue-router'
import { saveTableApi, getTableDetApi } from '@/api/table'
import { TableData } from '@/api/table/types'
import { useEmitt } from '@/hooks/web/useEmitt'
import { useEmitt } from '@/hooks/event/useEmitt'
const { emitter } = useEmitt()
Expand Down Expand Up @@ -64,3 +64,4 @@ const save = async () => {
</template>
</ContentDetailWrap>
</template>
@/hooks/event/useEmitt
3 changes: 2 additions & 1 deletion src/views/Example/Page/ExamplePage.vue
Expand Up @@ -9,7 +9,7 @@ import { useTable } from '@/hooks/web/useTable'
import { TableData } from '@/api/table/types'
import { h, reactive, ref, unref } from 'vue'
import { useRouter } from 'vue-router'
import { useEmitt } from '@/hooks/web/useEmitt'
import { useEmitt } from '@/hooks/event/useEmitt'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
defineOptions({
Expand Down Expand Up @@ -265,3 +265,4 @@ const action = (row: TableData, type: string) => {
/>
</ContentWrap>
</template>
@/hooks/event/useEmitt

0 comments on commit dfea91c

Please sign in to comment.