weekPicker.vue 15 KB

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