index.uts 4.07 KB
/**
 * Vuex 风格 Store(uni-app x 专用模块方案)
 * 模块化重构版本:手动导入模块,自动注册
 */
import { reactive } from "vue";

import { state as audioState, AudioStateType } from "@/store/modules/audio.uts";
import * as audio from "@/store/modules/audio.uts";
import { state as boutiqueState, BoutiqueStateType } from "@/store/modules/boutique.uts";
import * as boutique from "@/store/modules/boutique.uts";
import { state as pointState, PointStateType } from "@/store/modules/point.uts";
import * as point from "@/store/modules/point.uts";
import { state as sysState, SysStateType } from "@/store/modules/sys.uts";
import * as sys from "@/store/modules/sys.uts";
import { state as updateState, UpdateStateType } from "@/store/modules/update.uts";
import * as update from "@/store/modules/update.uts";
import { state as userState, UserStateType } from "@/store/modules/user.uts";
// 1. 导入所有模块
// UTS 目前不支持 import.meta.glob,需要手动导入
import * as user from "@/store/modules/user.uts";

// 2. 模块列表(新增模块需在此处添加)
// 这里使用 any 类型数组来规避类型检查问题,实际运行时会通过 UTSJSONObject 处理
const modules: any[] = [user, audio, sys, point, boutique, update];

// 聚合所有模块的 getters
const allGetters = {} as UTSJSONObject;

// 遍历模块进行聚合
for (const module_ of modules) {
	const mod = module_ as UTSJSONObject;
	
	// 动态聚合 getters
	const modGetters = mod.getters as null | UTSJSONObject;
	if (modGetters !== null) {
		modGetters.toMap().forEach((getterFn, getterName) => {
			allGetters[getterName] = getterFn;
		});
	}
}

// ============ 聚合 State ============
// 为了保持类型安全和代码提示,State 仍然建议静态定义
export class RootState {
	audio: AudioStateType = audioState;
	boutique: BoutiqueStateType = boutiqueState;
	point: PointStateType = pointState;
	sys: SysStateType = sysState;
	update: UpdateStateType = updateState;
	user: UserStateType = userState;
}

export const state = reactive(new RootState());

// ============ 全局 Getters ============
function _getSiteData(): any {
	const site = (state as UTSJSONObject).site;
	return (site !== null ? (site as UTSJSONObject).siteData : null) as any;
}
allGetters.siteData = (): any => _getSiteData();

export const getters = allGetters;

// ============ 动态 Dispatch 实现 ============
export function dispatch(actionName: string, payload: any = ({} as any)): any | null {
	// 遍历所有已加载的模块查找 action
	for (const module_ of modules) {
		const mod = module_ as UTSJSONObject;
		const actions = mod.actions as null | UTSJSONObject;
		if (actions !== null) {
			const handler = actions[actionName];
			if (typeof handler === "function") {
				// 执行找到的第一个匹配 Action
				return (handler as (payload: any) => any | null)(payload);
			}
		}
	}
	console.warn(`[Store] Action not found: ${actionName}`);
	return null;
}

/**
 * 录音业务联动:更新视图并写入当前车信息。
 * action 名字符串仅在 .uts 字面量中出现,避免 uvue 内 dispatch 首参触发 Android kotlin/java String 桥接报错。
 */
export function dispatchRecordCarSelectLinkage(selectItemVal: any): void {
	dispatch("updateViewKey");
	dispatch("setCurrentCarInfo", selectItemVal);
}

/** 写入门店车品牌列表到 store(action 名仅在 .uts 字面量) */
export function dispatchSetCarBrandsList(carBrandListData: any): void {
	dispatch("setCarBrandsList", carBrandListData);
}

/** commit:兼容 mapMutations,内部转调 dispatch */
export function commit(type: string, payload?: any): void {
	// 忽略命名空间前缀,直接查找方法名
	const name = type.includes("/") ? type.split("/")[1] : type;
	dispatch(name, payload != null ? payload : ({} as any));
}

// ============ Store 对象导出 ============
export const store = {
	state,
	getters,
	dispatch: (actionName: string, payload?: any): any | null => dispatch(actionName, payload != null ? payload : ({} as any)),
	commit: (type: string, payload?: any): void => commit(type, payload)
};

export default store;