regionAlbums.vue 14 KB

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