s-calendar-select.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <template>
  2. <view>
  3. <!-- <view style="height: 24px;"></view> -->
  4. <scroll-view :scroll-y="true" :style="'height: '+[scrollHeight]+'rpx;'" @scrolltolower="scrollTolower"
  5. :lower-threshold="500">
  6. <view class="calendar" v-for="(item,index) in dayList" :key="index">
  7. <view class="year-month">{{item.year+'年'+item.month+'月'}}</view>
  8. <view class="weeks">
  9. <view v-for="(item,index) in week" :key="index">{{item}}</view>
  10. </view>
  11. <view class="line">
  12. </view>
  13. <view class="days">
  14. <view class="day" :class="getClass(item.year,item.month,ite)" v-for="(ite,ind) in item.day"
  15. :key="ind" :style="''+['width:'+dayWidth+'px;height:80rpx']"
  16. @click="selDay(item.year,item.month,ite)">
  17. <view class="dd" :style="'height: '+[dayWidth]+'rpx;'">{{ite}}</view>
  18. </view>
  19. </view>
  20. </view>
  21. </scroll-view>
  22. </view>
  23. </template>
  24. <script>
  25. export default {
  26. name: "s-calendar1",
  27. data() {
  28. return {
  29. isChecked: null,
  30. //日历
  31. year: '', //当前年份
  32. month: '', //当前月份
  33. day: '', //当前天
  34. week: ['日', '一', '二', '三', '四', '五', '六'],
  35. dayList: [], //列表
  36. startYear: '', //选择开始年份
  37. startMonth: '', //选择开始月份
  38. startDay: '', //选择开始天
  39. endYear: '', //选择结束年份
  40. endMonth: '', //选择结束月份
  41. endDay: '', //选择结束日
  42. dayWidth: '',
  43. // 合并后的日期
  44. startDate: '',
  45. endDate: '',
  46. // 是否是同一天
  47. isSameDay: false
  48. };
  49. },
  50. computed: {
  51. //总选择的天数
  52. allDay: function() {
  53. if (!this.startYear || !this.startMonth || !this.startDay || !this.endYear || !this.endMonth || !this
  54. .endDay) {
  55. return 0;
  56. }
  57. let start = new Date(this.startYear, this.startMonth - 1, this.startDay);
  58. let end = new Date(this.endYear, this.endMonth - 1, this.endDay);
  59. return this.countDays(start, end);
  60. },
  61. //组件高度,默认全屏高度
  62. scrollHeight: function() {
  63. // return uni.getSystemInfoSync().windowHeight - 24;
  64. return 500
  65. }
  66. },
  67. created() {
  68. this.dayWidth = uni.getSystemInfoSync().screenWidth / 7;
  69. this.setNow();
  70. this.setDay()
  71. },
  72. methods: {
  73. /**
  74. * 设置当前年月日
  75. */
  76. setNow() {
  77. let date = new Date();
  78. this.year = date.getFullYear();
  79. this.month = date.getMonth() + 1;
  80. this.day = date.getDate();
  81. },
  82. /**
  83. * 设置日历天,当前月分和后两个月
  84. */
  85. setDay() {
  86. //将日历增加3个月
  87. for (var j = 0; j < 3; j++) {
  88. let y = this.getNextNDate(this.year, this.month, j);
  89. let date = new Date(y.year, y.month - 1, 1);
  90. let date1 = new Date(y.year, y.month, 0);
  91. //获取当前月第一天是周几,0等于周日
  92. let oneDay = date.getDay();
  93. //获取当前月天数
  94. let monthDay = date1.getDate();
  95. let dayList = [];
  96. for (var i = 0; i < oneDay; i++) {
  97. dayList.push('')
  98. }
  99. for (var i = 0; i < monthDay; i++) {
  100. dayList.push(i + 1)
  101. }
  102. this.dayList.push({
  103. year: y.year,
  104. month: y.month,
  105. day: dayList
  106. })
  107. }
  108. },
  109. /**
  110. * 获取选中样式
  111. */
  112. getClass(year, month, day) {
  113. // 今天之前的样式
  114. let fullYear = new Date(year, month - 1, day)
  115. let yy = fullYear.getFullYear() + ''
  116. let mm = fullYear.getMonth() + 1 < 10 ? '0' + (fullYear.getMonth() + 1) : fullYear.getMonth() + 1
  117. let dd = fullYear.getDate() < 10 ? '0' + fullYear.getDate() : fullYear.getDate() + ''
  118. let yymmdd = yy + mm + dd
  119. let Dtoday = new Date()
  120. let YY = Dtoday.getFullYear() + ''
  121. let MM = Dtoday.getMonth() + 1 < 10 ? '0' + (Dtoday.getMonth() + 1) : Dtoday.getMonth() + 1
  122. let DD = Dtoday.getDate() < 10 ? '0' + Dtoday.getDate() : Dtoday.getDate() + ''
  123. let YYMMDD = YY + MM + DD
  124. if (yymmdd < YYMMDD) {
  125. return 'yestoday'
  126. }
  127. // 今天的样式
  128. let allDay = fullYear.toDateString()
  129. let today = Dtoday.toDateString()
  130. if (today === allDay) {
  131. return 'today'
  132. }
  133. if (!year || !month || !day) {
  134. return '';
  135. }
  136. //只选择了1天
  137. if (!this.endYear || !this.endMonth || !this.endDay) {
  138. if (this.startYear == year && this.startMonth == month && this.startDay == day) {
  139. return 'day-yuan'
  140. } else {
  141. return '';
  142. }
  143. } else {
  144. //选择了多天
  145. let startTime = new Date(this.startYear, this.startMonth - 1, this.startDay).getTime()
  146. let endTime = new Date(this.endYear, this.endMonth - 1, this.endDay).getTime()
  147. let nowTime = new Date(year, month - 1, day).getTime();
  148. if (startTime == nowTime) {
  149. if (this.isSameDay) {
  150. return 'sameDay'
  151. } else {
  152. return 'day-left';
  153. }
  154. }
  155. if (endTime == nowTime) {
  156. return 'day-right';
  157. }
  158. if (startTime < nowTime && endTime > nowTime) {
  159. return 'day-none'
  160. }
  161. if (startTime === nowTime && endTime === nowTime) {
  162. console.log(111);
  163. }
  164. }
  165. },
  166. selDay(year, month, day) {
  167. if (!this.startYear || !this.startMonth || !this.startDay) {
  168. let startTime = new Date(year, month - 1, day) //day+1可以选择当日
  169. let nowTime = new Date()
  170. if (startTime < nowTime) {
  171. uni.showToast({
  172. title: '开始日期不能早于等于当前日期',
  173. icon: 'none'
  174. })
  175. return;
  176. } else {
  177. this.startYear = year;
  178. this.startMonth = month;
  179. this.startDay = day;
  180. }
  181. this.$emit('getDate', 'noEndDate')
  182. } else if (!this.endYear || !this.endMonth || !this.endDay) {
  183. let startTime = new Date(this.startYear, this.startMonth - 1, this.startDay).getTime()
  184. let endTime = new Date(year, month - 1, day).getTime()
  185. if (endTime < startTime) {
  186. uni.showToast({
  187. title: '运输结束日期不能早于等于当前日期',
  188. icon: 'none'
  189. })
  190. return;
  191. }
  192. if (endTime == startTime) {
  193. // 不能选择当天
  194. this.startYear = '';
  195. this.startMonth = '';
  196. this.startDay = '';
  197. // this.endYear = this.startYear
  198. // this.endMonth = this.startMonth
  199. // this.endDay = this.startDay
  200. // 可以选择当天
  201. // this.endYear = year;
  202. // this.endMonth = month;
  203. // this.endDay = day;
  204. // console.log(this.startDay);
  205. // console.log(this.endDay);
  206. // this.isSameDay = true
  207. // console.log(this.isSameDay);
  208. return;
  209. }
  210. this.endYear = year;
  211. this.endMonth = month;
  212. this.endDay = day;
  213. this.startDate = this.startYear + '-' + this.startMonth + '-' + this.startDay
  214. this.endDate = this.endYear + '-' + this.endMonth + '-' + this.endDay
  215. // console.log('开始日期:' + this.startYear + '-' + this.startMonth + '-' + this.startDay);
  216. // console.log('结束日期:' + this.endYear + '-' + this.endMonth + '-' + this.endDay);
  217. // console.log('共' + this.allDay + '天');
  218. this.$emit('getDate', {
  219. startDate: this.startDate,
  220. endDate: this.endDate
  221. })
  222. } else {
  223. this.$emit('getDate', 'else')
  224. let startTime = new Date(year, month - 1, day + 1).getTime()
  225. let nowTime = new Date().getTime()
  226. if (startTime < nowTime) {
  227. uni.showToast({
  228. title: '开始时间不能小于当前时间',
  229. icon: 'none'
  230. })
  231. return;
  232. } else {
  233. this.endYear = '';
  234. this.endMonth = '';
  235. this.endDay = '';
  236. this.startYear = year;
  237. this.startMonth = month;
  238. this.startDay = day;
  239. }
  240. }
  241. },
  242. /**
  243. * 滚动到底
  244. */
  245. scrollTolower() {
  246. let y = this.getNextNDate(this.year, this.month, 3);
  247. this.year = y.year;
  248. this.month = y.month;
  249. this.setDay()
  250. },
  251. /**
  252. * 计算天数
  253. */
  254. countDays(startTime, endTime) {
  255. return parseInt((endTime.getTime() - startTime.getTime()) / 24 / 60 / 60 / 1000)
  256. },
  257. /**
  258. * 获取year,month后n天的日期
  259. */
  260. getNextNDate(year, month, n) {
  261. let m = 0;
  262. if (n > 12) {
  263. m = parseInt(n / 12);
  264. year += m;
  265. }
  266. let mm = n - m * 12;
  267. if (month + mm > 12) {
  268. year += 1;
  269. month = month + mm - 12;
  270. } else {
  271. month += mm;
  272. }
  273. return {
  274. year: year,
  275. month: month
  276. }
  277. }
  278. },
  279. }
  280. </script>
  281. <style>
  282. /* 同一天的样式 */
  283. .sameDay {
  284. border-radius: 100% 100% 100% 100%;
  285. background: rgba(127, 181, 255, 1);
  286. color: #ffffff !important;
  287. }
  288. .sameDay::after {
  289. display: block;
  290. content: '开始/结束';
  291. color: #ffffff;
  292. font-size: 18rpx;
  293. font-weight: 400;
  294. position: absolute;
  295. top: 50rpx;
  296. left: 18rpx;
  297. }
  298. .line {
  299. background: rgba(153, 153, 153, 1);
  300. /* height: 2rpx; */
  301. width: 100%;
  302. margin-top: 27rpx;
  303. }
  304. .weeks {
  305. width: 100%;
  306. height: 24px;
  307. display: flex;
  308. justify-content: space-around;
  309. /* border-top: 2rpx solid #EBEBEB;
  310. border-bottom: 2rpx solid #EBEBEB; */
  311. line-height: 30px;
  312. font-size: 30rpx;
  313. font-weight: bold;
  314. color: #00000 !important;
  315. /* position: fixed; */
  316. top: var(--window-top);
  317. /* background: #FFFFFF; */
  318. z-index: 9;
  319. }
  320. .calendar {
  321. border-bottom: 16rpx solid #F7F7F7;
  322. padding-bottom: 40rpx;
  323. }
  324. .calendar:nth-last-child(1) {
  325. border: none;
  326. }
  327. .year-month {
  328. height: 40rpx;
  329. font-size: 30rpx;
  330. font-family: PingFangSC-Regular, PingFang SC;
  331. font-weight: bold;
  332. color: #000000;
  333. line-height: 44rpx;
  334. text-align: center;
  335. margin: 29rpx 0;
  336. }
  337. .days {
  338. display: flex;
  339. flex-wrap: wrap;
  340. }
  341. .day {
  342. text-align: center;
  343. /* margin-top: 40rpx; */
  344. position: relative;
  345. }
  346. .today {
  347. color: #FFB900 !important;
  348. }
  349. .yestoday {
  350. color: #cccccc !important;
  351. }
  352. .today::after {
  353. display: block;
  354. content: '今天';
  355. color: #FFB900 !important;
  356. font-size: 20rpx;
  357. font-weight: 400;
  358. position: absolute;
  359. top: 50rpx;
  360. left: 34rpx;
  361. }
  362. .day-yuan {
  363. border-radius: 100%;
  364. background: rgba(127, 181, 255, 1);
  365. color: #ffffff !important;
  366. }
  367. .day-yuan::after {
  368. display: block;
  369. content: '开始';
  370. color: #ffffff !important;
  371. font-size: 20rpx;
  372. font-weight: 400;
  373. position: absolute;
  374. top: 50rpx;
  375. left: 34rpx;
  376. }
  377. .col-fff {
  378. color: #fff !important;
  379. }
  380. .col-333 {
  381. color: #333 !important;
  382. }
  383. .col-999 {
  384. color: #999999 !important;
  385. }
  386. .day-left {
  387. border-radius: 100% 0 0 100%;
  388. background: rgba(127, 181, 255, 1);
  389. color: #ffffff !important;
  390. }
  391. .day-left::after {
  392. display: block;
  393. content: '开始';
  394. color: #ffffff;
  395. font-size: 20rpx;
  396. font-weight: 400;
  397. position: absolute;
  398. top: 50rpx;
  399. left: 34rpx;
  400. }
  401. .day-right {
  402. border-radius: 0 100% 100% 0;
  403. background: rgba(127, 181, 255, 1);
  404. color: #ffffff !important;
  405. }
  406. .day-right::after {
  407. display: block;
  408. content: '结束';
  409. color: #ffffff;
  410. font-size: 20rpx;
  411. font-weight: 400;
  412. position: absolute;
  413. top: 50rpx;
  414. left: 32rpx;
  415. }
  416. .day-none {
  417. background: RGBA(177, 200, 232, 1);
  418. }
  419. .dd {
  420. position: absolute;
  421. top: 0;
  422. bottom: 0;
  423. left: 0;
  424. right: 0;
  425. margin: auto;
  426. font-size: 34rpx;
  427. font-family: DINAlternate-Bold, DINAlternate;
  428. font-weight: bold;
  429. /* color: #333333; */
  430. }
  431. </style>