海南旅游项目 前端仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

651 lines
19 KiB

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