treeAlbumPopup.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. <view class="time-line-scroll">
  13. <scroll-view class="time-line-container" scroll-x :scroll-into-view="scrollIntoViewId" scroll-with-animation @scroll="handleScroll" @scrollend="handleScrollEnd">
  14. <view class="time-line">
  15. <!-- 月份标记 -->
  16. <view class="month-markers">
  17. <view class="month-item" v-for="(month, index) in monthList" :key="index">
  18. <text class="month-text">{{ month.label }}</text>
  19. <view class="month-dot" :class="{'active': month.isCurrent}"></view>
  20. </view>
  21. </view>
  22. <!-- 日期刻度 -->
  23. <view class="date-markers">
  24. <view class="date-item" v-for="(date, index) in dateList" :key="index" :class="{'current-date': date.isCurrent}">
  25. <text class="date-text" :class="{'current': date.isCurrent}">{{ date.display }}</text>
  26. <view class="date-dot" :class="{'current': date.isCurrent}"></view>
  27. </view>
  28. </view>
  29. </view>
  30. </scroll-view>
  31. <!-- 中间竖线标志 -->
  32. <view class="center-line"></view>
  33. </view>
  34. <view class="swiper-wrap">
  35. <swiper class="swiper">
  36. <swiper-item v-for="(item,index) in photoList" :key="index">
  37. <image class="img" :src="getImageUrl(item.filename)"></image>
  38. </swiper-item>
  39. </swiper>
  40. <!-- <view class="arrow left">
  41. <up-icon name="arrow-left" bold color="#F0D09C" size="22"></up-icon>
  42. </view>
  43. <view class="arrow right">
  44. <up-icon name="arrow-right" bold color="#F0D09C" size="22"></up-icon>
  45. </view> -->
  46. </view>
  47. </view>
  48. </view>
  49. </up-popup>
  50. </template>
  51. <script setup>
  52. import config from "@/api/config.js"
  53. import TREE from '@/api/tree.js'
  54. import {
  55. ref,
  56. watch,
  57. nextTick
  58. } from "vue";
  59. const resize = "?x-oss-process=image/resize,w_1000";
  60. const props = defineProps({
  61. show: {
  62. type: Boolean,
  63. defalut: false,
  64. },
  65. farmBuyId: {
  66. type: [String, Number],
  67. default: ''
  68. },
  69. sampleId: {
  70. type: [String, Number],
  71. default: 110939
  72. }
  73. });
  74. const showPopup = ref(false);
  75. const handleClose = ()=>{
  76. showPopup.value = false
  77. }
  78. const monthList = ref([])
  79. const dateList = ref([])
  80. const scrollIntoViewId = ref('')
  81. const selectedDateStr = ref('')
  82. const centerDate = ref('')
  83. const formatToDisplay = (dateStr)=>{
  84. // dateStr: YYYY-MM-DD
  85. const [y,m,d] = dateStr.split('-')
  86. return `${m}月${d}日`
  87. }
  88. const getTodayStr = ()=>{
  89. const d = new Date()
  90. const mm = `${d.getMonth()+1}`.padStart(2,'0')
  91. const dd = `${d.getDate()}`.padStart(2,'0')
  92. return `${d.getFullYear()}-${mm}-${dd}`
  93. }
  94. const generateTimeLineData = () => {
  95. const today = new Date()
  96. const currentYear = today.getFullYear()
  97. const currentMonth = today.getMonth() + 1
  98. const currentDay = today.getDate()
  99. // 生成月份列表(往前一年)
  100. const months = []
  101. for(let i = 11; i >= 0; i--) {
  102. const date = new Date(currentYear, currentMonth - 1 - i, 1)
  103. const month = date.getMonth() + 1
  104. const year = date.getFullYear()
  105. const isCurrent = month === currentMonth && year === currentYear
  106. months.push({
  107. year,
  108. month,
  109. label: `${month}月`,
  110. isCurrent,
  111. dateStr: `${year}-${month.toString().padStart(2,'0')}-01`
  112. })
  113. }
  114. monthList.value = months
  115. // 生成当前月份的日期刻度(每两天一个)
  116. const dates = []
  117. const daysInMonth = new Date(currentYear, currentMonth, 0).getDate()
  118. for(let day = 1; day <= daysInMonth; day += 2) {
  119. const dateStr = `${currentYear}-${currentMonth.toString().padStart(2,'0')}-${day.toString().padStart(2,'0')}`
  120. const isCurrent = day === currentDay
  121. dates.push({
  122. dateStr,
  123. display: `${currentMonth}月${day}日`,
  124. isCurrent,
  125. day
  126. })
  127. }
  128. dateList.value = dates
  129. // 设置当前日期为选中状态
  130. selectedDateStr.value = getTodayStr()
  131. centerDate.value = getTodayStr()
  132. }
  133. const fetchTreeImages = async (dateStr)=>{
  134. try{
  135. const params = {
  136. page: 1,
  137. limit: 1,
  138. treeId: 110939,
  139. date: dateStr,
  140. }
  141. const {data} = await TREE.treeImageList(params)
  142. photoList.value = data || []
  143. }catch(err){
  144. console.log('treeImageList error:', err)
  145. }
  146. }
  147. const getImageUrl = (filename) => {
  148. if (filename.startsWith("https")) {
  149. return filename; // 直接使用完整 URL
  150. } else {
  151. return config.BASE_IMG_URL + filename + resize; // 拼接基础 URL
  152. }
  153. };
  154. const handleScroll = (e) => {
  155. // 滚动中实时计算中间位置
  156. const scrollLeft = e.detail.scrollLeft
  157. const viewWidth = e.detail.scrollWidth - e.detail.scrollLeft
  158. // 计算中间位置对应的日期
  159. const centerPosition = scrollLeft + viewWidth / 2
  160. const itemWidth = 120 // 每个时间项的宽度
  161. const estimatedIndex = Math.floor(centerPosition / itemWidth)
  162. if(dateList.value[estimatedIndex]){
  163. centerDate.value = dateList.value[estimatedIndex].dateStr
  164. }
  165. }
  166. const handleScrollEnd = (e) => {
  167. // 滚动停止时获取中间位置的日期并调用接口
  168. const scrollLeft = e.detail.scrollLeft
  169. const viewWidth = e.detail.scrollWidth - e.detail.scrollLeft
  170. const centerPosition = scrollLeft + viewWidth / 2
  171. const itemWidth = 120
  172. const estimatedIndex = Math.floor(centerPosition / itemWidth)
  173. if(dateList.value[estimatedIndex]){
  174. const finalDate = dateList.value[estimatedIndex].dateStr
  175. centerDate.value = finalDate
  176. selectedDateStr.value = finalDate
  177. console.log('滑动停止,中间位置日期:', finalDate)
  178. fetchTreeImages(finalDate)
  179. }
  180. }
  181. const handleDateClick = (dateStr)=>{
  182. selectedDateStr.value = dateStr
  183. centerDate.value = dateStr
  184. fetchTreeImages(dateStr)
  185. }
  186. const photoList = ref([]);
  187. watch(
  188. () => props.show,
  189. async (val) => {
  190. showPopup.value = val;
  191. if(val){
  192. generateTimeLineData()
  193. const today = getTodayStr()
  194. fetchTreeImages(today)
  195. }
  196. }
  197. );
  198. watch(
  199. () => props.farmBuyId,
  200. (val) => {
  201. if(showPopup.value && val){
  202. generateTimeLineData()
  203. }
  204. }
  205. )
  206. </script>
  207. <style lang="scss" scoped>
  208. .album-popup {
  209. width: 92vw;
  210. .album-title {
  211. padding: 30rpx 0 46rpx 30rpx;
  212. position: relative;
  213. background-image: linear-gradient(120deg, #79C4FF 13%, #FFFFFF 66%);
  214. border-radius: 40rpx 40rpx 0 0;
  215. line-height: 44rpx;
  216. .name {
  217. color: #004275;
  218. font-size: 48rpx;
  219. font-family: 'PangMenZhengDao';
  220. .sub-name {
  221. font-size: 24rpx;
  222. }
  223. }
  224. .icon {
  225. position: absolute;
  226. top: -100rpx;
  227. right: -26rpx;
  228. width: 364rpx;
  229. height: 300rpx;
  230. }
  231. }
  232. .album-cont {
  233. position: relative;
  234. z-index: 2;
  235. margin-top: -34rpx;
  236. padding: 32rpx 0 20rpx;
  237. background: #fff;
  238. border-radius: 40rpx;
  239. .time-line-scroll{
  240. margin-bottom: 16rpx;
  241. width: 100%;
  242. position: relative;
  243. }
  244. .time-line-container{
  245. width: 100%;
  246. white-space: nowrap;
  247. }
  248. .time-line{
  249. display: flex;
  250. flex-direction: column;
  251. padding: 0 20rpx;
  252. position: relative;
  253. }
  254. .center-line{
  255. position: absolute;
  256. left: 50%;
  257. top: 0;
  258. bottom: 0;
  259. width: 2rpx;
  260. background: #2199F8;
  261. z-index: 10;
  262. transform: translateX(-50%);
  263. pointer-events: none;
  264. }
  265. .month-markers{
  266. display: flex;
  267. align-items: flex-end;
  268. margin-bottom: 20rpx;
  269. .month-item{
  270. display: flex;
  271. flex-direction: column;
  272. align-items: center;
  273. min-width: 120rpx;
  274. padding: 0 12rpx;
  275. .month-text{
  276. font-size: 28rpx;
  277. color: #777777;
  278. margin-bottom: 8rpx;
  279. }
  280. .month-dot{
  281. width: 16rpx;
  282. height: 16rpx;
  283. background: #777777;
  284. border-radius: 50%;
  285. &.active{
  286. background: #2199F8;
  287. }
  288. }
  289. }
  290. }
  291. .date-markers{
  292. display: flex;
  293. align-items: flex-end;
  294. .date-item{
  295. display: flex;
  296. flex-direction: column;
  297. align-items: center;
  298. min-width: 120rpx;
  299. padding: 0 12rpx;
  300. .date-text{
  301. font-size: 24rpx;
  302. color: #777777;
  303. margin-bottom: 8rpx;
  304. &.current{
  305. color: #2199F8;
  306. }
  307. }
  308. .date-dot{
  309. width: 8rpx;
  310. height: 8rpx;
  311. background: #ccc;
  312. border-radius: 50%;
  313. &.current{
  314. background: #2199F8;
  315. width: 16rpx;
  316. height: 16rpx;
  317. }
  318. }
  319. }
  320. }
  321. .swiper-wrap{
  322. position: relative;
  323. padding: 0 20rpx;
  324. .swiper {
  325. width: 100%;
  326. height: 496rpx;
  327. .img{
  328. width: 100%;
  329. height: 100%;
  330. border-radius: 16rpx;
  331. }
  332. }
  333. .arrow{
  334. position: absolute;
  335. top: calc(50% - 40rpx);
  336. width: 80rpx;
  337. height: 80rpx;
  338. display: flex;
  339. align-items: center;
  340. justify-content: center;
  341. border-radius: 50%;
  342. background: rgba(0, 0, 0, .6);
  343. }
  344. .left{
  345. left: 32rpx;
  346. }
  347. .right{
  348. right: 32rpx;
  349. }
  350. }
  351. }
  352. }
  353. </style>