u-calendar.vue 18 KB

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