lx-calendar.vue 16 KB

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