| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- <template>
- <popup class="interact-popup" v-model:show="show" closeable :close-on-click-overlay="false" @closed="handleClosed">
- <div class="interact-header">
- <div class="interact-title">{{ currentData.farmWorkName || currentData.name }}</div>
- </div>
- <div class="interact-form">
- <div class="form-item">
- <div class="form-label">
- <span class="required">*</span>
- 请选择互动时间
- </div>
- <div class="form-input-wrapper">
- <el-select v-model="formData.phenologyId" size="large" placeholder="请选择物候期" :editable="false">
- <el-option
- v-for="item in phenologyList"
- :key="item.id"
- :label="item.name"
- :value="item.id"
- ></el-option>
- </el-select>
- </div>
- </div>
- <div class="form-item">
- <div class="form-label">
- <span class="required">*</span>
- 请选择强制触发互动时间
- </div>
- <div class="form-input-wrapper">
- <el-date-picker
- v-model="formData.interactionTime"
- size="large"
- style="width: 100%"
- type="date"
- placeholder="请选择日期"
- :editable="false"
- />
- </div>
- </div>
- <div class="form-item">
- <div class="form-label">
- <span class="required">*</span>
- 请设置互动问题
- </div>
- <el-input
- v-model="formData.interactionQuestion"
- type="textarea"
- :rows="4"
- placeholder="请设置互动问题"
- class="question-textarea"
- />
- </div>
- </div>
- <div class="interact-buttons">
- <div class="btn-delete" @click="handleDeleteInteract">删除互动</div>
- <div class="btn-save" :class="{ disabled: isSaving }" @click="handleSaveInteract">
- {{ saveButtonText }}
- </div>
- </div>
- </popup>
- </template>
- <script setup>
- import { Popup } from "vant";
- import { ref, computed } from "vue";
- import { ElMessage, ElMessageBox } from "element-plus";
- // Emits
- const emit = defineEmits(["handleSaveSuccess", "handleDeleteInteract"]);
- // 响应式数据
- const show = ref(false);
- const arrangeId = ref(null);
- const isSaving = ref(false);
- const formData = ref({
- phenologyId: "",
- interactionTime: "",
- interactionQuestion: "",
- });
- // 计算属性
- const saveButtonText = computed(() => (isSaving.value ? "保存中..." : "保存修改"));
- // 工具函数
- const resetInteractData = () => {
- formData.value = {
- phenologyId: "",
- interactionTime: "",
- interactionQuestion: "",
- };
- };
- // 验证函数
- const validateInteractForm = () => {
- if (!formData.value.phenologyId) {
- ElMessage.warning("请选择互动时间");
- return false;
- }
- if (!formData.value.interactionTime) {
- ElMessage.warning("请选择强制触发互动时间");
- return false;
- }
- if (!formData.value.interactionQuestion?.trim()) {
- ElMessage.warning("请设置互动问题");
- return false;
- }
- return true;
- };
- const phenologyList = ref(null);
- const getPhenologyList = async (containerSpaceTimeId) => {
- if (!containerSpaceTimeId) {
- phenologyList.value = [];
- return;
- }
- const res = await VE_API.monitor.listPhenology({ containerSpaceTimeId });
- if (res.code === 0) {
- phenologyList.value = res.data || [];
- }
- };
- const getFarmWorkArrangeDetail = async (id) => {
- const { data, code } = await VE_API.farm.getFarmWorkArrangeDetail({ id });
- if(code === 0) {
- formData.value = {
- phenologyId: data.phenologyId,
- interactionTime: data.interactionTime,
- interactionQuestion: data.interactionQuestion || "",
- };
- }
- };
- const currentData = ref(null);
- // 显示弹窗方法
- const showPopup = async (data) => {
- // 重置数据
- resetInteractData();
- await getPhenologyList(data.containerSpaceTimeId);
- await getFarmWorkArrangeDetail(data.id);
- // 设置数据
- currentData.value = data;
- isSaving.value = false;
- show.value = true;
- };
- // 事件处理函数
- const handleDeleteInteract = () => {
- ElMessageBox.confirm("确定要删除该互动设置吗?", "提示", {
- confirmButtonText: "确定",
- cancelButtonText: "取消",
- type: "warning",
- })
- .then(() => {
- emit("handleDeleteInteract", { arrangeId: arrangeId.value });
- show.value = false;
- ElMessage.success("删除成功");
- })
- .catch(() => {
- // 用户取消,不做任何操作
- });
- };
- function formatDate(date) {
- // 如果已经是字符串格式 YYYY-MM-DD,直接返回
- if (typeof date === "string" && /^\d{4}-\d{2}-\d{2}$/.test(date)) {
- return date;
- }
- // 如果是 Date 对象,进行转换
- if (date instanceof Date) {
- let year = date.getFullYear();
- let month = String(date.getMonth() + 1).padStart(2, "0");
- let day = String(date.getDate()).padStart(2, "0");
- return `${year}-${month}-${day}`;
- }
- // 其他情况返回原值
- return date;
- }
- const handleSaveInteract = () => {
- if (isSaving.value || !validateInteractForm()) return;
- isSaving.value = true;
- const paramsObj = {
- id: currentData.value.id,
- ...formData.value,
- interactionTime: formatDate(formData.value.interactionTime),
- };
- VE_API.monitor
- .updateFarmWorkArrange(paramsObj)
- .then((res) => {
- if (res.code === 0) {
- ElMessage.success("保存成功");
- show.value = false;
- emit("handleSaveSuccess", paramsObj);
- } else {
- ElMessage.error(res.message || "保存失败");
- }
- })
- .catch((error) => {
- console.error("保存互动设置失败:", error);
- ElMessage.error("保存失败,请重试");
- })
- .finally(() => {
- isSaving.value = false;
- });
- };
- const handleClosed = () => {
- resetInteractData();
- };
- // 暴露方法
- defineExpose({
- showPopup,
- });
- </script>
- <style lang="scss" scoped>
- .interact-popup {
- width: 90%;
- box-sizing: border-box;
- background: #ffffff;
- padding: 0;
- border-radius: 10px;
- ::v-deep {
- .van-popup__close-icon {
- color: #000;
- }
- }
- .interact-header {
- background: linear-gradient(180deg, #d1ebff 0%, #ffffff 100%);
- padding: 20px 18px;
- text-align: center;
- border-radius: 10px 10px 0 0;
- .interact-title {
- font-size: 18px;
- font-weight: 600;
- color: #000;
- }
- }
- .interact-form {
- padding: 0 18px 20px;
- .form-item {
- margin-bottom: 16px;
- &:last-child {
- margin-bottom: 0;
- }
- .form-label {
- color: #000;
- font-weight: 500;
- margin-bottom: 12px;
- display: flex;
- align-items: center;
- .required {
- color: #ff4d4f;
- margin-right: 4px;
- }
- }
- .form-input-wrapper {
- position: relative;
- ::v-deep {
- .el-input__inner {
- caret-color: transparent;
- padding-right: 40px;
- }
- .el-input__suffix {
- display: none;
- }
- }
- .time-icon {
- position: absolute;
- right: 12px;
- top: 50%;
- transform: translateY(-50%);
- color: #909399;
- pointer-events: none;
- z-index: 1;
- font-size: 16px;
- }
- }
- .question-textarea {
- ::v-deep {
- .el-textarea__inner {
- resize: none;
- line-height: 1.5;
- min-height: 80px;
- }
- }
- }
- }
- }
- .interact-buttons {
- display: flex;
- gap: 12px;
- padding: 0 18px 20px;
- .btn-delete,
- .btn-save {
- flex: 1;
- padding: 8px;
- border-radius: 25px;
- font-size: 16px;
- text-align: center;
- cursor: pointer;
- transition: all 0.3s;
- user-select: none;
- }
- .btn-delete {
- background: #ffffff;
- border: 1px solid #ff4d4f;
- color: #ff4d4f;
- &:hover {
- background: #fff5f5;
- }
- &:active {
- opacity: 0.8;
- transform: scale(0.98);
- }
- }
- .btn-save {
- background: #2199f8;
- color: #fff;
- border: none;
- &:hover:not(.disabled) {
- background: #1a8ae6;
- }
- &:active:not(.disabled) {
- opacity: 0.9;
- transform: scale(0.98);
- }
- &.disabled {
- opacity: 0.6;
- cursor: not-allowed;
- pointer-events: none;
- }
- }
- }
- }
- </style>
|