patrolPhoto.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <template>
  2. <div class="patrol-photo">
  3. <custom-header name="农场照片"></custom-header>
  4. <div class="patrol-content">
  5. <div class="photo-header">
  6. <el-date-picker style="width: 120px" v-model="value1" type="date" placeholder="全部日期" />
  7. <div class="select-wrap">
  8. <div
  9. :class="['select-item', { active: tabActive === index }]"
  10. v-for="(item, index) in tabsList"
  11. :key="index"
  12. @click="handleTabAct(index)"
  13. >
  14. {{ item }}
  15. </div>
  16. </div>
  17. </div>
  18. <div class="photo-wrap">
  19. <List
  20. class="photo-list"
  21. v-model:loading="loading"
  22. :finished="finished"
  23. :immediate-check="false"
  24. finished-text="没有更多了"
  25. @load="getPhotoList"
  26. >
  27. <div class="photo-item" v-for="(dateItem, dateIndex) in photoListByDate" :key="dateIndex">
  28. <div class="photo-item-top">
  29. <span class="date-text">{{ dateItem.dateText }}</span>
  30. <div class="weather-wrap">
  31. <div class="weather-item">
  32. <i :class="'qi-'+ dateItem.weather.iconDay + '-fill'"></i>
  33. <span>{{ dateItem.weather.textDay }}</span>
  34. </div>
  35. <div class="weather-item">
  36. <img src="@/assets/img/home/temperature.png" alt="">
  37. <span>{{ dateItem.weather.tempMax }}℃</span>
  38. </div>
  39. <div class="weather-item">
  40. <img src="@/assets/img/home/humidity.png" alt="">
  41. <span>{{ dateItem.weather.humidity }}%</span>
  42. </div>
  43. </div>
  44. </div>
  45. <div class="photo-img-wrap">
  46. <photo-provider :photo-closable="true">
  47. <photo-consumer
  48. v-for="src in dateItem.images"
  49. intro="执行照片"
  50. :key="src"
  51. :src="src.path"
  52. >
  53. <div class="photo-img">
  54. <img :src="src.path" />
  55. </div>
  56. </photo-consumer>
  57. </photo-provider>
  58. </div>
  59. </div>
  60. </List>
  61. </div>
  62. </div>
  63. </div>
  64. </template>
  65. <script setup>
  66. import { ref, onMounted, nextTick } from "vue";
  67. import { useRouter, useRoute } from "vue-router";
  68. import { List } from "vant";
  69. import { base_img_url2, resize } from "@/api/config";
  70. import customHeader from "@/components/customHeader.vue";
  71. const router = useRouter();
  72. const route = useRoute();
  73. const tabActive = ref(0);
  74. const tabsList = ["物候", "病虫", "生长"];
  75. const handleTabAct = (index) => {
  76. tabActive.value = index;
  77. };
  78. const value1 = ref("");
  79. // 按日期分组的图片列表
  80. const photoListByDate = ref([]);
  81. const loading = ref(false);
  82. const finished = ref(false);
  83. // 日期列表
  84. const dateList = ref([]);
  85. // 当前加载的日期索引
  86. const currentDateIndex = ref(0);
  87. // 是否正在加载日期列表
  88. const isLoadingDates = ref(false);
  89. // 是否正在请求图片数据(独立标志,避免与 List 组件的 loading 冲突)
  90. const isRequestingImages = ref(false);
  91. // 当前页码
  92. const currentPageIndex = ref(0);
  93. const farmId = ref(766);
  94. // 格式化日期显示
  95. const formatDate = (dateStr) => {
  96. if (!dateStr) return "";
  97. const date = new Date(dateStr);
  98. const year = date.getFullYear();
  99. const month = date.getMonth() + 1;
  100. const day = date.getDate();
  101. return `${year}年${month}月${day}日`;
  102. };
  103. // 获取有图片的日期列表
  104. const findHasImagesDate = (isLoadMore = false) => {
  105. if (isLoadingDates.value) return Promise.resolve();
  106. isLoadingDates.value = true;
  107. const params = {
  108. farmId: farmId.value,
  109. pageIndex: currentPageIndex.value,
  110. limit: 10,
  111. };
  112. return VE_API.farm
  113. .findHasImagesDate(params)
  114. .then(({ data }) => {
  115. if (data && Array.isArray(data) && data.length > 0) {
  116. if (isLoadMore) {
  117. // 如果是加载更多,追加到现有日期列表
  118. dateList.value = [...dateList.value, ...data];
  119. } else {
  120. // 首次加载,重置日期列表
  121. dateList.value = data;
  122. currentDateIndex.value = 0;
  123. currentPageIndex.value = 1;
  124. }
  125. finished.value = false;
  126. loading.value = false;
  127. isLoadingDates.value = false;
  128. // 如果是首次加载,等待 List 组件触发加载
  129. if (!isLoadMore && data.length > 0) {
  130. loading.value = false;
  131. finished.value = false;
  132. isRequestingImages.value = false;
  133. // 使用 nextTick 确保 DOM 更新后,List 组件能检测到需要加载
  134. nextTick(() => {
  135. if (!finished.value && currentDateIndex.value < dateList.value.length) {
  136. getPhotoList();
  137. }
  138. });
  139. }
  140. } else {
  141. // 没有更多日期了
  142. finished.value = true;
  143. isLoadingDates.value = false;
  144. }
  145. })
  146. .catch((error) => {
  147. console.error("findHasImagesDate 请求失败:", error);
  148. isLoadingDates.value = false;
  149. finished.value = true;
  150. });
  151. };
  152. // 加载下一个日期的图片
  153. const loadNextDateImages = () => {
  154. // 如果已经加载完所有日期,尝试加载更多日期
  155. if (currentDateIndex.value >= dateList.value.length) {
  156. // 不是直接设置 finished,而是尝试获取更多日期
  157. loadMoreDates();
  158. return;
  159. }
  160. // 如果已经标记为完成,直接返回
  161. if (finished.value) {
  162. return;
  163. }
  164. // 如果正在请求,直接返回(避免重复请求)
  165. if (isRequestingImages.value) {
  166. return;
  167. }
  168. // 设置请求标志和 loading 状态
  169. isRequestingImages.value = true;
  170. loading.value = true;
  171. const currentDate = dateList.value[currentDateIndex.value];
  172. const params = {
  173. farmId: farmId.value,
  174. // farmId: route.query.farmId,
  175. date: currentDate,
  176. };
  177. VE_API.farm
  178. .getImageInfo(params)
  179. .then(({ data }) => {
  180. if (data.images.length > 0) {
  181. // 处理图片数据,添加图片路径
  182. const photoArr = data.images.map((item) => ({
  183. ...item,
  184. path: base_img_url2 + (item.resFilename || item.filename) + "?x-oss-process=image/resize,w_300",
  185. }));
  186. // 将当前日期的图片数据添加到列表中
  187. photoListByDate.value.push({
  188. date: currentDate,
  189. dateText: formatDate(currentDate),
  190. images: photoArr,
  191. weather: data.weather,
  192. });
  193. }
  194. // 移动到下一个日期
  195. currentDateIndex.value++;
  196. })
  197. .catch((error) => {
  198. console.error("获取图片失败:", error);
  199. // 请求失败时也移动到下一个日期,避免卡住
  200. currentDateIndex.value++;
  201. })
  202. .finally(() => {
  203. // 使用 finally 确保状态总是被重置
  204. isRequestingImages.value = false;
  205. loading.value = false;
  206. // 检查是否还有更多日期需要加载
  207. if (currentDateIndex.value >= dateList.value.length) {
  208. // 当前日期列表已加载完,尝试加载更多日期
  209. loadMoreDates();
  210. }
  211. });
  212. };
  213. // 加载更多日期
  214. const loadMoreDates = () => {
  215. if (isLoadingDates.value || finished.value) {
  216. return;
  217. }
  218. // pageIndex + 1,获取下一页日期
  219. currentPageIndex.value++;
  220. loading.value = true;
  221. findHasImagesDate(true).then(() => {
  222. // 如果获取到新日期,继续加载图片
  223. if (currentDateIndex.value < dateList.value.length) {
  224. loadNextDateImages();
  225. }
  226. });
  227. };
  228. // 滚动加载触发
  229. const getPhotoList = () => {
  230. // 如果已经完成,直接返回
  231. if (finished.value) {
  232. loading.value = false; // 确保 loading 状态被重置
  233. return;
  234. }
  235. // 调用加载函数(loadNextDateImages 内部会检查 loading 状态)
  236. loadNextDateImages();
  237. };
  238. onMounted(() => {
  239. farmId.value = route.query.farmId;
  240. findHasImagesDate();
  241. });
  242. </script>
  243. <style lang="scss" scoped>
  244. ::v-deep {
  245. .van-list__finished-text {
  246. width: 100%;
  247. }
  248. .el-input__wrapper {
  249. box-shadow: none;
  250. border: 1px solid #2199f8;
  251. --el-input-placeholder-color: #2199f8;
  252. }
  253. .el-input__inner,
  254. .el-input__prefix {
  255. color: #2199f8;
  256. }
  257. }
  258. .patrol-photo {
  259. width: 100%;
  260. height: 100vh;
  261. background: #fff;
  262. .patrol-content {
  263. padding: 0 12px;
  264. width: 100%;
  265. height: calc(100vh - 50px);
  266. box-sizing: border-box;
  267. margin-top: 10px;
  268. .photo-header {
  269. display: flex;
  270. width: 100%;
  271. margin-bottom: 20px;
  272. .select-wrap {
  273. display: flex;
  274. width: calc(100% - 120px);
  275. .select-item {
  276. margin-left: 12px;
  277. flex: 1;
  278. color: #666666;
  279. padding: 6px 0;
  280. text-align: center;
  281. background: #f5f5f5;
  282. border-radius: 5px;
  283. &.active {
  284. color: #2199f8;
  285. background: #e0f1fe;
  286. }
  287. }
  288. }
  289. }
  290. .photo-wrap {
  291. width: 100%;
  292. height: calc(100vh - 95px);
  293. overflow: auto;
  294. .photo-list {
  295. width: 100%;
  296. .photo-item {
  297. width: 100%;
  298. .photo-item-top {
  299. display: flex;
  300. align-items: center;
  301. margin-bottom: 12px;
  302. .date-text {
  303. font-weight: 500;
  304. font-size: 14px;
  305. color: #333;
  306. margin-right: 20px;
  307. }
  308. .weather-wrap {
  309. display: flex;
  310. align-items: center;
  311. .weather-item {
  312. display: flex;
  313. gap: 4px;
  314. align-items: center;
  315. font-size: 12px;
  316. margin-right: 10px;
  317. img {
  318. width: 16px;
  319. height: 16px;
  320. }
  321. }
  322. }
  323. }
  324. .photo-img-wrap {
  325. display: flex;
  326. flex-wrap: wrap;
  327. gap: 10px;
  328. // .region-text {
  329. // width: 68px;
  330. // text-align: center;
  331. // font-weight: 500;
  332. // font-size: 12px;
  333. // padding: 5px 0;
  334. // border-radius: 4px;
  335. // border: 1px solid #bbbbbb;
  336. // color: #666;
  337. // margin-bottom: 8px;
  338. // }
  339. .photo-img {
  340. width: 110px;
  341. height: 110px;
  342. img {
  343. width: 100%;
  344. height: 100%;
  345. border-radius: 8px;
  346. object-fit: cover;
  347. }
  348. }
  349. }
  350. }
  351. .photo-item + .photo-item {
  352. margin-top: 20px;
  353. }
  354. }
  355. }
  356. }
  357. }
  358. </style>