interactPopup.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <template>
  2. <popup class="interact-popup" v-model:show="show" closeable :close-on-click-overlay="false" @closed="handleClosed">
  3. <div class="interact-header">
  4. <div class="interact-title">{{ currentData.farmWorkName || currentData.name }}</div>
  5. </div>
  6. <div class="interact-form">
  7. <div class="form-item">
  8. <div class="form-label">
  9. <span class="required">*</span>
  10. 请选择互动时间
  11. </div>
  12. <div class="form-input-wrapper">
  13. <el-select v-model="formData.phenologyId" size="large" placeholder="请选择物候期" :editable="false">
  14. <el-option
  15. v-for="item in phenologyList"
  16. :key="item.id"
  17. :label="item.name"
  18. :value="item.id"
  19. ></el-option>
  20. </el-select>
  21. </div>
  22. </div>
  23. <div class="form-item">
  24. <div class="form-label">
  25. <span class="required">*</span>
  26. 请选择强制触发互动时间
  27. </div>
  28. <div class="form-input-wrapper">
  29. <el-date-picker
  30. v-model="formData.interactionTime"
  31. size="large"
  32. style="width: 100%"
  33. type="date"
  34. placeholder="请选择日期"
  35. :editable="false"
  36. />
  37. </div>
  38. </div>
  39. <div class="form-item">
  40. <div class="form-label">
  41. <span class="required">*</span>
  42. 请设置互动问题
  43. </div>
  44. <el-input
  45. v-model="formData.interactionQuestion"
  46. type="textarea"
  47. :rows="4"
  48. placeholder="请设置互动问题"
  49. class="question-textarea"
  50. />
  51. </div>
  52. </div>
  53. <div class="interact-buttons">
  54. <div class="btn-delete" @click="handleDeleteInteract">删除互动</div>
  55. <div class="btn-save" :class="{ disabled: isSaving }" @click="handleSaveInteract">
  56. {{ saveButtonText }}
  57. </div>
  58. </div>
  59. </popup>
  60. </template>
  61. <script setup>
  62. import { Popup } from "vant";
  63. import { ref, computed } from "vue";
  64. import { ElMessage, ElMessageBox } from "element-plus";
  65. // Emits
  66. const emit = defineEmits(["handleSaveSuccess", "handleDeleteInteract"]);
  67. // 响应式数据
  68. const show = ref(false);
  69. const arrangeId = ref(null);
  70. const isSaving = ref(false);
  71. const formData = ref({
  72. phenologyId: "",
  73. interactionTime: "",
  74. interactionQuestion: "",
  75. });
  76. // 计算属性
  77. const saveButtonText = computed(() => (isSaving.value ? "保存中..." : "保存修改"));
  78. // 工具函数
  79. const resetInteractData = () => {
  80. formData.value = {
  81. phenologyId: "",
  82. interactionTime: "",
  83. interactionQuestion: "",
  84. };
  85. };
  86. // 验证函数
  87. const validateInteractForm = () => {
  88. if (!formData.value.phenologyId) {
  89. ElMessage.warning("请选择互动时间");
  90. return false;
  91. }
  92. if (!formData.value.interactionTime) {
  93. ElMessage.warning("请选择强制触发互动时间");
  94. return false;
  95. }
  96. if (!formData.value.interactionQuestion?.trim()) {
  97. ElMessage.warning("请设置互动问题");
  98. return false;
  99. }
  100. return true;
  101. };
  102. const phenologyList = ref(null);
  103. const getPhenologyList = async (containerSpaceTimeId) => {
  104. if (!containerSpaceTimeId) {
  105. phenologyList.value = [];
  106. return;
  107. }
  108. const res = await VE_API.monitor.listPhenology({ containerSpaceTimeId });
  109. if (res.code === 0) {
  110. phenologyList.value = res.data || [];
  111. }
  112. };
  113. const getFarmWorkArrangeDetail = async (id) => {
  114. const { data, code } = await VE_API.farm.getFarmWorkArrangeDetail({ id });
  115. if(code === 0) {
  116. formData.value = {
  117. phenologyId: data.phenologyId,
  118. interactionTime: data.interactionTime,
  119. interactionQuestion: data.interactionQuestion || "",
  120. };
  121. }
  122. };
  123. const currentData = ref(null);
  124. // 显示弹窗方法
  125. const showPopup = async (data) => {
  126. // 重置数据
  127. resetInteractData();
  128. await getPhenologyList(data.containerSpaceTimeId);
  129. await getFarmWorkArrangeDetail(data.id);
  130. // 设置数据
  131. currentData.value = data;
  132. isSaving.value = false;
  133. show.value = true;
  134. };
  135. // 事件处理函数
  136. const handleDeleteInteract = () => {
  137. ElMessageBox.confirm("确定要删除该互动设置吗?", "提示", {
  138. confirmButtonText: "确定",
  139. cancelButtonText: "取消",
  140. type: "warning",
  141. })
  142. .then(() => {
  143. emit("handleDeleteInteract", { arrangeId: arrangeId.value });
  144. show.value = false;
  145. ElMessage.success("删除成功");
  146. })
  147. .catch(() => {
  148. // 用户取消,不做任何操作
  149. });
  150. };
  151. function formatDate(date) {
  152. // 如果已经是字符串格式 YYYY-MM-DD,直接返回
  153. if (typeof date === "string" && /^\d{4}-\d{2}-\d{2}$/.test(date)) {
  154. return date;
  155. }
  156. // 如果是 Date 对象,进行转换
  157. if (date instanceof Date) {
  158. let year = date.getFullYear();
  159. let month = String(date.getMonth() + 1).padStart(2, "0");
  160. let day = String(date.getDate()).padStart(2, "0");
  161. return `${year}-${month}-${day}`;
  162. }
  163. // 其他情况返回原值
  164. return date;
  165. }
  166. const handleSaveInteract = () => {
  167. if (isSaving.value || !validateInteractForm()) return;
  168. isSaving.value = true;
  169. const paramsObj = {
  170. id: currentData.value.id,
  171. ...formData.value,
  172. interactionTime: formatDate(formData.value.interactionTime),
  173. };
  174. VE_API.monitor
  175. .updateFarmWorkArrange(paramsObj)
  176. .then((res) => {
  177. if (res.code === 0) {
  178. ElMessage.success("保存成功");
  179. show.value = false;
  180. emit("handleSaveSuccess", paramsObj);
  181. } else {
  182. ElMessage.error(res.message || "保存失败");
  183. }
  184. })
  185. .catch((error) => {
  186. console.error("保存互动设置失败:", error);
  187. ElMessage.error("保存失败,请重试");
  188. })
  189. .finally(() => {
  190. isSaving.value = false;
  191. });
  192. };
  193. const handleClosed = () => {
  194. resetInteractData();
  195. };
  196. // 暴露方法
  197. defineExpose({
  198. showPopup,
  199. });
  200. </script>
  201. <style lang="scss" scoped>
  202. .interact-popup {
  203. width: 90%;
  204. box-sizing: border-box;
  205. background: #ffffff;
  206. padding: 0;
  207. border-radius: 10px;
  208. ::v-deep {
  209. .van-popup__close-icon {
  210. color: #000;
  211. }
  212. }
  213. .interact-header {
  214. background: linear-gradient(180deg, #d1ebff 0%, #ffffff 100%);
  215. padding: 20px 18px;
  216. text-align: center;
  217. border-radius: 10px 10px 0 0;
  218. .interact-title {
  219. font-size: 18px;
  220. font-weight: 600;
  221. color: #000;
  222. }
  223. }
  224. .interact-form {
  225. padding: 0 18px 20px;
  226. .form-item {
  227. margin-bottom: 16px;
  228. &:last-child {
  229. margin-bottom: 0;
  230. }
  231. .form-label {
  232. color: #000;
  233. font-weight: 500;
  234. margin-bottom: 12px;
  235. display: flex;
  236. align-items: center;
  237. .required {
  238. color: #ff4d4f;
  239. margin-right: 4px;
  240. }
  241. }
  242. .form-input-wrapper {
  243. position: relative;
  244. ::v-deep {
  245. .el-input__inner {
  246. caret-color: transparent;
  247. padding-right: 40px;
  248. }
  249. .el-input__suffix {
  250. display: none;
  251. }
  252. }
  253. .time-icon {
  254. position: absolute;
  255. right: 12px;
  256. top: 50%;
  257. transform: translateY(-50%);
  258. color: #909399;
  259. pointer-events: none;
  260. z-index: 1;
  261. font-size: 16px;
  262. }
  263. }
  264. .question-textarea {
  265. ::v-deep {
  266. .el-textarea__inner {
  267. resize: none;
  268. line-height: 1.5;
  269. min-height: 80px;
  270. }
  271. }
  272. }
  273. }
  274. }
  275. .interact-buttons {
  276. display: flex;
  277. gap: 12px;
  278. padding: 0 18px 20px;
  279. .btn-delete,
  280. .btn-save {
  281. flex: 1;
  282. padding: 8px;
  283. border-radius: 25px;
  284. font-size: 16px;
  285. text-align: center;
  286. cursor: pointer;
  287. transition: all 0.3s;
  288. user-select: none;
  289. }
  290. .btn-delete {
  291. background: #ffffff;
  292. border: 1px solid #ff4d4f;
  293. color: #ff4d4f;
  294. &:hover {
  295. background: #fff5f5;
  296. }
  297. &:active {
  298. opacity: 0.8;
  299. transform: scale(0.98);
  300. }
  301. }
  302. .btn-save {
  303. background: #2199f8;
  304. color: #fff;
  305. border: none;
  306. &:hover:not(.disabled) {
  307. background: #1a8ae6;
  308. }
  309. &:active:not(.disabled) {
  310. opacity: 0.9;
  311. transform: scale(0.98);
  312. }
  313. &.disabled {
  314. opacity: 0.6;
  315. cursor: not-allowed;
  316. pointer-events: none;
  317. }
  318. }
  319. }
  320. }
  321. </style>