海南旅游项目 前端仓库
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.

657 lines
19 KiB

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