date.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. <template>
  2. <div class="calendar-wrapper">
  3. <!-- 月份变换区 -->
  4. <div class="header rowJcAc" v-if="headerBar">
  5. <!-- <div class="pre " @click="changeMonth('pre')">上个月</div> -->
  6. <div class="yearMonth">
  7. <div class="arrowIcon rowJcAc" @click="changeMonth('pre')" v-if="monthOpen">
  8. <i class="el-icon-arrow-left"></i>
  9. </div>
  10. {{y+'-'+formatNum(m)}}
  11. <div class="arrowIcon rowJcAc" @click="changeMonth('next')" v-if="monthOpen">
  12. <i class="el-icon-arrow-right"></i>
  13. </div>
  14. </div>
  15. <!-- <div class="next" @click="changeMonth('next')">下个月</div> -->
  16. </div>
  17. <!-- 星期区 -->
  18. <div class="week">
  19. <div class="week-day" v-for="(item,index) in weekDay" :key="index">{{item}}</div>
  20. </div>
  21. <!-- 日历显示区 -->
  22. <div :class="{hide : !monthOpen}" class="content">
  23. <div :style="{top:positionTop + 'px'}" class="days">
  24. <div class="item rowJcAc" v-for="(item,index) in dates" :key="index">
  25. <div class="day" @click="selectOne(item,$event)" :class="{
  26. choose:choose==`${item.year}-${item.month}-${item.date}`,
  27. todayChoose: isTodayChoose(item.year,item.month,item.date) ,
  28. nolm:!item.isCurM,
  29. today:isToday(item.year,item.month,item.date),
  30. notallowed:isFutureDay(item.year,item.month,item.date)
  31. }">
  32. <div class="markDay" v-if="isMarkDay(item.year,item.month,item.date)"
  33. :class="[choose==`${item.year}-${item.month}-${item.date}` ? 'markDayChoose':'markDayNoChoose']">
  34. </div>
  35. <p>{{Number(item.date)}}
  36. <div :class="calendarDaycount(item.year,item.month,item.date)"></div>
  37. </p>
  38. </div>
  39. </div>
  40. </div>
  41. </div>
  42. <!-- 伸缩按钮:待定 -->
  43. <!--<div class="bottomLine rowJcAc" v-if="collapsible" @click="toggle">-->
  44. <!--<div></div>-->
  45. <!--</div>-->
  46. <!-- 伸缩按钮:待定 -->
  47. <!-- <img
  48. src="https://i.loli.net/2020/07/16/2MmZsucVTlRjSwK.png"
  49. mode="scaleToFill"
  50. v-if="collapsible"
  51. @click="toggle"
  52. class="weektoggle"
  53. :class="{ down: monthOpen }"
  54. /> -->
  55. <div class="date-what">
  56. <div class="d-w-info">
  57. <div class="d-w-i-color d-state-zc"></div>
  58. 正常
  59. </div>
  60. <div class="d-w-info">
  61. <div class="d-w-i-color d-state-t10"></div>
  62. ≤10
  63. </div>
  64. <div class="d-w-info">
  65. <div class="d-w-i-color d-state-b10"></div>
  66. >10
  67. </div>
  68. </div>
  69. </div>
  70. </template>
  71. <script>
  72. import {
  73. getEventByCalendar
  74. } from '@/api/forest'
  75. export default {
  76. name: "ren-calendar",
  77. props: {
  78. // 星期几为第一天(0为星期日)
  79. weekstart: {
  80. type: Number,
  81. default: 0,
  82. },
  83. // 标记的日期
  84. markDays: {
  85. type: Array,
  86. default: () => {
  87. return [];
  88. },
  89. },
  90. //是否展示月份切换按钮
  91. headerBar: {
  92. type: Boolean,
  93. default: true,
  94. },
  95. // 是否展开
  96. open: {
  97. type: Boolean,
  98. default: true,
  99. },
  100. //是否可收缩
  101. collapsible: {
  102. type: Boolean,
  103. default: true,
  104. },
  105. //未来日期是否不可点击
  106. disabledAfter: {
  107. type: Boolean,
  108. default: true,
  109. },
  110. // 默认选中日期
  111. selectDate: {
  112. type: String,
  113. default: null
  114. }
  115. },
  116. data() {
  117. return {
  118. weektext: ["日", "一", "二", "三", "四", "五", "六"],
  119. y: new Date().getFullYear(), // 年
  120. m: new Date().getMonth() + 1, // 月
  121. dates: [], // 当前月的日期数据
  122. positionTop: 0,
  123. monthOpen: false,
  124. choose: "",
  125. isCurM: true,
  126. eventByCalendarList:[]
  127. };
  128. },
  129. created() {
  130. this.monthDay(this.y, this.m);
  131. // !this.open && this.toggle();
  132. },
  133. watch: {
  134. // 点击返回今天
  135. selectDate() {
  136. if (this.selectDate) {
  137. this.choose = this.selectDate
  138. this.y = Number(this.selectDate.split('-')[0])
  139. this.m = Number(this.selectDate.split('-')[1])
  140. this.monthDay(this.y, this.m);
  141. if (!this.monthOpen) {
  142. let index = -1;
  143. this.dates.forEach((i, x) => {
  144. this.isChoose(i.year, i.month, i.date) && (index = x);
  145. });
  146. this.positionTop = -((Math.ceil((index + 1) / 7) || 1) - 1) * 42;
  147. }
  148. }
  149. },
  150. m() {
  151. this.$emit('changeMonth', {
  152. year: this.y,
  153. month: this.m
  154. })
  155. },
  156. dates() {
  157. this.$emit('changeDates', {
  158. dates: this.dates
  159. })
  160. }
  161. },
  162. mounted() {
  163. if (this.selectDate) {
  164. this.choose = this.selectDate
  165. this.y = this.selectDate.split('-')[0]
  166. this.m = this.selectDate.split('-')[1]
  167. } else {
  168. this.choose = this.getToday().date;
  169. }
  170. this.toggle()
  171. },
  172. computed: {
  173. // 顶部星期栏
  174. weekDay() {
  175. return this.weektext
  176. .slice(this.weekstart)
  177. .concat(this.weektext.slice(0, this.weekstart));
  178. },
  179. },
  180. methods: {
  181. // 标记日期状态颜色
  182. calendarDaycount(y, m, d) {
  183. let day=y + "-" + this.formatNum(m) + "-" + this.formatNum(d)
  184. if(new Date(day)<new Date()){
  185. if(this.eventByCalendarList!=null&&this.eventByCalendarList.length>0){
  186. for (let i = 0; i < this.eventByCalendarList.length; i++) {
  187. if(this.eventByCalendarList[i].day==day){
  188. if(this.eventByCalendarList[i].daycount>0&&this.eventByCalendarList[i].daycount<=10){
  189. return "date-state-pointer d-state-t10"
  190. }else if(this.eventByCalendarList[i].daycount>10){
  191. return "date-state-pointer d-state-b10"
  192. }else{
  193. return "date-state-pointer d-state-zc"
  194. }
  195. }
  196. }
  197. }
  198. }else{
  199. return "date-state-pointer";
  200. }
  201. },
  202. formatNum(num) {
  203. let res = Number(num);
  204. return res < 10 ? "0" + res : res;
  205. },
  206. getToday() {
  207. let date = new Date();
  208. let y = date.getFullYear();
  209. let m = date.getMonth();
  210. let d = date.getDate();
  211. let week = new Date().getDay();
  212. let weekText = ["日", "一", "二", "三", "四", "五", "六"];
  213. let formatWeek = "星期" + weekText[week];
  214. let today = {
  215. date: y + "-" + this.formatNum(m + 1) + "-" + this.formatNum(d),
  216. week: formatWeek,
  217. };
  218. return today;
  219. },
  220. // 获取当前月份数据
  221. async monthDay(y, month) {
  222. let param={ day: y+"-"+this.formatNum(Number(month)) }
  223. await getEventByCalendar(param).then(res => {
  224. //获取日历颜色状态
  225. this.eventByCalendarList=res.data;
  226. })
  227. this.dates = [];
  228. let m = Number(month);
  229. let firstDayOfMonth = new Date(y, m - 1, 1).getDay(); // 当月第一天星期几
  230. let lastDateOfMonth = new Date(y, m, 0).getDate(); // 当月最后一天
  231. let lastDayOfLastMonth = new Date(y, m - 1, 0).getDate(); // 上一月的最后一天
  232. let weekstart = this.weekstart == 7 ? 0 : this.weekstart;
  233. let startDay = (() => {
  234. // 周初有几天是上个月的
  235. if (firstDayOfMonth == weekstart) {
  236. return 0;
  237. } else if (firstDayOfMonth > weekstart) {
  238. return firstDayOfMonth - weekstart;
  239. } else {
  240. return 7 - weekstart + firstDayOfMonth;
  241. }
  242. })();
  243. let endDay = 7 - ((startDay + lastDateOfMonth) % 7); // 结束还有几天是下个月的
  244. for (let i = 1; i <= startDay; i++) {
  245. this.dates.push({
  246. date: this.formatNum(lastDayOfLastMonth - startDay + i),
  247. day: weekstart + i - 1 || 7,
  248. month: m - 1 >= 1 ? this.formatNum(m - 1) : 12,
  249. year: m - 1 >= 1 ? y : y - 1,
  250. isCurM: false
  251. });
  252. }
  253. for (let j = 1; j <= lastDateOfMonth; j++) {
  254. this.dates.push({
  255. date: this.formatNum(j),
  256. day: (j % 7) + firstDayOfMonth - 1 || 7,
  257. month: this.formatNum(m),
  258. year: y,
  259. isCurM: true, //是否当前月份
  260. });
  261. }
  262. for (let k = 1; k <= endDay; k++) {
  263. this.dates.push({
  264. date: this.formatNum(k),
  265. day: (lastDateOfMonth + startDay + weekstart + k - 1) % 7 || 7,
  266. month: m + 1 <= 12 ? this.formatNum(m + 1) : 1,
  267. year: m + 1 <= 12 ? y : y + 1,
  268. isCurM: false
  269. });
  270. }
  271. },
  272. isFutureDay(y, m, d) {
  273. //是否未来日期
  274. let ymd = `${y}/${m}/${d}`;
  275. let formatDY = new Date(ymd.replace(/-/g, "/"));
  276. let showTime = formatDY.getTime();
  277. let curTime = new Date().getTime();
  278. if (showTime > curTime) {
  279. return true;
  280. } else {
  281. return false;
  282. }
  283. },
  284. // 标记日期
  285. isMarkDay(y, m, d) {
  286. let flag = false;
  287. for (let i = 0; i < this.markDays.length; i++) {
  288. let dy = `${y}-${m}-${d}`;
  289. if (this.markDays[i] == dy) {
  290. flag = true;
  291. break;
  292. }
  293. }
  294. return flag;
  295. },
  296. isToday(y, m, d) {
  297. let checkD = y + "-" + m + "-" + d;
  298. let today = this.getToday().date;
  299. if (checkD == today) {
  300. return true;
  301. } else {
  302. return false;
  303. }
  304. },
  305. isTodayChoose(year, month, day) {
  306. let checkD = year + "-" + month + "-" + day;
  307. let today = this.getToday().date;
  308. return this.choose == `${year}-${month}-${day}` && checkD == today
  309. },
  310. isChoose(y, m, d) {
  311. let checkD = y + "-" + m + "-" + d;
  312. return this.choose == checkD
  313. },
  314. // 展开收起
  315. toggle() {
  316. this.monthOpen = !this.monthOpen;
  317. if (this.monthOpen) {
  318. this.positionTop = 0;
  319. } else {
  320. let index = -1;
  321. this.dates.forEach((i, x) => {
  322. this.isChoose(i.year, i.month, i.date) && (index = x);
  323. });
  324. this.positionTop = -((Math.ceil((index + 1) / 7) || 1) - 1) * 42;
  325. }
  326. },
  327. // 点击回调
  328. selectOne(i) {
  329. let markDay = this.isMarkDay(i.year, i.month, i.date);
  330. let date = `${i.year}-${i.month}-${i.date}`;
  331. let selectD = new Date(date).getTime();
  332. let curTime = new Date().getTime();
  333. let week = new Date(date).getDay();
  334. let weekText = ["日", "一", "二", "三", "四", "五", "六"];
  335. let formatWeek = "星期" + weekText[week];
  336. let response = {
  337. date: date,
  338. week: formatWeek,
  339. };
  340. // if (!i.isCurM) {
  341. // // console.log('不在当前月范围内');
  342. // return false;
  343. // }
  344. if (selectD > curTime) {
  345. if (this.disabledAfter) {
  346. console.log("未来日期不可选");
  347. return false;
  348. } else {
  349. this.choose = date;
  350. if (markDay && i.isCurM) {
  351. this.$emit("onDayClick", response);
  352. }
  353. }
  354. } else {
  355. this.choose = date;
  356. if (i.isCurM) {
  357. this.$emit("onDayClick", response);
  358. } else {
  359. this.y = i.year
  360. this.m = i.month
  361. this.monthDay(this.y, this.m);
  362. this.$emit("onDayClick", response);
  363. }
  364. }
  365. this.$emit("setDate", response);
  366. // console.log(response);
  367. this.$emit("selectDay", date);
  368. },
  369. //改变年月
  370. changYearMonth(y, m) {
  371. this.monthDay(y, m);
  372. this.y = y;
  373. this.m = m;
  374. },
  375. changeMonth(type) {
  376. this.y = parseInt(this.y)
  377. this.m = parseInt(this.m)
  378. if (type == "pre") {
  379. if (this.m + 1 == 2) {
  380. this.m = 12;
  381. this.y = parseInt(this.y) - 1;
  382. } else {
  383. this.m = this.m - 1;
  384. }
  385. } else {
  386. if (this.m + 1 == 13) {
  387. this.m = 1;
  388. this.y = this.y + 1;
  389. } else {
  390. this.m = this.m + 1;
  391. }
  392. }
  393. this.monthDay(this.y, this.m);
  394. // this.$emit("changeMonth");
  395. },
  396. },
  397. };
  398. </script>
  399. <style lang="scss" scoped>
  400. .notallowed{
  401. cursor: not-allowed !important
  402. }
  403. @import '@/assets/styles/base.scss';
  404. //状态
  405. .d-state-zc {
  406. background-color: $date-state1;
  407. }
  408. .d-state-t10 {
  409. background-color: $date-state2;
  410. }
  411. .d-state-b10 {
  412. background-color: $date-state3;
  413. }
  414. .bottomLine {
  415. padding-top: 12px;
  416. padding-bottom: 12px;
  417. >div {
  418. width: 32px;
  419. height: 3px;
  420. background: none;
  421. border-radius: 2px;
  422. }
  423. }
  424. .nolm {
  425. color: #2b4376 !important;
  426. }
  427. .calendar-wrapper {
  428. text-align: center;
  429. .header {
  430. .yearMonth {
  431. font-size: 16px;
  432. line-height: 23px;
  433. color: $inBlue;
  434. margin: 0 32px;
  435. display: flex;
  436. justify-content: center;
  437. }
  438. }
  439. .week {
  440. display: flex;
  441. justify-content: space-around;
  442. width: 100%;
  443. align-items: center;
  444. height: 19px;
  445. line-height: 19px;
  446. font-size: 13px;
  447. font-weight: 600;
  448. margin-top: 5px;
  449. color: $inBlue;
  450. }
  451. .content {
  452. position: relative;
  453. transition: height 0.4s ease;
  454. .days {
  455. transition: top 0, 3s;
  456. display: flex;
  457. align-items: center;
  458. flex-wrap: wrap;
  459. position: relative;
  460. font-size: 14px;
  461. line-height: 19px;
  462. .item {
  463. position: relative;
  464. display: block;
  465. height: 33px;
  466. line-height: 33px;
  467. width: calc(100% / 7);
  468. .day {
  469. font-style: normal;
  470. display: inline-block;
  471. position: relative;
  472. vertical-align: middle;
  473. width: 30px;
  474. font-weight: 500;
  475. height: 30px;
  476. line-height: 30px;
  477. overflow: hidden;
  478. border-radius: 50%;
  479. cursor: pointer;
  480. color: #3d8bbc;
  481. font-size: 11px;
  482. .date-state-pointer {
  483. position: absolute;
  484. bottom: 1px;
  485. left: 50%;
  486. transform: translateX(-50%);
  487. width: 4px;
  488. height: 4px;
  489. border-radius: 5px;
  490. }
  491. &.choose {
  492. background-color: #3a4350;
  493. color: #fff !important;
  494. }
  495. &.todayChoose {
  496. background-color: #1A7AF8 !important;
  497. color: #fff;
  498. }
  499. }
  500. .notSigned {
  501. font-style: normal;
  502. width: 8rpx;
  503. height: 8rpx;
  504. background: #fa7268;
  505. border-radius: 10rpx;
  506. position: absolute;
  507. left: 50%;
  508. bottom: 0;
  509. pointer-events: none;
  510. }
  511. .today {
  512. color: #ffffff;
  513. background-color: #3c56b5;
  514. }
  515. .markDayNoChoose {
  516. background: #1A7AF8;
  517. }
  518. .markDayChoose {
  519. background: #fff;
  520. }
  521. .markDay {
  522. width: 4px;
  523. height: 4px;
  524. border-radius: 50%;
  525. position: absolute;
  526. left: 16px;
  527. bottom: 4px;
  528. pointer-events: none;
  529. }
  530. }
  531. }
  532. }
  533. .hide {
  534. height: 42px !important;
  535. }
  536. .arrowIcon {
  537. width: 40px;
  538. height: 23px;
  539. cursor: pointer;
  540. margin: 0 5px;
  541. }
  542. .arrowIcon:hover {
  543. background-color: #202e62;
  544. i {
  545. color: #fff;
  546. }
  547. }
  548. // .weektoggle {
  549. // width: 85px;
  550. // height: 32px;
  551. // position: relative;
  552. // left: 50%;
  553. // margin-left: -42px;
  554. // bottom: -42px;
  555. // &.down {
  556. // transform: rotate(180deg);
  557. // bottom: 0;
  558. // }
  559. // }
  560. .date-what{
  561. width: 100%;
  562. color: $inBlue;
  563. display: flex;
  564. align-items: center;
  565. padding:.4rem 1rem ;
  566. position: absolute;
  567. bottom: 0;
  568. justify-content: space-between;
  569. .d-w-info{
  570. display: flex;
  571. align-items: center;
  572. .d-w-i-color{
  573. width: 1rem;
  574. height: 1rem;
  575. margin-right: .5rem;
  576. border-radius: .2rem;
  577. }
  578. }
  579. }
  580. }
  581. </style>