tn-calendar.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. <template>
  2. <tn-popup
  3. v-model="value"
  4. mode="bottom"
  5. :popup="false"
  6. length="auto"
  7. :borderRadius="borderRadius"
  8. :safeAreaInsetBottom="safeAreaInsetBottom"
  9. :maskCloseable="maskCloseable"
  10. :closeBtn="closeBtn"
  11. :zIndex="elIndex"
  12. @close="close"
  13. >
  14. <view class="tn-calendar-class tn-calendar">
  15. <!-- 头部 -->
  16. <view class="tn-calendar__header">
  17. <view v-if="!$slots.tooltip || !$slots.$tooltip" class="tn-calendar__header__text">
  18. {{ toolTips }}
  19. </view>
  20. <view v-else>
  21. <slot name="tooltip"></slot>
  22. </view>
  23. </view>
  24. <!-- 操作提示信息 -->
  25. <view class="tn-calendar__action">
  26. <view v-if="changeYear" class="tn-calendar__action__icon" :style="{backgroundColor: yearArrowColor}" @tap.stop="changeYearHandler(false)">
  27. <view><text class="tn-icon-left"></text></view>
  28. </view>
  29. <view v-if="changeMonth" class="tn-calendar__action__icon" :style="{backgroundColor: monthArrowColor}" @tap.stop="changeMonthHandler(false)">
  30. <view><text class="tn-icon-left"></text></view>
  31. </view>
  32. <view class="tn-calendar__action__text">{{ dateTitle }}</view>
  33. <view v-if="changeMonth" class="tn-calendar__action__icon" :style="{backgroundColor: monthArrowColor}" @tap.stop="changeMonthHandler(true)">
  34. <view><text class="tn-icon-right"></text></view>
  35. </view>
  36. <view v-if="changeYear" class="tn-calendar__action__icon" :style="{backgroundColor: yearArrowColor}" @tap.stop="changeYearHandler(true)">
  37. <view><text class="tn-icon-right"></text></view>
  38. </view>
  39. </view>
  40. <!-- 星期中文标识 -->
  41. <view class="tn-calendar__week-day-zh">
  42. <view v-for="(item,index) in weekDayZh" :key="index" class="tn-calendar__week-day-zh__text">{{ item }}</view>
  43. </view>
  44. <!-- 日历主体 -->
  45. <view class="tn-calendar__content">
  46. <!-- 前置空白部分 -->
  47. <block v-for="(item, index) in weekdayArr" :key="index">
  48. <view class="tn-calendar__content__item"></view>
  49. </block>
  50. <view
  51. v-for="(item, index) in daysArr"
  52. :key="index"
  53. class="tn-calendar__content__item"
  54. :class="{
  55. 'tn-hover': disabledChoose(year, month, index + 1),
  56. 'tn-calendar__content--start-date': (mode === 'range' && startDate == `${year}-${month}-${index+1}`) || mode === 'date',
  57. 'tn-calendar__content--end-date': (mode === 'range' && endDate == `${year}-${month}-${index+1}`) || mode === 'date'
  58. }"
  59. :style="{
  60. backgroundColor: colorValue(index, 'bg')
  61. }"
  62. @tap.stop="dateClick(index)"
  63. >
  64. <view class="tn-calendar__content__item__text" :style="{color: colorValue(index, 'text')}">
  65. <view>{{ item.day }}</view>
  66. </view>
  67. <view class="tn-calendar__content__item__tips" :style="{color: item.color}">
  68. {{ item.bottomInfo }}
  69. </view>
  70. </view>
  71. <view class="tn-calendar__content__month--bg">{{ month }}</view>
  72. </view>
  73. <!-- 底部 -->
  74. <view class="tn-calendar__bottom">
  75. <view class="tn-calendar__bottom__choose">
  76. <text>{{ mode === 'date' ? activeDate : startDate }}</text>
  77. <text v-if="endDate">至{{ endDate }}</text>
  78. </view>
  79. <view class="tn-calendar__bottom__btn" :style="{backgroundColor: btnColor}" @click="handleBtnClick(false)">
  80. <view class="tn-calendar__bottom__btn--text">确定</view>
  81. </view>
  82. </view>
  83. </view>
  84. </tn-popup>
  85. </template>
  86. <script>
  87. import Calendar from '../../libs/utils/calendar.js'
  88. export default {
  89. name: 'tn-calendar',
  90. props: {
  91. // 双向绑定控制组件弹出与收起
  92. value: {
  93. type: Boolean,
  94. default: false
  95. },
  96. // 模式
  97. // date -> 单日期 range -> 日期范围
  98. mode: {
  99. type: String,
  100. default: 'date'
  101. },
  102. // 是否允许切换年份
  103. changeYear: {
  104. type: Boolean,
  105. default: true
  106. },
  107. // 是否允许切换月份
  108. changeMonth: {
  109. type: Boolean,
  110. default: true
  111. },
  112. // 可切换的最大年份
  113. maxYear: {
  114. type: [Number, String],
  115. default: 2100
  116. },
  117. // 可切换的最小年份
  118. minYear: {
  119. type: [Number, String],
  120. default: 1970
  121. },
  122. // 最小日期(不在范围被不允许选择)
  123. minDate: {
  124. type: String,
  125. default: '1970-01-01'
  126. },
  127. // 最大日期,如果为空则默认为今天
  128. maxDate: {
  129. type: String,
  130. default: ''
  131. },
  132. // 切换月份按钮的颜色
  133. monthArrowColor: {
  134. type: String,
  135. default: '#AAAAAA'
  136. },
  137. // 切换年份按钮的颜色
  138. yearArrowColor: {
  139. type: String,
  140. default: '#C8C8C8'
  141. },
  142. // 默认字体颜色
  143. color: {
  144. type: String,
  145. default: '#080808'
  146. },
  147. // 选中|起始结束日期背景颜色
  148. activeBgColor: {
  149. type: String,
  150. default: '#01BEFF'
  151. },
  152. // 选中|起始结束日期文字颜色
  153. activeColor: {
  154. type: String,
  155. default: '#FFFFFF'
  156. },
  157. // 范围日期内的背景颜色
  158. rangeBgColor: {
  159. type: String,
  160. default: '#E6E6E655'
  161. },
  162. // 范围日期内的文字颜色
  163. rangeColor: {
  164. type: String,
  165. default: '#01BEFF'
  166. },
  167. // 起始日期显示的文字,mode=range时生效
  168. startText: {
  169. type: String,
  170. default: '开始'
  171. },
  172. // 结束日期显示的文字,mode=range时生效
  173. endText: {
  174. type: String,
  175. default: '结束'
  176. },
  177. // 按钮背景颜色
  178. btnColor: {
  179. type: String,
  180. default: '#01BEFF'
  181. },
  182. // 农历文字的颜色
  183. lunarColor: {
  184. type: String,
  185. default: '#AAAAAA'
  186. },
  187. // 选中日期是否有选中效果
  188. isActiveCurrent: {
  189. type: Boolean,
  190. default: true
  191. },
  192. // 切换年月是否触发事件,mode=date时生效
  193. isChange: {
  194. type: Boolean,
  195. default: false
  196. },
  197. // 是否显示农历
  198. showLunar: {
  199. type: Boolean,
  200. default: true
  201. },
  202. // 顶部提示文字
  203. toolTips: {
  204. type: String,
  205. default: '请选择日期'
  206. },
  207. // 显示圆角的大小
  208. borderRadius: {
  209. type: Number,
  210. default: 8
  211. },
  212. // 是否开启底部安全区适配,开启的话,会在iPhoneX机型底部添加一定的内边距
  213. safeAreaInsetBottom: {
  214. type: Boolean,
  215. default: false
  216. },
  217. // 是否可以通过点击遮罩进行关闭
  218. maskCloseable: {
  219. type: Boolean,
  220. default: true
  221. },
  222. // zIndex
  223. zIndex: {
  224. type: Number,
  225. default: 0
  226. },
  227. // 是否显示关闭按钮
  228. closeBtn: {
  229. type: Boolean,
  230. default: false
  231. },
  232. },
  233. computed: {
  234. dateChange() {
  235. return `${this.mode}-${this.minDate}-${this.maxDate}`
  236. },
  237. elIndex() {
  238. return this.zIndex ? this.zIndex : this.$t.zIndex.popup
  239. },
  240. colorValue() {
  241. return (index, type) => {
  242. let color = type === 'bg' ? '' : this.color
  243. let day = index + 1
  244. let date = `${this.year}-${this.month}-${day}`
  245. let timestamp = new Date(date.replace(/\-/g,'/')).getTime()
  246. let start = this.startDate.replace(/\-/g,'/')
  247. let end = this.endDate.replace(/\-/g,'/')
  248. if ((this.mode === 'date' && this.isActiveCurrent && this.activeDate == date) || this.startDate == date || this.endDate == date) {
  249. color = type === 'bg' ? this.activeBgColor : this.activeColor
  250. } else if (this.endDate && timestamp > new Date(start).getTime() && timestamp < new Date(end).getTime()) {
  251. color = type === 'bg' ? this.rangeBgColor : this.rangeColor
  252. }
  253. return color
  254. }
  255. }
  256. },
  257. data() {
  258. return {
  259. // 星期几,1-7
  260. weekday: 1,
  261. weekdayArr: [],
  262. // 星期对应的中文
  263. weekDayZh: ['日','一','二','三','四','五','六'],
  264. // 当前月有多少天
  265. days: 0,
  266. daysArr: [],
  267. year: 2021,
  268. month: 0,
  269. day: 0,
  270. startYear: 0,
  271. startMonth: 0,
  272. startDay: 0,
  273. endYear: 0,
  274. endMonth: 0,
  275. endDay: 0,
  276. today: '',
  277. activeDate: '',
  278. startDate: '',
  279. endDate: '',
  280. min: null,
  281. max: null,
  282. // 日期标题
  283. dateTitle: '',
  284. // 标记是否已经选择了开始日期
  285. chooseStart: false
  286. }
  287. },
  288. watch: {
  289. dateChange() {
  290. this.init()
  291. }
  292. },
  293. created() {
  294. this.init()
  295. },
  296. methods: {
  297. // 初始化
  298. init() {
  299. let now = new Date()
  300. this.year = now.getFullYear()
  301. this.month = now.getMonth() + 1
  302. this.day = now.getDate()
  303. this.today = `${this.year}-${this.month}-${this.day}`
  304. this.activeDate = this.today
  305. this.min = this.initDate(this.minDate)
  306. this.max = this.initDate(this.maxDate || this.today)
  307. this.startDate = ''
  308. this.startYear = 0
  309. this.startMonth = 0
  310. this.startDay = 0
  311. this.endDate = ''
  312. this.endYear = 0
  313. this.endMonth = 0
  314. this.endDay = 0
  315. this.chooseStart = false
  316. this.changeData()
  317. },
  318. // 切换月份
  319. changeMonthHandler(add) {
  320. if (add) {
  321. let month = this.month + 1
  322. let year = month > 12 ? this.year + 1 : this.year
  323. if (!this.checkRange(year)) {
  324. this.month = month > 12 ? 1 : month
  325. this.year = year
  326. this.changeData()
  327. }
  328. } else {
  329. let month = this.month - 1
  330. let year = month < 1 ? this.year - 1 : this.year
  331. if (!this.checkRange(year)) {
  332. this.month = month < 1 ? 12 : month
  333. this.year = year
  334. this.changeData()
  335. }
  336. }
  337. },
  338. // 切换年份
  339. changeYearHandler(add) {
  340. let year = add ? this.year + 1 : this.year - 1
  341. if (!this.checkRange(year)) {
  342. this.year = year
  343. this.changeData()
  344. }
  345. },
  346. // 日期点击事件
  347. dateClick(day) {
  348. day += 1
  349. if (!this.disabledChoose(this.year, this.month, day)) {
  350. this.day = day
  351. let date = `${this.year}-${this.month}-${day}`
  352. if (this.mode === 'date') {
  353. this.activeDate = date
  354. } else {
  355. let startTimeCompare = new Date(date.replace(/\-/g,'/')).getTime() < new Date(this.startDate.replace(/\-/g,'/')).getTime()
  356. if (!this.chooseStart || startTimeCompare) {
  357. this.startDate = date
  358. this.startYear = this.year
  359. this.startMonth = this.month
  360. this.startDay = this.day
  361. this.endYear = 0
  362. this.endMonth = 0
  363. this.endDay = 0
  364. this.endDate = ''
  365. this.activeDate = ''
  366. this.chooseStart = true
  367. } else {
  368. this.endDate = date
  369. this.endYear = this.year
  370. this.endMonth = this.month
  371. this.endDay = this.day
  372. this.chooseStart = false
  373. }
  374. }
  375. this.daysArr = this.handleDaysArr()
  376. }
  377. },
  378. // 修改日期数据
  379. changeData() {
  380. this.days = this.getMonthDay(this.year, this.month)
  381. this.daysArr = this.handleDaysArr()
  382. this.weekday = this.getMonthFirstWeekDay(this.year, this.month)
  383. this.weekdayArr = this.generateArray(1, this.weekday)
  384. this.dateTitle = `${this.year}年${this.month}月`
  385. if (this.isChange && this.mode === 'date') {
  386. this.handleBtnClick(true)
  387. }
  388. },
  389. // 处理按钮点击
  390. handleBtnClick(show) {
  391. if (!show) {
  392. this.close()
  393. }
  394. if (this.mode === 'date') {
  395. let arr = this.activeDate.split('-')
  396. let year = this.isChange ? this.year : Number(arr[0])
  397. let month = this.isChange ? this.month : Number(arr[1])
  398. let day = this.isChange ? this.day : Number(arr[2])
  399. let days = this.getMonthDay(year, month)
  400. let result = `${year}-${this.formatNumber(month)}-${this.formatNumber(day)}`
  401. let weekText = this.getWeekText(result)
  402. let isToday = false
  403. if (`${year}-${month}-${day}` === this.today) {
  404. isToday = true
  405. }
  406. this.$emit('change', {
  407. year,
  408. month,
  409. day,
  410. days,
  411. week: weekText,
  412. isToday,
  413. date: result,
  414. // 是否为切换年月操作
  415. switch: show
  416. })
  417. } else {
  418. if (!this.startDate || !this.endDate) return
  419. let startMonth = this.formatNumber(this.startMonth)
  420. let startDay = this.formatNumber(this.startDay)
  421. let startDate = `${this.startYear}-${startMonth}-${startDay}`
  422. let startWeek = this.getWeekText(startDate)
  423. let endMonth = this.formatNumber(this.endMonth)
  424. let endDay = this.formatNumber(this.endDay)
  425. let endDate = `${this.endYear}-${endMonth}-${endDay}`
  426. let endWeek = this.getWeekText(endDate)
  427. this.$emit('change', {
  428. startYear: this.startYear,
  429. startMonth: this.startMonth,
  430. startDay: this.startDay,
  431. startDate,
  432. startWeek,
  433. endYear: this.endYear,
  434. endMonth: this.endMonth,
  435. endDay: this.endDay,
  436. endDate,
  437. endWeek
  438. })
  439. }
  440. },
  441. // 判断是否允许选择
  442. disabledChoose(year, month, day) {
  443. let flag = true
  444. let date = `${year}/${month}/${day}`
  445. let min = `${this.min.year}/${this.min.month}/${this.min.day}`
  446. let max = `${this.max.year}/${this.max.month}/${this.max.day}`
  447. let timestamp = new Date(date).getTime()
  448. if (timestamp >= new Date(min).getTime() && timestamp <= new Date(max).getTime()) {
  449. flag = false
  450. }
  451. return flag
  452. },
  453. // 检查是否在日期范围内
  454. checkRange(year) {
  455. let overstep = false
  456. if (year < this.minYear || year > this.maxYear) {
  457. uni.showToast({
  458. title: '所选日期超出范围',
  459. icon: 'none'
  460. })
  461. overstep = true
  462. }
  463. return overstep
  464. },
  465. // 处理日期
  466. initDate(date) {
  467. let fdate = date.split('-')
  468. return {
  469. year: Number(fdate[0] || 1970),
  470. month: Number(fdate[1] || 1),
  471. day: Number(fdate[2] || 1)
  472. }
  473. },
  474. // 处理日期数组
  475. handleDaysArr() {
  476. let days = this.generateArray(1, this.days)
  477. let daysArr = days.map((item) => {
  478. let bottomInfo = this.showLunar ? Calendar.solar2lunar(this.year, this.month, item).IDayCn : ''
  479. let color = this.showLunar ? this.lunarColor : this.activeColor
  480. if (
  481. (this.mode === 'date' && this.day == item) ||
  482. (this.mode === 'range' && (this.startDay == item || this.endDay == item))
  483. ) {
  484. color = this.activeColor
  485. }
  486. if (this.mode === 'range') {
  487. if (this.startDay == item && this.startDay != this.endDay) {
  488. bottomInfo = this.startText
  489. }
  490. if (this.endDay == item) {
  491. bottomInfo = this.endText
  492. }
  493. }
  494. return {
  495. day: item,
  496. color: color,
  497. bottomInfo: bottomInfo
  498. }
  499. })
  500. return daysArr
  501. },
  502. // 获取对应月有多少天
  503. getMonthDay(year, month) {
  504. return new Date(year, month, 0).getDate()
  505. },
  506. // 获取对应月的第一天时星期几
  507. getMonthFirstWeekDay(year, month) {
  508. return new Date(`${year}/${month}/01 00:00:00`).getDay()
  509. },
  510. // 获取对应星期的文本
  511. getWeekText(date) {
  512. date = new Date(`${date.replace(/\-/g, '/')} 00:00:00`)
  513. let week = date.getDay()
  514. return '星期' + this.weekDayZh[week]
  515. },
  516. // 生成日期天数数组
  517. generateArray(start, end) {
  518. return Array.from(new Array(end + 1).keys()).slice(start)
  519. },
  520. // 格式化数字
  521. formatNumber(num) {
  522. return num < 10 ? '0' + num : num + ''
  523. },
  524. // 关闭窗口
  525. close() {
  526. this.$emit('input', false)
  527. }
  528. }
  529. }
  530. </script>
  531. <style lang="scss" scoped>
  532. .tn-calendar {
  533. color: $tn-font-color;
  534. &__header {
  535. width: 100%;
  536. box-sizing: border-box;
  537. font-size: 30rpx;
  538. background-color: #FFFFFF;
  539. color: $tn-main-color;
  540. &__text {
  541. display: flex;
  542. flex-direction: row;
  543. align-items: center;
  544. justify-content: center;
  545. margin-top: 30rpx;
  546. padding: 0 60rpx;
  547. }
  548. }
  549. &__action {
  550. display: flex;
  551. flex-direction: row;
  552. justify-content: center;
  553. align-items: center;
  554. padding: 40rpx 0 40rpx 0;
  555. &__icon {
  556. display: flex;
  557. align-items: center;
  558. justify-content: center;
  559. margin: 0 16rpx;
  560. width: 32rpx;
  561. height: 32rpx;
  562. font-size: 20rpx;
  563. // line-height: 32rpx;
  564. border-radius: 50%;
  565. color: #FFFFFF;
  566. }
  567. &__text {
  568. padding: 0 16rpx;
  569. color: $tn-font-color;
  570. font-size: 32rpx;
  571. font-weight: bold;
  572. }
  573. }
  574. &__week-day-zh {
  575. display: flex;
  576. flex-direction: row;
  577. align-items: center;
  578. justify-content: center;
  579. padding: 12rpx 0;
  580. overflow: hidden;
  581. box-shadow: 16rpx 6rpx 8rpx 0 #E6E6E6;
  582. margin-bottom: 2rpx;
  583. &__text {
  584. flex: 1;
  585. text-align: center;
  586. }
  587. }
  588. &__content {
  589. display: flex;
  590. flex-direction: row;
  591. flex-wrap: wrap;
  592. width: 100%;
  593. padding: 12rpx 0;
  594. box-sizing: border-box;
  595. background-color: #F7F7F7;
  596. position: relative;
  597. &__item {
  598. display: flex;
  599. flex-direction: column;
  600. align-items: center;
  601. justify-content: center;
  602. width: 14.2857%;
  603. padding: 12rpx 0;
  604. margin: 6rpx 0;
  605. overflow: hidden;
  606. position: relative;
  607. z-index: 2;
  608. // box-shadow: inset 0rpx 0rpx 22rpx 4rpx rgba(255,255,255, 0.52);
  609. &__text {
  610. display: flex;
  611. flex-direction: column;
  612. align-items: center;
  613. justify-content: center;
  614. height: 80rpx;
  615. font-size: 32rpx;
  616. position: relative;
  617. }
  618. &__tips {
  619. position: absolute;
  620. width: 100%;
  621. line-height: 24rpx;
  622. left: 0;
  623. bottom: 8rpx;
  624. text-align: center;
  625. z-index: 2;
  626. transform-origin: center center;
  627. transform: scale(0.8);
  628. }
  629. }
  630. &--start-date {
  631. border-top-left-radius: 8rpx;
  632. border-bottom-left-radius: 8rpx;
  633. }
  634. &--end-date {
  635. border-top-right-radius: 8rpx;
  636. border-bottom-right-radius: 8rpx;
  637. }
  638. &__month {
  639. &--bg {
  640. position: absolute;
  641. font-size: 200rpx;
  642. line-height: 200rpx;
  643. left: 50%;
  644. top: 50%;
  645. transform: translate(-50%, -50%);
  646. color: $tn-font-holder-color;
  647. z-index: 1;
  648. }
  649. }
  650. }
  651. &__bottom {
  652. display: flex;
  653. flex-direction: column;
  654. align-items: center;
  655. justify-content: center;
  656. width: 100%;
  657. background-color: #F7F7F7;
  658. padding: 0 40rpx 30rpx;
  659. box-sizing: border-box;
  660. font-size: 24rpx;
  661. color: $tn-font-sub-color;
  662. &__choose {
  663. height: 50rpx;
  664. }
  665. &__btn {
  666. display: flex;
  667. align-items: center;
  668. justify-content: center;
  669. width: 100%;
  670. height: 60rpx;
  671. border-radius: 40rpx;
  672. color: #FFFFFF;
  673. font-size: 28rpx;
  674. }
  675. }
  676. }
  677. </style>