index.vue 20 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
<template>
  <div class="dashboard-container">
    <!-- 欢迎区域 -->
    <div class="welcome-section">
      <div class="welcome-content">
        <h1 class="welcome-title">欢迎使用 Apple经销商ERP系统</h1>
        <p class="welcome-subtitle" v-if="loading">正在加载用户信息...</p>
        <p class="welcome-subtitle" v-else>您好,{{ userInfo?.username || userInfo?.realName || '用户' }}!今天是 {{ currentDate }}</p>
      </div>
      <div class="user-info">
        <div class="user-avatar">
          <span>{{ getUserInitials() }}</span>
        </div>
        <div class="user-details">
          <div class="user-name">{{ userInfo?.username || userInfo?.realName || '未知用户' }}</div>
          <div class="user-role">{{ getRoleName(userInfo) || '普通用户' }}</div>
        </div>
      </div>
    </div>

    <!-- 核心业务统计 -->
    <div class="stats-section">
      <h2 class="section-title">核心业务概览</h2>
      <div class="stats-grid">
        <div class="stat-card primary">
          <div class="stat-icon">📦</div>
          <div class="stat-content">
            <div class="stat-label">今日订单</div>
            <div class="stat-value">{{ statsLoading ? '...' : (todayStats.todayOrderCount || 0) }}</div>
            <div class="stat-change positive">{{ todayStats.orderGrowthRate || '+12.5%' }}</div>
          </div>
        </div>
        
        <div class="stat-card success">
          <div class="stat-icon">🚚</div>
          <div class="stat-content">
            <div class="stat-label">待出库</div>
            <div class="stat-value">{{ statsLoading ? '...' : (todayStats.pendingDeliveryCount || 0) }}</div>
            <div class="stat-change">需要处理</div>
          </div>
        </div>
        
        <div class="stat-card warning">
          <div class="stat-icon">🧾</div>
          <div class="stat-content">
            <div class="stat-label">待开票</div>
            <div class="stat-value">{{ statsLoading ? '...' : (todayStats.pendingInvoiceCount || 0) }}</div>
            <div class="stat-change">待处理</div>
          </div>
        </div>
        
        <div class="stat-card info">
          <div class="stat-icon">💰</div>
          <div class="stat-content">
            <div class="stat-label">今日销售额</div>
            <div class="stat-value">¥{{ statsLoading ? '...' : formatAmount(todayStats.todaySalesAmount || 0) }}</div>
            <div class="stat-change positive">{{ todayStats.salesGrowthRate || '+8.2%' }}</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="main-content">
      <!-- 快速操作 -->
      <div class="quick-actions-card">
        <h3 class="card-title">快速操作</h3>
        <div class="actions-grid">
          <button class="action-btn primary" @click="navigateTo('/main/order')">
            <div class="action-icon">📋</div>
            <div class="action-text">
              <div class="action-title">订单管理</div>
              <div class="action-desc">查看和管理订单</div>
            </div>
          </button>
          
          <button class="action-btn success" @click="navigateTo('/main/delivery')">
            <div class="action-icon">🚚</div>
            <div class="action-text">
              <div class="action-title">出库管理</div>
              <div class="action-desc">处理出库业务</div>
            </div>
          </button>
          
          <button class="action-btn warning" @click="navigateTo('/main/invoice')">
            <div class="action-icon">🧾</div>
            <div class="action-text">
              <div class="action-title">发票管理</div>
              <div class="action-desc">开具和管理发票</div>
            </div>
          </button>
          
          <button class="action-btn info" @click="navigateTo('/main/rebate')">
            <div class="action-icon">💰</div>
            <div class="action-text">
              <div class="action-title">返利管理</div>
              <div class="action-desc">返利计算和发放</div>
            </div>
          </button>
          
          <button class="action-btn secondary" @click="navigateTo('/main/product')">
            <div class="action-icon">📱</div>
            <div class="action-text">
              <div class="action-title">产品管理</div>
              <div class="action-desc">管理产品信息</div>
            </div>
          </button>
          
          <button class="action-btn secondary" @click="navigateTo('/main/dealer')">
            <div class="action-icon">🏢</div>
            <div class="action-text">
              <div class="action-title">经销商管理</div>
              <div class="action-desc">管理经销商信息</div>
            </div>
          </button>
        </div>
      </div>

      <!-- 系统信息 -->
      <div class="system-info-card">
        <h3 class="card-title">系统信息</h3>
        <div class="info-list">
          <div class="info-item">
            <div class="info-label">系统版本</div>
            <div class="info-value">{{ todayStats.systemVersion }}</div>
          </div>
          <div class="info-item">
            <div class="info-label">当前时间</div>
            <div class="info-value">{{ currentTime }}</div>
          </div>
          <div class="info-item">
            <div class="info-label">系统状态</div>
            <div class="info-value" :class="{ 'status-normal': todayStats.systemStatus === '正常运行', 'status-error': todayStats.systemStatus !== '正常运行' }">
              {{ todayStats.systemStatus }}
            </div>
          </div>
          <div class="info-item">
            <div class="info-label">在线用户</div>
            <div class="info-value">{{ todayStats.onlineUserCount }}</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 最近活动 -->
    <div class="recent-activities">
      <h3 class="card-title">最近活动</h3>
      <div class="activity-list" v-if="!activitiesLoading">
        <div class="activity-item" v-for="activity in recentActivities" :key="activity.id">
          <div class="activity-icon" :class="activity.type">
            {{ getActivityIcon(activity.type) }}
          </div>
          <div class="activity-content">
            <div class="activity-title">{{ activity.title }}</div>
            <div class="activity-time">{{ activity.time }}</div>
          </div>
        </div>
        <div v-if="recentActivities.length === 0" class="no-activities">
          暂无最近活动
        </div>
      </div>
      <div v-else class="loading-activities">
        正在加载活动数据...
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import { request } from '@/utils/request'
import dashboardApi, { type DashboardStats, type RecentActivity } from '@/api/dashboard'

