Files
WXProgram/miniprogram/pages/staff/admin-dashboard.ts
2025-10-26 13:15:04 +08:00

350 lines
8.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 管理员界面 - 全屏管理面板
import { UserInfo } from '../../types';
import { Role } from '../../utils/roleUtils';
import userService from '../../services/userService';
import statisticsService from '../../services/statisticsService';
import { API_BASE_URL } from '../../services/apiService';
Page({
data: {
userInfo: null as UserInfo | null,
// Grid布局的功能菜单
menuItems: [
{
id: 'employee',
name: '员工管理',
icon: '👥',
color: '#3498db',
description: '管理员工信息'
},
{
id: 'order',
name: '订单管理',
icon: '📦',
color: '#e74c3c',
description: '处理订单分配'
},
{
id: 'warehouse',
name: '仓库管理',
icon: '🏭',
color: '#f39c12',
description: '管理库存信息'
},
{
id: 'statistics',
name: '数据统计',
icon: '📊',
color: '#9b59b6',
description: '查看业务数据'
},
{
id: 'settings',
name: '系统设置',
icon: '⚙️',
color: '#34495e',
description: '系统配置管理'
},
{
id: 'map',
name: '地图监控',
icon: '🗺️',
color: '#27ae60',
description: '实时位置监控'
}
],
// 统计数据
stats: {
employeeCount: 0,
orderCount: 0,
warehouseCount: 0
},
// 当前时间
currentTime: ''
},
onLoad: function() {
this.getUserInfo();
this.loadStatistics();
this.updateCurrentTime();
// 每秒更新一次时间
setInterval(() => {
this.updateCurrentTime();
}, 1000);
},
onShow: function() {
this.loadStatistics();
this.updateCurrentTime();
},
/**
* 获取完整的头像URL
* @param avatarPath 头像相对路径
* @returns 完整的头像URL
*/
getFullAvatarUrl: function(avatarPath: string | undefined): string {
if (!avatarPath) {
return '/images/user-avatar.png';
}
// 如果已经是完整URL直接返回
if (avatarPath.startsWith('http')) {
return avatarPath;
}
// 将相对路径转换为完整URL
return `${API_BASE_URL}${avatarPath}`;
},
/**
* 处理用户信息预先准备好完整的头像URL
* @param userInfo 原始用户信息
* @returns 处理后的用户信息
*/
processUserInfo: function(userInfo: any): any {
if (!userInfo) {
return {
id: 0,
role: Role.ADMIN,
name: '管理员',
fullAvatarUrl: '/images/user-avatar.png'
};
}
// 预先准备好完整的头像URL
const processedUserInfo = { ...userInfo };
if (userInfo.avatarPath) {
processedUserInfo.fullAvatarUrl = this.getFullAvatarUrl(userInfo.avatarPath);
} else {
processedUserInfo.fullAvatarUrl = '/images/user-avatar.png';
}
return processedUserInfo;
},
/**
* 获取用户信息
*/
getUserInfo: async function() {
try {
const app = getApp<any>();
// 如果全局数据中没有用户信息,尝试从用户服务获取
if (!app.globalData.userInfo) {
const userInfo = await userService.getUserInfo();
if (userInfo) {
app.globalData.userInfo = userInfo;
}
}
if (app.globalData.userInfo) {
const userInfo = this.processUserInfo(app.globalData.userInfo);
this.setData({ userInfo });
// 验证是否为管理员
if (userInfo.role !== Role.ADMIN) {
wx.showToast({
title: '无权限访问',
icon: 'none'
});
wx.navigateBack();
}
} else {
// 如果仍然没有用户信息,显示默认信息
this.setData({
userInfo: this.processUserInfo(null)
});
}
} catch (error) {
console.error('获取用户信息失败:', error);
// 出错时显示默认信息
this.setData({
userInfo: this.processUserInfo(null)
});
}
},
/**
* 加载统计数据
*/
loadStatistics: async function() {
try {
wx.showLoading({
title: '加载数据中...'
});
// 使用统计服务获取真实数据
const stats = await statisticsService.getSystemStats();
this.setData({
stats: {
employeeCount: stats.employeeCount,
orderCount: stats.orderCount,
warehouseCount: stats.warehouseCount
}
});
wx.hideLoading();
} catch (error) {
console.error('加载统计数据失败:', error);
wx.hideLoading();
// 出错时使用默认值
this.setData({
stats: {
employeeCount: 0,
orderCount: 0,
warehouseCount: 0
}
});
wx.showToast({
title: '数据加载失败',
icon: 'none'
});
}
},
/**
* 菜单项点击事件
*/
onMenuItemTap: function(e: any) {
const itemId = e.currentTarget.dataset.id;
switch (itemId) {
case 'employee':
wx.navigateTo({
url: '/pages/staff/employee-management'
});
break;
case 'order':
wx.navigateTo({
url: '/pages/staff/order-management'
});
break;
case 'warehouse':
wx.navigateTo({
url: '/pages/staff/warehouse-management'
});
break;
case 'statistics':
wx.navigateTo({
url: '/pages/staff/statistics'
});
break;
case 'settings':
wx.navigateTo({
url: '/pages/staff/settings'
});
break;
case 'map':
wx.navigateTo({
url: '/pages/map/map'
});
break;
}
},
/**
* 返回首页
*/
goBack: function() {
wx.navigateBack();
},
/**
* 刷新数据
*/
onRefresh: function() {
this.loadStatistics();
this.updateCurrentTime();
wx.showToast({
title: '数据已刷新',
icon: 'success'
});
},
/**
* 更新当前时间
*/
updateCurrentTime: function() {
const now = new Date();
const timeString = now.toLocaleString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
});
this.setData({
currentTime: timeString
});
},
/**
* 解绑微信处理
*/
onUnbindWechat: async function() {
try {
// 确认对话框
wx.showModal({
title: '确认解绑',
content: '解绑微信后,您将需要重新登录并绑定账号。确定要继续吗?',
confirmText: '确定解绑',
confirmColor: '#e74c3c',
cancelText: '取消',
success: async (res) => {
if (res.confirm) {
// 显示加载中
wx.showLoading({
title: '解绑中...',
mask: true
});
try {
// 调用解绑服务
const result = await userService.unbindWechat();
wx.hideLoading();
if (result.success) {
wx.showToast({
title: '解绑成功',
icon: 'success',
duration: 2000
});
// 延迟跳转到登录页面
setTimeout(() => {
wx.reLaunch({
url: '/pages/login/login'
});
}, 2000);
} else {
wx.showToast({
title: result.message || '解绑失败',
icon: 'none'
});
}
} catch (error) {
wx.hideLoading();
wx.showToast({
title: '解绑失败,请重试',
icon: 'none'
});
console.error('解绑微信错误:', error);
}
}
}
});
} catch (error) {
console.error('解绑微信处理错误:', error);
wx.showToast({
title: '操作失败,请重试',
icon: 'none'
});
}
}
});