|
|
@@ -117,6 +117,10 @@ const SESSION = {
|
|
|
MANUAL_VIEW: "ENTRY_MANUAL_VIEW",
|
|
|
INVITE_TYPE: "ENTRY_INVITE_TYPE",
|
|
|
ADMIN_ID: "ENTRY_ADMIN_ID",
|
|
|
+ /** farmer_crop_detail 返回的品类详情,按 crop_id 缓存 */
|
|
|
+ CROP_DETAIL: "ENTRY_CROP_DETAIL",
|
|
|
+ /** crops 接口返回的城市等元信息 */
|
|
|
+ CROP_META: "ENTRY_CROP_META",
|
|
|
};
|
|
|
|
|
|
/** 用户手动添加的农机归入此分组,始终置顶 */
|
|
|
@@ -268,6 +272,25 @@ function updateCropGroupLabel(category, apiCropGroup) {
|
|
|
(category?.firstCrop ? `${category.firstCrop}类` : "");
|
|
|
}
|
|
|
|
|
|
+/** 缓存 farmer_crop_detail 品类信息,供提交时取 crop_code / crop_group */
|
|
|
+function saveCropDetail(data) {
|
|
|
+ if (data?.crop_id == null || data?.crop_id === "") return;
|
|
|
+ const cache = readJson(SESSION.CROP_DETAIL) || {};
|
|
|
+ cache[String(data.crop_id)] = {
|
|
|
+ crop_id: data.crop_id,
|
|
|
+ crop_code: data.crop_code || "",
|
|
|
+ crop_group: data.crop_group || "",
|
|
|
+ crop_name: data.crop_name || "",
|
|
|
+ crop_type: data.crop_type || "",
|
|
|
+ };
|
|
|
+ writeJson(SESSION.CROP_DETAIL, cache);
|
|
|
+}
|
|
|
+
|
|
|
+function getCropDetailMap() {
|
|
|
+ const cache = readJson(SESSION.CROP_DETAIL);
|
|
|
+ return cache && typeof cache === "object" ? cache : {};
|
|
|
+}
|
|
|
+
|
|
|
// ---------------------------------------------------------------------------
|
|
|
// 选中态:恢复 / 持久化
|
|
|
// ---------------------------------------------------------------------------
|
|
|
@@ -381,6 +404,7 @@ async function fetchEquipmentList() {
|
|
|
const res = await VE_API.entry.getMachines({ crop_id: cropId });
|
|
|
if (res.code === 200) {
|
|
|
const data = res.data || {};
|
|
|
+ saveCropDetail(data);
|
|
|
updateCropGroupLabel(category, data.crop_group);
|
|
|
equipmentGroups.value = mapMachinesToGroups(data.machines);
|
|
|
} else {
|
|
|
@@ -458,10 +482,6 @@ function getVarietyDraftList() {
|
|
|
return [];
|
|
|
}
|
|
|
|
|
|
-function getPhenophaseLabel(item) {
|
|
|
- return item.phenophase || item.phenologyName || String(item.phenologyId || "");
|
|
|
-}
|
|
|
-
|
|
|
function getUserId() {
|
|
|
const id = localStorage.getItem("MINI_USER_ID");
|
|
|
return id != null && id !== "" ? Number(id) : 0;
|
|
|
@@ -474,40 +494,107 @@ function getAdminId() {
|
|
|
return getUserId();
|
|
|
}
|
|
|
|
|
|
-/** 第一步常驻点位选点时写入的区县 adcode */
|
|
|
-function getCountyCode() {
|
|
|
- const location = readJson(SESSION.LOCATION);
|
|
|
- return location?.adcode ? String(location.adcode) : "";
|
|
|
-}
|
|
|
-
|
|
|
-/** 汇总各步骤 session 数据,供 addPlotInfo 提交 */
|
|
|
-function buildPlotPayload(includeEquipment = true) {
|
|
|
+/** 汇总各步骤 session 数据,供 addFarmInfo 提交 */
|
|
|
+function buildFarmPayload(includeEquipment = true) {
|
|
|
const baForm = readJson(SESSION.BA_FORM) || {};
|
|
|
const varietyList = getVarietyDraftList().filter((item) => item?.id != null && item.id !== "");
|
|
|
const equipmentCache = readJson(SESSION.EQUIPMENT);
|
|
|
const equipmentList = includeEquipment ? flattenEquipment(equipmentCache) : [];
|
|
|
-
|
|
|
- return {
|
|
|
+ const categories = readJson(SESSION.CATEGORY) || [];
|
|
|
+ const location = readJson(SESSION.LOCATION);
|
|
|
+ const cropDetailMap = getCropDetailMap();
|
|
|
+ const taskCache = readJson(SESSION.TASK);
|
|
|
+ const taskList = Array.isArray(taskCache)
|
|
|
+ ? taskCache
|
|
|
+ : [...(taskCache?.field || []), ...(taskCache?.fruit || [])];
|
|
|
+ const farmTaskId = Number(taskList[0]?.id);
|
|
|
+
|
|
|
+ const primaryCropId =
|
|
|
+ varietyList[0]?.categoryId ??
|
|
|
+ getCategoryForMachines()?.id ??
|
|
|
+ categories[0]?.id;
|
|
|
+ const primaryDetail = cropDetailMap[String(primaryCropId)] || {};
|
|
|
+
|
|
|
+ const cropBigType =
|
|
|
+ primaryDetail.crop_group ||
|
|
|
+ varietyList.find((item) => item.firstCrop)?.firstCrop ||
|
|
|
+ categories.find((item) => item.firstCrop)?.firstCrop ||
|
|
|
+ "";
|
|
|
+
|
|
|
+ const farmLocation =
|
|
|
+ location?.point ||
|
|
|
+ varietyList.find((item) => item.location)?.location ||
|
|
|
+ "";
|
|
|
+
|
|
|
+ const payload = {
|
|
|
user_name: baForm.name,
|
|
|
tel: baForm.phone,
|
|
|
- admin_id: getAdminId(),
|
|
|
+ admin_id: 94494,
|
|
|
+ // admin_id: getAdminId(),
|
|
|
user_id: getUserId(),
|
|
|
- county_code: getCountyCode(),
|
|
|
machine_id: equipmentList
|
|
|
.map((item) => Number(item.id))
|
|
|
.filter((id) => Number.isFinite(id)),
|
|
|
- crops: varietyList.map((item) => ({
|
|
|
- crop_type: item.categoryName,
|
|
|
- crop_id: Number(item.categoryId),
|
|
|
- variety_list: [String(item.id)],
|
|
|
- phenophase: getPhenophaseLabel(item),
|
|
|
- start_time: item?.startTime,
|
|
|
- plant_area: Number(item.area),
|
|
|
- point: item.location,
|
|
|
- })),
|
|
|
+ farm_location: farmLocation,
|
|
|
+ farm_address: location?.address || "",
|
|
|
+ crop_big_type: cropBigType,
|
|
|
+ crops: varietyList.map((item) => {
|
|
|
+ const detail = cropDetailMap[String(item.categoryId)] || {};
|
|
|
+ return {
|
|
|
+ crop_type: detail.crop_name || item.categoryName,
|
|
|
+ crop_code: detail.crop_code || "",
|
|
|
+ crop_id: Number(item.categoryId),
|
|
|
+ variety_list: String(item.varietyCode || item.id),
|
|
|
+ phenophase_code: String(item.phenophaseCode || item.phenologyId || ""),
|
|
|
+ };
|
|
|
+ }),
|
|
|
+ };
|
|
|
+
|
|
|
+ if (Number.isFinite(farmTaskId)) {
|
|
|
+ payload.farm_task_id = farmTaskId;
|
|
|
+ }
|
|
|
+
|
|
|
+ return payload;
|
|
|
+}
|
|
|
+
|
|
|
+/** 点击「完成」时用于初始化报告的参数(提交成功时缓存,清空 session 前) */
|
|
|
+const pendingReportParams = ref(null);
|
|
|
+
|
|
|
+/** 从当前 session 组装 report/generate 参数 */
|
|
|
+function buildReportParams() {
|
|
|
+ const baForm = readJson(SESSION.BA_FORM) || {};
|
|
|
+ const varietyList = getVarietyDraftList().filter((item) => item?.id != null && item.id !== "");
|
|
|
+ const cropDetailMap = getCropDetailMap();
|
|
|
+ const meta = readJson(SESSION.CROP_META) || {};
|
|
|
+ const first = varietyList[0];
|
|
|
+ const detail = first ? cropDetailMap[String(first.categoryId)] || {} : {};
|
|
|
+
|
|
|
+ return {
|
|
|
+ region: meta.city || meta.county || "",
|
|
|
+ crop: detail.crop_name || first?.categoryName || "",
|
|
|
+ variety: first?.name || "",
|
|
|
+ tel: baForm.phone || "",
|
|
|
};
|
|
|
}
|
|
|
|
|
|
+/** 跳转长势报告前生成报告 */
|
|
|
+async function generateGrowthReport(params) {
|
|
|
+ if (!params?.region || !params?.crop || !params?.variety || !params?.tel) {
|
|
|
+ console.warn("generateReport skipped: missing params", params);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ const raw = await VE_API.report.generateReport(params);
|
|
|
+ const res = Array.isArray(raw) ? raw[0] : raw;
|
|
|
+ if (res?.code !== 200) {
|
|
|
+ console.warn("generateReport failed", res?.msg || res?.message || res);
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ console.error("generateReport error", error);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
function clearEntrySession() {
|
|
|
Object.values(SESSION).forEach((key) => sessionStorage.removeItem(key));
|
|
|
}
|
|
|
@@ -528,13 +615,16 @@ async function submitEntry(includeEquipment = true) {
|
|
|
|
|
|
submitting.value = true;
|
|
|
try {
|
|
|
- const params = buildPlotPayload(includeEquipment);
|
|
|
- const res = await VE_API.entry.addPlotInfo(params);
|
|
|
- if (res.code === 200) {
|
|
|
+ const params = buildFarmPayload(includeEquipment);
|
|
|
+ const raw = await VE_API.entry.addFarmInfo(params);
|
|
|
+ // 部分接口返回 [body, httpStatus]
|
|
|
+ const res = Array.isArray(raw) ? raw[0] : raw;
|
|
|
+ if (res?.code === 200) {
|
|
|
+ pendingReportParams.value = buildReportParams();
|
|
|
clearEntrySession();
|
|
|
showSuccessPopup.value = true;
|
|
|
} else {
|
|
|
- ElMessage.error(res.msg || "提交失败,请稍后再试");
|
|
|
+ ElMessage.error(res?.msg || res?.message || "提交失败,请稍后再试");
|
|
|
}
|
|
|
} catch (error) {
|
|
|
console.error("entry submit failed", error);
|
|
|
@@ -544,7 +634,10 @@ async function submitEntry(includeEquipment = true) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-function handleComplete() {
|
|
|
+async function handleComplete() {
|
|
|
+ // 只触发初始化,不等待结果;新增已成功即可跳转
|
|
|
+ generateGrowthReport(pendingReportParams.value);
|
|
|
+ pendingReportParams.value = null;
|
|
|
showSuccessPopup.value = false;
|
|
|
emit("confirm");
|
|
|
}
|