Преглед изворни кода

fix(cfc-web): add null/undefined guards in TeacherDashboard loadStats

Replace direct property access with safe defaults to prevent crashes
when API returns malformed responses (e.g. non-array family/team lists,
missing order records).
Xiaogang Liao пре 2 месеци
родитељ
комит
5d2f5d89b6
1 измењених фајлова са 28 додато и 24 уклоњено
  1. 28 24
      cfc-web/src/views/Dashboard.vue

+ 28 - 24
cfc-web/src/views/Dashboard.vue

@@ -318,7 +318,7 @@
                 </div>
               </div>
               <div class="td-family-stats">
-                <el-progress type="circle" :percentage="family.taskCompletionRate || 0" :width="50" :stroke-width="4"></el-progress>
+                <el-progress type="circle" :percentage="parseFloat(family.taskCompletionRate) || 0" :width="50" :stroke-width="4"></el-progress>
               </div>
             </div>
             <div v-if="families.length === 0" class="td-empty-state">
@@ -501,29 +501,33 @@ export default {
           getGuideDashboardStats().catch(err => { console.error('获取统计数据失败:', err); return { data: {} } })
         ])
         const [familyRes, teamRes, orderRes, statsRes] = results
-        if (familyRes.data) {
-          this.families = familyRes.data.slice(0, 5)
-          this.teacherStats.serviceFamilyCount = familyRes.data.length
-          this.teacherStats.activeFamilyCount = familyRes.data.filter(f => f.status === 'active').length
-        }
-        if (teamRes.data) {
-          this.teamMembers = teamRes.data.slice(0, 5).map(m => ({ ...m, trend: 0 }))
-        }
-        if (orderRes.data && orderRes.data.records) {
-          this.recentOrders = orderRes.data.records.slice(0, 10)
-          const totalSales = orderRes.data.records.reduce((sum, o) => sum + (o.amount || 0), 0)
-          this.teacherStats.monthlySales = totalSales
-          this.teacherStats.myCommission = Math.round(totalSales * 0.1)
-          this.todayStats.newOrders = orderRes.data.records.filter(o => {
-            const today = new Date().toDateString()
-            return new Date(o.createdAt).toDateString() === today
-          }).length
-        }
-        if (statsRes.data) {
-          this.todayStats.newFamilies = statsRes.data.totalFamilies || 0
-          this.todayStats.completedTasks = statsRes.data.todayCompletedTasks || 0
-          this.todayStats.pendingReviews = statsRes.data.pendingReviewTasks || 0
-        }
+
+        // Guard against malformed responses
+        var familyList = familyRes && familyRes.data ? familyRes.data : []
+        var teamList = teamRes && teamRes.data ? teamRes.data : []
+        var orderData = orderRes && orderRes.data ? orderRes.data : { records: [] }
+        var statsData = statsRes && statsRes.data ? statsRes.data : {}
+
+        this.families = (Array.isArray(familyList) ? familyList : []).slice(0, 5)
+        this.teacherStats.serviceFamilyCount = this.families.length
+        this.teacherStats.activeFamilyCount = this.families.filter(function(f) { return f.status === 'active' }).length
+
+        this.teamMembers = (Array.isArray(teamList) ? teamList : []).slice(0, 5).map(function(m) { return Object.assign({}, m, { trend: 0 }) })
+
+        var records = Array.isArray(orderData.records) ? orderData.records : []
+        this.recentOrders = records.slice(0, 10)
+        var totalSales = records.reduce(function(sum, o) { return sum + (o.amount || 0) }, 0)
+        this.teacherStats.monthlySales = totalSales
+        this.teacherStats.myCommission = Math.round(totalSales * 0.1)
+        var self = this
+        this.todayStats.newOrders = records.filter(function(o) {
+          var today = new Date().toDateString()
+          return o.createdAt && new Date(o.createdAt).toDateString() === today
+        }).length
+
+        this.todayStats.newFamilies = statsData.totalFamilies || 0
+        this.todayStats.completedTasks = statsData.todayCompletedTasks || 0
+        this.todayStats.pendingReviews = statsData.pendingReviewTasks || 0
       } catch (e) {
         console.error('加载教师工作台数据失败:', e)
       }