lx-calendar.vue 16 KB

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