receiption.uts 8.69 KB

import http from "@/api/request/http.uts";
import { logger } from "@/services/logger";

/**
 * 统一 API 日志包装(避免到处散落 logger 调用)
 * - UTS 里不依赖 uni.logger(项目已改为显式 import 使用)
 */
function safeToJson(value: any): string {
	try {
		return JSON.stringify(value);
	} catch (e) {
		return "[unserializable]";
	}
}

function logAndReturn(
	name: string,
	task: Promise<any>,
	meta: UTSJSONObject
): Promise<UTSJSONObject> {
	return (async (): Promise<UTSJSONObject> => {
		const startAt = Date.now();
		const startPayload: UTSJSONObject = { name: name, startAt: startAt };
		for (const k in meta) {
			startPayload[k] = meta[k];
		}
		logger.info(startPayload, "API");

		try {
			const res = await task;
			const costMs = Date.now() - startAt;
			// ✅ 临时禁用:注释掉响应日志,避免控制台输出大量数据
			// logger.info({ name: name, costMs: costMs, res: safeToJson(res) } as UTSJSONObject, "API");
			return res as UTSJSONObject;
		} catch (err) {
			const costMs = Date.now() - startAt;
			const errPayload: UTSJSONObject = { name: name, costMs: costMs, err: `${err}` };
			for (const k in meta) {
				errPayload[k] = meta[k];
			}
			logger.error(errPayload, "API");
			// UTS 侧 throw 需要 Throwable,这里统一转 Error(带上 name 便于与网关原始文案对照)
			throw new Error(`[API:${name}] ${err}`);
		}
	})();
}

function withProjectName(projectName: string, params: UTSJSONObject): UTSJSONObject {
	const data: UTSJSONObject = { projectName: projectName };
	for (const k in params) {
		data[k] = params[k];
	}
	return data;
}

function fetchWithLog(
	name: string,
	api: string,
	params: UTSJSONObject,
	needToken: boolean,
	env: string
): Promise<UTSJSONObject> {
	const meta: UTSJSONObject = {
		method: "FETCH",
		api: api,
		needToken: needToken,
		env: env,
		params: safeToJson(params)
	};
	return logAndReturn(name, http.fetch(api, params, needToken, env), meta);
}

function shukeRequestWithLog(
	name: string,
	url: string,
	params: UTSJSONObject,
	method: string,
	showLoading: boolean
): Promise<UTSJSONObject> {
	const meta: UTSJSONObject = {
		method: method,
		url: url,
		showLoading: showLoading,
		params: safeToJson(params)
	};
	return logAndReturn(name, http.shukeRequest(url, params, method, showLoading), meta);
}

// export async function getToken(params: UTSJSONObject): Promise<UTSJSONObject> {
// 	const data: UTSJSONObject = {
// 		projectName: "boutique",
// 		...params
// 	};
// 	return http.fetch("dj.api.smartcarlife.recepiton.customer.oss.getToken", data, false, "dj");
// }
// export async function saveRecCustomer(params: UTSJSONObject): Promise<boolean> {
// 	try{
// 	const data: UTSJSONObject = {
// 		projectName: "boutique",
// 		...params
// 	};
// 	return await http.fetch("dj.api.smartcarlife.recepiton.customer.saveRecCustomer", data, false, "dj");
// 	}catch (error) {
// 		return false;
// 	}
// }
export async function searchRecordingEndTime(params: UTSJSONObject): Promise<string> {
	const data = withProjectName("boutique", params);
	// returnAll=true:避免 ROP 在 code!=0 时直接 reject 被 logAndReturn 二次包装;在此统一解析业务码与 data
	const res = await fetchWithLog(
		"searchRecordingEndTime",
		"dj.api.smartcarlife.recepiton.customer.searchRecordingEndTime",
		data,
		true,
		"dj"
	) as UTSJSONObject;
	const code = res["code"];
	if (`${code}` != "0") {
		const msg = res["message"] != null && `${res["message"]}` != "" ? `${res["message"]}` : "searchRecordingEndTime 调用失败";
		throw new Error(msg);
	}
	const biz = res["data"];
	if (biz == null) {
		return "";
	}
	if (typeof biz === "string") {
		return biz;
	}
	// 后端若将时间包在对象内,至少给出可观测字符串(调用方 RecordSave 会再 toStr / 日期归一化)
	return `${biz}`;
}

export async function getMerchantPerformance(params: UTSJSONObject): Promise<any> {
	const data = withProjectName("boutique", params);
	return await fetchWithLog(
		"getMerchantPerformance",
		"dj.api.car.boutique.inspection.findMerchantPerformance",
		data,
		true,
		"dj"
	);
}

