Skip to content

Commit

Permalink
feat: add store module (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuchg authored Jul 21, 2022
1 parent 6701db9 commit e9195dd
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
17 changes: 17 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
11 changes: 10 additions & 1 deletion src/store/index.ts
Original file line number Diff line number Diff line change
@@ -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 }
34 changes: 34 additions & 0 deletions src/store/stores/appSetting.ts
Original file line number Diff line number Diff line change
@@ -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<ThemeType>('light')
const loading = ref<number>()

/**
* 修改系统主题
*/
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'] }],
},
},
)
17 changes: 17 additions & 0 deletions src/store/stores/index.ts
Original file line number Diff line number Diff line change
@@ -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,
}
30 changes: 30 additions & 0 deletions src/store/stores/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore(
'user',
() => {
const token = ref<string>('')
const userInfo = ref<object>()

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'] }],
},
},
)
20 changes: 20 additions & 0 deletions src/store/utils/index.ts
Original file line number Diff line number Diff line change
@@ -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<string, any>
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]
})
}
}
})
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"resolveJsonModule": true,
"esModuleInterop": true,
"lib": ["esnext", "dom"],

"types": ["pinia-plugin-persist"],
"baseUrl": "./",
"paths": {
"@/*": ["src/*"]
Expand Down

0 comments on commit e9195dd

Please sign in to comment.