interactPopup.vue 9.2 KB

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