lx-calendar.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. <template>
  2. <view class="date">
  3. <view class="head">
  4. <view class="icon" @click="switch_month_week('prev',true)"><text class="iconfont icon-fanhui" :class="[currentMonth == nowMonth ? 'month_disabled' : '']" /></view>
  5. <view class="title">{{nowYear+'年'+nowMonth+'月'}}</view>
  6. <view class="icon" @click="switch_month_week('next',true)"><text class="iconfont next icon-fanhui" :class="[currentMonth + 1 == nowMonth ? 'month_disabled' : '']" /></view>
  7. </view>
  8. <view class="date_dl" >
  9. <view class="dd" v-for="(item,index) in week" :key="index">{{item}}</view>
  10. </view>
  11. <swiper :style="{height:(retract ? 2 * 80 : (week_list.length + 1) * 80 ) + 'rpx'}" :current="current" circular @change="change_date">
  12. <swiper-item>
  13. <view class="date_dl" v-show="!retract || index == to_prev_week_index" v-for="(item,index) in week_list_prev_co" :key="index">
  14. <view class="dd" @click="item_click(vo,index,key)" v-for="(vo,key) in item" :key="key">
  15. <!-- <view class="num" :class="[vo.today ? 'today' : '',vo.type == 'month' ? 'month' : (retract ? '' : 'disabled')]">{{vo.day}}</view> -->
  16. <view class="num" :class="[vo.today ? 'today' : '', vo.dot ? '' : 'disabled']">{{vo.day}}</view>
  17. <view v-show="vo.dot && (vo.type == 'month' || retract)" class="dot"></view>
  18. </view>
  19. </view>
  20. <view @click="open" class="retract icon" v-show="false"><text class="iconfont next icon-fanhui" :class="[retract ? '' : 'retract_icon']" /></view>
  21. </swiper-item>
  22. <swiper-item>
  23. <view class="date_dl" v-show="!retract || index == to_week_index" v-for="(item,index) in week_list" :key="index">
  24. <view class="dd" @click="item_click(vo,index,key)" v-for="(vo,key) in item" :key="key">
  25. <!-- <view class="num" :class="[vo.today ? 'today' : '',vo.type == 'month' ? 'month' : (retract ? '' : 'disabled')]">{{vo.day}}</view> -->
  26. <view class="num" :class="[vo.today ? 'today' : '', vo.dot ? '' : 'disabled']">{{vo.day}}</view>
  27. <view v-show="vo.dot && (vo.type == 'month' || retract)" class="dot"></view>
  28. </view>
  29. </view>
  30. <view @click="open" class="retract icon" v-show="false"><text class="iconfont next icon-fanhui" :class="[retract ? '' : 'retract_icon']" /></view>
  31. </swiper-item>
  32. <swiper-item>
  33. <view class="date_dl" v-show="!retract || index == to_next_week_index" v-for="(item,index) in week_list_next_co" :key="index">
  34. <view class="dd" @click="item_click(vo,index,key)" v-for="(vo,key) in item" :key="key">
  35. <!-- <view class="num" :class="[vo.today ? 'today' : '',vo.type == 'month' ? 'month' : (retract ? '' : 'disabled')]">{{vo.day}}</view> -->
  36. <view class="num" :class="[vo.today ? 'today' : '', vo.dot ? '' : 'disabled']">{{vo.day}}</view>
  37. <view v-show="vo.dot && (vo.type == 'month' || retract)" class="dot"></view>
  38. </view>
  39. </view>
  40. <view @click="open" class="retract icon" v-show="false"><text class="iconfont next icon-fanhui" :class="[retract ? '' : 'retract_icon']" /></view>
  41. </swiper-item>
  42. </swiper>
  43. </view>
  44. </template>
  45. <script>
  46. export default {
  47. props:{
  48. value:{
  49. type:[String,Number],
  50. default:''
  51. },
  52. dot_lists:{
  53. type:Array,
  54. default:()=>{
  55. return [];
  56. }
  57. }
  58. },
  59. data(){
  60. return {
  61. debug:false,
  62. week:['日','一','二','三','四','五','六'],
  63. week_list:[],
  64. week_list_prev:[],
  65. week_list_prev_week:[],
  66. week_list_next:[],
  67. week_list_next_week:[],
  68. now_date:'',
  69. start_date:'',
  70. end_date:'',
  71. prev_date:'',
  72. next_date:'',
  73. nowYear:'',
  74. nowMonth:'',
  75. currentMonth:'',
  76. nowDay:'',
  77. retract:true,
  78. to_week_index:0,
  79. to_prev_week_index:0,
  80. to_next_week_index:0,
  81. nowTime:0,
  82. dot_list:[],
  83. current:1,
  84. date:'',
  85. }
  86. },
  87. watch:{
  88. value(value){
  89. this.get_date(this.date_parse(value));
  90. },
  91. dot_lists:{
  92. immediate:true,
  93. handler(value){
  94. this.dot_list = value;
  95. this.set_doc_lists_update()
  96. }
  97. }
  98. },
  99. computed:{
  100. week_list_prev_co(){
  101. return this.retract ? this.week_list_prev_week : this.week_list_prev
  102. },
  103. week_list_next_co(){
  104. return this.retract ? this.week_list_next_week : this.week_list_next
  105. }
  106. },
  107. created(){
  108. this.init();
  109. },
  110. methods:{
  111. change(){
  112. let value = {
  113. fulldate:this.date.replace(/-(\d)(?!\d)/g, '-0$1')
  114. };
  115. this.$emit('change',value)
  116. },
  117. init(){
  118. console.log(this.value)
  119. if(this.value){
  120. this.get_date(this.date_parse(this.value));
  121. }else{
  122. this.get_date();
  123. }
  124. let date = new Date();
  125. this.currentMonth = date.getMonth() + 1;
  126. this.doc_list_update();
  127. this.update_month();
  128. this.open();
  129. },
  130. open(){
  131. this.retract = !this.retract;
  132. this.get_date(this.nowTime);
  133. this.set_to_day('week_list_prev')
  134. this.set_to_day('week_list_next')
  135. this.change_week();
  136. if(this.retract){
  137. this.update_swiper_item('week')
  138. }else{
  139. this.update_swiper_item('month')
  140. }
  141. this.set_doc_lists_update();
  142. },
  143. change_week(){
  144. if(this.to_week_index < this.week_list.length - 1){
  145. this.to_next_week_index = this.to_week_index + 1;
  146. this.week_list_next_week = this.week_list;
  147. }else{
  148. this.to_next_week_index = 0;
  149. this.week_list_next_week = this.week_list_next;
  150. }
  151. if(this.to_week_index == 0 ){
  152. this.update_month();
  153. // if(){
  154. let next_day = this.week_list_prev[this.week_list_prev.length - 1][6].day;
  155. // }
  156. this.to_prev_week_index = this.week_list_prev.length - 1 - Math.ceil(next_day / 7);
  157. this.week_list_prev_week = JSON.parse(JSON.stringify(this.week_list_prev));
  158. }else{
  159. this.to_prev_week_index = this.to_week_index - 1;
  160. this.week_list_prev_week = this.week_list;
  161. }
  162. // if(this.current == 1){
  163. // }
  164. // let to_week_index = this.to_week_index;
  165. // if(this.current == 2){
  166. // this.to_next_week_index = this.to_week_index;
  167. // this.to_week_index = this.to_week_index - 1;
  168. // this.to_prev_week_index = this.to_next_week_index + 1;
  169. // }else if(this.current == 0){
  170. // this.to_next_week_index = this.to_week_index;
  171. // this.to_week_index = this.to_week_index - 1;
  172. // this.to_prev_week_index = this.to_next_week_index + 1;
  173. // }
  174. },
  175. change_date_week(type){
  176. let week_list = this.week_list;
  177. let to_week_index = this.to_week_index;
  178. if(type == 'prev'){
  179. this.to_week_index = this.to_prev_week_index;
  180. this.to_prev_week_index = this.to_next_week_index
  181. this.to_next_week_index = to_week_index;
  182. this.week_list = this.week_list_prev_week
  183. this.week_list_prev_week = this.week_list_next_week;
  184. this.week_list_next_week = week_list;
  185. }else if(type == 'next'){
  186. this.to_week_index = this.to_next_week_index;
  187. this.to_next_week_index = this.to_prev_week_index
  188. this.to_prev_week_index = to_week_index;
  189. this.week_list = this.week_list_next_week
  190. this.week_list_next_week = this.week_list_prev_week;
  191. this.week_list_prev_week = week_list;
  192. }
  193. this.set_to_day_all();
  194. },
  195. change_date_month(type){
  196. let week_list = this.week_list;
  197. if(type == 'prev'){
  198. this.week_list = this.week_list_prev
  199. this.week_list_prev = this.week_list_next;
  200. this.week_list_next = week_list;
  201. }else if(type == 'next'){
  202. this.week_list = this.week_list_next
  203. this.week_list_next = this.week_list_prev;
  204. this.week_list_prev = week_list;
  205. }
  206. },
  207. change_date(e){
  208. //return;
  209. let primary_current = this.current
  210. let current = e.detail.current;
  211. this.current = current;
  212. if(primary_current - current == -1 || primary_current - current == 2){
  213. if(this.retract){
  214. this.switch_month_week('next')
  215. this.change_week()
  216. if(primary_current - current == -1 && current != 1){
  217. this.change_date_week('prev')
  218. }else if(primary_current - current == 2){
  219. this.change_date_week('next')
  220. }
  221. }else{
  222. this.get_date(this.get_month('next'));
  223. this.update_month();
  224. if(primary_current - current == -1 && current != 1){
  225. this.change_date_month('prev')
  226. }else if(primary_current - current == 2){
  227. this.change_date_month('next')
  228. }
  229. }
  230. }else{
  231. if(this.retract){
  232. this.switch_month_week('prev')
  233. this.change_week()
  234. if(primary_current - current == 1 && current != 1){
  235. this.change_date_week('next')
  236. }else if(primary_current - current == -2){
  237. this.change_date_week('prev')
  238. }
  239. }else{
  240. this.get_date(this.get_month('prev'));
  241. this.update_month();
  242. if(primary_current - current == 1 && current != 1){
  243. this.change_date_month('next')
  244. }else if(primary_current - current == -2){
  245. this.change_date_month('prev')
  246. }
  247. }
  248. }
  249. this.set_to_day_all();
  250. this.set_doc_lists_update();
  251. this.change()
  252. },
  253. update_month(){
  254. this.get_date(this.get_month('prev'),'prev');
  255. this.get_date(this.get_month('next'),'next');
  256. },
  257. set_doc_lists_update(){
  258. this.doc_list_update('week_list');
  259. this.doc_list_update('week_list_prev');
  260. this.doc_list_update('week_list_next');
  261. this.doc_list_update('week_list_prev_week')
  262. this.doc_list_update('week_list_next_week')
  263. },
  264. doc_list_update(week_list = 'week_list'){
  265. let list = [];
  266. this[week_list].map((item,index)=>{
  267. list.push(item.map((vo,key)=>{
  268. if (vo.day == ""){
  269. vo.dot = false;
  270. }
  271. else if(this.dot_list.indexOf(vo.date) > -1 || this.dot_list.indexOf(vo.date.replace(/-(\d)(?!\d)/g, '-0$1')) > -1 ){
  272. vo.dot = true;
  273. }else{
  274. vo.dot = false;
  275. }
  276. return {...vo}
  277. }))
  278. })
  279. this[week_list] = list;
  280. },
  281. set_to_day(type){
  282. let list = [];
  283. this[type].map((item,index)=>{
  284. list.push(item.map((vo,key)=>{
  285. if(vo.date == `${this.date}`){
  286. vo.today = true;
  287. }else{
  288. vo.today = false;
  289. }
  290. return {...vo};
  291. }))
  292. })
  293. this[type] = list;
  294. },
  295. item_click(item,item_index = -1){
  296. // if(!this.retract && item.type !== 'month'){
  297. // return false;
  298. // }
  299. if(!item.dot){
  300. return false;
  301. }
  302. console.log(item.date)
  303. this.date = item.date;
  304. if(item.type == 'month'){
  305. this.nowDay = item.day;
  306. if(item_index >= 0) this.to_week_index = item_index;
  307. }else if(this.retract){
  308. this.nowDay = item.day;
  309. }
  310. let now_arr = item.date.split('-')
  311. this.nowYear = now_arr[0];
  312. this.nowMonth = now_arr[1];
  313. this.nowDay = now_arr[2];
  314. this.set_to_day_all(item_index);
  315. this.nowTime = this.date_parse(`${item.date}`);
  316. this.change()
  317. this.set_doc_lists_update();
  318. },
  319. set_to_day_all(item_index){
  320. this.set_to_day('week_list')
  321. this.set_to_day('week_list_prev')
  322. this.set_to_day('week_list_next')
  323. this.set_to_day('week_list_prev_week')
  324. this.set_to_day('week_list_next_week')
  325. },
  326. get_month(type){
  327. let nowMonth = this.nowMonth;
  328. let nowYear = this.nowYear;
  329. let nowDay = this.nowDay;
  330. if(type == 'prev'){
  331. if(nowMonth == 1){
  332. nowMonth = 12;
  333. nowYear = nowYear - 1;
  334. }else{
  335. nowMonth--;
  336. }
  337. }else if(type == 'next'){
  338. if(nowMonth == 12){
  339. nowMonth = 1;
  340. nowYear = nowYear + 1;
  341. }else{
  342. nowMonth++;
  343. }
  344. }
  345. let days = this.get_month_days(nowMonth,nowYear);
  346. if(nowDay > days){
  347. nowDay = days;
  348. }
  349. return this.date_parse(`${nowYear}-${nowMonth}-${nowDay}`);
  350. },
  351. date_parse(str){
  352. return Date.parse(str.replace(/-(\d)(?!\d)/g, '-0$1'));
  353. },
  354. switch_month_week(type = 'next',update_week = false){
  355. if (this.currentMonth >= this.nowMonth && type=="prev"){
  356. return;
  357. }
  358. if (this.currentMonth < this.nowMonth && type == "next"){
  359. return;
  360. }
  361. // this.nowDay = '';
  362. this.date = '';
  363. if(this.retract){
  364. if(type == 'prev'){
  365. this.get_date(this.nowTime - 86400 * 7 * 1000);
  366. }else if(type == 'next'){
  367. this.get_date(this.nowTime + 86401 * 7 * 1000);
  368. }
  369. if(update_week){
  370. this.update_swiper_item('week');
  371. this.set_doc_lists_update();
  372. }
  373. }else{
  374. this.get_date(this.get_month(type))
  375. this.update_swiper_item('month');
  376. }
  377. this.set_doc_lists_update();
  378. this.set_to_day_all();
  379. if(update_week){
  380. this.change()
  381. }
  382. },
  383. update_swiper_item(type = 'month'){
  384. if(type == 'month'){
  385. if(this.current == 0){
  386. this.change_date_month('next')
  387. }else if(this.current == 2){
  388. this.change_date_month('prev')
  389. }
  390. }else if(type == 'week'){
  391. if(this.current == 0){
  392. this.change_date_week('next')
  393. }else if(this.current == 2){
  394. this.change_date_week('prev')
  395. }
  396. }
  397. },
  398. next(){
  399. this.get_date(this.next_date)
  400. },
  401. get_date(value = '',type = 'same'){
  402. let date = new Date();
  403. if(value){
  404. date = new Date(value);
  405. }
  406. let nowMonth = date.getMonth() + 1,
  407. nowYear = date.getFullYear(),
  408. nowDay = date.getDate(),
  409. nowTime = date.getTime(),
  410. nowWeek = date.getDay();
  411. let days = this.get_month_days(nowMonth,nowYear);
  412. let start_date = new Date(nowYear,nowMonth - 1, 1);
  413. let end_date = new Date(nowYear,nowMonth - 1, days);
  414. let prev_date = new Date(start_date.getTime() - 1);
  415. let prev_date_days = prev_date.getDate();
  416. let next_date = new Date(end_date.getTime() + 86401 * 1000);
  417. let next_date_days = next_date.getDate();
  418. let start_week = start_date.getDay();
  419. let date_arrs = [];
  420. let week_list = [];
  421. let count_days = 35;
  422. for(let i = prev_date_days - start_week + 1; i <= prev_date_days; i++){
  423. date_arrs.push({
  424. day:"",
  425. type:'prev',
  426. date:`${prev_date.getFullYear()}-${prev_date.getMonth()+1}-${i}`
  427. })
  428. }
  429. for(let i = 1; i <= days; i++){
  430. date_arrs.push({
  431. day:i,
  432. type:'month',
  433. today:i == nowDay ? true : false,
  434. date:`${nowYear}-${nowMonth}-${i}`
  435. })
  436. if(i == nowDay && type == 'same'){
  437. // this.date = `${nowYear}-${nowMonth}-${i}`;
  438. }
  439. }
  440. if(this.debug) console.log(value,date,this.date,`${next_date.getFullYear()}-${next_date.getMonth()+1}-${next_date.getDate()}`)
  441. let date_arrs_length = date_arrs.length;
  442. if(date_arrs_length > 35){
  443. count_days = 42;
  444. }
  445. for(let i = 1; i <= count_days - date_arrs_length; i++){
  446. date_arrs.push({
  447. day:"",
  448. type:'next',
  449. date:`${next_date.getFullYear()}-${next_date.getMonth()+1}-${i}`
  450. })
  451. }
  452. for(let i = 0; i < date_arrs.length / 7; i++){
  453. let arr = [];
  454. for(let j = 0; j < 7; j++){
  455. if(date_arrs[i * 7 + j].today){
  456. if(type == 'same'){
  457. this.to_week_index = i
  458. }
  459. }
  460. arr.push(date_arrs[i * 7 + j]);
  461. }
  462. week_list.push(arr);
  463. }
  464. if(type == 'same'){
  465. this.week_list = week_list;
  466. this.nowYear = nowYear;
  467. this.nowMonth = nowMonth;
  468. this.nowDay = nowDay;
  469. this.nowTime = nowTime;
  470. this.start_date = start_date;
  471. this.end_date = end_date;
  472. this.prev_date = prev_date;
  473. this.next_date = next_date;
  474. }else if(type == 'prev'){
  475. this.week_list_prev = week_list;
  476. }else if(type == 'next'){
  477. this.week_list_next = week_list;
  478. }
  479. },
  480. get_month_days(nowMonth,nowYear){
  481. let month_arr = [1,3,5,7,8,10,12];
  482. let days = 0;
  483. if(nowMonth == 2){
  484. if(nowYear % 4 == 0){
  485. days = 29;
  486. }else{
  487. days = 28;
  488. }
  489. }else if(month_arr.indexOf(nowMonth) >= 0){
  490. days = 31;
  491. }else{
  492. days = 30;
  493. }
  494. return days;
  495. }
  496. }
  497. }
  498. </script>
  499. <style lang="less">
  500. @import (less) '../../static/lx-calendar/fonts/iconfont.css';
  501. @color:#007aff;
  502. @color_disabled:#a1a1a1;
  503. @color_standard:#333;
  504. @color_border:#f5f5f5;
  505. .date{
  506. width: 750rpx;
  507. }
  508. .head{
  509. display: flex; align-items: center; height: 100rpx; justify-content: center;
  510. border-bottom: 1rpx solid @color_border; color:@color_standard;
  511. .title{
  512. width: 200rpx; font-size: 30rpx; text-align: center;
  513. }
  514. .icon{
  515. display: block;
  516. .next{
  517. transform: rotate(180deg); display: block;
  518. }
  519. }
  520. }
  521. .month_disabled{
  522. color: @color_disabled;
  523. }
  524. .retract{
  525. display: flex; justify-content: center; align-items: center; height: 80rpx;;
  526. .iconfont{
  527. transform: rotate(270deg);
  528. &.retract_icon{
  529. transform: rotate(90deg);
  530. }
  531. }
  532. }
  533. .date_dl{
  534. display: flex; width: 100%;
  535. .dd{
  536. flex:1; text-align: center;
  537. height: 80rpx;
  538. font-size: 26rpx;
  539. display: flex;
  540. flex-direction: column;
  541. align-items: center;
  542. justify-content: center;
  543. position: relative;
  544. .num{
  545. width: 60rpx; height: 60rpx; border-radius: 50%; line-height: 60rpx;
  546. &.disabled{
  547. color: @color_disabled;
  548. }
  549. &.month{
  550. color: @color_standard;
  551. }
  552. &.today{
  553. background: @color; color:#fff;
  554. }
  555. }
  556. .dot{
  557. width: 12rpx; height: 12rpx; border-radius: 50%; background: #7bd571;
  558. position: absolute; bottom: 0; left: 50%; transform: translateX(-50%);
  559. }
  560. }
  561. }
  562. </style>