date.vue 14 KB

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