lx-calendar.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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(this.dot_list.indexOf(vo.date) > -1 || this.dot_list.indexOf(vo.date.replace(/-(\d)(?!\d)/g, '-0$1')) > -1 ){
  265. vo.dot = true;
  266. }else{
  267. vo.dot = false;
  268. }
  269. return {...vo}
  270. }))
  271. })
  272. this[week_list] = list;
  273. },
  274. set_to_day(type){
  275. let list = [];
  276. this[type].map((item,index)=>{
  277. list.push(item.map((vo,key)=>{
  278. if(vo.date == `${this.date}`){
  279. vo.today = true;
  280. }else{
  281. vo.today = false;
  282. }
  283. return {...vo};
  284. }))
  285. })
  286. this[type] = list;
  287. },
  288. item_click(item,item_index = -1){
  289. // if(!this.retract && item.type !== 'month'){
  290. // return false;
  291. // }
  292. if(!item.dot){
  293. return false;
  294. }
  295. console.log(item.date)
  296. this.date = item.date;
  297. if(item.type == 'month'){
  298. this.nowDay = item.day;
  299. if(item_index >= 0) this.to_week_index = item_index;
  300. }else if(this.retract){
  301. this.nowDay = item.day;
  302. }
  303. let now_arr = item.date.split('-')
  304. this.nowYear = now_arr[0];
  305. this.nowMonth = now_arr[1];
  306. this.nowDay = now_arr[2];
  307. this.set_to_day_all(item_index);
  308. this.nowTime = this.date_parse(`${item.date}`);
  309. this.change()
  310. this.set_doc_lists_update();
  311. },
  312. set_to_day_all(item_index){
  313. this.set_to_day('week_list')
  314. this.set_to_day('week_list_prev')
  315. this.set_to_day('week_list_next')
  316. this.set_to_day('week_list_prev_week')
  317. this.set_to_day('week_list_next_week')
  318. },
  319. get_month(type){
  320. let nowMonth = this.nowMonth;
  321. let nowYear = this.nowYear;
  322. let nowDay = this.nowDay;
  323. if(type == 'prev'){
  324. if(nowMonth == 1){
  325. nowMonth = 12;
  326. nowYear = nowYear - 1;
  327. }else{
  328. nowMonth--;
  329. }
  330. }else if(type == 'next'){
  331. if(nowMonth == 12){
  332. nowMonth = 1;
  333. nowYear = nowYear + 1;
  334. }else{
  335. nowMonth++;
  336. }
  337. }
  338. let days = this.get_month_days(nowMonth,nowYear);
  339. if(nowDay > days){
  340. nowDay = days;
  341. }
  342. return this.date_parse(`${nowYear}-${nowMonth}-${nowDay}`);
  343. },
  344. date_parse(str){
  345. return Date.parse(str.replace(/-(\d)(?!\d)/g, '-0$1'));
  346. },
  347. switch_month_week(type = 'next',update_week = false){
  348. if(this.retract){
  349. if(type == 'prev'){
  350. this.get_date(this.nowTime - 86400 * 7 * 1000);
  351. }else if(type == 'next'){
  352. this.get_date(this.nowTime + 86401 * 7 * 1000);
  353. }
  354. if(update_week){
  355. this.update_swiper_item('week');
  356. this.set_doc_lists_update();
  357. }
  358. }else{
  359. this.get_date(this.get_month(type))
  360. this.update_swiper_item('month');
  361. }
  362. this.set_doc_lists_update();
  363. this.set_to_day_all();
  364. if(update_week){
  365. this.change()
  366. }
  367. },
  368. update_swiper_item(type = 'month'){
  369. if(type == 'month'){
  370. if(this.current == 0){
  371. this.change_date_month('next')
  372. }else if(this.current == 2){
  373. this.change_date_month('prev')
  374. }
  375. }else if(type == 'week'){
  376. if(this.current == 0){
  377. this.change_date_week('next')
  378. }else if(this.current == 2){
  379. this.change_date_week('prev')
  380. }
  381. }
  382. },
  383. next(){
  384. this.get_date(this.next_date)
  385. },
  386. get_date(value = '',type = 'same'){
  387. let date = new Date();
  388. if(value){
  389. date = new Date(value);
  390. }
  391. let nowMonth = date.getMonth() + 1,
  392. nowYear = date.getFullYear(),
  393. nowDay = date.getDate(),
  394. nowTime = date.getTime(),
  395. nowWeek = date.getDay();
  396. let days = this.get_month_days(nowMonth,nowYear);
  397. let start_date = new Date(nowYear,nowMonth - 1, 1);
  398. let end_date = new Date(nowYear,nowMonth - 1, days);
  399. let prev_date = new Date(start_date.getTime() - 1);
  400. let prev_date_days = prev_date.getDate();
  401. let next_date = new Date(end_date.getTime() + 86401 * 1000);
  402. let next_date_days = next_date.getDate();
  403. let start_week = start_date.getDay();
  404. let date_arrs = [];
  405. let week_list = [];
  406. let count_days = 35;
  407. for(let i = prev_date_days - start_week + 1; i <= prev_date_days; i++){
  408. date_arrs.push({
  409. day:i,
  410. type:'prev',
  411. date:`${prev_date.getFullYear()}-${prev_date.getMonth()+1}-${i}`
  412. })
  413. }
  414. for(let i = 1; i <= days; i++){
  415. date_arrs.push({
  416. day:i,
  417. type:'month',
  418. today:i == nowDay ? true : false,
  419. date:`${nowYear}-${nowMonth}-${i}`
  420. })
  421. if(i == nowDay && type == 'same'){
  422. this.date = `${nowYear}-${nowMonth}-${i}`;
  423. }
  424. }
  425. if(this.debug) console.log(value,date,this.date,`${next_date.getFullYear()}-${next_date.getMonth()+1}-${next_date.getDate()}`)
  426. let date_arrs_length = date_arrs.length;
  427. // if(date_arrs_length > 35){
  428. count_days = 42;
  429. // }
  430. for(let i = 1; i <= count_days - date_arrs_length; i++){
  431. date_arrs.push({
  432. day:i,
  433. type:'next',
  434. date:`${next_date.getFullYear()}-${next_date.getMonth()+1}-${i}`
  435. })
  436. }
  437. for(let i = 0; i < date_arrs.length / 7; i++){
  438. let arr = [];
  439. for(let j = 0; j < 7; j++){
  440. if(date_arrs[i * 7 + j].today){
  441. if(type == 'same'){
  442. this.to_week_index = i
  443. }
  444. }
  445. arr.push(date_arrs[i * 7 + j]);
  446. }
  447. week_list.push(arr);
  448. }
  449. if(type == 'same'){
  450. this.week_list = week_list;
  451. this.nowYear = nowYear;
  452. this.nowMonth = nowMonth;
  453. this.nowDay = nowDay;
  454. this.nowTime = nowTime;
  455. this.start_date = start_date;
  456. this.end_date = end_date;
  457. this.prev_date = prev_date;
  458. this.next_date = next_date;
  459. }else if(type == 'prev'){
  460. this.week_list_prev = week_list;
  461. }else if(type == 'next'){
  462. this.week_list_next = week_list;
  463. }
  464. },
  465. get_month_days(nowMonth,nowYear){
  466. let month_arr = [1,3,5,7,8,10,12];
  467. let days = 0;
  468. if(nowMonth == 2){
  469. if(nowYear % 4 == 0){
  470. days = 29;
  471. }else{
  472. days = 28;
  473. }
  474. }else if(month_arr.indexOf(nowMonth) >= 0){
  475. days = 31;
  476. }else{
  477. days = 30;
  478. }
  479. return days;
  480. }
  481. }
  482. }
  483. </script>
  484. <style lang="less">
  485. @import (less) '../../static/lx-calendar/fonts/iconfont.css';
  486. @color:#007aff;
  487. @color_disabled:#f1f1f1;
  488. @color_standard:#333;
  489. @color_border:#f5f5f5;
  490. .date{
  491. width: 750rpx;
  492. }
  493. .head{
  494. display: flex; align-items: center; height: 100rpx; justify-content: center;
  495. border-bottom: 1rpx solid @color_border; color:@color_standard;
  496. .title{
  497. width: 200rpx; font-size: 30rpx; text-align: center;
  498. }
  499. .icon{
  500. display: block;
  501. .next{
  502. transform: rotate(180deg); display: block;
  503. }
  504. }
  505. }
  506. .retract{
  507. display: flex; justify-content: center; align-items: center; height: 80rpx;;
  508. .iconfont{
  509. transform: rotate(270deg);
  510. &.retract_icon{
  511. transform: rotate(90deg);
  512. }
  513. }
  514. }
  515. .date_dl{
  516. display: flex; width: 100%;
  517. .dd{
  518. flex:1; text-align: center;
  519. height: 80rpx;
  520. font-size: 26rpx;
  521. display: flex;
  522. flex-direction: column;
  523. align-items: center;
  524. justify-content: center;
  525. position: relative;
  526. .num{
  527. width: 60rpx; height: 60rpx; border-radius: 50%; line-height: 60rpx;
  528. &.disabled{
  529. color: @color_disabled;
  530. }
  531. &.month{
  532. color: @color_standard;
  533. }
  534. &.today{
  535. background: @color; color:#fff;
  536. }
  537. }
  538. .dot{
  539. width: 12rpx; height: 12rpx; border-radius: 50%; background: #7bd571;
  540. position: absolute; bottom: 0; left: 50%; transform: translateX(-50%);
  541. }
  542. }
  543. }
  544. </style>