s-calendar-select.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. if (!year || !month || !day) {
  114. return '';
  115. }
  116. //只选择了1天
  117. if (!this.endYear || !this.endMonth || !this.endDay) {
  118. if (this.startYear == year && this.startMonth == month && this.startDay == day) {
  119. return 'day-yuan'
  120. } else {
  121. return '';
  122. }
  123. } else {
  124. //选择了多天
  125. let startTime = new Date(this.startYear, this.startMonth - 1, this.startDay).getTime()
  126. let endTime = new Date(this.endYear, this.endMonth - 1, this.endDay).getTime()
  127. let nowTime = new Date(year, month - 1, day).getTime();
  128. if (startTime == nowTime) {
  129. if (this.isSameDay) {
  130. return 'sameDay'
  131. } else {
  132. return 'day-left';
  133. }
  134. }
  135. if (endTime == nowTime) {
  136. return 'day-right';
  137. }
  138. if (startTime < nowTime && endTime > nowTime) {
  139. return 'day-none'
  140. }
  141. if (startTime === nowTime && endTime === nowTime) {
  142. console.log(111);
  143. }
  144. }
  145. },
  146. selDay(year, month, day) {
  147. if (!this.startYear || !this.startMonth || !this.startDay) {
  148. let startTime = new Date(year, month - 1, day + 1)
  149. let nowTime = new Date()
  150. if (startTime < nowTime) {
  151. uni.showToast({
  152. title: '开始时间不能小于当前时间',
  153. icon: 'none'
  154. })
  155. return;
  156. } else {
  157. this.startYear = year;
  158. this.startMonth = month;
  159. this.startDay = day;
  160. }
  161. } else if (!this.endYear || !this.endMonth || !this.endDay) {
  162. let startTime = new Date(this.startYear, this.startMonth - 1, this.startDay).getTime()
  163. let endTime = new Date(year, month - 1, day).getTime()
  164. if (endTime < startTime) {
  165. uni.showToast({
  166. title: '不能小于开始时间',
  167. icon: 'none'
  168. })
  169. return;
  170. }
  171. if (endTime == startTime) {
  172. // this.startYear = '';
  173. // this.startMonth = '';
  174. // this.startDay = '';
  175. // this.endYear = this.startYear
  176. // this.endMonth = this.startMonth
  177. // this.endDay = this.startDay
  178. this.endYear = year;
  179. this.endMonth = month;
  180. this.endDay = day;
  181. console.log(this.startDay);
  182. console.log(this.endDay);
  183. this.isSameDay = true
  184. console.log(this.isSameDay);
  185. // return;
  186. }
  187. this.endYear = year;
  188. this.endMonth = month;
  189. this.endDay = day;
  190. this.startDate = this.startYear + '-' + this.startMonth + '-' + this.startDay
  191. this.endDate = this.endYear + '-' + this.endMonth + '-' + this.endDay
  192. // console.log('开始日期:' + this.startYear + '-' + this.startMonth + '-' + this.startDay);
  193. // console.log('结束日期:' + this.endYear + '-' + this.endMonth + '-' + this.endDay);
  194. // console.log('共' + this.allDay + '天');
  195. this.$emit('getDate', {
  196. startDate: this.startDate,
  197. endDate: this.endDate
  198. })
  199. } else {
  200. let startTime = new Date(year, month - 1, day + 1).getTime()
  201. let nowTime = new Date().getTime()
  202. if (startTime < nowTime) {
  203. uni.showToast({
  204. title: '开始时间不能小于当前时间',
  205. icon: 'none'
  206. })
  207. return;
  208. } else {
  209. this.endYear = '';
  210. this.endMonth = '';
  211. this.endDay = '';
  212. this.startYear = year;
  213. this.startMonth = month;
  214. this.startDay = day;
  215. }
  216. }
  217. },
  218. /**
  219. * 滚动到底
  220. */
  221. scrollTolower() {
  222. let y = this.getNextNDate(this.year, this.month, 3);
  223. this.year = y.year;
  224. this.month = y.month;
  225. this.setDay()
  226. },
  227. /**
  228. * 计算天数
  229. */
  230. countDays(startTime, endTime) {
  231. return parseInt((endTime.getTime() - startTime.getTime()) / 24 / 60 / 60 / 1000)
  232. },
  233. /**
  234. * 获取year,month后n天的日期
  235. */
  236. getNextNDate(year, month, n) {
  237. let m = 0;
  238. if (n > 12) {
  239. m = parseInt(n / 12);
  240. year += m;
  241. }
  242. let mm = n - m * 12;
  243. if (month + mm > 12) {
  244. year += 1;
  245. month = month + mm - 12;
  246. } else {
  247. month += mm;
  248. }
  249. return {
  250. year: year,
  251. month: month
  252. }
  253. }
  254. },
  255. }
  256. </script>
  257. <style>
  258. /* 同一天的样式 */
  259. .sameDay {
  260. border-radius: 100% 100% 100% 100%;
  261. background: rgba(127, 181, 255, 1);
  262. color: #ffffff !important;
  263. }
  264. .sameDay::after {
  265. display: block;
  266. content: '开始/结束';
  267. color: #ffffff;
  268. font-size: 18rpx;
  269. font-weight: 400;
  270. position: absolute;
  271. top: 50rpx;
  272. left: 18rpx;
  273. }
  274. .line {
  275. background: rgba(153, 153, 153, 1);
  276. /* height: 2rpx; */
  277. width: 100%;
  278. margin-top: 27rpx;
  279. }
  280. .weeks {
  281. width: 100%;
  282. height: 24px;
  283. display: flex;
  284. justify-content: space-around;
  285. /* border-top: 2rpx solid #EBEBEB;
  286. border-bottom: 2rpx solid #EBEBEB; */
  287. line-height: 30px;
  288. font-size: 30rpx;
  289. font-weight: bold;
  290. color: #00000 !important;
  291. /* position: fixed; */
  292. top: var(--window-top);
  293. /* background: #FFFFFF; */
  294. z-index: 9;
  295. }
  296. .calendar {
  297. border-bottom: 16rpx solid #F7F7F7;
  298. padding-bottom: 40rpx;
  299. }
  300. .calendar:nth-last-child(1) {
  301. border: none;
  302. }
  303. .year-month {
  304. height: 40rpx;
  305. font-size: 30rpx;
  306. font-family: PingFangSC-Regular, PingFang SC;
  307. font-weight: bold;
  308. color: #000000;
  309. line-height: 44rpx;
  310. text-align: center;
  311. margin: 29rpx 0;
  312. }
  313. .days {
  314. display: flex;
  315. flex-wrap: wrap;
  316. }
  317. .day {
  318. text-align: center;
  319. /* margin-top: 40rpx; */
  320. position: relative;
  321. }
  322. .day-yuan {
  323. border-radius: 100%;
  324. background: rgba(127, 181, 255, 1);
  325. color: #ffffff !important;
  326. }
  327. .day-yuan::after {
  328. display: block;
  329. content: '开始';
  330. color: #ffffff !important;
  331. font-size: 20rpx;
  332. font-weight: 400;
  333. position: absolute;
  334. top: 50rpx;
  335. left: 34rpx;
  336. }
  337. .col-fff {
  338. color: #fff !important;
  339. }
  340. .col-333 {
  341. color: #333 !important;
  342. }
  343. .col-999 {
  344. color: #999999 !important;
  345. }
  346. .day-left {
  347. border-radius: 100% 0 0 100%;
  348. background: rgba(127, 181, 255, 1);
  349. color: #ffffff !important;
  350. }
  351. .day-left::after {
  352. display: block;
  353. content: '开始';
  354. color: #ffffff;
  355. font-size: 20rpx;
  356. font-weight: 400;
  357. position: absolute;
  358. top: 50rpx;
  359. left: 34rpx;
  360. }
  361. .day-right {
  362. border-radius: 0 100% 100% 0;
  363. background: rgba(127, 181, 255, 1);
  364. color: #ffffff !important;
  365. }
  366. .day-right::after {
  367. display: block;
  368. content: '结束';
  369. color: #ffffff;
  370. font-size: 20rpx;
  371. font-weight: 400;
  372. position: absolute;
  373. top: 50rpx;
  374. left: 32rpx;
  375. }
  376. .day-none {
  377. background: RGBA(177, 200, 232, 1);
  378. }
  379. .dd {
  380. position: absolute;
  381. top: 0;
  382. bottom: 0;
  383. left: 0;
  384. right: 0;
  385. margin: auto;
  386. font-size: 34rpx;
  387. font-family: DINAlternate-Bold, DINAlternate;
  388. font-weight: bold;
  389. /* color: #333333; */
  390. }
  391. </style>