mapHelpers.uts 3.91 KB
/**
 * Vuex 风格 mapHelpers,兼容旧项目 mapState、mapGetters、mapMutations
 * 用法与 vuex 一致,迁移时只需改 import 路径
 *
 * import { mapState, mapGetters, mapMutations } from '@/store/mapHelpers.uts'
 * import store from '@/store/index.uts'
 */
import type { RootState } from "./index.uts";

/** 组件实例类型(含 $store) */
type ComponentThis = {
	$store: {
		commit: (type: string, payload?: any) => void
		dispatch: (name: string, payload?: any) => void
		getters: Record<string, any>
		state: RootState
	}
}

/**
 * mapState - 映射 state 到 computed
 * mapState(['user']) => { user: () => this.$store.state.user }
 * mapState('audio', ['isEnd','isStart']) => { isEnd: () => this.$store.state.audio.isEnd, ... }
 * mapState(['point.tabIndex']) => { 'point.tabIndex': () => this.$store.state.point.tabIndex }
 */
export function mapState(map: string[]): Record<string, () => any>
export function mapState(namespace: string, map: string[]): Record<string, () => any>
export function mapState(
	namespaceOrMap: string | string[],
	map?: string[]
): Record<string, () => any> {
	if (typeof namespaceOrMap === "string" && map != null) {
		// mapState('audio', ['isEnd','isStart'])
		const ns = namespaceOrMap;
		const result: Record<string, () => any> = {};
		for (const key of map) {
			result[key] = function (this: ComponentThis): any {
				const moduleState = (this.$store.state as any)[ns];
				return moduleState != null ? moduleState[key] : null;
			};
		}
		return result;
	}
	// mapState(['user','point.tabIndex'])
	const keys = namespaceOrMap as string[];
	const result: Record<string, () => any> = {};
	for (const path of keys) {
		const localKey = path.includes(".") ? path : path;
		result[localKey] = function (this: ComponentThis): any {
			const parts = path.split(".");
			let v: any = this.$store.state;
			for (const part of parts) {
				v = v != null ? v[part] : null;
			}
			return v;
		};
	}
	return result;
}

/**
 * mapGetters - 映射 getters 到 computed
 * mapGetters(['customerInfo','receptionStatus'])
 */
export function mapGetters(keys: string[]): Record<string, () => any> {
	const result: Record<string, () => any> = {};
	for (const key of keys) {
		result[key] = function (this: ComponentThis): any {
			const g = this.$store.getters as Record<string, any>;
			if (g == null || g[key] === undefined) return null;
			const v = g[key];
			return typeof v === "function" ? v() : v;
		};
	}
	return result;
}

/**
 * mapMutations - 映射 mutations 到 methods
 * mapMutations('audio', ['setReceptionStatus','setBooks','setPerRecordNumber'])
 * mapMutations(['clearCustomerInfo'])
 */
export function mapMutations(map: string[]): Record<string, (payload?: any) => void>
export function mapMutations(
	namespace: string,
	map: string[]
): Record<string, (payload?: any) => void>
export function mapMutations(
	namespaceOrMap: string | string[],
	map?: string[]
): Record<string, (payload?: any) => void> {
	if (typeof namespaceOrMap === "string" && map != null) {
		const ns = namespaceOrMap;
		const result: Record<string, (payload?: any) => void> = {};
		for (const key of map) {
			result[key] = function (this: ComponentThis, payload?: any): void {
				this.$store.commit(`${ns}/${key}`, payload);
			};
		}
		return result;
	}
	const keys = namespaceOrMap as string[];
	const result: Record<string, (payload?: any) => void> = {};
	for (const key of keys) {
		result[key] = function (this: ComponentThis, payload?: any): void {
			this.$store.commit(key, payload);
		};
	}
	return result;
}

/**
 * mapActions - 映射 actions 到 methods(兼容旧项目若有使用)
 * mapActions(['setCustomerInfo','clearHistory'])
 */
export function mapActions(keys: string[]): Record<string, (payload?: any) => void> {
	const result: Record<string, (payload?: any) => void> = {};
	for (const key of keys) {
		result[key] = function (this: ComponentThis, payload?: any): void {
			this.$store.dispatch(key, payload);
		};
	}
	return result;
}