/* 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-30 11:07: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: typeof storeInfo.state === 'function' ? storeInfo.state : function () { return storeInfo.state }, getters: storeInfo.getters, actions: storeInfo.actions }) } const flatStore: (storeMap: Record, parentName?: string) => any = (storeMap: Record = {}, parentName: string = '') => { return Object.keys(storeMap).reduce((store, key: string) => { const name = [parentName, key].filter(str => str.trim()).join('/'); store[name] = storeMap[key]; if ('modules' in storeMap[key]) { return { ...store, ...flatStore(storeMap[key].modules, name) } } return store }, {} as any) } const storeMap = flatStore({ app, user, theme, tagsView, permission, }); 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 () { if (!$STORE) build() return obj[key]($STORE.state) } return methods }, {} as Record, ) } export function mapGetters(list: string[]) { return list.reduce( (gettersMap, key) => { gettersMap[key] = function () { if (!$STORE) build() return $STORE.getters[key] } return gettersMap }, {} as Record, ) } export function mapMutations(list: string[]) { return list.reduce( (mutations, key) => { mutations[key] = function (...args) { if (!$STORE) build() return $STORE.commit(key, ...args) } return mutations }, {} as Record any>, ) } export function mapActions(list: string[]) { return list.reduce( (mutations, key) => { mutations[key] = function (...args) { if (!$STORE) build() 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 getState = (state: any, storeName: string) => { return new Proxy(state, { get(target: any, prop: string) { if (typeof prop === 'symbol') { return target[prop] } if (prop in target) { return target[prop] } else { const _prop = `${storeName}/${prop}`; if (_prop in entityStore) { return entityStore[_prop].$state } else if (_prop in storeObj) { entityStore[_prop] = storeObj[_prop](); } return entityStore[_prop]?.$state } }, set(target: any, p: string, newValue: any, receiver: any) { if (p in target) { target[p] = newValue; } else { const _prop = `${storeName}/${p}`; if (_prop in entityStore) { entityStore[_prop].$state[p] = newValue } } return true; } }) } const state = new Proxy(storeObj, { get(target: any, prop: string) { try { if (prop in entityStore) { return getState(entityStore[prop].$state, prop) } else { entityStore[prop] = target[prop](); } return getState(entityStore[prop].$state, prop) } catch (e: any) { // console.error(e) return null } }, }) 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](getState(entityStore[storeName].$state, storeName)) } else { entityStore[storeName] = target[storeName]() } return getterMap[storeName][prop](getState(entityStore[storeName].$state, storeName)) }, set(target, key, value) { return Reflect.set(target, key, value) }, }) let currentStoreName = '' $STORE = { state, getters, // commit 执行的就是 mutations 方法 commit(name: string, ...args: any) { let action = name let path = name; if (name.includes('/')) { const arr = name.split('/') // name = arr[0] action = arr.pop()!; path = arr.join('/') } // else { const storeName = Object.keys(mutationMap).find((key) => action in mutationMap[key]) // if (!storeName) { // storeName = Object.keys(mutationMap).find((key) => { // return Object.keys(mutationMap[key]).includes(action) // }) // } if (!storeName) return // 如果没有找到内容的时候,就进行读取,将会默认生成 Store 信息 if (!(storeName in entityStore)) { $STORE.state[storeName] } const store = entityStore[storeName] if (!store) return null const fun = mutationMap[storeName][action] if (!fun) return null return fun(getState(store.$state, storeName), ...args) }, // dispatch 执行的是 actions 方法 dispatch(name: string, ...args: any[]) { let action = name let path = name; if (name.includes('/')) { const arr = name.split('/') // name = arr[0] action = arr.pop()!; path = arr.join('/') } // else { const storeName = Object.keys(actionMap).find((key) => action in actionMap[key]) if (!storeName) return null // 如果没有找到内容的时候,就进行读取,将会默认生成 Store 信息 if (!(storeName in entityStore)) { $STORE.state[storeName] } const store = entityStore[storeName] currentStoreName = storeName if (!store) return null const fun = store[action] if (!fun) return null return fun( { state: getState(store.$state, storeName), commit(name: string, ...args: any) { return $STORE.commit(storeName + '/' + name, ...args) }, dispatch: $STORE.dispatch, }, ...args, ) // } }, } return $STORE } export default build