regionAlbums.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <template>
  2. <div class="region-albums">
  3. <div class="region-albums__hero" :style="heroStyle">
  4. <div class="region-albums__hero-mask" :class="{ 'is-light': isEmpty }"></div>
  5. <div class="region-albums__nav" @click="goBack">
  6. <el-icon class="region-albums__back-icon" color="#fff" :size="18">
  7. <ArrowLeftBold />
  8. </el-icon>
  9. </div>
  10. <div class="region-albums__hero-info">
  11. <span class="region-albums__title">{{ regionName }}</span>
  12. <span class="region-albums__count">{{ t("agriFile.photoCount", { count: totalCount }) }}</span>
  13. </div>
  14. </div>
  15. <div class="region-albums__panel" v-loading="loading">
  16. <div v-if="!loading && isEmpty" class="region-albums__empty">
  17. <img
  18. class="region-albums__empty-icon"
  19. src="@/assets/img/agricultural/empty.png"
  20. alt=""
  21. />
  22. <div class="region-albums__empty-text">{{ t("agriFile.emptyAlbumTip") }}</div>
  23. </div>
  24. <div v-else class="region-albums__album-list">
  25. <div
  26. v-for="group in dateGroups"
  27. :key="group.date"
  28. class="album-group"
  29. >
  30. <div class="album-group__header">
  31. <div class="album-group__date">{{ group.dateLabel }}</div>
  32. <div v-if="group.remark" class="album-group__remark">{{ group.remark }}</div>
  33. </div>
  34. <div class="album-group__grid">
  35. <div
  36. v-for="(item, index) in group.list"
  37. :key="item.id || `${group.date}-${index}`"
  38. class="album-group__item"
  39. @click="previewImage(group, index)"
  40. >
  41. <img class="album-group__img" :src="item.url" alt="" loading="lazy" />
  42. </div>
  43. </div>
  44. </div>
  45. </div>
  46. </div>
  47. <div class="region-albums__fab" @click="handleAdd">
  48. <el-icon :size="28" color="#fff"><Plus /></el-icon>
  49. </div>
  50. <album-upload-popup
  51. v-model:show="showUploadPopup"
  52. @confirm="handleUploadConfirm"
  53. />
  54. <ImagePreviewPopup
  55. v-model:show="showPreview"
  56. :images="previewImages"
  57. :start-index="previewIndex"
  58. :show-remark="true"
  59. @remark-change="handleRemarkChange"
  60. @remark-confirm="handleRemarkConfirm"
  61. />
  62. </div>
  63. </template>
  64. <script setup>
  65. import { computed, onActivated, onDeactivated, onMounted, ref } from "vue";
  66. import { useRoute, useRouter } from "vue-router";
  67. import { ElMessage } from "element-plus";
  68. import { ArrowLeftBold, Plus } from "@element-plus/icons-vue";
  69. import { base_img_url2, resize_300 } from "@/api/config";
  70. import { useI18n } from "@/i18n";
  71. import ImagePreviewPopup from "@/components/popup/ImagePreviewPopup.vue";
  72. import albumUploadPopup from "../components/albumUploadPopup.vue";
  73. const { t } = useI18n();
  74. const route = useRoute();
  75. const router = useRouter();
  76. const loading = ref(false);
  77. const farmImgs = ref([]);
  78. const imageCount = ref(0);
  79. const zoneNameFromApi = ref("");
  80. const imageList = ref([]);
  81. const showPreview = ref(false);
  82. const previewImages = ref([]);
  83. const previewIndex = ref(0);
  84. const showUploadPopup = ref(false);
  85. const farmId = computed(() => route.query.farm_id || "");
  86. const zoneId = computed(() => route.query.zone_id || "");
  87. const getSelectedFarm = () => {
  88. try {
  89. return JSON.parse(localStorage.getItem("selectedFarmData") || "{}");
  90. } catch {
  91. return {};
  92. }
  93. };
  94. const regionName = computed(
  95. () => zoneNameFromApi.value || route.query.name || t("agriFile.zoneOne")
  96. );
  97. const totalCount = computed(() => imageCount.value);
  98. const heroCover = computed(() => {
  99. const first = imageList.value[0];
  100. return first?.url || "";
  101. });
  102. const heroStyle = computed(() => {
  103. if (!heroCover.value) {
  104. return {
  105. background: "linear-gradient(180deg, #8ec8f5 0%, #5aadeb 55%, #3d9ae0 100%)",
  106. };
  107. }
  108. return {
  109. backgroundImage: `url(${heroCover.value})`,
  110. };
  111. });
  112. const formatDateLabel = (dateStr) => {
  113. const raw = String(dateStr || "").slice(0, 10);
  114. const parts = raw.split("-");
  115. if (parts.length < 3) return raw;
  116. return `${parts[0]}年${parts[1]}月${parts[2]}日`;
  117. };
  118. const resolveImageUrl = (path, compact = false) => {
  119. if (!path) return "";
  120. const text = String(path).trim();
  121. if (!text) return "";
  122. if (/^https?:\/\//i.test(text)) return text;
  123. return `${base_img_url2}${text.replace(/^\//, "")}${compact ? resize_300 || "" : ""}`;
  124. };
  125. const dateGroups = computed(() =>
  126. (farmImgs.value || []).map((group) => ({
  127. date: group.time || "",
  128. dateLabel: formatDateLabel(group.time),
  129. remark: group.desc || "",
  130. list: (group.imgList || []).map((img) => ({
  131. id: img.id,
  132. url: resolveImageUrl(img.cloud_url, true),
  133. fullUrl: resolveImageUrl(img.cloud_url),
  134. uploadDate: group.time || String(img.created_time || "").slice(0, 10),
  135. growText: img.desc || group.desc || "",
  136. zone_name: img.zone_name || "",
  137. })),
  138. }))
  139. );
  140. const isEmpty = computed(() => !loading.value && !imageList.value.length);
  141. const applyAlbumData = (data) => {
  142. const groups = Array.isArray(data?.farmImgs) ? data.farmImgs : [];
  143. farmImgs.value = groups;
  144. imageCount.value = Number(data?.image_count) || 0;
  145. zoneNameFromApi.value = groups[0]?.imgList?.[0]?.zone_name || "";
  146. imageList.value = groups.flatMap((group) =>
  147. (group.imgList || []).map((img) => ({
  148. id: img.id,
  149. url: resolveImageUrl(img.cloud_url, true),
  150. fullUrl: resolveImageUrl(img.cloud_url),
  151. uploadDate: group.time || String(img.created_time || "").slice(0, 10),
  152. growText: img.desc || group.desc || "",
  153. zone_name: img.zone_name || "",
  154. }))
  155. );
  156. };
  157. const fetchAlbumImages = async () => {
  158. if (!farmId.value || !zoneId.value) {
  159. applyAlbumData(null);
  160. loading.value = false;
  161. return;
  162. }
  163. loading.value = true;
  164. try {
  165. const res = await VE_API.questionnaire.getZoneImages({
  166. farm_id: farmId.value,
  167. zone_id: zoneId.value,
  168. });
  169. if (res?.code === 200) {
  170. applyAlbumData(res.data);
  171. } else {
  172. applyAlbumData(null);
  173. if (res?.msg) ElMessage.error(res.msg);
  174. }
  175. } catch {
  176. applyAlbumData(null);
  177. ElMessage.error("获取相册失败,请稍后再试");
  178. } finally {
  179. loading.value = false;
  180. }
  181. };
  182. const formatPreviewDate = (dateStr) => {
  183. const raw = String(dateStr || "").slice(0, 10);
  184. return raw ? raw.replace(/-/g, "/") : "";
  185. };
  186. const previewImage = (group, index) => {
  187. previewImages.value = group.list.map((item) => ({
  188. url: item.fullUrl || item.url,
  189. date: formatPreviewDate(item.uploadDate),
  190. // 临时物候标签,有正式字段后替换
  191. tag: item.phenologyName || item.tag || "预测物候期",
  192. remark: item.growText || item.watermarkMsg || "",
  193. id: item.id,
  194. }));
  195. previewIndex.value = index;
  196. showPreview.value = true;
  197. };
  198. const handleRemarkChange = ({ index, remark }) => {
  199. const previewItem = previewImages.value[index];
  200. if (!previewItem) return;
  201. previewItem.remark = remark;
  202. };
  203. const handleRemarkConfirm = ({ index, remark, image }) => {
  204. const previewItem = previewImages.value[index];
  205. if (previewItem) previewItem.remark = remark;
  206. // 回写到列表数据(临时本地保存,后续可接保存备注接口)
  207. const targetId = image?.id || previewItem?.id;
  208. if (targetId == null) return;
  209. const target = imageList.value.find((item) => String(item.id) === String(targetId));
  210. if (target) {
  211. target.growText = remark;
  212. }
  213. };
  214. const handleAdd = () => {
  215. showUploadPopup.value = true;
  216. };
  217. const handleUploadConfirm = async ({ images, date } = {}) => {
  218. const cloud_url = Array.isArray(images) ? images : [];
  219. if (!cloud_url.length) {
  220. ElMessage.warning("请先上传照片");
  221. return;
  222. }
  223. const farm = getSelectedFarm();
  224. const farmIdVal = farmId.value || farm.farm_id || farm.id || "";
  225. const zoneIdVal = zoneId.value || farm.zone_id || farm.zoneId || "";
  226. if (!farmIdVal || !zoneIdVal) {
  227. ElMessage.warning("缺少农场或地块信息");
  228. return;
  229. }
  230. try {
  231. const res = await VE_API.questionnaire.uploadImages({
  232. cloud_url,
  233. farm_id: farmIdVal,
  234. zone_id: zoneIdVal,
  235. category_code: farm.category_code || "",
  236. zone_name: regionName.value || farm.variety_name || "",
  237. create_time: date,
  238. });
  239. if (res?.code === 200) {
  240. await fetchAlbumImages();
  241. return;
  242. }
  243. ElMessage.error(res?.msg || "上传失败,请稍后再试");
  244. } catch {
  245. ElMessage.error("上传失败,请稍后再试");
  246. }
  247. };
  248. const goBack = () => {
  249. router.back();
  250. };
  251. const syncAlbumByRoute = () => {
  252. fetchAlbumImages();
  253. };
  254. onMounted(() => {
  255. syncAlbumByRoute();
  256. });
  257. onActivated(() => {
  258. syncAlbumByRoute();
  259. });
  260. onDeactivated(() => {
  261. showPreview.value = false;
  262. });
  263. </script>
  264. <style lang="scss" scoped>
  265. .region-albums {
  266. min-height: 100vh;
  267. background: #f5f7fb;
  268. position: relative;
  269. &__hero {
  270. position: relative;
  271. height: 160px;
  272. background-size: cover;
  273. background-position: center;
  274. background-repeat: no-repeat;
  275. }
  276. &__hero-mask {
  277. position: absolute;
  278. inset: 0;
  279. background: linear-gradient(
  280. 180deg,
  281. rgba(0, 0, 0, 0.25) 0%,
  282. rgba(0, 0, 0, 0.05) 45%,
  283. rgba(0, 0, 0, 0.35) 100%
  284. );
  285. &.is-light {
  286. background: linear-gradient(180deg, #d2ebff 0%, #c2dcef 70%, #8ca1b2 100%);
  287. }
  288. }
  289. &__nav {
  290. position: absolute;
  291. z-index: 2;
  292. top: 14px;
  293. left: 12px;
  294. width: 32px;
  295. height: 32px;
  296. border-radius: 50%;
  297. display: flex;
  298. align-items: center;
  299. justify-content: center;
  300. background: rgba(0, 0, 0, 0.25);
  301. cursor: pointer;
  302. }
  303. &__hero-info {
  304. position: absolute;
  305. z-index: 2;
  306. left: 10px;
  307. bottom: 36px;
  308. display: flex;
  309. align-items: center;
  310. gap: 8px;
  311. }
  312. &__title {
  313. font-size: 18px;
  314. line-height: 27px;
  315. font-weight: 500;
  316. color: #fff;
  317. text-shadow: 0 1px 4px rgba(0, 0, 0, 0.35);
  318. }
  319. &__count {
  320. padding: 0 6px;
  321. height: 24px;
  322. line-height: 24px;
  323. border-radius: 4px;
  324. backdrop-filter: blur(4px);
  325. background: rgba(255, 255, 255, 0.2);
  326. color: #fff;
  327. font-size: 14px;
  328. box-sizing: border-box;
  329. }
  330. &__panel {
  331. position: relative;
  332. z-index: 3;
  333. margin-top: -20px;
  334. height: calc(100vh - 140px);
  335. background: #fff;
  336. border-radius: 12px 12px 0 0;
  337. padding-top: 12px;
  338. box-sizing: border-box;
  339. }
  340. &__empty {
  341. height: 100%;
  342. min-height: 320px;
  343. display: flex;
  344. flex-direction: column;
  345. align-items: center;
  346. justify-content: center;
  347. padding: 40px 20px 80px;
  348. box-sizing: border-box;
  349. }
  350. &__empty-icon {
  351. width: 58px;
  352. height: 56px;
  353. object-fit: contain;
  354. }
  355. &__empty-text {
  356. margin-top: 16px;
  357. font-size: 14px;
  358. line-height: 20px;
  359. color: rgba(0, 0, 0, 0.71);
  360. text-align: center;
  361. }
  362. &__album-list {
  363. overflow-y: auto;
  364. height: 100%;
  365. padding: 0 6px 40px;
  366. box-sizing: border-box;
  367. }
  368. &__fab {
  369. position: fixed;
  370. right: 18px;
  371. bottom: 148px;
  372. z-index: 20;
  373. width: 56px;
  374. height: 56px;
  375. border-radius: 50%;
  376. background: #2199f8;
  377. display: flex;
  378. align-items: center;
  379. justify-content: center;
  380. box-shadow: 0 6px 16px rgba(33, 153, 248, 0.4);
  381. cursor: pointer;
  382. }
  383. }
  384. .album-group {
  385. & + & {
  386. margin-top: 22px;
  387. }
  388. &__header {
  389. margin-bottom: 14px;
  390. padding: 0 6px;
  391. }
  392. &__date {
  393. font-size: 16px;
  394. line-height: 22px;
  395. color: #010101;
  396. }
  397. &__remark {
  398. margin-top: 2px;
  399. font-size: 12px;
  400. line-height: 18px;
  401. color: rgba(1, 1, 1, 0.5);
  402. }
  403. &__grid {
  404. display: grid;
  405. grid-template-columns: repeat(3, 1fr);
  406. gap: 4px;
  407. }
  408. &__item {
  409. position: relative;
  410. width: 100%;
  411. padding-top: 100%;
  412. border-radius: 6px;
  413. overflow: hidden;
  414. background: #f2f2f2;
  415. }
  416. &__img {
  417. position: absolute;
  418. inset: 0;
  419. width: 100%;
  420. height: 100%;
  421. object-fit: cover;
  422. display: block;
  423. }
  424. }
  425. </style>