lx-calendar.vue 15 KB

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