const router = useRouter()
const currentTime = ref('')
const currentDate = ref('')
const userInfo = ref<any>(null)
const loading = ref(false)
const statsLoading = ref(false)

// 今日统计数据
const todayStats = ref<DashboardStats>({
  todayOrderCount: 0,
  pendingDeliveryCount: 0,
  pendingInvoiceCount: 0,
  todaySalesAmount: 0,
  onlineUserCount: 0,
  systemStatus: '正常运行',
  systemVersion: 'v1.0.0',
  orderGrowthRate: '+12.5%',
  salesGrowthRate: '+8.2%'
})

// 最近活动数据
const recentActivities = ref<RecentActivity[]>([])
const activitiesLoading = ref(false)

// 获取用户姓名首字母
const getUserInitials = () => {
  const name = userInfo.value?.username || userInfo.value?.realName || 'U'
  return name.charAt(0).toUpperCase()
}

// 获取角色名称
const getRoleName = (userInfo: any) => {
  // 优先使用roles字段中的角色信息
  if (userInfo?.roles && Array.isArray(userInfo.roles) && userInfo.roles.length > 0) {
    return userInfo.roles[0].roleName || '普通用户'
  }
  
  // 如果没有roles字段,回退到authorities
  const authorities = userInfo?.authorities
  if (authorities && Array.isArray(authorities)) {
    // 查找ROLE_开头的权限
    const roleAuthority = authorities.find(auth => 
      auth.authority && auth.authority.startsWith('ROLE_')
    )
    
    if (roleAuthority) {
      // 移除ROLE_前缀并转换为中文
      const roleName = roleAuthority.authority.replace('ROLE_', '')
      const roleMap: { [key: string]: string } = {
        'ADMIN': '管理员',
        'USER': '普通用户',
        'MANAGER': '经理',
        'OPERATOR': '操作员'
      }
      return roleMap[roleName] || roleName
    }
  }
  
  return '普通用户'
}

// 格式化金额
const formatAmount = (amount: number) => {
  return amount.toLocaleString('zh-CN', { minimumFractionDigits: 2 })
}

// 获取活动图标
const getActivityIcon = (type: string) => {
  const iconMap: { [key: string]: string } = {
    'order': '📋',
    'delivery': '🚚',
    'invoice': '🧾',
    'rebate': '💰',
    'system': '⚙️'
  }
  return iconMap[type] || '📄'
}

// 获取今日统计数据
const fetchTodayStats = async () => {
  try {
    statsLoading.value = true
    console.log('开始获取首页统计数据...')
    
    const response = await dashboardApi.getDashboardStats()
    todayStats.value = response
    console.log('首页统计数据获取成功:', response)
  } catch (error: any) {
    console.error('获取统计数据失败:', error)
    console.error('错误详情:', error?.message)
    // 如果API调用失败,使用默认值
    todayStats.value = {
      todayOrderCount: 0,
      pendingDeliveryCount: 0,
      pendingInvoiceCount: 0,
      todaySalesAmount: 0,
      onlineUserCount: 0,
      systemStatus: '系统异常',
      systemVersion: 'v1.0.0',
      orderGrowthRate: '0%',
      salesGrowthRate: '0%'
    }
  } finally {
    statsLoading.value = false
  }
}

