tanyanfei 7 maanden geleden
bovenliggende
commit
b0f79b0f74
2 gewijzigde bestanden met toevoegingen van 49 en 35 verwijderingen
  1. 22 15
      pages/avg/avg.js
  2. 27 20
      pages/matchdetail/matchdetail.js

+ 22 - 15
pages/avg/avg.js

@@ -96,7 +96,20 @@ Page({
       }
     })
   },
-
+ //计算周数
+ getWeekNumber:function(dateString) {
+  const date = new Date(dateString);
+  const year = date.getFullYear();
+  // 将年份的第一个周一作为第一周的开始
+  const firstWeekStart = new Date(year, 0, 1);
+  firstWeekStart.setDate(firstWeekStart.getDate() - firstWeekStart.getDay() + 1);
+  // 计算给定日期是一年中的第几周
+  const currentDate = new Date(date);
+  currentDate.setDate(currentDate.getDate() - currentDate.getDay() + 1);
+  let milliseconds = currentDate.getTime() - firstWeekStart.getTime();
+  const weeks = Math.round(milliseconds / (7 * 24 * 60 * 60 * 1000));
+  return weeks + 1; // 因为getWeek返回的是年初至今的周数,所以加1得到完整的周数
+},
   loadAvg(){
     var group = this.data.list[0]
     var param = {
@@ -105,24 +118,18 @@ Page({
     $api.getAvg(param).then(res => {
       if(res.data.code == 0){
         records = res.data.data.list.reverse()
-      var week=['一','二','三','四','五','六','七','八','九','十','十一','十二','十三','十四','十五']
-        var days=res.data.data.list.reverse(),weeks=[[]]
+        var days=res.data.data.list,weeks=[]
         days=days.reverse()
-        week.forEach((item,index)=>{
-          weeks[index]={
-            name:'第'+item+'周',
+        for(let i=0;i<52;i++){
+          weeks.push({
+            name:'第'+(i+1)+'周',
             data:[],
             total:0
-          }
-        })
-        var i=0
+          })
+        }
         days.forEach(item=>{
-          var w=new Date(item.stock_date).getDay() 
-          item.day=w
-          if(w==1){
-            i++
-          }
-          weeks[i].data.push(item)
+          var w=this.getWeekNumber(item.stock_date)
+          weeks[w-1].data.push(item)
         })
         weeks.forEach(item=>{
           if(item.data.length>0){

+ 27 - 20
pages/matchdetail/matchdetail.js

@@ -154,27 +154,34 @@ Page({
   toThousandFilter(num) {
     return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
   },
-
+    //计算周数
+    getWeekNumber:function(dateString) {
+      const date = new Date(dateString);
+      const year = date.getFullYear();
+      // 将年份的第一个周一作为第一周的开始
+      const firstWeekStart = new Date(year, 0, 1);
+      firstWeekStart.setDate(firstWeekStart.getDate() - firstWeekStart.getDay() + 1);
+      // 计算给定日期是一年中的第几周
+      const currentDate = new Date(date);
+      currentDate.setDate(currentDate.getDate() - currentDate.getDay() + 1);
+      let milliseconds = currentDate.getTime() - firstWeekStart.getTime();
+      const weeks = Math.round(milliseconds / (7 * 24 * 60 * 60 * 1000));
+      return weeks + 1; // 因为getWeek返回的是年初至今的周数,所以加1得到完整的周数
+    },
   getWeek(list){
-    var week=['一','二','三','四','五','六','七','八','九','十','十一','十二','十三','十四','十五']
-        var days=list,weeks=[[]]
-        days=days.reverse()
-        week.forEach((item,index)=>{
-          weeks[index]={
-            name:'第'+item+'周',
-            data:[],
-            total:0
-          }
-        })
-        var i=0
-        days.forEach(item=>{
-          var w=new Date(item.stock_date).getDay() 
-          item.day=w
-          if(w==1){
-            i++
-          }
-          weeks[i].data.push(item)
-        })
+    var days=list,weeks=[]
+    days=days.reverse()
+    for(let i=0;i<52;i++){
+      weeks.push({
+        name:'第'+(i+1)+'周',
+        data:[],
+        total:0
+      })
+    }
+    days.forEach(item=>{
+      var w=this.getWeekNumber(item.stock_date)
+      weeks[w-1].data.push(item)
+    })
         weeks.forEach(item=>{
           if(item.data.length>0){
             var total=0