u-calendar.vue 16 KB

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