regionAlbums.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 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, 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. /** 临时默认参数,后续改成正式接口后可只走 query */
  77. const TEMP_ORGAN_ID = "101532";
  78. const TEMP_AREA_ID = "90178";
  79. const TEMP_DATE = "2026-08-19";
  80. const loading = ref(false);
  81. const imageList = ref([]);
  82. const showPreview = ref(false);
  83. const previewImages = ref([]);
  84. const previewIndex = ref(0);
  85. const showUploadPopup = ref(false);
  86. const regionName = computed(
  87. () => route.query.regionName || route.query.name || t("agriFile.zoneOne")
  88. );
  89. const organId = computed(() => route.query.organId || TEMP_ORGAN_ID);
  90. const areaId = computed(() => route.query.areaId || TEMP_AREA_ID);
  91. const queryDate = computed(() => route.query.date || TEMP_DATE);
  92. const forceEmpty = computed(
  93. () => route.query.noImage === "true" || route.query.noImage === true
  94. );
  95. const totalCount = computed(() => imageList.value.length);
  96. const heroCover = computed(() => {
  97. if (forceEmpty.value || !imageList.value.length) return "";
  98. const first = imageList.value[0];
  99. if (first?.baseMap) return first.baseMap;
  100. if (first?.url) return first.url;
  101. return "";
  102. });
  103. const heroStyle = computed(() => {
  104. if (!heroCover.value) {
  105. return {
  106. background: "linear-gradient(180deg, #8ec8f5 0%, #5aadeb 55%, #3d9ae0 100%)",
  107. };
  108. }
  109. return {
  110. backgroundImage: `url(${heroCover.value})`,
  111. };
  112. });
  113. const formatDateLabel = (dateStr) => {
  114. const raw = String(dateStr || "").slice(0, 10);
  115. const parts = raw.split("-");
  116. if (parts.length < 3) return raw;
  117. return `${parts[0]}年${parts[1]}月${parts[2]}日`;
  118. };
  119. const getImageUrl = (item) => {
  120. if (!item?.filename) return "";
  121. if (/^https?:\/\//i.test(item.filename)) return item.filename;
  122. return `${base_img_url2}${item.filename}${resize_300 || ""}`;
  123. };
  124. const getFullImageUrl = (item) => {
  125. if (!item?.filename) return "";
  126. if (/^https?:\/\//i.test(item.filename)) return item.filename;
  127. return `${base_img_url2}${item.filename}`;
  128. };
  129. const dateGroups = computed(() => {
  130. const map = new Map();
  131. imageList.value.forEach((item) => {
  132. const date = String(item.uploadDate || "").slice(0, 10) || "未知日期";
  133. if (!map.has(date)) {
  134. map.set(date, {
  135. date,
  136. dateLabel: formatDateLabel(date),
  137. remark: item.growText || item.watermarkMsg || "",
  138. list: [],
  139. });
  140. }
  141. const group = map.get(date);
  142. if (!group.remark && (item.growText || item.watermarkMsg)) {
  143. group.remark = item.growText || item.watermarkMsg;
  144. }
  145. group.list.push(item);
  146. });
  147. return [...map.values()].sort((a, b) => String(b.date).localeCompare(String(a.date)));
  148. });
  149. const isEmpty = computed(
  150. () => forceEmpty.value || (!loading.value && !imageList.value.length)
  151. );
  152. const normalizeList = (raw) => {
  153. const list = Array.isArray(raw)
  154. ? raw
  155. : Array.isArray(raw?.list)
  156. ? raw.list
  157. : Array.isArray(raw?.records)
  158. ? raw.records
  159. : [];
  160. return list
  161. .map((item) => ({
  162. ...item,
  163. url: getImageUrl(item),
  164. fullUrl: getFullImageUrl(item),
  165. }))
  166. .filter((item) => item.url);
  167. };
  168. const fetchAlbumImages = async () => {
  169. if (forceEmpty.value) {
  170. imageList.value = [];
  171. loading.value = false;
  172. return;
  173. }
  174. loading.value = true;
  175. try {
  176. // TODO: 临时接口,后续替换正式相册接口
  177. const res = await VE_API.monitor.getRegionAlbumImages({
  178. organId: organId.value,
  179. areaId: areaId.value,
  180. date: queryDate.value,
  181. limit: 100,
  182. page: 1,
  183. });
  184. const ok = res?.code === 200 || res?.code === 0 || res?.code === 1 || res?.success;
  185. if (ok) {
  186. imageList.value = normalizeList(res.data);
  187. } else {
  188. imageList.value = [];
  189. if (res?.msg) ElMessage.error(res.msg);
  190. }
  191. } catch {
  192. imageList.value = [];
  193. ElMessage.error("获取相册失败,请稍后再试");
  194. } finally {
  195. loading.value = false;
  196. }
  197. };
  198. const formatPreviewDate = (dateStr) => {
  199. const raw = String(dateStr || "").slice(0, 10);
  200. return raw ? raw.replace(/-/g, "/") : "";
  201. };
  202. const previewImage = (group, index) => {
  203. previewImages.value = group.list.map((item) => ({
  204. url: item.fullUrl || item.url,
  205. date: formatPreviewDate(item.uploadDate),
  206. // 临时物候标签,有正式字段后替换
  207. tag: item.phenologyName || item.tag || "预测物候期",
  208. remark: item.growText || item.watermarkMsg || "",
  209. id: item.id,
  210. }));
  211. previewIndex.value = index;
  212. showPreview.value = true;
  213. };
  214. const handleRemarkChange = ({ index, remark }) => {
  215. const previewItem = previewImages.value[index];
  216. if (!previewItem) return;
  217. previewItem.remark = remark;
  218. };
  219. const handleRemarkConfirm = ({ index, remark, image }) => {
  220. const previewItem = previewImages.value[index];
  221. if (previewItem) previewItem.remark = remark;
  222. // 回写到列表数据(临时本地保存,后续可接保存备注接口)
  223. const targetId = image?.id || previewItem?.id;
  224. if (targetId == null) return;
  225. const target = imageList.value.find((item) => String(item.id) === String(targetId));
  226. if (target) {
  227. target.growText = remark;
  228. }
  229. };
  230. const handleAdd = () => {
  231. showUploadPopup.value = true;
  232. };
  233. const handleUploadConfirm = ({ images, description }) => {
  234. // TODO: 正式上传相册接口确定后在此提交
  235. // images: OSS 相对路径数组;description: 描述文案
  236. ElMessage.success(`已选择 ${images.length} 张照片${description ? ",描述已填写" : ""}`);
  237. // 临时本地插入,便于联调样式;正式接口返回后改为 fetchAlbumImages()
  238. const today = new Date();
  239. const y = today.getFullYear();
  240. const m = String(today.getMonth() + 1).padStart(2, "0");
  241. const d = String(today.getDate()).padStart(2, "0");
  242. const uploadDate = `${y}-${m}-${d}`;
  243. const localItems = images.map((filename, index) => ({
  244. id: `local-${Date.now()}-${index}`,
  245. filename,
  246. uploadDate,
  247. growText: description || "",
  248. watermarkMsg: "",
  249. url: `${base_img_url2}${filename}${resize_300 || ""}`,
  250. fullUrl: `${base_img_url2}${filename}`,
  251. }));
  252. imageList.value = [...localItems, ...imageList.value];
  253. };
  254. const goBack = () => {
  255. router.back();
  256. };
  257. const syncAlbumByRoute = () => {
  258. // 空相册:立即清空缓存照片,避免 keepAlive 残留上一相册数据
  259. if (forceEmpty.value) {
  260. imageList.value = [];
  261. loading.value = false;
  262. return;
  263. }
  264. fetchAlbumImages();
  265. };
  266. onMounted(() => {
  267. syncAlbumByRoute();
  268. });
  269. onActivated(() => {
  270. syncAlbumByRoute();
  271. });
  272. </script>
  273. <style lang="scss" scoped>
  274. .region-albums {
  275. min-height: 100vh;
  276. background: #f5f7fb;
  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: linear-gradient(180deg, #d2ebff 0%, #c2dcef 70%, #8ca1b2 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(0, 0, 0, 0.25);
  310. cursor: pointer;
  311. }
  312. &__hero-info {
  313. position: absolute;
  314. z-index: 2;
  315. left: 10px;
  316. bottom: 36px;
  317. display: flex;
  318. align-items: center;
  319. gap: 8px;
  320. }
  321. &__title {
  322. font-size: 18px;
  323. line-height: 27px;
  324. font-weight: 500;
  325. color: #fff;
  326. text-shadow: 0 1px 4px rgba(0, 0, 0, 0.35);
  327. }
  328. &__count {
  329. padding: 0 6px;
  330. height: 24px;
  331. line-height: 24px;
  332. border-radius: 4px;
  333. backdrop-filter: blur(4px);
  334. background: rgba(255, 255, 255, 0.2);
  335. color: #fff;
  336. font-size: 14px;
  337. box-sizing: border-box;
  338. }
  339. &__panel {
  340. position: relative;
  341. z-index: 3;
  342. margin-top: -20px;
  343. height: calc(100vh - 140px);
  344. background: #fff;
  345. border-radius: 12px 12px 0 0;
  346. padding-top: 12px;
  347. box-sizing: border-box;
  348. }
  349. &__empty {
  350. height: 100%;
  351. min-height: 320px;
  352. display: flex;
  353. flex-direction: column;
  354. align-items: center;
  355. justify-content: center;
  356. padding: 40px 20px 80px;
  357. box-sizing: border-box;
  358. }
  359. &__empty-icon {
  360. width: 58px;
  361. height: 56px;
  362. object-fit: contain;
  363. }
  364. &__empty-text {
  365. margin-top: 16px;
  366. font-size: 14px;
  367. line-height: 20px;
  368. color: rgba(0, 0, 0, 0.71);
  369. text-align: center;
  370. }
  371. &__album-list {
  372. overflow-y: auto;
  373. height: 100%;
  374. padding: 0 6px 40px;
  375. box-sizing: border-box;
  376. }
  377. &__fab {
  378. position: fixed;
  379. right: 18px;
  380. bottom: 148px;
  381. z-index: 20;
  382. width: 56px;
  383. height: 56px;
  384. border-radius: 50%;
  385. background: #2199f8;
  386. display: flex;
  387. align-items: center;
  388. justify-content: center;
  389. box-shadow: 0 6px 16px rgba(33, 153, 248, 0.4);
  390. cursor: pointer;
  391. }
  392. }
  393. .album-group {
  394. & + & {
  395. margin-top: 22px;
  396. }
  397. &__header {
  398. margin-bottom: 14px;
  399. padding: 0 6px;
  400. }
  401. &__date {
  402. font-size: 16px;
  403. line-height: 22px;
  404. color: #010101;
  405. }
  406. &__remark {
  407. margin-top: 2px;
  408. font-size: 12px;
  409. line-height: 18px;
  410. color: rgba(1, 1, 1, 0.5);
  411. }
  412. &__grid {
  413. display: grid;
  414. grid-template-columns: repeat(3, 1fr);
  415. gap: 4px;
  416. }
  417. &__item {
  418. position: relative;
  419. width: 100%;
  420. padding-top: 100%;
  421. border-radius: 6px;
  422. overflow: hidden;
  423. background: #f2f2f2;
  424. }
  425. &__img {
  426. position: absolute;
  427. inset: 0;
  428. width: 100%;
  429. height: 100%;
  430. object-fit: cover;
  431. display: block;
  432. }
  433. }
  434. </style>