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

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