diff --git a/package.json b/package.json index 668a513..da12581 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "date-fns": "^2.28.0", "lodash-es": "^4.17.21", "pinia": "^2.0.14", + "pinia-plugin-persist": "^1.0.0", "vue": "^3.2.33", "vue-router": "^4.0.16" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 486dd3e..ce5dd5c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -28,6 +28,7 @@ specifiers: lodash-es: ^4.17.21 mockjs: ^1.1.0 pinia: ^2.0.14 + pinia-plugin-persist: ^1.0.0 postcss: ^8.4.12 postcss-html: ^1.3.0 postcss-less: ^6.0.0 @@ -55,6 +56,7 @@ dependencies: date-fns: 2.28.0 lodash-es: 4.17.21 pinia: 2.0.14_ub5l46u3nefphax5x2tezui4oq + pinia-plugin-persist: 1.0.0_pinia@2.0.14+vue@3.2.37 vue: 3.2.37 vue-router: 4.1.0_vue@3.2.37 @@ -3989,6 +3991,21 @@ packages: dev: true optional: true + /pinia-plugin-persist/1.0.0_pinia@2.0.14+vue@3.2.37: + resolution: {integrity: sha512-M4hBBd8fz/GgNmUPaaUsC29y1M09lqbXrMAHcusVoU8xlQi1TqgkWnnhvMikZwr7Le/hVyMx8KUcumGGrR6GVw==} + peerDependencies: + '@vue/composition-api': ^1.0.0 + pinia: ^2.0.0 + vue: ^2.0.0 || >=3.0.0 + peerDependenciesMeta: + '@vue/composition-api': + optional: true + dependencies: + pinia: 2.0.14_ub5l46u3nefphax5x2tezui4oq + vue: 3.2.37 + vue-demi: 0.12.5_vue@3.2.37 + dev: false + /pinia/2.0.14_ub5l46u3nefphax5x2tezui4oq: resolution: {integrity: sha512-0nPuZR4TetT/WcLN+feMSjWJku3SQU7dBbXC6uw+R6FLQJCsg+/0pzXyD82T1FmAYe0lsx+jnEDQ1BLgkRKlxA==} peerDependencies: diff --git a/src/main.ts b/src/main.ts index 2174fee..7274c9e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,7 +5,7 @@ import '@/theme/index.less' import { createApp } from 'vue' import { idux } from './plugins' import router from './router' -import { store } from './store' +import store from './store' import App from './App.vue' createApp(App).use(idux).use(store).use(router).mount('#app') diff --git a/src/store/index.ts b/src/store/index.ts index 953e453..c04a85d 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -1,3 +1,12 @@ import { createPinia } from 'pinia' +import { App } from 'vue' +import piniaPersist from 'pinia-plugin-persist' -export const store = createPinia() +const store = createPinia() + +const install = (app: App): void => { + store.use(piniaPersist) + + app.use(store) +} +export default { install } diff --git a/src/store/stores/appSetting.ts b/src/store/stores/appSetting.ts new file mode 100644 index 0000000..a9efc37 --- /dev/null +++ b/src/store/stores/appSetting.ts @@ -0,0 +1,34 @@ +import { defineStore } from 'pinia' +import { ref } from 'vue' + +export type ThemeType = 'light' | 'dark' + +export const useAppSettingStore = defineStore( + 'appSetting', + () => { + const appTheme = ref('light') + const loading = ref() + + /** + * 修改系统主题 + */ + function changeTheme(theme: ThemeType) { + appTheme.value = theme + } + function changeLoadingProgress(progress: number) { + loading.value = progress + } + return { + theme: appTheme, + loading, + changeLoadingProgress, + changeTheme, + } + }, + { + persist: { + enabled: true, + strategies: [{ storage: localStorage, paths: ['appTheme'] }], + }, + }, +) diff --git a/src/store/stores/index.ts b/src/store/stores/index.ts new file mode 100644 index 0000000..69b1dce --- /dev/null +++ b/src/store/stores/index.ts @@ -0,0 +1,17 @@ +import { useAppSettingStore } from './appSetting' + +// const allModules = import.meta.globEager('./**/*.ts') + +// Object.keys(allModules).forEach(path => { +// const fileName = path.split('/')[1] +// const module = allModules[path][fileName] || allModules[path].default || allModules[path] +// Object.getOwnPropertyNames(module) +// .filter(item => item.match(/^use/)) +// .forEach(item => { +// appStore[item] = module[item] +// }) +// }) + +export default { + useAppSettingStore, +} diff --git a/src/store/stores/user.ts b/src/store/stores/user.ts new file mode 100644 index 0000000..f054847 --- /dev/null +++ b/src/store/stores/user.ts @@ -0,0 +1,30 @@ +import { defineStore } from 'pinia' +import { ref } from 'vue' + +export const useUserStore = defineStore( + 'user', + () => { + const token = ref('') + const userInfo = ref() + + async function login() {} + async function logout() {} + + async function getUserInfo() { + return userInfo.value + } + + return { + token, + login, + logout, + getUserInfo, + } + }, + { + persist: { + enabled: true, + strategies: [{ storage: localStorage, paths: ['token', 'userInfo'] }], + }, + }, +) diff --git a/src/store/utils/index.ts b/src/store/utils/index.ts new file mode 100644 index 0000000..db494ee --- /dev/null +++ b/src/store/utils/index.ts @@ -0,0 +1,20 @@ +/** + * 重构$reset() + * @desc 因为setup模式编程不支持reset方法,这里要手动重构 + * @param appStore + */ +export const installResetFunc = (appStore: object): void => + Object.values(appStore).forEach(item => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const initState = {} as Record + if (item) { + Object.entries(item.$state).forEach(item => { + initState[item[0]] = item[1] + }) + item.reset = () => { + Object.keys(item.$state).forEach(state => { + item.$state[state] = initState[state] + }) + } + } + }) diff --git a/tsconfig.json b/tsconfig.json index 282c385..16f5b08 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,7 +10,7 @@ "resolveJsonModule": true, "esModuleInterop": true, "lib": ["esnext", "dom"], - + "types": ["pinia-plugin-persist"], "baseUrl": "./", "paths": { "@/*": ["src/*"]