practice-time-picker.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <template>
  2. <view v-if="visible" class="picker-mask" @click="handleMaskClick">
  3. <view class="picker-container" @click.stop>
  4. <!-- 头部 -->
  5. <view class="picker-header">
  6. <text class="picker-title">从业时间选择</text>
  7. <text class="picker-cancel" @click="handleCancel">取消</text>
  8. </view>
  9. <!-- 日期选择列 -->
  10. <view class="picker-content">
  11. <picker-view
  12. class="picker-view"
  13. :value="pickerValue"
  14. @change="handlePickerChange"
  15. :indicator-style="indicatorStyle"
  16. :mask-style="maskStyle"
  17. >
  18. <!-- 年列 -->
  19. <picker-view-column>
  20. <view
  21. v-for="(year, index) in years"
  22. :key="index"
  23. class="picker-item"
  24. :class="{ 'picker-item-selected': pickerValue[0] === index }"
  25. >
  26. {{ year }}年
  27. </view>
  28. </picker-view-column>
  29. <!-- 月列 -->
  30. <picker-view-column>
  31. <view
  32. v-for="(month, index) in months"
  33. :key="index"
  34. class="picker-item"
  35. :class="{ 'picker-item-selected': pickerValue[1] === index }"
  36. >
  37. {{ month }}月
  38. </view>
  39. </picker-view-column>
  40. <!-- 日列 -->
  41. <picker-view-column>
  42. <view
  43. v-for="(day, index) in days"
  44. :key="index"
  45. class="picker-item"
  46. :class="{ 'picker-item-selected': pickerValue[2] === index }"
  47. >
  48. {{ day }}日
  49. </view>
  50. </picker-view-column>
  51. </picker-view>
  52. </view>
  53. <!-- 底部按钮 -->
  54. <view class="picker-footer">
  55. <view class="picker-btn reset-btn" @click="handleReset">重置</view>
  56. <view class="picker-btn confirm-btn" @click="handleConfirm">确定</view>
  57. </view>
  58. </view>
  59. </view>
  60. </template>
  61. <script>
  62. export default {
  63. name: 'PracticeTimePicker',
  64. props: {
  65. visible: {
  66. type: Boolean,
  67. default: false
  68. },
  69. value: {
  70. type: String,
  71. default: ''
  72. }
  73. },
  74. data() {
  75. const now = new Date()
  76. return {
  77. pickerValue: [0, 0, 0],
  78. selectedYear: now.getFullYear(),
  79. selectedMonth: now.getMonth() + 1,
  80. selectedDay: now.getDate(),
  81. originalYear: now.getFullYear(),
  82. originalMonth: now.getMonth() + 1,
  83. originalDay: now.getDate(),
  84. indicatorStyle: 'height: 50px; background-color: rgba(0, 122, 255, 0.15);',
  85. maskStyle: 'background-color: rgba(255, 255, 255, 0.8);',
  86. startYear: 1950,
  87. endYear: now.getFullYear()
  88. }
  89. },
  90. computed: {
  91. years() {
  92. const years = []
  93. for (let i = this.startYear; i <= this.endYear; i++) {
  94. years.push(i)
  95. }
  96. return years
  97. },
  98. months() {
  99. return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  100. },
  101. days() {
  102. const daysInMonth = new Date(this.selectedYear, this.selectedMonth, 0).getDate()
  103. const days = []
  104. for (let i = 1; i <= daysInMonth; i++) {
  105. days.push(i)
  106. }
  107. return days
  108. }
  109. },
  110. watch: {
  111. visible(newVal) {
  112. if (newVal) {
  113. // 延迟初始化,确保 DOM 已渲染
  114. this.$nextTick(() => {
  115. this.initPicker()
  116. })
  117. }
  118. },
  119. value(newVal) {
  120. if (newVal && this.visible) {
  121. this.parseValue(newVal)
  122. this.$nextTick(() => {
  123. this.updatePickerValue()
  124. })
  125. }
  126. },
  127. selectedMonth() {
  128. // 月份变化时,需要重新计算天数
  129. this.$nextTick(() => {
  130. this.updateDayIndex()
  131. })
  132. },
  133. selectedYear() {
  134. // 年份变化时,需要重新计算天数(考虑闰年)
  135. this.$nextTick(() => {
  136. this.updateDayIndex()
  137. })
  138. }
  139. },
  140. methods: {
  141. initPicker() {
  142. if (this.value) {
  143. this.parseValue(this.value)
  144. } else {
  145. const now = new Date()
  146. this.selectedYear = now.getFullYear()
  147. this.selectedMonth = now.getMonth() + 1
  148. this.selectedDay = now.getDate()
  149. }
  150. this.originalYear = this.selectedYear
  151. this.originalMonth = this.selectedMonth
  152. this.originalDay = this.selectedDay
  153. // 等待计算属性更新后再设置 pickerValue
  154. this.$nextTick(() => {
  155. this.updatePickerValue()
  156. })
  157. },
  158. parseValue(value) {
  159. // 解析日期字符串,支持格式:YYYY-MM-DD 或 YYYY年MM月DD日
  160. let year, month, day
  161. if (value.includes('-')) {
  162. const parts = value.split('-')
  163. year = parseInt(parts[0])
  164. month = parseInt(parts[1])
  165. day = parseInt(parts[2])
  166. } else if (value.includes('年')) {
  167. const yearMatch = value.match(/(\d+)年/)
  168. const monthMatch = value.match(/(\d+)月/)
  169. const dayMatch = value.match(/(\d+)日/)
  170. year = yearMatch ? parseInt(yearMatch[1]) : new Date().getFullYear()
  171. month = monthMatch ? parseInt(monthMatch[1]) : new Date().getMonth() + 1
  172. day = dayMatch ? parseInt(dayMatch[1]) : new Date().getDate()
  173. } else {
  174. const now = new Date()
  175. year = now.getFullYear()
  176. month = now.getMonth() + 1
  177. day = now.getDate()
  178. }
  179. this.selectedYear = year
  180. this.selectedMonth = month
  181. this.selectedDay = day
  182. },
  183. updatePickerValue() {
  184. try {
  185. const yearIndex = this.years.indexOf(this.selectedYear)
  186. const monthIndex = this.months.indexOf(this.selectedMonth)
  187. const dayIndex = this.days.indexOf(this.selectedDay)
  188. this.pickerValue = [
  189. yearIndex >= 0 ? yearIndex : 0,
  190. monthIndex >= 0 ? monthIndex : 0,
  191. dayIndex >= 0 ? dayIndex : 0
  192. ]
  193. } catch (error) {
  194. console.error('updatePickerValue error:', error)
  195. // 如果出错,使用默认值
  196. this.pickerValue = [0, 0, 0]
  197. }
  198. },
  199. updateDayIndex() {
  200. // 如果当前选中的日期超过了新月份的天数,调整为该月最后一天
  201. this.$nextTick(() => {
  202. const daysInMonth = new Date(this.selectedYear, this.selectedMonth, 0).getDate()
  203. if (this.selectedDay > daysInMonth) {
  204. this.selectedDay = daysInMonth
  205. }
  206. this.updatePickerValue()
  207. })
  208. },
  209. handlePickerChange(e) {
  210. const values = e.detail.value
  211. if (!values || values.length < 3) {
  212. return
  213. }
  214. const newYear = this.years[values[0]]
  215. const newMonth = this.months[values[1]]
  216. // 如果年份或月份变化,需要重新计算天数
  217. if (newYear !== this.selectedYear || newMonth !== this.selectedMonth) {
  218. this.selectedYear = newYear
  219. this.selectedMonth = newMonth
  220. // 重新计算天数,确保日期不超过当月最大天数
  221. const daysInMonth = new Date(this.selectedYear, this.selectedMonth, 0).getDate()
  222. if (this.selectedDay > daysInMonth) {
  223. this.selectedDay = daysInMonth
  224. }
  225. // 更新日期索引
  226. this.$nextTick(() => {
  227. const dayIndex = this.days.indexOf(this.selectedDay)
  228. this.pickerValue = [values[0], values[1], dayIndex >= 0 ? dayIndex : 0]
  229. })
  230. } else {
  231. // 只有日期变化,需要确保索引有效
  232. const dayIndex = Math.min(values[2], this.days.length - 1)
  233. this.selectedDay = this.days[dayIndex] || 1
  234. this.pickerValue = [values[0], values[1], dayIndex]
  235. }
  236. },
  237. handleMaskClick() {
  238. this.handleCancel()
  239. },
  240. handleCancel() {
  241. // 恢复原始值
  242. this.selectedYear = this.originalYear
  243. this.selectedMonth = this.originalMonth
  244. this.selectedDay = this.originalDay
  245. this.updatePickerValue()
  246. this.$emit('close')
  247. },
  248. handleReset() {
  249. const now = new Date()
  250. this.selectedYear = now.getFullYear()
  251. this.selectedMonth = now.getMonth() + 1
  252. this.selectedDay = now.getDate()
  253. this.updatePickerValue()
  254. },
  255. handleConfirm() {
  256. // 格式化日期字符串,确保月份和日期是两位数
  257. const monthStr = this.selectedMonth < 10 ? `0${this.selectedMonth}` : `${this.selectedMonth}`
  258. const dayStr = this.selectedDay < 10 ? `0${this.selectedDay}` : `${this.selectedDay}`
  259. const dateStr = `${this.selectedYear}-${monthStr}-${dayStr}`
  260. this.$emit('confirm', dateStr)
  261. this.$emit('close')
  262. }
  263. }
  264. }
  265. </script>
  266. <style lang="scss" scoped>
  267. .picker-mask {
  268. position: fixed;
  269. top: 0;
  270. left: 0;
  271. right: 0;
  272. bottom: 0;
  273. background-color: rgba(0, 0, 0, 0.5);
  274. z-index: 1000;
  275. display: flex;
  276. align-items: flex-end;
  277. }
  278. .picker-container {
  279. width: 100%;
  280. background-color: #ffffff;
  281. border-radius: 32rpx 32rpx 0 0;
  282. overflow: hidden;
  283. animation: slideUp 0.3s ease-out;
  284. }
  285. @keyframes slideUp {
  286. from {
  287. transform: translateY(100%);
  288. }
  289. to {
  290. transform: translateY(0);
  291. }
  292. }
  293. .picker-header {
  294. display: flex;
  295. align-items: center;
  296. justify-content: center;
  297. height: 88rpx;
  298. padding: 0 60rpx;
  299. position: relative;
  300. border-bottom: 1rpx solid #f0f0f0;
  301. }
  302. .picker-title {
  303. font-size: 36rpx;
  304. font-weight: 500;
  305. color: #000000;
  306. }
  307. .picker-cancel {
  308. position: absolute;
  309. right: 60rpx;
  310. font-size: 32rpx;
  311. color: #999999;
  312. }
  313. .picker-content {
  314. height: 400rpx;
  315. padding: 20rpx 0;
  316. }
  317. .picker-view {
  318. height: 100%;
  319. width: 100%;
  320. }
  321. .picker-item {
  322. height: 50px;
  323. line-height: 50px;
  324. display: flex;
  325. align-items: center;
  326. justify-content: center;
  327. font-size: 32rpx;
  328. color: #333333;
  329. text-align: center;
  330. }
  331. .picker-item-selected {
  332. color: #007AFF !important;
  333. font-weight: 600;
  334. }
  335. .picker-footer {
  336. display: flex;
  337. padding: 30rpx;
  338. gap: 20rpx;
  339. border-top: 1rpx solid #f0f0f0;
  340. }
  341. .picker-btn {
  342. flex: 1;
  343. height: 88rpx;
  344. display: flex;
  345. align-items: center;
  346. justify-content: center;
  347. border-radius: 16rpx;
  348. font-size: 32rpx;
  349. font-weight: 400;
  350. }
  351. .reset-btn {
  352. background-color: #ffffff;
  353. border: 2rpx solid #007AFF;
  354. color: #000000;
  355. }
  356. .confirm-btn {
  357. background-color: #007AFF;
  358. color: #ffffff;
  359. }
  360. </style>