u-calendar.vue 19 KB

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