/* eslint-disable @typescript-eslint/no-unsafe-function-type */ /* eslint-disable @typescript-eslint/no-explicit-any */ /* * @Author: liujing10 809002582@qq.com * @Date: 2025-05-07 14:05:07 * @LastEditors: liujing10 809002582@qq.com * @LastEditTime: 2025-05-14 11:46:04 * @Description: * 通过自定义封装,将pinia转换成vuex的使用方式,并且可以挂在至vue全局上。 * 使用方式与vuex一样。 * 例如:this.$store.app.xxxx * this.$store.dispatch('xxxx',xxxx) * this.$store.commit('xxxx',xxxxx) * * * import { createPinia } from 'pinia' * import getStore from '@/store/piniaToVuex' * * 初始化 pinia * instance.use(createPinia()) * 挂在至 Vue 原型连上 * Properties.$store = getStore(); */ import { defineStore } from 'pinia'; import app from '@/store/modules/app' import user from '@/store/modules/user' import theme from '@/store/modules/theme' import tagsView from '@/store/modules/tagsView' import permission from '@/store/modules/permission' const toStore = (name: string, storeInfo: any) => { return defineStore(name, { state: storeInfo.state, getters: storeInfo.getters, actions: storeInfo.actions }) } const storeMap = { app, user, theme, tagsView, permission, } as const; const actionMap: any = Object.keys(storeMap).reduce((obj, key) => { return { ...obj, [key]: (storeMap as any)[key].actions } }, {} as Record); const mutationMap: any = Object.keys(storeMap).reduce((obj, key) => { return { ...obj, [key]: (storeMap as any)[key].mutations } }, {} as Record); const getterMap: any = Object.keys(storeMap).reduce((obj, key) => { return { ...obj, [key]: (storeMap as any)[key].getters } }, {} as Record); let $STORE: any = null; export function mapState(obj: any) { return Object.keys(obj).reduce((methods, key) => { methods[key] = function () { return obj[key]($STORE.state) } return methods }, {} as Record) } export function mapGetters(list: string[]) { return list.reduce((gettersMap, key) => { gettersMap[key] = function () { return $STORE.getters[key] } return gettersMap }, {} as Record) } export function mapMutations(list: string[]) { return list.reduce((mutations, key) => { mutations[key] = function (...args) { return $STORE.commit(key, ...args) } return mutations }, {} as Record any>) } export function mapActions(list: string[]) { return list.reduce((mutations, key) => { mutations[key] = function (...args) { return $STORE.dispatch(key, ...args) } return mutations }, {} as Record any>) } const build = () => { if ($STORE) return $STORE; const storeObj = Object.keys(storeMap).reduce((obj, key) => { return { ...obj, [key]: toStore(key, (storeMap as any)[key]) } }, {}); const entityStore:any = {}; const state = new Proxy(storeObj, { get(target: any, prop) { if (prop in entityStore) { return entityStore[prop].$state; } else { entityStore[prop] = target[prop]() } return entityStore[prop].$state; } }) const getters = new Proxy(storeObj, { get(target:any, prop) { const storeName = Object.keys(getterMap).find(key => prop in getterMap[key]); if(!storeName){ return null } if (storeName in entityStore) { return getterMap[storeName][prop](entityStore[storeName].$state) } else { entityStore[storeName] = target[storeName]() } return getterMap[storeName][prop](entityStore[storeName].$state) }, set(target, key, value) { if (key === 'age' && typeof value !== 'number') { throw new Error('Age must be a number!'); } return Reflect.set(target, key, value); } }) $STORE = { state, getters, commit(name: string, ...args: any) { if (name.includes('/')) { } else { const storeName = Object.keys(mutationMap).find(key => name in mutationMap[key]); if (!storeName) return null; const store = entityStore[storeName]; if (!store) return null; const fun = mutationMap[storeName][name]; if (!fun) return null; return fun(store.$state, ...args); } }, dispatch(name: string, ...args: any[]) { if (name.includes('/')) { } else { const storeName = Object.keys(actionMap).find(key => name in actionMap[key]); if (!storeName) return null; const store = entityStore[storeName] if (!store) return null; const fun = store?.[name]; if (!fun) return null; return fun({ state: store.$state, commit: $STORE.commit, dispatch: $STORE.dispatch }, ...args); } } } return $STORE } export default build