// 获取最近活动
const fetchRecentActivities = async () => {
  try {
    activitiesLoading.value = true
    console.log('开始获取最近活动数据...')
    
    const response = await dashboardApi.getRecentActivities()
    recentActivities.value = response
    console.log('最近活动数据获取成功:', response)
  } catch (error: any) {
    console.error('获取最近活动失败:', error)
    console.error('错误详情:', error?.message)
    // 如果API调用失败,使用默认值
    recentActivities.value = []
  } finally {
    activitiesLoading.value = false
  }
}

// 获取用户信息
const fetchUserInfo = async () => {
  try {
    loading.value = true
    const response = await request.get('/api/auth/userinfo')
    userInfo.value = response
    console.log('用户信息:', response)
  } catch (error) {
    console.error('获取用户信息失败:', error)
    // 如果获取失败,尝试从本地存储获取
    const storedUserInfo = localStorage.getItem('userInfo')
    if (storedUserInfo) {
      userInfo.value = JSON.parse(storedUserInfo)
    }
  } finally {
    loading.value = false
  }
}

// 更新时间
const updateTime = () => {
  const now = new Date()
  currentTime.value = now.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  })
  currentDate.value = now.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    weekday: 'long'
  })
}

onMounted(() => {
  // 更新当前时间
  updateTime()
  setInterval(updateTime, 1000)
  
  // 检查是否已登录
  const token = localStorage.getItem('token')
  if (!token) {
    router.push('/')
    return
  }
  
  // 获取用户信息、统计数据和最近活动
  fetchUserInfo()
  fetchTodayStats()
  fetchRecentActivities()
})

// 页面跳转函数
const navigateTo = (path: string) => {
  router.push(path).catch(err => {
    console.error('页面跳转失败:', err)
  })
}

const logout = () => {
  // 清除本地存储
  localStorage.removeItem('token')
  localStorage.removeItem('userInfo')
  
  // 跳转到登录页
  router.push('/')
}
</script>

<style scoped lang="scss">
.dashboard-container {
  padding: 24px;
  min-height: 100vh;
  background: #f5f7fa;
}

// 欢迎区域
.welcome-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 32px;
  border-radius: 12px;
  margin-bottom: 32px;
  box-shadow: 0 8px 32px rgba(102, 126, 234, 0.3);

  .welcome-content {
    .welcome-title {
      font-size: 28px;
      font-weight: 700;
      margin: 0 0 8px 0;
      text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    }

    .welcome-subtitle {
      font-size: 16px;
      margin: 0;
      opacity: 0.9;
    }
  }

  .user-info {
    display: flex;
    align-items: center;
    gap: 16px;

    .user-avatar {
      width: 60px;
      height: 60px;
      background: rgba(255, 255, 255, 0.2);
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 24px;
      font-weight: bold;
      backdrop-filter: blur(10px);
    }

    .user-details {
      .user-name {
        font-size: 18px;
        font-weight: 600;
        margin-bottom: 4px;
      }

      .user-role {
        font-size: 14px;
        opacity: 0.8;
      }
    }
  }
}

// 统计区域
.stats-section {
  margin-bottom: 32px;

  .section-title {
    font-size: 20px;
    font-weight: 600;
    color: #333;
    margin: 0 0 20px 0;
  }

  .stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 20px;
  }

  .stat-card {
    background: white;
    padding: 24px;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
    display: flex;
    align-items: center;
    gap: 20px;
    transition: all 0.3s ease;
    border-left: 4px solid;

    &:hover {
      transform: translateY(-4px);
      box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
    }

    &.primary {
      border-left-color: #3498db;
    }

    &.success {
      border-left-color: #2ecc71;
    }

    &.warning {
      border-left-color: #f39c12;
    }

    &.info {
      border-left-color: #9b59b6;
    }

    .stat-icon {
      font-size: 32px;
      width: 60px;
      height: 60px;
      display: flex;
      align-items: center;
      justify-content: center;
      border-radius: 12px;
      background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
    }

    .stat-content {
      flex: 1;

      .stat-label {
        font-size: 14px;
        color: #666;
        margin-bottom: 8px;
        font-weight: 500;
      }

      .stat-value {
        font-size: 28px;
        font-weight: 700;
        color: #333;
        margin-bottom: 4px;
      }

      .stat-change {
        font-size: 12px;
        font-weight: 500;

        &.positive {
          color: #2ecc71;
        }

        &:not(.positive) {
          color: #666;
        }
      }
    }
  }
}

// 主要内容区域
.main-content {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 24px;
  margin-bottom: 32px;
}

