system-api.uts 8.17 KB
import { env } from "@/api/env.uts";
/**
 * 系统/配置 API(从 gl_tape/api/module/systemApi.js 迁移)
 * 可先返回 mock 对象,后续接入真实配置中心
 */
import http from "@/api/request/http.uts";
import { storage } from "@/utils/storage.uts";

const ENV_STR = env == "production" ? "prod" : (env == "develop" ? "develop" : "test");
const CONFIG_MODULE = "app-tape";
const BOUTIQUE_CONFIG_MODULE = "boutique-pad-config";
const CONFIG_BASE = "https://config-center-backend.glsx.com.cn/glsxAdmin/config";
const CONFIG_URL = `${CONFIG_BASE}?moduleUrl=${CONFIG_MODULE}&env=${ENV_STR}`;
const BOUTIQUE_CONFIG_URL = `${CONFIG_BASE}?moduleUrl=${BOUTIQUE_CONFIG_MODULE}&env=${ENV_STR}`;

const _emptyList: any[] = [] as any[];
const defaultBoutiqueMock: UTSJSONObject = {
	"createTime": 0,
	"moduleList": _emptyList,
	"toolList": _emptyList,
	"defaultCarId": ""
};

function parseStorageJson(key: string): null | UTSJSONObject {
	const raw = storage.get(key);
	if (raw == null) return null;
	if (typeof raw === "string") {
		const s = (raw as string).trim();
		if (s == "" || s.length === 0) return null;
		try {
			return JSON.parse(s) as UTSJSONObject;
		} catch {
			return null;
		}
	}
	return raw as UTSJSONObject;
}

/** 获取 APP 配置 */
export function getMockData(immediate: boolean = false): Promise<UTSJSONObject> {
	const cached = parseStorageJson("mock_data");
	if (!immediate && cached != null) {
		const createTime = (cached.createTime as number) ?? 0;
		if (Date.now() - createTime < 5 * 60 * 1000) {
			return Promise.resolve(cached);
		}
	}
	return http.uni_request({ "url": CONFIG_URL }).then((data: any): UTSJSONObject => {
		const result = (data != null ? data : {}) as UTSJSONObject;
		result.createTime = Date.now();
		// ✅ 性能优化:异步写入 Storage,避免阻塞主线程
		setTimeout(() => {
			storage.set("mock_data", result, 0);
		}, 0);
		return result;
	}).catch((): UTSJSONObject => {
		const fallback: UTSJSONObject = { "createTime": Date.now() };
		return fallback;
	});
}

/** 获取轻改配置(moduleList, toolList, defaultCarId) */
export function getBoutiqueMockData(immediate: boolean = false): Promise<UTSJSONObject> {
	const cached = parseStorageJson("boutique_mock_data");
	if (!immediate && cached != null) {
		const createTime = (cached.createTime as number) ?? 0;
		if (Date.now() - createTime < 5 * 60 * 1000) {
			return Promise.resolve(cached);
		}
	}
	return http.uni_request({ "url": BOUTIQUE_CONFIG_URL }).then((data: any): UTSJSONObject => {
		const result = (data != null ? data : {}) as UTSJSONObject;
		result.createTime = Date.now();
		result.moduleList = result.moduleList != null ? result.moduleList : _emptyList;
		result.toolList = result.toolList != null ? result.toolList : _emptyList;
		result.defaultCarId = result.defaultCarId != null ? result.defaultCarId : "";
		// ✅ 性能优化:异步写入 Storage,避免阻塞主线程
		setTimeout(() => {
			storage.set("boutique_mock_data", result, 0);
		}, 0);
		return result;
	}).catch((): UTSJSONObject => {
		const fallback = { ...defaultBoutiqueMock, "createTime": Date.now() } as UTSJSONObject;
		return fallback;
	});
}

/**
 * 获取后台延保素材ID
 */
export function getEquityMaterialId(data: UTSJSONObject): Promise<any> {
	return http.post({
		method: "dj.api.smartcarlife.reception.home.page.material.view",
		projectName: "boutique",
		...data
	} as UTSJSONObject);
}

/**
 * 获取后台延保素材配置
 */
export function getEquityMaterialConfig(data: UTSJSONObject): Promise<any> {
	return http.post({
		method: "dj.api.car.boutique.reception.home.page.entrance.material.detail",
		projectName: "boutique",
		...data
	} as UTSJSONObject);
}

/**
 * 权益配置
 */
export function findReceptionpriproConfigList(data: UTSJSONObject): Promise<any> {
	return http.post({
		method: "dj.api.smartcarlife.recepiton.customer.findReceptionpriproConfigList",
		projectName: "boutique",
		...data
	} as UTSJSONObject);
}

