calendar.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // components/calendar/calendar.js
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. ySelData: {
  8. type: String,
  9. value: ""
  10. },
  11. yDateTimes:{
  12. type:Array,
  13. value:[]
  14. },
  15. // 设置哪一天为什么颜色
  16. yDayColor:{
  17. type:Array,
  18. value:[]
  19. },
  20. // 颜色映射表
  21. yEmotions:{
  22. type:Object,
  23. value:{}
  24. },
  25. before_show:{ //是否作为日期组件由按钮触发弹出(为true时是)
  26. type:Boolean,
  27. value:false
  28. },
  29. // task_show:{ //只有这个为1时,“和遮罩层有关的事件才会响应”
  30. // type:Boolean,
  31. // value:true
  32. // }
  33. },
  34. /**
  35. * 组件的初始数据
  36. */
  37. data: {
  38. now_selectedDate:'', //当前是几月几号
  39. selectedDate:'', //选中的几月几号
  40. selectedWeek:'', //选中的是星期几
  41. curYear:2022, //当前年份
  42. curMonth:0, //当前月份
  43. daysCountArr:[31,28,31,30,31,30,31,31,30,31,30,31],
  44. weekArr:['星期日','星期一','星期二','星期三','星期四','星期五','星期六'],
  45. dateList:[],
  46. now_event:"",
  47. },
  48. observers:{
  49. "yDayColor"(val){
  50. if(val.length){
  51. this.getDateList(this.data.curYear,this.data.curMonth-1,this.data.now_date);
  52. }
  53. }
  54. },
  55. lifetimes:{
  56. // 在这个生命周期中只能拿到传参的初始值(就是页面data中的值)
  57. attached(){
  58. var today=new Date();
  59. var curYear=today.getFullYear();
  60. var curMonth=today.getMonth()+1;
  61. var d=today.getDate();
  62. var i=today.getDay();
  63. var selData = curYear+'-'+curMonth+'-'+d;
  64. var selectedDate=this.properties.ySelData?this.properties.ySelData:selData;
  65. var now_date=curMonth+'-'+d;
  66. var selectedWeek=this.data.weekArr[i];
  67. this.setData({
  68. curYear,
  69. curMonth,
  70. selectedDate,
  71. selectedWeek,
  72. now_date,
  73. now_selectedDate:selData,
  74. closed:true
  75. })
  76. // // 初始化事件、传来的节日标注、style
  77. this.getDateList(curYear,curMonth-1,now_date);
  78. this.triggerEvent('timeload',{selectedDate,selectedWeek,event:this.data.now_event})
  79. },
  80. ready(){
  81. // 初始化事件、传来的节日标注、style
  82. // this.getDateList(this.data.curYear,this.data.curMonth-1,this.data.now_date);
  83. }
  84. },
  85. /**
  86. * 组件的方法列表
  87. */
  88. methods: {
  89. getDateList(y,mon,now_date){
  90. var vm=this;
  91. let _year=new Date();
  92. let _dayColor=this.data.yDayColor;
  93. let _emotion=this.data.yEmotions;
  94. let daysCountArr=this.data.daysCountArr;
  95. let _ydateTimes=this.data.yDateTimes;
  96. let _new_days=`${_year.getFullYear()}-(${_year.getMonth()}+1)-${_year.getDate()}`;
  97. // 判断闰年
  98. daysCountArr[1]=(y%4==0 && y%100!=0 || y%400==0)?29:28
  99. let dateList=[]
  100. dateList[0]=[]
  101. let weekIndex=0;
  102. for(var i=0;i<daysCountArr[mon];i++){
  103. var week=new Date(Date.UTC(y,mon,i+1)).getDay();
  104. dateList[weekIndex].push({
  105. value:y+'-'+(mon+1)+'-'+(i+1),
  106. dateTime:(mon+1)+'-'+(i+1),
  107. date:i+1,
  108. week:week
  109. });
  110. for(let j in dateList[weekIndex]){
  111. for(let k in _ydateTimes){
  112. if(dateList[weekIndex][j].dateTime==_ydateTimes[k].day){
  113. dateList[weekIndex][j].event=_ydateTimes[k].target
  114. }
  115. if(dateList[weekIndex][j].value==_ydateTimes[k].day){
  116. dateList[weekIndex][j].event=_ydateTimes[k].target
  117. }
  118. if(now_date==_ydateTimes[k].day){
  119. vm.setData({
  120. now_event:_ydateTimes[k].target
  121. })
  122. }
  123. }
  124. // 心情签到独立于特殊日期标记设计
  125. // 判断当前日期是不是设置了心情的日期
  126. for(let m of _dayColor){
  127. (dateList[weekIndex][j].value==m.day || dateList[weekIndex][j].value==_new_days) && !dateList[weekIndex][j].colors && (dateList[weekIndex][j].colors=_emotion[m.serene])
  128. }
  129. }
  130. if(week==6){
  131. weekIndex++;
  132. dateList[weekIndex]=[]
  133. }
  134. }
  135. vm.setData({
  136. dateList
  137. })
  138. },
  139. // 选中日期
  140. selectDate(e){
  141. var vm=this;
  142. vm.setData({
  143. selectedDate:e.currentTarget.dataset.date.value,
  144. selectedWeek:vm.data.weekArr[e.currentTarget.dataset.date.week]
  145. })
  146. this.triggerEvent('timechanged',{
  147. selectedDate:e.currentTarget.dataset.date.value,
  148. selectedWeek:vm.data.weekArr[e.currentTarget.dataset.date.week],
  149. event:(e.currentTarget.dataset.date.event?e.currentTarget.dataset.date.event:"")
  150. })
  151. },
  152. // 上个月
  153. preMonth(){
  154. var vm=this;
  155. var curYear=vm.data.curYear;
  156. var curMonth=vm.data.curMonth;
  157. curYear=curMonth-1?curYear:curYear-1;
  158. curMonth=curMonth-1?curMonth-1:12;
  159. vm.setData({
  160. curYear,
  161. curMonth
  162. });
  163. vm.getDateList(curYear,curMonth-1,this.data.now_date);
  164. },
  165. // 上年
  166. preYear(){
  167. var vm=this;
  168. var curYear=vm.data.curYear;
  169. var curMonth=vm.data.curMonth;
  170. curYear-=1;
  171. vm.setData({
  172. curYear,
  173. curMonth
  174. });
  175. vm.getDateList(curYear,curMonth-1,this.data.now_date);
  176. },
  177. // 下个月
  178. nextMonth(){
  179. var vm=this;
  180. var curYear=vm.data.curYear;
  181. var curMonth=vm.data.curMonth;
  182. curYear=curMonth+1==13?curYear+1:curYear;
  183. curMonth=curMonth+1==13?1:curMonth+1;
  184. vm.setData({
  185. curYear,
  186. curMonth
  187. });
  188. vm.getDateList(curYear,curMonth-1,this.data.now_date);
  189. },
  190. // 下年
  191. nextYear(){
  192. var vm=this;
  193. var curYear=vm.data.curYear;
  194. var curMonth=vm.data.curMonth;
  195. curYear+=1;
  196. vm.setData({
  197. curYear,
  198. curMonth
  199. });
  200. vm.getDateList(curYear,curMonth-1,this.data.now_date);
  201. },
  202. // 在弹出式场景下关闭日历
  203. closed(){
  204. if(this.properties.before_show){
  205. this.setData({
  206. closed:false
  207. })
  208. }
  209. }
  210. }
  211. })