u-calendar.vue 19 KB

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