lx-calendar.vue 17 KB

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