/** 获取素材配置 */
export async function getSoureData(
	shopMerchantId: number | string,
	sceneType: number | string,
	params: UTSJSONObject = {}
): Promise<any> {
	try {
		const res = await getEquityMaterialId({
			merchantId: shopMerchantId,
			...params
		});

		if (res != null && (res as UTSJSONObject).id != null) {
			console.log((res as UTSJSONObject).id, "res.idres.idres.idres.id");

			const data = await getEquityMaterialConfig({
				id: (res as UTSJSONObject).id
			});

			if (data != null) {
				const dataObj = data as UTSJSONObject;
				const storageName = `source_${sceneType}_${shopMerchantId}`;
				let toolList: Array<any> = [];
				if (dataObj.toolList != null) {
					toolList = dataObj.toolList as Array<any>;
				}
				let materialInfoList: Array<any> = [];
				if (dataObj.materialInfoList != null) {
					materialInfoList = dataObj.materialInfoList as Array<any>;
				}
				if (toolList.length == 0 && materialInfoList.length > 0) {
					toolList = materialInfoList.map((item: any) => {
						const obj = item as UTSJSONObject;
						const shortTitle =
							(obj.materialTitle as null | string)
							?? (obj.detailTitle as null | string)
							?? (obj.materialName as null | string)
							?? "";
						const shortTitleUrl =
							(obj.thumbnail as null | string)
							?? (obj.thumbnailUrl as null | string)
							?? (obj.homeEntranceCover as null | string)
							?? (obj.contentPicUrl as null | string)
							?? "";
						return {
							id: obj.id,
							shortTitle,
							shortTitleUrl
						} as UTSJSONObject;
					});
				}

				const insurance = (materialInfoList as Array<any>).find((e: any) => (e as UTSJSONObject).componentName == "insurance");
				const hasEquity = (toolList as Array<any>).find((e: any) => (e as UTSJSONObject).id == (insurance as UTSJSONObject).id);

				if (insurance != null && hasEquity != null) {
					let configList: Array<any> = [];

					try {
						configList = await findReceptionpriproConfigList({}) as Array<any>;
						// ✅ 性能优化:异步写入 Storage,避免阻塞主线程
						setTimeout(() => {
							storage.set("findReceptionpriproConfigList", configList, 0);
						}, 0);
					} catch {
						configList = storage.get("findReceptionpriproConfigList") as Array<any>;
					}
					console.log("[equity] findReceptionpriproConfigList:", configList);

					const toolOtherList = (configList as Array<any>).map((i: any) => {
						return {
							id: ((hasEquity as UTSJSONObject).id as number) + 100 + ((i as UTSJSONObject).id as number),
							shortTitle: (i as UTSJSONObject).productName,
							shortTitleUrl: (i as UTSJSONObject).procuctLog
						} as UTSJSONObject;
					});

					toolList = toolList.concat(toolOtherList);

					const moduleOtherList = (configList as Array<any>).map((i: any)  => {
						const iObj = i as UTSJSONObject;
						const productIntro =
							(iObj.briefIntroduction as null | string)
							?? (iObj.productIntroduction as null | string)
							?? (iObj.introduction as null | string)
							?? (iObj.desc as null | string)
							?? "";
						return {
							id: ((hasEquity as UTSJSONObject).id as number) + 100 + (iObj.id as number),
							title: iObj.productName,
							shortTitleUrl: iObj.procuctLog,
							productPoster: iObj.productPoster,
							briefIntroduction: productIntro,
							showAlt: false,
							componentName: "CaseCenter",
							receptionpriproCusCaseList: iObj.receptionpriproCusCaseList,
							hide: true,
							detailTitle: iObj.productName,
							thumbnail: "https://img02.glsx.com.cn/weapp/resource/app-tape/common/qsyw.webp"
						} as UTSJSONObject;
					});
					console.log("[equity] moduleOtherList:", moduleOtherList);

					materialInfoList = materialInfoList.concat(moduleOtherList);
				}

				/** 权益素材直接使用网络 URL:App 端 `<image>` / un-image 可加载 https,转 base64 体积大、慢且无必要 */
				dataObj.toolList = toolList;
				dataObj.moduleList = materialInfoList;
				
				// ✅ 返回数据,由调用方决定是否缓存
				return dataObj;
			} else {
				throw new Error("获取数据失败");
			}
		} else {
			throw new Error("未找到素材 ID");
		}
	} catch (error) {
		uni.hideLoading();
		if (error != null) {
			uni.showToast({
				title: `${error}`,
				icon: "none",
				duration: 2000
			});
		}
		return Promise.reject(error);
	}
}