/** 查询当前门店客户档案列表(预录列表) */
export async function getCustomerFileList(params: UTSJSONObject): Promise<any> {
	const data = withProjectName("boutique", params);
	return await fetchWithLog(
		"getCustomerFileList",
		"dj.api.smartcarlife.customervisits.customerInfoPage",
		data,
		true,
		"dj"
	);
}
/** 查询当前门店接待复盘列表(接待复盘列表) */
export async function getReceptionList(params: UTSJSONObject): Promise<any> {
	const data = withProjectName("boutique", params);
	return await fetchWithLog(
		"getReceptionList",
		"dj.api.smartcarlife.reception.customer.page",
		data,
		true,
		"dj"
	);
}
export async function findAllSence(params: UTSJSONObject): Promise<any> {
	const data = withProjectName("boutique", params);
	return await fetchWithLog(
		"findAllSence",
		"dj.api.smartcarlife.customervisits.findAllSence",
		data,
		true,
		"dj"
	);
}
// 根据客户标签
export async function findCustomerTags(params: UTSJSONObject): Promise<any> {
	const data = withProjectName("boutique", params);
	return await fetchWithLog(
		"findCustomerTags",
		"dj.api.smartcarlife.customervisits.findSenceTag",
		data,
		true,
		"dj"
	);
}
export async function saveVisitsInfo(params: UTSJSONObject): Promise<any> {
	const data = withProjectName("boutique", params);
	return await fetchWithLog(
		"saveVisitsInfo",
		"dj.api.smartcarlife.customervisits.saveVisitsInfo",
		data,
		true,
		"dj"
	);
}
export async function updateVisitsInfo(params: UTSJSONObject): Promise<any> {
	const data = withProjectName("boutique", params);
	return await fetchWithLog(
		"updateVisitsInfo",
		"dj.api.smartcarlife.customervisits.updateVisitsInfo",
		data,
		true,
		"dj"
	);
}

export async function findVisitsInfo(params: UTSJSONObject): Promise<any> {
	const data = withProjectName("boutique", params);
	return await fetchWithLog(
		"findVisitsInfo",
		"dj.api.smartcarlife.customervisits.findVisitsInfo",
		data,
		true,
		"dj"
	);
}

export async function findCheckResultByBatchId(params: UTSJSONObject): Promise<any> {
	const data = withProjectName("pad", params);
	return await fetchWithLog(
		"findCheckResultByBatchId",
		"dj.pad.api.reception.findCheckResultByBatchId",
		data,
		true,
		"dj"
	);
}
export async function getShortUrl(params: UTSJSONObject): Promise<any> {
	const data = withProjectName("boutique", params);
	return await fetchWithLog(
		"getShortUrl",
		"dj.api.car.boutique.inspection.batch.file.short.url",
		data,
		true,
		"dj"
	);
}
export async function getAiagent(params: UTSJSONObject): Promise<any> {
	const data = withProjectName("pad", params);
	return await fetchWithLog(
		"getAiagent",
		"dj.pad.api.aiagent.get.special.agent",
		data,
		true,
		"dj"
	);
}
export async function apiShukeLoginInit(params: UTSJSONObject): Promise<UTSJSONObject> {
	return await shukeRequestWithLog(
		"apiShukeLoginInit",
		"linkwe-gateway/auth/accountLoginSp/init",
		params,
		"POST",
		true
	);
}

export async function saveRecCustomer(params: UTSJSONObject): Promise<UTSJSONObject> {
	return await shukeRequestWithLog(
		"saveRecCustomer",
		"linkwe-gateway/reception/reception/save",
		params,
		"POST",
		true
	);
}
export async function getList(params: UTSJSONObject): Promise<UTSJSONObject> {
	return await shukeRequestWithLog(
		"getList",
		"linkwe-gateway/reception/reception/page",
		params,
		"POST",
		false
	);
}

export async function getToken(params: UTSJSONObject): Promise<UTSJSONObject> {
	return await shukeRequestWithLog(
		"getToken",
		"linkwe-gateway/file/get/config",
		params,
		"GET",
		true
	);
}
export async function findCheckResultListByReceptionId(params: UTSJSONObject): Promise<UTSJSONObject> {
	const name = "findCheckResultListByReceptionId";
	logger.info(
		{
			name: name,
			phase: "request",
			params: safeToJson(params)
		} as UTSJSONObject,
		"API"
	);

	const res = await shukeRequestWithLog(
		"findCheckResultListByReceptionId",
		"linkwe-gateway/reception/reception/findCheckResultListByReceptionId",
		params,
		"GET",
		true
	);
	logger.info(
		{
			name: name,
			phase: "response",
			res: safeToJson(res)
		} as UTSJSONObject,
		"API"
	);
	return res;
}

export async function findConversationDetailByReceptionId(params: UTSJSONObject): Promise<UTSJSONObject> {
	const name = "findConversationDetailByReceptionId";
	logger.info(
		{
			name: name,
			phase: "request",
			params: safeToJson(params)
		} as UTSJSONObject,
		"API"
	);

	const res = await shukeRequestWithLog(
		"findConversationDetailByReceptionId",
		"linkwe-gateway/reception/reception/findConversationDetailByReceptionId",
		params,
		"GET",
		true
	);
	logger.info(
		{
			name: name,
			phase: "response",
			res: safeToJson(res)
		} as UTSJSONObject,
		"API"
	);
	return res;
}