Files
WXProgram/miniprogram/pages/staff/admin-dashboard.ts

350 lines
8.2 KiB
TypeScript
Raw Normal View History

2025-10-19 23:38:54 +08:00
// 管理员界面 - 全屏管理面板
import { UserInfo } from '../../types';
2025-10-26 13:15:04 +08:00
import { Role } from '../../utils/roleUtils';
2025-10-21 21:51:51 +08:00
import userService from '../../services/userService';
2025-10-26 13:15:04 +08:00
import statisticsService from '../../services/statisticsService';
import { API_BASE_URL } from '../../services/apiService';
2025-10-19 23:38:54 +08:00
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: ''
},
2025-10-26 13:15:04 +08:00
onLoad: function() {
2025-10-19 23:38:54 +08:00
this.getUserInfo();
this.loadStatistics();
this.updateCurrentTime();
// 每秒更新一次时间
setInterval(() => {
this.updateCurrentTime();
}, 1000);
},
2025-10-26 13:15:04 +08:00
onShow: function() {
2025-10-19 23:38:54 +08:00
this.loadStatistics();
this.updateCurrentTime();
},
2025-10-26 13:15:04 +08:00
/**
* 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;
},
2025-10-19 23:38:54 +08:00
/**
*
*/
2025-10-26 13:15:04 +08:00
getUserInfo: async function() {
try {
const app = getApp<any>();
// 如果全局数据中没有用户信息,尝试从用户服务获取
if (!app.globalData.userInfo) {
const userInfo = await userService.getUserInfo();
if (userInfo) {
app.globalData.userInfo = userInfo;
}
}
2025-10-19 23:38:54 +08:00
2025-10-26 13:15:04 +08:00
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)
2025-10-19 23:38:54 +08:00
});
}
2025-10-26 13:15:04 +08:00
} catch (error) {
console.error('获取用户信息失败:', error);
// 出错时显示默认信息
this.setData({
userInfo: this.processUserInfo(null)
});
2025-10-19 23:38:54 +08:00
}
},
/**
*
*/
2025-10-26 13:15:04 +08:00
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'
});
}
2025-10-19 23:38:54 +08:00
},
/**
*
*/
2025-10-26 13:15:04 +08:00
onMenuItemTap: function(e: any) {
2025-10-19 23:38:54 +08:00
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;
}
},
/**
*
*/
2025-10-26 13:15:04 +08:00
goBack: function() {
2025-10-19 23:38:54 +08:00
wx.navigateBack();
},
/**
*
*/
2025-10-26 13:15:04 +08:00
onRefresh: function() {
2025-10-19 23:38:54 +08:00
this.loadStatistics();
this.updateCurrentTime();
wx.showToast({
title: '数据已刷新',
icon: 'success'
});
},
/**
*
*/
2025-10-26 13:15:04 +08:00
updateCurrentTime: function() {
2025-10-19 23:38:54 +08:00
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
});
2025-10-21 21:51:51 +08:00
},
/**
*
*/
2025-10-26 13:15:04 +08:00
onUnbindWechat: async function() {
2025-10-21 21:51:51 +08:00
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'
});
}
2025-10-19 23:38:54 +08:00
}
});