// 快速操作卡片
.quick-actions-card {
  background: white;
  padding: 24px;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);

  .card-title {
    font-size: 18px;
    font-weight: 600;
    color: #333;
    margin: 0 0 20px 0;
  }

  .actions-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 16px;
  }

  .action-btn {
    background: white;
    border: 2px solid #e9ecef;
    border-radius: 12px;
    padding: 20px;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    gap: 16px;
    text-align: left;

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
    }

    &.primary {
      border-color: #3498db;
      background: linear-gradient(135deg, #3498db 0%, #2980b9 100%);
      color: white;

      &:hover {
        background: linear-gradient(135deg, #2980b9 0%, #1f618d 100%);
      }
    }

    &.success {
      border-color: #2ecc71;
      background: linear-gradient(135deg, #2ecc71 0%, #27ae60 100%);
      color: white;

      &:hover {
        background: linear-gradient(135deg, #27ae60 0%, #229954 100%);
      }
    }

    &.warning {
      border-color: #f39c12;
      background: linear-gradient(135deg, #f39c12 0%, #e67e22 100%);
      color: white;

      &:hover {
        background: linear-gradient(135deg, #e67e22 0%, #d35400 100%);
      }
    }

    &.info {
      border-color: #9b59b6;
      background: linear-gradient(135deg, #9b59b6 0%, #8e44ad 100%);
      color: white;

      &:hover {
        background: linear-gradient(135deg, #8e44ad 0%, #7d3c98 100%);
      }
    }

    &.secondary {
      border-color: #95a5a6;
      background: linear-gradient(135deg, #95a5a6 0%, #7f8c8d 100%);
      color: white;

      &:hover {
        background: linear-gradient(135deg, #7f8c8d 0%, #6c7b7d 100%);
      }
    }

    .action-icon {
      font-size: 24px;
      width: 40px;
      height: 40px;
      display: flex;
      align-items: center;
      justify-content: center;
      background: rgba(255, 255, 255, 0.2);
      border-radius: 8px;
    }

    .action-text {
      .action-title {
        font-size: 16px;
        font-weight: 600;
        margin-bottom: 4px;
      }

      .action-desc {
        font-size: 12px;
        opacity: 0.8;
      }
    }
  }
}

// 系统信息卡片
.system-info-card {
  background: white;
  padding: 24px;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);

  .card-title {
    font-size: 18px;
    font-weight: 600;
    color: #333;
    margin: 0 0 20px 0;
  }

  .info-list {
    .info-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 12px 0;
      border-bottom: 1px solid #f0f0f0;

      &:last-child {
        border-bottom: none;
      }

      .info-label {
        font-weight: 500;
        color: #666;
        font-size: 14px;
      }

      .info-value {
        color: #333;
        font-weight: 500;

        &.status-normal {
          color: #2ecc71;
        }

        &.status-error {
          color: #e74c3c;
        }
      }
    }
  }
}

// 最近活动
.recent-activities {
  background: white;
  padding: 24px;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);

  .card-title {
    font-size: 18px;
    font-weight: 600;
    color: #333;
    margin: 0 0 20px 0;
  }

  .activity-list {
    .activity-item {
      display: flex;
      align-items: center;
      gap: 16px;
      padding: 12px 0;
      border-bottom: 1px solid #f0f0f0;

      &:last-child {
        border-bottom: none;
      }

      .activity-icon {
        width: 40px;
        height: 40px;
        border-radius: 8px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 16px;

        &.order {
          background: #e3f2fd;
          color: #1976d2;
        }

        &.delivery {
          background: #e8f5e8;
          color: #2e7d32;
        }

        &.invoice {
          background: #fff3e0;
          color: #f57c00;
        }

        &.rebate {
          background: #f3e5f5;
          color: #7b1fa2;
        }

        &.system {
          background: #f1f8e9;
          color: #558b2f;
        }
      }

      .activity-content {
        flex: 1;

        .activity-title {
          font-size: 14px;
          color: #333;
          margin-bottom: 4px;
          font-weight: 500;
        }

        .activity-time {
          font-size: 12px;
          color: #666;
        }
      }
    }
  }
  
  .no-activities, .loading-activities {
    text-align: center;
    color: #666;
    padding: 20px;
    font-size: 14px;
  }
}

// 响应式设计
@media (max-width: 1200px) {
  .main-content {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 768px) {
  .dashboard-container {
    padding: 16px;
  }

  .welcome-section {
    flex-direction: column;
    text-align: center;
    gap: 20px;

    .user-info {
      justify-content: center;
    }
  }

  .stats-grid {
    grid-template-columns: 1fr;
  }

  .actions-grid {
    grid-template-columns: 1fr;
  }
}
</style>