treeAlbumPopup.vue 9.8 KB

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