u-calendar.vue 18 KB

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