calendar.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. const util = require("../../../../utils/util");
  2. const app = getApp();
  3. Component({
  4. properties: {
  5. chackedDate: {
  6. type: String,
  7. observer: function (newVal) {
  8. if (this.data.chackedDate){
  9. let arr = newVal.split('-');
  10. let newDate=arr[0]+'-'+parseInt(arr[1])+'-'+parseInt(arr[2]);
  11. this.setData({
  12. chackedDate: newDate,
  13. todayMonth:parseInt(arr[1]),
  14. todayYear:arr[0]
  15. });
  16. }
  17. }
  18. },
  19. leaderUid: {
  20. type: String,
  21. observer: function (newVal) {
  22. this.setData({leaderUid:newVal});
  23. }
  24. },
  25. show: {
  26. type: Boolean,
  27. observer: function (newVal) {
  28. let arr = this.data.chackedDate.split('-');
  29. this.setData({
  30. show: newVal,
  31. month:parseInt(arr[1]),
  32. year:arr[0]
  33. });
  34. if (this.data.show) this.onloadData();
  35. }
  36. },
  37. },
  38. data: {
  39. height:'65%',
  40. show:false,
  41. leaderUid:'',
  42. inputMonth:'',//传入的月份
  43. chackedDate:'',
  44. slideOne: "", // 左右滑动的class样式
  45. slideFlag: false, // 左右滑动定义的状态变量
  46. // 页面输出的数据
  47. week: ['日','一', '二', '三', '四', '五', '六'],
  48. months: ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'],
  49. calendar:'',//输入日历
  50. month: new Date().getMonth() + 1,//当前页面的月
  51. year: new Date().getFullYear(),//当前页面的年
  52. today: '',//今天
  53. todaySecond: new Date().getTime(),//今天的秒
  54. todayYear: new Date().getFullYear(),//今天所在的年
  55. todayMonth: new Date().getMonth() + 1,//今天坐在的月
  56. clickDate:'',
  57. activeData:[],//活动列表
  58. imgPath: util.config.imgPath
  59. },
  60. methods: {
  61. // 弹窗关闭
  62. close(e) {
  63. this.triggerEvent("close", e.detail);
  64. },
  65. onloadData(){
  66. let date = new Date()
  67. this.setData({
  68. today: this.data.chackedDate? this.data.chackedDate : this.formatDate(),
  69. clickDate: this.data.chackedDate? this.data.chackedDate : this.formatDate()
  70. });
  71. this.generateAllDays(this.data.year, this.data.month)
  72. this.setBarTitle(this.data.todayMonth)
  73. },
  74. setBarTitle(month){
  75. this.setData({
  76. title:month + '月'
  77. })
  78. },
  79. digit: function (n) {
  80. if (n == null || n === "" || n == undefined) {
  81. return "";
  82. } else {
  83. n = n.toString();
  84. return n[1] ? n : '' + n;
  85. }
  86. },
  87. //生成日期格式:2019-5-7
  88. formatDate: function (date = new Date()) {
  89. // 年月日
  90. var year = date.getFullYear()
  91. var month = date.getMonth() + 1
  92. var day = date.getDate()
  93. // 返回值
  94. return [year, this.digit(month), this.digit(day)].map(this.digit).join('-');
  95. },
  96. //生成当前需要显示的全部日期
  97. generateAllDays(year, month) {
  98. let lastMonth = this.lastMonthDays(year, month),
  99. thisMonth = this.currentMonthDays(year, month),
  100. nextMonth = this.nextMonthDays(year, month),
  101. days = [].concat(lastMonth, thisMonth, nextMonth)
  102. let that =this,data={};
  103. data.startDate=days[0].date;
  104. data.endDate=days[days.length-1].date;
  105. if (that.data.leaderUid !='') {
  106. data.leaderUid=that.data.leaderUid
  107. }
  108. util.ajax({
  109. func: "v2/gwcourse/schedule",
  110. // data: { "startDate": days[0].date, "endDate": days[days.length-1].date }
  111. data:data
  112. }, function (res) {
  113. if (res.code == 0) {
  114. let data = res.data;
  115. if (!util.isObjEmpty(data)){
  116. days.forEach((item,index)=>{
  117. data.forEach((el,i)=>{
  118. if (util.formatDate(item.date,'yyyy-MM-dd', false) == el.date){
  119. item.num = el.num;
  120. }
  121. })
  122. })
  123. that.setData({ calendar: days});
  124. }
  125. }else{
  126. util.showTips(res.reason);
  127. }
  128. })
  129. that.setData({ calendar: days });
  130. },
  131. //生成需要显示的上月值
  132. lastMonthDays(year, month) {
  133. const lastMonth = parseInt(month)===1?12: parseInt(month) - 1,
  134. lastMonthYear = parseInt(month) === 1 && parseInt(lastMonth) === 12 ? `${parseInt(year) - 1}` : year,
  135. lastNum = this.getNumOfDays(lastMonthYear, lastMonth) //上月天数
  136. let startWeek = this.getWeekOfDate(year, month - 1, 1), days = []
  137. if (startWeek == 6) {
  138. return days
  139. }
  140. const startDay = lastNum - startWeek
  141. return this.generateDays(lastMonthYear, lastMonth, lastNum, { startNum: startDay, notCurrent: true })
  142. },
  143. //生成本月的值
  144. currentMonthDays(year, month) {
  145. const numOfDays = this.getNumOfDays(year, month)
  146. return this.generateDays(year, month, numOfDays)
  147. },
  148. /**
  149. * 生成下个月应显示天
  150. * @param {any} year
  151. * @param {any} month
  152. * @returns
  153. */
  154. nextMonthDays(year, month) {
  155. const nextMonth = parseInt(month) === 12?1:parseInt(month) + 1,
  156. nextMonthYear = parseInt(month) === 12 && parseInt(nextMonth) === 1 ? `${parseInt(year) + 1}` : year,
  157. nextNum = this.getNumOfDays(nextMonthYear, nextMonth) //下月天数
  158. let endWeek = this.getWeekOfDate(year, month), days = [], daysNum = 0
  159. if (endWeek == 6) {
  160. return days
  161. } else if (endWeek == 7) {
  162. daysNum = 6
  163. } else {
  164. daysNum = 6 - endWeek
  165. }
  166. return this.generateDays(nextMonthYear, nextMonth, daysNum, { startNum: 1, notCurrent: true })
  167. },
  168. //生成本月的上,或者中,或者下
  169. generateDays(year, month, daysNum, option = {startNum: 1, notCurrent: false}) {
  170. const weekMap = ['一', '二', '三', '四', '五', '六', '日']
  171. let days = []
  172. for (let i = option.startNum; i <= daysNum; i++) {
  173. let week = weekMap[new Date(year, month - 1, i).getUTCDay()]
  174. let day = i;
  175. days.push({
  176. date: `${year}-${month}-${day}`,
  177. event: false,
  178. day,
  179. week,
  180. month,
  181. year,
  182. dateSecond: new Date(`${year}/${month}/${day}`).getTime()
  183. })
  184. }
  185. return days
  186. },
  187. getNumOfDays(year, month, day = 0) {
  188. return new Date(year, month, day).getDate()
  189. },
  190. getWeekOfDate(year, month, day = 0) {
  191. let dateOfMonth = new Date(year, month, 0).getUTCDay() + 1;
  192. dateOfMonth == 7 ? dateOfMonth = 0 : '';
  193. return dateOfMonth;
  194. },
  195. formatDay(day) {
  196. return `${(day + '').length > 1 ? '' : '0'}${day}`
  197. },
  198. formatMonth(month) {
  199. let monthStr = ''
  200. if (month > 12 || month < 1) {
  201. monthStr = Math.abs(month - 12) + ''
  202. } else {
  203. monthStr = month + ''
  204. }
  205. monthStr = `${monthStr.length > 1 ? '' : '0'}${monthStr}`
  206. return monthStr
  207. },
  208. filterData(data){
  209. let n = data.map((item,index)=>{
  210. data[index].date = this.filterDate(item.date)
  211. return data[index]
  212. })
  213. return n;
  214. },
  215. //处理日期带0的格式
  216. filterDate(data){
  217. let date = data.split("-"),
  218. year = date[0],
  219. month = date[1] ,
  220. day = date[2] ;
  221. if(month.slice(0,1)==0) month = month.slice(1, 2);
  222. if(day.slice(0, 1) == 0) day = day.slice(1, 2);
  223. return `${year}-${month}-${day}`
  224. },
  225. touchStart(e) {
  226. this.setData({
  227. startX: e.changedTouches[0].pageX
  228. })
  229. },
  230. touchEnd(e) {
  231. if (!this.data.slideFlag) {
  232. this.setData({
  233. slideFlag: true,
  234. endX: e.changedTouches[0].pageX
  235. })
  236. let disX = e.changedTouches[0].pageX - this.data.startX;
  237. if (disX < -60) {
  238. this.setData({
  239. slideOne: "animated fadeOutLeft"
  240. })
  241. setTimeout(() => {
  242. this.tapNext();
  243. }, 300);
  244. setTimeout(() => {
  245. this.setData({
  246. slideFlag: false,
  247. slideOne: ""
  248. })
  249. }, 800);
  250. } else if (disX > 60) {
  251. let date = new Date();
  252. if ((this.data.year + '-' + this.data.month) == date.getFullYear() + '-' + (date.getMonth() + 1)){
  253. this.setData({
  254. slideFlag: false
  255. })
  256. return false;
  257. }
  258. this.setData({
  259. slideOne: "animated fadeOutRight"
  260. })
  261. setTimeout(() => {
  262. this.tapPrev();
  263. }, 300);
  264. setTimeout(() => {
  265. this.setData({
  266. slideFlag: false,
  267. slideOne: ""
  268. })
  269. }, 800);
  270. } else {
  271. this.setData({
  272. slideFlag: false
  273. })
  274. }
  275. }
  276. },
  277. /**
  278. * 左右滑动调用的函数
  279. */
  280. tapPrev() {//上一页
  281. let year = this.data.year, month = this.data.month;
  282. month--;
  283. if (month < 1) {
  284. year--;
  285. month = 12;
  286. }
  287. this.setBarTitle(month)
  288. this.getData(year, month)
  289. },
  290. tapNext() {//下一页
  291. let year = this.data.year, month = this.data.month;
  292. month++;
  293. if (month > 12){
  294. year++;
  295. month = 1;
  296. }
  297. this.setBarTitle(month)
  298. this.getData(year, month)
  299. },
  300. getData(year, month){
  301. this.setData({
  302. month, year
  303. })
  304. this.generateAllDays(year, month)
  305. },
  306. //点击具体的日期
  307. bindDayTap(e){
  308. let data = e.currentTarget.dataset;
  309. if (data.datesecond > this.data.todaySecond || data.date == this.data.today){//大于等于今天
  310. this.setData({ clickDate: data.date })
  311. if (data.month != this.data.month){
  312. this.getData(data.year, data.month)//获取上下月
  313. this.setBarTitle(data.month)
  314. }
  315. let date = data.year + "-" + this.formatMonth(data.month) + "-" + this.formatDay(data.day);
  316. this.getActiveDetail(date)
  317. }
  318. },
  319. getActiveDetail(date){
  320. this.setData({
  321. month:this.data.inputMonth
  322. })
  323. this.triggerEvent("getCalendarInfo", date);
  324. },
  325. }
  326. })