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