Files
threeonecheck_web/unpackage/dist/dev/mp-weixin/components/hazard/HazardFormPopup.js

658 lines
23 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
const common_vendor = require("../../common/vendor.js");
const request_api = require("../../request/api.js");
const request_three_one_api_area = require("../../request/three_one_api/area.js");
const utils_upload = require("../../utils/upload.js");
const components_hazard_hazardForm = require("./hazardForm.js");
if (!Array) {
const _easycom_up_upload2 = common_vendor.resolveComponent("up-upload");
const _easycom_up_input2 = common_vendor.resolveComponent("up-input");
const _easycom_up_textarea2 = common_vendor.resolveComponent("up-textarea");
const _easycom_u_popup2 = common_vendor.resolveComponent("u-popup");
(_easycom_up_upload2 + _easycom_up_input2 + _easycom_up_textarea2 + _easycom_u_popup2)();
}
const _easycom_up_upload = () => "../../uni_modules/uview-plus/components/u-upload/u-upload.js";
const _easycom_up_input = () => "../../uni_modules/uview-plus/components/u-input/u-input.js";
const _easycom_up_textarea = () => "../../uni_modules/uview-plus/components/u-textarea/u-textarea.js";
const _easycom_u_popup = () => "../../uni_modules/uview-plus/components/u-popup/u-popup.js";
if (!Math) {
(_easycom_up_upload + _easycom_up_input + _easycom_up_textarea + _easycom_u_popup)();
}
const LOCATION_PICKER_DRAFT_KEY = "hazard_form_location_picker_draft";
const _sfc_main = {
__name: "HazardFormPopup",
props: {
show: {
type: Boolean,
default: false
},
title: {
type: String,
default: "填写隐患信息"
},
mode: {
type: String,
default: "collect"
},
modelValue: {
type: Object,
default: null
},
extraParams: {
type: Object,
default: () => ({})
},
showDraftBanner: {
type: Boolean,
default: false
},
bodyHeight: {
type: String,
default: ""
},
canvasId: {
type: String,
default: "hazardFormWatermarkCanvas"
}
},
emits: ["update:show", "update:modelValue", "success", "confirm", "clear-draft"],
setup(__props, { expose: __expose, emit: __emit }) {
const props = __props;
const emit = __emit;
const popupVisible = common_vendor.computed({
get: () => props.show,
set: (value) => emit("update:show", value)
});
const isApiSuccess = (res) => Number(res == null ? void 0 : res.code) === 0 || Number(res == null ? void 0 : res.code) === 200;
const bodyStyle = common_vendor.computed(() => props.bodyHeight ? { height: props.bodyHeight } : {});
const canvasWidth = common_vendor.ref(300);
const canvasHeight = common_vendor.ref(300);
const submitting = common_vendor.ref(false);
const aiAnalyzing = common_vendor.ref(false);
const syncingFromParent = common_vendor.ref(false);
const formData = common_vendor.reactive({
title: "",
level: 0,
source: 0,
description: "",
tagIndex: 0,
tagId: null,
regulationId: null,
regulationName: ""
});
const address = common_vendor.ref("");
const lng = common_vendor.ref(0);
const lat = common_vendor.ref(0);
const areaId = common_vendor.ref("");
const areaName = common_vendor.ref("");
const fileList = common_vendor.ref([]);
const tagOptions = common_vendor.ref([]);
const showLawPopup = common_vendor.ref(false);
const lawKeyword = common_vendor.ref("");
const selectedLawId = common_vendor.ref(null);
const selectedLawName = common_vendor.ref("");
const lawList = common_vendor.ref([]);
const lawLoading = common_vendor.ref(false);
const lawPageNum = common_vendor.ref(1);
const lawPageSize = common_vendor.ref(10);
const hasMoreLaw = common_vendor.ref(true);
const showAreaPicker = common_vendor.ref(false);
const areaList = common_vendor.ref([]);
const tempAreaId = common_vendor.ref("");
const choosingLocation = common_vendor.ref(false);
const suppressModelEmit = common_vendor.ref(false);
const uploadInstance = common_vendor.getCurrentInstance();
const { afterRead, deletePic } = utils_upload.createUploadListHandlers(fileList, {
watermark: {
canvasId: props.canvasId,
canvasWidthRef: canvasWidth,
canvasHeightRef: canvasHeight,
instance: uploadInstance
}
});
function getPayload() {
syncTagIdFromIndex();
return {
formData: {
title: formData.title,
level: formData.level,
source: formData.source,
description: formData.description,
tagIndex: formData.tagIndex,
tagId: formData.tagId,
regulationId: formData.regulationId,
regulationName: formData.regulationName
},
address: address.value,
lng: lng.value,
lat: lat.value,
areaId: areaId.value,
areaName: areaName.value,
fileList: fileList.value,
tagOptions: tagOptions.value
};
}
function applyPayload(payload) {
if (!payload)
return;
syncingFromParent.value = true;
const data = payload.formData || {};
formData.title = data.title || "";
formData.level = data.level ?? 0;
formData.source = data.source ?? 0;
formData.description = data.description || "";
formData.tagIndex = data.tagIndex ?? 0;
formData.tagId = data.tagId ?? null;
formData.regulationId = data.regulationId ?? null;
formData.regulationName = data.regulationName || "";
address.value = payload.address || "";
lng.value = payload.lng || 0;
lat.value = payload.lat || 0;
areaId.value = payload.areaId || "";
areaName.value = payload.areaName || "";
fileList.value = payload.fileList ? [...payload.fileList] : [];
selectedLawId.value = data.regulationId ?? null;
selectedLawName.value = data.regulationName || "";
syncingFromParent.value = false;
}
function hasPayloadContent(payload) {
if (!payload)
return false;
const data = payload.formData || {};
const files = payload.fileList || [];
return !!(data.title || data.description || data.regulationName || payload.address || files.length > 0);
}
function getLocationPickerDraftKey() {
var _a;
return ((_a = props.extraParams) == null ? void 0 : _a.locationDraftKey) || LOCATION_PICKER_DRAFT_KEY;
}
function saveLocationPickerDraft() {
const snapshot = getPayload();
try {
common_vendor.index.setStorageSync(getLocationPickerDraftKey(), snapshot);
} catch (error) {
common_vendor.index.__f__("error", "at components/hazard/HazardFormPopup.vue:400", "保存选点草稿失败:", error);
}
return snapshot;
}
function loadLocationPickerDraft() {
try {
const draft = common_vendor.index.getStorageSync(getLocationPickerDraftKey());
return draft && typeof draft === "object" ? draft : null;
} catch (error) {
return null;
}
}
function clearLocationPickerDraft() {
try {
common_vendor.index.removeStorageSync(getLocationPickerDraftKey());
} catch (error) {
common_vendor.index.__f__("error", "at components/hazard/HazardFormPopup.vue:418", "清除选点草稿失败:", error);
}
}
function restoreAfterLocationPicker(patch = {}) {
const draft = loadLocationPickerDraft();
if (!draft)
return false;
applyPayload({
...draft,
...patch,
formData: {
...draft.formData || {},
...patch.formData || {}
},
fileList: patch.fileList ?? draft.fileList ?? []
});
clearLocationPickerDraft();
return true;
}
function reopenPopupAfterLocation() {
common_vendor.nextTick$1(() => {
choosingLocation.value = false;
popupVisible.value = true;
emitModelValue();
});
}
function resetForm() {
applyPayload(components_hazard_hazardForm.createEmptyHazardPayload());
selectedLawId.value = null;
selectedLawName.value = "";
tempAreaId.value = "";
clearLocationPickerDraft();
}
function syncTagIdFromIndex() {
const selectedTag = tagOptions.value[formData.tagIndex];
formData.tagId = (selectedTag == null ? void 0 : selectedTag.id) ?? formData.tagId ?? null;
}
function onChooseSelect(field, index) {
formData[field] = Number(index);
if (field === "tagIndex") {
syncTagIdFromIndex();
}
emitModelValue();
}
function emitModelValue() {
if (suppressModelEmit.value || syncingFromParent.value || props.modelValue === null)
return;
if (props.mode === "collect" || props.mode === "direct")
return;
emit("update:modelValue", getPayload());
}
function closePopup() {
popupVisible.value = false;
}
function handleClose() {
closePopup();
}
function validateForm() {
if (fileList.value.length === 0) {
common_vendor.index.showToast({ title: "请上传隐患图片", icon: "none" });
return false;
}
if (!formData.title) {
common_vendor.index.showToast({ title: "请输入隐患标题", icon: "none" });
return false;
}
if (!address.value) {
common_vendor.index.showToast({ title: "请输入隐患位置", icon: "none" });
return false;
}
if (!formData.description) {
common_vendor.index.showToast({ title: "请输入隐患描述", icon: "none" });
return false;
}
return true;
}
async function handleConfirm() {
if (!validateForm())
return;
const payload = getPayload();
if (props.mode === "collect") {
if (props.modelValue !== null) {
emit("update:modelValue", payload);
}
emit("confirm", payload);
closePopup();
common_vendor.index.showToast({ title: "隐患信息已暂存", icon: "success" });
return;
}
submitting.value = true;
try {
const params = components_hazard_hazardForm.buildHiddenDangerParams(payload, props.extraParams);
const res = await request_api.addHiddenDanger(params);
if (isApiSuccess(res)) {
suppressModelEmit.value = true;
closePopup();
emit("success", { ...payload, hazardId: res.data });
await common_vendor.nextTick$1();
resetForm();
common_vendor.index.showToast({ title: res.msg || "新增成功", icon: "success" });
} else {
common_vendor.index.showToast({ title: res.msg || "新增失败", icon: "none" });
}
} catch (error) {
common_vendor.index.__f__("error", "at components/hazard/HazardFormPopup.vue:534", error);
common_vendor.index.showToast({ title: "请求失败", icon: "none" });
} finally {
suppressModelEmit.value = false;
submitting.value = false;
}
}
function openLawPopup() {
showLawPopup.value = true;
if (lawList.value.length === 0) {
fetchRegulationList();
}
}
async function fetchRegulationList(isLoadMore = false) {
var _a;
if (lawLoading.value)
return;
lawLoading.value = true;
try {
const params = {
pageNum: lawPageNum.value,
pageSize: lawPageSize.value,
status: 1
};
if ((_a = lawKeyword.value) == null ? void 0 : _a.trim()) {
params.keyword = lawKeyword.value.trim();
}
const res = await request_api.getRegulationList(params);
if (res.code === 0) {
const records = res.data.records || res.data || [];
lawList.value = isLoadMore ? [...lawList.value, ...records] : records;
const total = res.data.total || 0;
hasMoreLaw.value = lawList.value.length < total;
}
} catch (error) {
common_vendor.index.__f__("error", "at components/hazard/HazardFormPopup.vue:569", "获取法规列表失败:", error);
} finally {
lawLoading.value = false;
}
}
function searchRegulation() {
lawPageNum.value = 1;
lawList.value = [];
hasMoreLaw.value = true;
fetchRegulationList();
}
function loadMoreLaw() {
if (!hasMoreLaw.value || lawLoading.value)
return;
lawPageNum.value++;
fetchRegulationList(true);
}
function selectLaw(item) {
selectedLawId.value = item.id;
selectedLawName.value = item.depict || item.keyword || "";
}
function confirmLaw() {
if (selectedLawId.value) {
formData.regulationId = selectedLawId.value;
formData.regulationName = selectedLawName.value;
}
showLawPopup.value = false;
emitModelValue();
}
function openAreaPicker() {
tempAreaId.value = areaId.value;
showAreaPicker.value = true;
}
async function fetchAreaList() {
var _a;
try {
const res = await request_three_one_api_area.getAreaList();
if (res.code === 0 && ((_a = res.data) == null ? void 0 : _a.records)) {
areaList.value = res.data.records;
}
} catch (error) {
common_vendor.index.__f__("error", "at components/hazard/HazardFormPopup.vue:614", "获取区域列表失败:", error);
}
}
function confirmAreaSelect() {
if (tempAreaId.value) {
areaId.value = tempAreaId.value;
const selected = areaList.value.find((item) => item.id === tempAreaId.value);
areaName.value = selected ? selected.name : "";
}
showAreaPicker.value = false;
emitModelValue();
}
function chooseLocation() {
choosingLocation.value = true;
saveLocationPickerDraft();
closePopup();
setTimeout(() => {
common_vendor.index.getLocation({
type: "gcj02",
success: (locationRes) => {
openChooseLocation(locationRes.latitude, locationRes.longitude);
},
fail: () => {
openChooseLocation();
}
});
}, 300);
}
function openChooseLocation(latitude, longitude) {
const options = {};
if (latitude != null && longitude != null) {
options.latitude = latitude;
options.longitude = longitude;
}
common_vendor.index.chooseLocation({
...options,
success: (res) => {
restoreAfterLocationPicker({
address: res.address + (res.name ? `(${res.name})` : ""),
lng: res.longitude,
lat: res.latitude
});
reopenPopupAfterLocation();
},
fail: (err) => {
restoreAfterLocationPicker();
reopenPopupAfterLocation();
if (err.errMsg && err.errMsg.indexOf("cancel") === -1) {
common_vendor.index.showToast({ title: "选择位置失败", icon: "none" });
}
}
});
}
async function handleAiAnalyze() {
const imageFiles = fileList.value.filter((f) => {
return f.status === "success" && f.url.toLowerCase().match(/\.(jpg|jpeg|png|gif|bmp|webp)$/);
});
if (imageFiles.length === 0) {
common_vendor.index.showToast({ title: "请先上传隐患图片", icon: "none" });
return;
}
aiAnalyzing.value = true;
try {
const analyzeRes = await request_api.analyzeHazardImage({ imageUrl: imageFiles[0].url });
if (analyzeRes.code === 0 && analyzeRes.data) {
const aiData = analyzeRes.data;
if (aiData.title)
formData.title = aiData.title;
if (aiData.description)
formData.description = aiData.description;
if (aiData.level) {
const levelIndex = components_hazard_hazardForm.AI_LEVEL_MAP[aiData.level];
if (levelIndex !== void 0) {
formData.level = levelIndex;
}
}
emitModelValue();
common_vendor.index.showToast({ title: "AI分析完成已自动填充", icon: "success", duration: 2e3 });
} else {
common_vendor.index.showToast({ title: analyzeRes.msg || "AI分析失败", icon: "none" });
}
} catch (error) {
common_vendor.index.__f__("error", "at components/hazard/HazardFormPopup.vue:699", "AI分析接口调用失败:", error);
common_vendor.index.showToast({ title: "AI分析失败请重试", icon: "none" });
} finally {
aiAnalyzing.value = false;
}
}
async function fetchTagOptions() {
try {
const res = await request_api.getHiddenDangerLabelList();
if (res.code === 0) {
tagOptions.value = res.data.map((item) => ({
id: item.id,
title: item.name
}));
syncTagIdFromIndex();
}
} catch (error) {
common_vendor.index.__f__("error", "at components/hazard/HazardFormPopup.vue:717", "获取标签列表失败:", error);
}
}
common_vendor.watch(
() => props.modelValue,
(val) => {
if (props.mode === "direct")
return;
if (hasPayloadContent(val)) {
applyPayload(val);
}
},
{ deep: true, immediate: true }
);
common_vendor.watch(
() => props.show,
(val) => {
if (val && areaList.value.length === 0) {
fetchAreaList();
}
if (!val)
return;
if (choosingLocation.value) {
return;
}
const locationDraft = loadLocationPickerDraft();
if (locationDraft) {
applyPayload(locationDraft);
clearLocationPickerDraft();
return;
}
if (hasPayloadContent(props.modelValue)) {
applyPayload(props.modelValue);
}
}
);
fetchTagOptions();
fetchAreaList();
__expose({
getPayload,
applyPayload,
resetForm,
validateForm
});
return (_ctx, _cache) => {
return common_vendor.e({
a: common_vendor.t(__props.title),
b: common_vendor.o(handleClose),
c: __props.showDraftBanner
}, __props.showDraftBanner ? {
d: common_vendor.o(($event) => emit("clear-draft"))
} : {}, {
e: common_vendor.f(common_vendor.unref(components_hazard_hazardForm.SOURCE_OPTIONS), (item, index, i0) => {
return {
a: common_vendor.t(item.title),
b: item.id,
c: formData.source === index ? 1 : "",
d: common_vendor.o(($event) => onChooseSelect("source", index), item.id)
};
}),
f: common_vendor.o(common_vendor.unref(afterRead)),
g: common_vendor.o(common_vendor.unref(deletePic)),
h: common_vendor.p({
fileList: fileList.value,
name: "1",
multiple: true,
imageMode: "aspectFill",
maxCount: 10
}),
i: __props.canvasId,
j: canvasWidth.value,
k: canvasHeight.value,
l: canvasWidth.value + "px",
m: canvasHeight.value + "px",
n: !aiAnalyzing.value
}, !aiAnalyzing.value ? {} : {}, {
o: common_vendor.t(aiAnalyzing.value ? "AI识别中..." : "AI 识别隐患"),
p: aiAnalyzing.value,
q: aiAnalyzing.value,
r: common_vendor.o(handleAiAnalyze),
s: common_vendor.o(($event) => formData.title = $event),
t: common_vendor.p({
placeholder: "请输入内容",
border: "surround",
modelValue: formData.title
}),
v: common_vendor.f(common_vendor.unref(components_hazard_hazardForm.LEVEL_OPTIONS), (item, index, i0) => {
return {
a: common_vendor.t(item.title),
b: item.id,
c: formData.level === index ? 1 : "",
d: common_vendor.o(($event) => onChooseSelect("level", index), item.id)
};
}),
w: common_vendor.o(($event) => address.value = $event),
x: common_vendor.p({
placeholder: "请输入地址",
border: "surround",
modelValue: address.value
}),
y: common_vendor.o(chooseLocation),
z: common_vendor.t(formData.regulationName || "请选择法律依据"),
A: !formData.regulationName ? 1 : "",
B: common_vendor.o(openLawPopup),
C: common_vendor.t(areaName.value || "请选择隐患区域"),
D: !areaName.value ? 1 : "",
E: common_vendor.o(openAreaPicker),
F: common_vendor.o(($event) => formData.description = $event),
G: common_vendor.p({
placeholder: "请输入内容",
modelValue: formData.description
}),
H: common_vendor.f(tagOptions.value, (item, index, i0) => {
return {
a: common_vendor.t(item.title),
b: item.id,
c: formData.tagIndex === index ? 1 : "",
d: common_vendor.o(($event) => onChooseSelect("tagIndex", index), item.id)
};
}),
I: common_vendor.s(bodyStyle.value),
J: common_vendor.o(handleClose),
K: submitting.value,
L: common_vendor.o(handleConfirm),
M: common_vendor.o(handleClose),
N: common_vendor.o(($event) => popupVisible.value = $event),
O: common_vendor.p({
mode: "center",
round: "20",
["safe-area-inset-bottom"]: false,
show: popupVisible.value
}),
P: common_vendor.o(($event) => showLawPopup.value = false),
Q: common_vendor.o(searchRegulation),
R: lawKeyword.value,
S: common_vendor.o(($event) => lawKeyword.value = $event.detail.value),
T: common_vendor.o(searchRegulation),
U: lawLoading.value && lawList.value.length === 0
}, lawLoading.value && lawList.value.length === 0 ? {} : !lawLoading.value && lawList.value.length === 0 ? {} : common_vendor.e({
W: common_vendor.f(lawList.value, (item, k0, i0) => {
return {
a: common_vendor.t(item.depict),
b: common_vendor.t(item.legalBasis),
c: selectedLawId.value === item.id ? 1 : "",
d: item.id,
e: common_vendor.o(($event) => selectLaw(item), item.id)
};
}),
X: lawLoading.value
}, lawLoading.value ? {} : {}), {
V: !lawLoading.value && lawList.value.length === 0,
Y: common_vendor.o(loadMoreLaw),
Z: common_vendor.o(($event) => showLawPopup.value = false),
aa: common_vendor.o(confirmLaw),
ab: common_vendor.o(($event) => showLawPopup.value = false),
ac: common_vendor.p({
show: showLawPopup.value,
mode: "center",
round: "20",
["safe-area-inset-bottom"]: false
}),
ad: common_vendor.o(($event) => showAreaPicker.value = false),
ae: common_vendor.o(confirmAreaSelect),
af: common_vendor.f(areaList.value, (item, k0, i0) => {
return common_vendor.e({
a: item.color,
b: common_vendor.t(item.name),
c: tempAreaId.value === item.id
}, tempAreaId.value === item.id ? {} : {}, {
d: item.id,
e: tempAreaId.value === item.id ? 1 : "",
f: common_vendor.o(($event) => tempAreaId.value = item.id, item.id)
});
}),
ag: areaList.value.length === 0
}, areaList.value.length === 0 ? {} : {}, {
ah: common_vendor.o(($event) => showAreaPicker.value = false),
ai: common_vendor.p({
show: showAreaPicker.value,
mode: "bottom",
round: "20"
})
});
};
}
};
const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-fd540320"]]);
wx.createComponent(Component);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/components/hazard/HazardFormPopup.js.map