DateSelector.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <template>
  2. <view class="date-selector-card">
  3. <view class="selector-header">
  4. <text class="selector-label">历史数据日期</text>
  5. <text v-if="selectedDate" class="clear-btn" @click="clearDate">清除</text>
  6. </view>
  7. <view class="date-input" @click="openDatePicker">
  8. <text class="date-text">{{ selectedDate ? formatDateDisplay(selectedDate) : '最新数据' }}</text>
  9. <text class="date-icon">📅</text>
  10. </view>
  11. <!-- 自定义日期选择弹窗 -->
  12. <view v-if="showDatePicker" class="date-picker-mask" @tap="closeDatePicker" @touchmove.stop.prevent>
  13. <view class="date-picker-popup" @tap.stop @touchmove.stop>
  14. <view class="picker-header">
  15. <text class="picker-cancel" @tap="closeDatePicker">取消</text>
  16. <text class="picker-title">选择日期</text>
  17. <text class="picker-confirm" @tap="confirmDate">确定</text>
  18. </view>
  19. <!-- 年月选择 -->
  20. <view class="month-selector">
  21. <view class="month-nav" @tap="prevMonth">
  22. <text class="nav-arrow">‹</text>
  23. </view>
  24. <text class="current-month">{{ tempYear }}年{{ tempMonth }}月</text>
  25. <view class="month-nav" @tap="nextMonth">
  26. <text class="nav-arrow">›</text>
  27. </view>
  28. </view>
  29. <!-- 星期标题 -->
  30. <view class="weekday-row">
  31. <text class="weekday-item" v-for="day in weekDays" :key="day">{{ day }}</text>
  32. </view>
  33. <!-- 日期网格 -->
  34. <view class="days-grid">
  35. <view
  36. v-for="(day, index) in calendarDays"
  37. :key="index"
  38. :class="['day-item', {
  39. 'empty': !day,
  40. 'selected': day && isSelected(day),
  41. 'today': day && isToday(day)
  42. }]"
  43. @tap="selectDayHandler(day)"
  44. >
  45. <text v-if="day" class="day-text">{{ day }}</text>
  46. </view>
  47. </view>
  48. <!-- 底部安全区域占位 -->
  49. <view class="safe-area-placeholder"></view>
  50. </view>
  51. </view>
  52. </view>
  53. </template>
  54. <script setup>
  55. import { ref, computed } from 'vue'
  56. const emit = defineEmits(['dateChange'])
  57. const selectedDate = ref('')
  58. const showDatePicker = ref(false)
  59. const tempYear = ref(0)
  60. const tempMonth = ref(0)
  61. const tempDay = ref(0)
  62. const weekDays = ['日', '一', '二', '三', '四', '五', '六']
  63. // 获取今天的日期
  64. const getTodayDate = () => {
  65. const now = new Date()
  66. return {
  67. year: now.getFullYear(),
  68. month: now.getMonth() + 1,
  69. day: now.getDate()
  70. }
  71. }
  72. // 格式化日期显示
  73. const formatDateDisplay = (dateStr) => {
  74. if (!dateStr) return '最新数据'
  75. const [year, month, day] = dateStr.split('-')
  76. return `${year}年${month}月${day}日`
  77. }
  78. // 打开日期选择器
  79. const openDatePicker = () => {
  80. const today = getTodayDate()
  81. if (selectedDate.value) {
  82. const [year, month, day] = selectedDate.value.split('-')
  83. tempYear.value = parseInt(year)
  84. tempMonth.value = parseInt(month)
  85. tempDay.value = parseInt(day)
  86. } else {
  87. tempYear.value = today.year
  88. tempMonth.value = today.month
  89. tempDay.value = 0
  90. }
  91. showDatePicker.value = true
  92. // 锁定页面滚动
  93. // #ifdef H5
  94. document.body.style.overflow = 'hidden'
  95. document.body.style.position = 'fixed'
  96. document.body.style.width = '100%'
  97. // #endif
  98. }
  99. // 关闭日期选择器
  100. const closeDatePicker = () => {
  101. showDatePicker.value = false
  102. // 恢复页面滚动
  103. // #ifdef H5
  104. document.body.style.overflow = ''
  105. document.body.style.position = ''
  106. document.body.style.width = ''
  107. // #endif
  108. }
  109. // 上一个月
  110. const prevMonth = () => {
  111. if (tempMonth.value === 1) {
  112. tempYear.value--
  113. tempMonth.value = 12
  114. } else {
  115. tempMonth.value--
  116. }
  117. }
  118. // 下一个月
  119. const nextMonth = () => {
  120. const today = getTodayDate()
  121. if (tempYear.value === today.year && tempMonth.value === today.month) {
  122. return
  123. }
  124. if (tempMonth.value === 12) {
  125. tempYear.value++
  126. tempMonth.value = 1
  127. } else {
  128. tempMonth.value++
  129. }
  130. }
  131. // 生成日历天数
  132. const calendarDays = computed(() => {
  133. const firstDay = new Date(tempYear.value, tempMonth.value - 1, 1).getDay()
  134. const daysInMonth = new Date(tempYear.value, tempMonth.value, 0).getDate()
  135. const days = []
  136. for (let i = 0; i < firstDay; i++) {
  137. days.push(null)
  138. }
  139. for (let i = 1; i <= daysInMonth; i++) {
  140. days.push(i)
  141. }
  142. return days
  143. })
  144. // 判断是否选中(临时选择状态)
  145. const isSelected = (day) => {
  146. return day === tempDay.value
  147. }
  148. // 判断是否今天
  149. const isToday = (day) => {
  150. const today = getTodayDate()
  151. return day === today.day &&
  152. tempYear.value === today.year &&
  153. tempMonth.value === today.month
  154. }
  155. // 选择日期
  156. const selectDay = (day) => {
  157. tempDay.value = day
  158. }
  159. // 日期选择处理函数(用于模板)
  160. const selectDayHandler = (day) => {
  161. if (!day) return
  162. tempDay.value = day
  163. console.log('选择日期:', day)
  164. }
  165. // 确认日期
  166. const confirmDate = () => {
  167. if (!tempDay.value) {
  168. uni.showToast({ title: '请选择日期', icon: 'none' })
  169. return
  170. }
  171. const dateStr = `${tempYear.value}-${String(tempMonth.value).padStart(2, '0')}-${String(tempDay.value).padStart(2, '0')}`
  172. selectedDate.value = dateStr
  173. showDatePicker.value = false
  174. // 恢复页面滚动
  175. // #ifdef H5
  176. document.body.style.overflow = ''
  177. document.body.style.position = ''
  178. document.body.style.width = ''
  179. // #endif
  180. emit('dateChange', dateStr)
  181. }
  182. // 清除日期
  183. const clearDate = () => {
  184. selectedDate.value = ''
  185. emit('dateChange', '')
  186. }
  187. </script>
  188. <style scoped>
  189. .date-selector-card {
  190. background: #ffffff;
  191. border-radius: 16rpx;
  192. padding: 24rpx;
  193. margin-bottom: 24rpx;
  194. box-shadow: 0 4rpx 12rpx rgba(37, 52, 94, 0.04);
  195. }
  196. .selector-header {
  197. display: flex;
  198. justify-content: space-between;
  199. align-items: center;
  200. margin-bottom: 16rpx;
  201. }
  202. .selector-label {
  203. font-size: 26rpx;
  204. font-weight: 600;
  205. color: #222222;
  206. }
  207. .clear-btn {
  208. font-size: 24rpx;
  209. color: #ef4444;
  210. padding: 8rpx 16rpx;
  211. background: rgba(239, 68, 68, 0.1);
  212. border-radius: 8rpx;
  213. }
  214. .date-input {
  215. display: flex;
  216. align-items: center;
  217. justify-content: space-between;
  218. padding: 20rpx 24rpx;
  219. background: #f7f8fc;
  220. border-radius: 12rpx;
  221. border: 2rpx solid #e5e7eb;
  222. }
  223. .date-text {
  224. font-size: 28rpx;
  225. color: #222222;
  226. }
  227. .date-icon {
  228. font-size: 32rpx;
  229. }
  230. /* 日期选择器弹窗 */
  231. .date-picker-mask {
  232. position: fixed;
  233. top: 0;
  234. left: 0;
  235. right: 0;
  236. bottom: 0;
  237. background: rgba(0, 0, 0, 0.5);
  238. display: flex;
  239. align-items: flex-end;
  240. justify-content: center;
  241. z-index: 99999 !important;
  242. box-sizing: border-box;
  243. /* 阻止触摸事件穿透 */
  244. overflow: hidden;
  245. touch-action: none;
  246. }
  247. .date-picker-popup {
  248. width: 100%;
  249. max-width: 600rpx;
  250. background: #ffffff;
  251. border-radius: 24rpx 24rpx 0 0;
  252. padding: 32rpx;
  253. padding-bottom: calc(32rpx + 120rpx + constant(safe-area-inset-bottom));
  254. padding-bottom: calc(32rpx + 120rpx + env(safe-area-inset-bottom));
  255. overflow-y: auto;
  256. box-sizing: border-box;
  257. position: relative;
  258. z-index: 100000 !important;
  259. /* 限制最大高度,确保不会太高 */
  260. max-height: 65vh;
  261. /* 隐藏滚动条 */
  262. scrollbar-width: none; /* Firefox */
  263. -ms-overflow-style: none; /* IE/Edge */
  264. /* 允许弹窗内部滚动 */
  265. -webkit-overflow-scrolling: touch;
  266. touch-action: pan-y;
  267. }
  268. .date-picker-popup::-webkit-scrollbar {
  269. display: none; /* Chrome/Safari/Opera */
  270. }
  271. .picker-header {
  272. display: flex;
  273. justify-content: space-between;
  274. align-items: center;
  275. margin-bottom: 32rpx;
  276. }
  277. .picker-cancel, .picker-confirm {
  278. font-size: 28rpx;
  279. color: #5d55e8;
  280. padding: 8rpx 16rpx;
  281. }
  282. .picker-title {
  283. font-size: 32rpx;
  284. font-weight: 600;
  285. color: #222222;
  286. }
  287. .month-selector {
  288. display: flex;
  289. justify-content: space-between;
  290. align-items: center;
  291. margin-bottom: 24rpx;
  292. padding: 16rpx 0;
  293. }
  294. .month-nav {
  295. width: 64rpx;
  296. height: 64rpx;
  297. display: flex;
  298. align-items: center;
  299. justify-content: center;
  300. background: #f7f8fc;
  301. border-radius: 50%;
  302. }
  303. .nav-arrow {
  304. font-size: 48rpx;
  305. color: #5d55e8;
  306. font-weight: bold;
  307. }
  308. .current-month {
  309. font-size: 32rpx;
  310. font-weight: 600;
  311. color: #222222;
  312. }
  313. .weekday-row {
  314. display: grid;
  315. grid-template-columns: repeat(7, 1fr);
  316. gap: 8rpx;
  317. margin-bottom: 16rpx;
  318. }
  319. .weekday-item {
  320. text-align: center;
  321. font-size: 24rpx;
  322. color: #9ca3af;
  323. padding: 12rpx 0;
  324. }
  325. .days-grid {
  326. display: grid;
  327. grid-template-columns: repeat(7, 1fr);
  328. gap: 8rpx;
  329. /* 确保网格底部有足够的内边距,让最后一行日期完全可见 */
  330. padding-bottom: 80rpx;
  331. }
  332. .day-item {
  333. width: 80rpx;
  334. height: 80rpx;
  335. display: flex;
  336. align-items: center;
  337. justify-content: center;
  338. border-radius: 12rpx;
  339. cursor: pointer;
  340. transition: background-color 0.2s;
  341. }
  342. .day-item:active {
  343. background: #f0f0f0;
  344. }
  345. .day-item.empty {
  346. visibility: hidden;
  347. pointer-events: none;
  348. }
  349. .day-text {
  350. font-size: 28rpx;
  351. color: #222222;
  352. }
  353. .day-item.selected {
  354. background: #5d55e8;
  355. }
  356. .day-item.selected .day-text {
  357. color: #ffffff;
  358. font-weight: 600;
  359. }
  360. .day-item.today .day-text {
  361. color: #5d55e8;
  362. font-weight: 600;
  363. }
  364. .day-item.today.selected .day-text {
  365. color: #ffffff;
  366. }
  367. /* 底部安全区域占位 */
  368. .safe-area-placeholder {
  369. /* 移除占位元素,改用 padding-bottom 处理 */
  370. height: 0;
  371. }
  372. </style>