treeAlbumPopup.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <template>
  2. <up-popup :show="showPopup" mode="center" round="20" @close="handleClose">
  3. <view class="album-popup">
  4. <view class="album-title">
  5. <view class="name">
  6. 果树相册
  7. <view class="sub-name">无人机实时监测生长情况</view>
  8. </view>
  9. <image class="icon" :src="`${config.BASIC_IMG}img/treePage/drone-icon-popup.png`"></image>
  10. </view>
  11. <view class="album-cont">
  12. <scroll-view class="time-line-scroll" scroll-x :scroll-into-view="scrollIntoViewId" scroll-with-animation>
  13. <view class="time-line">
  14. <view class="time-item" v-for="(item,index) in dateList" :key="index" :id="`d-${item.dateStr}`" @click="handleDateClick(item.dateStr)">
  15. <text v-if="item.showYear" class="year-flag">{{ item.year }}</text>
  16. <text :style="{color:(selectedDateStr===item.dateStr)?'#2199F8':'#777777'}">{{item.display}}</text>
  17. <view class="dot" :style="{background:(selectedDateStr===item.dateStr)?'#2199F8':'#777777'}"></view>
  18. <text class="today" v-if="item.isToday">今</text>
  19. </view>
  20. </view>
  21. </scroll-view>
  22. <view class="swiper-wrap">
  23. <swiper class="swiper">
  24. <swiper-item v-for="(item,index) in photoList" :key="index">
  25. <image class="img" :src="getImageUrl(item.filename)"></image>
  26. </swiper-item>
  27. </swiper>
  28. <!-- <view class="arrow left">
  29. <up-icon name="arrow-left" bold color="#F0D09C" size="22"></up-icon>
  30. </view>
  31. <view class="arrow right">
  32. <up-icon name="arrow-right" bold color="#F0D09C" size="22"></up-icon>
  33. </view> -->
  34. </view>
  35. </view>
  36. </view>
  37. </up-popup>
  38. </template>
  39. <script setup>
  40. import config from "@/api/config.js"
  41. import TREE from '@/api/tree.js'
  42. import {
  43. ref,
  44. watch,
  45. nextTick
  46. } from "vue";
  47. const resize = "?x-oss-process=image/resize,w_1000";
  48. const props = defineProps({
  49. show: {
  50. type: Boolean,
  51. defalut: false,
  52. },
  53. farmBuyId: {
  54. type: [String, Number],
  55. default: ''
  56. },
  57. treeId: {
  58. type: [String, Number],
  59. default: 110939
  60. }
  61. });
  62. const showPopup = ref(false);
  63. const handleClose = ()=>{
  64. showPopup.value = false
  65. }
  66. const dateList = ref([])
  67. const scrollIntoViewId = ref('')
  68. const selectedDateStr = ref('')
  69. const formatToDisplay = (dateStr)=>{
  70. // dateStr: YYYY-MM-DD
  71. const [y,m,d] = dateStr.split('-')
  72. return `${m}/${d}`
  73. }
  74. const getTodayStr = ()=>{
  75. const d = new Date()
  76. const mm = `${d.getMonth()+1}`.padStart(2,'0')
  77. const dd = `${d.getDate()}`.padStart(2,'0')
  78. return `${d.getFullYear()}-${mm}-${dd}`
  79. }
  80. const fetchHasImageDates = async ()=>{
  81. if(!props.farmBuyId) return
  82. try{
  83. const {data} = await TREE.findHasImagesDate({farmBuyId: props.farmBuyId})
  84. const today = getTodayStr()
  85. const arr = Array.isArray(data) ? data : []
  86. const mapped = arr.map(ds=>{
  87. const dateStr = (typeof ds === 'string') ? ds.split(' ')[0] : ''
  88. const isToday = dateStr === today
  89. return {
  90. dateStr,
  91. display: dateStr ? formatToDisplay(dateStr) : '',
  92. isToday,
  93. year: dateStr ? dateStr.slice(0,4) : ''
  94. }
  95. }).filter(i=>i.dateStr)
  96. mapped.sort((a,b)=> a.dateStr.localeCompare(b.dateStr))
  97. for(let i=0;i<mapped.length;i++){
  98. mapped[i].showYear = i===0 || mapped[i].year !== mapped[i-1].year
  99. }
  100. dateList.value = mapped
  101. if(dateList.value.length){
  102. const last = dateList.value[dateList.value.length-1]
  103. selectedDateStr.value = last.dateStr
  104. // 强制等渲染后再滚动到最右端
  105. scrollIntoViewId.value = ''
  106. await nextTick()
  107. scrollIntoViewId.value = `d-${last.dateStr}`
  108. }else{
  109. selectedDateStr.value = ''
  110. scrollIntoViewId.value = ''
  111. }
  112. }catch(e){
  113. // 静默失败
  114. }
  115. }
  116. const fetchTreeImages = async (dateStr)=>{
  117. try{
  118. const params = {
  119. page: 1,
  120. limit: 1,
  121. treeId: Number(props.treeId) || 110939,
  122. date: dateStr,
  123. }
  124. const {data} = await TREE.treeImageList(params)
  125. photoList.value = data || []
  126. }catch(err){
  127. console.log('treeImageList error:', err)
  128. }
  129. }
  130. const getImageUrl = (filename) => {
  131. if (filename.startsWith("https")) {
  132. return filename; // 直接使用完整 URL
  133. } else {
  134. return config.BASE_IMG_URL + filename + resize; // 拼接基础 URL
  135. }
  136. };
  137. const handleDateClick = (dateStr)=>{
  138. selectedDateStr.value = dateStr
  139. scrollIntoViewId.value = `d-${dateStr}`
  140. fetchTreeImages(dateStr)
  141. }
  142. const photoList = ref([]);
  143. watch(
  144. () => props.show,
  145. async (val) => {
  146. showPopup.value = val;
  147. if(val){
  148. await fetchHasImageDates()
  149. const today = getTodayStr()
  150. fetchTreeImages(today)
  151. }
  152. }
  153. );
  154. watch(
  155. () => props.farmBuyId,
  156. (val) => {
  157. if(showPopup.value && val){
  158. fetchHasImageDates()
  159. }
  160. }
  161. )
  162. </script>
  163. <style lang="scss" scoped>
  164. .album-popup {
  165. width: 92vw;
  166. .album-title {
  167. padding: 30rpx 0 46rpx 30rpx;
  168. position: relative;
  169. background-image: linear-gradient(120deg, #79C4FF 13%, #FFFFFF 66%);
  170. border-radius: 40rpx 40rpx 0 0;
  171. line-height: 44rpx;
  172. .name {
  173. color: #004275;
  174. font-size: 48rpx;
  175. font-family: 'PangMenZhengDao';
  176. .sub-name {
  177. font-size: 24rpx;
  178. }
  179. }
  180. .icon {
  181. position: absolute;
  182. top: -100rpx;
  183. right: -26rpx;
  184. width: 364rpx;
  185. height: 300rpx;
  186. }
  187. }
  188. .album-cont {
  189. position: relative;
  190. z-index: 2;
  191. margin-top: -34rpx;
  192. padding: 32rpx 0 20rpx;
  193. background: #fff;
  194. border-radius: 40rpx;
  195. .time-line-scroll{
  196. margin-bottom: 16rpx;
  197. width: 100%;
  198. white-space: nowrap;
  199. }
  200. .time-line{
  201. display: flex;
  202. padding: 0 20rpx;
  203. }
  204. .time-item{
  205. min-width: 100rpx;
  206. font-size: 24rpx;
  207. color: #777777;
  208. display: flex;
  209. flex-direction: column;
  210. align-items: center;
  211. position: relative;
  212. padding: 0 12rpx;
  213. &::before{
  214. content: '';
  215. position: absolute;
  216. top: 44%;
  217. left: 0;
  218. right: 0;
  219. height: 2rpx;
  220. background: rgba(136, 136, 136, 0.1);
  221. }
  222. .year-flag{
  223. font-size: 20rpx;
  224. color: #999;
  225. margin-bottom: 6rpx;
  226. }
  227. .dot{
  228. width: 14rpx;
  229. height: 14rpx;
  230. background: #777777;
  231. border-radius: 50%;
  232. }
  233. .today{
  234. color: #2199F8;
  235. margin-top: 8rpx;
  236. }
  237. }
  238. .swiper-wrap{
  239. position: relative;
  240. padding: 0 20rpx;
  241. .swiper {
  242. width: 100%;
  243. height: 496rpx;
  244. .img{
  245. width: 100%;
  246. height: 100%;
  247. border-radius: 16rpx;
  248. }
  249. }
  250. .arrow{
  251. position: absolute;
  252. top: calc(50% - 40rpx);
  253. width: 80rpx;
  254. height: 80rpx;
  255. display: flex;
  256. align-items: center;
  257. justify-content: center;
  258. border-radius: 50%;
  259. background: rgba(0, 0, 0, .6);
  260. }
  261. .left{
  262. left: 32rpx;
  263. }
  264. .right{
  265. right: 32rpx;
  266. }
  267. }
  268. }
  269. }
  270. </style>