添加管理员逻辑
This commit is contained in:
@@ -1,34 +1,48 @@
|
||||
// index.ts
|
||||
|
||||
// 引入服务和工具函数
|
||||
import { showToast } from '../../utils/helpers';
|
||||
|
||||
import userService from '../../services/userService';
|
||||
import locationTrackingService from '../../services/locationTrackingService';
|
||||
|
||||
// 引入主页面模块
|
||||
import { MainPageModule } from './modules/mainPageModule';
|
||||
|
||||
// 主页面组件接口定义
|
||||
interface IndexPageComponent {
|
||||
data: {
|
||||
mainPageModule: MainPageModule | null;
|
||||
// 核心UI状态
|
||||
showUserPanel: boolean;
|
||||
showOrderPanel: boolean;
|
||||
showDeliveryPersonModal: boolean;
|
||||
showWarehouseModal: boolean;
|
||||
};
|
||||
}
|
||||
// 导入类型定义
|
||||
import { IndexPageComponent } from '../../types';
|
||||
|
||||
// 主页面组件
|
||||
Component<IndexPageComponent>({
|
||||
Component<IndexPageComponent['data'], any, any, any, false>({
|
||||
data: {
|
||||
mainPageModule: null as MainPageModule | null,
|
||||
// 核心UI状态
|
||||
longitude: 116.397428,
|
||||
latitude: 39.90923,
|
||||
scale: 16,
|
||||
markers: [],
|
||||
userInfo: null,
|
||||
authStatus: {
|
||||
hasWxCode: false,
|
||||
userStatus: 'unknown'
|
||||
},
|
||||
showUserPanel: false,
|
||||
showOrderPanel: false,
|
||||
currentOrder: null,
|
||||
currentDeliveryPerson: null,
|
||||
currentWarehouse: null,
|
||||
currentPanelPosition: { x: 0, y: 0 },
|
||||
polyline: null,
|
||||
pendingOrders: [],
|
||||
currentRoute: null,
|
||||
showRoute: false,
|
||||
routeDistance: 0,
|
||||
routeDuration: 0,
|
||||
showWarehouseModal: false,
|
||||
showDeliveryPersonModal: false,
|
||||
showWarehouseModal: false
|
||||
warehouseModalState: 'bottom',
|
||||
deliveryPersonModalState: 'bottom',
|
||||
showSignOutButton: false,
|
||||
showSignInButton: false,
|
||||
showRegisterButton: false,
|
||||
showAuthButton: true,
|
||||
// mainPageModule: null as MainPageModule | null
|
||||
},
|
||||
|
||||
lifetimes: {
|
||||
@@ -45,21 +59,21 @@ Component<IndexPageComponent>({
|
||||
}
|
||||
},
|
||||
|
||||
show() {
|
||||
console.log('index page show');
|
||||
// 页面显示时调用主页面模块的onShow方法
|
||||
if (this.data.mainPageModule) {
|
||||
this.data.mainPageModule.onShow();
|
||||
}
|
||||
},
|
||||
// show() {
|
||||
// console.log('index page show');
|
||||
// // 页面显示时调用主页面模块的onShow方法
|
||||
// if (this.data.mainPageModule) {
|
||||
// this.data.mainPageModule.onShow();
|
||||
// }
|
||||
// },
|
||||
|
||||
hide() {
|
||||
console.log('index page hide');
|
||||
// 页面隐藏时调用主页面模块的onHide方法
|
||||
if (this.data.mainPageModule) {
|
||||
this.data.mainPageModule.onHide();
|
||||
}
|
||||
}
|
||||
// hide() {
|
||||
// console.log('index page hide');
|
||||
// // 页面隐藏时调用主页面模块的onHide方法
|
||||
// if (this.data.mainPageModule) {
|
||||
// this.data.mainPageModule.onHide();
|
||||
// }
|
||||
// }
|
||||
},
|
||||
|
||||
methods: {
|
||||
@@ -82,26 +96,17 @@ Component<IndexPageComponent>({
|
||||
|
||||
// 初始化页面
|
||||
async initPage() {
|
||||
const app = getApp<any>();
|
||||
|
||||
// 初始化主页面模块
|
||||
this.setData({
|
||||
mainPageModule: new MainPageModule(this)
|
||||
});
|
||||
|
||||
// 设置globalData中的loginModule引用,用于废弃方法的重定向
|
||||
if (this.data.mainPageModule) {
|
||||
app.globalData.loginModule = this.data.mainPageModule.getLoginModule();
|
||||
}
|
||||
|
||||
// 异步检查登录状态
|
||||
await this.checkAndUpdateLoginStatus();
|
||||
},
|
||||
|
||||
// 异步检查并更新登录状态
|
||||
async checkAndUpdateLoginStatus() {
|
||||
const app = getApp<any>();
|
||||
|
||||
try {
|
||||
// 获取登录模块
|
||||
if (!this.data.mainPageModule) {
|
||||
@@ -154,7 +159,9 @@ Component<IndexPageComponent>({
|
||||
},
|
||||
|
||||
// 异步检查登录状态
|
||||
async asyncCheckLoginStatus(loginModule: any): Promise<boolean> {
|
||||
async asyncCheckLoginStatus(loginModule: any): Promise<boolean> {
|
||||
getApp<any>();
|
||||
|
||||
const app = getApp<any>();
|
||||
|
||||
// 首先检查本地是否有登录状态
|
||||
@@ -181,9 +188,11 @@ Component<IndexPageComponent>({
|
||||
try {
|
||||
const loginResult = await userService.wxLogin();
|
||||
if (loginResult.success && loginResult.userInfo) {
|
||||
// 登录成功,更新全局用户信息
|
||||
app.globalData.userInfo = loginResult.userInfo;
|
||||
app.globalData.isLoggedIn = true;
|
||||
const app = getApp<any>();
|
||||
|
||||
// 登录成功,更新全局用户信息
|
||||
app.globalData.userInfo = loginResult.userInfo;
|
||||
app.globalData.isLoggedIn = true;
|
||||
|
||||
// 登录成功后,此时已经有token,可以安全获取用户状态
|
||||
const userStatus = await loginModule.determineUserStatus(loginResult.userInfo);
|
||||
@@ -243,18 +252,19 @@ Component<IndexPageComponent>({
|
||||
},
|
||||
|
||||
// 登录成功后统一刷新页面
|
||||
async refreshPageAfterLogin() {
|
||||
if (!this.data.mainPageModule) {
|
||||
console.error('mainPageModule未初始化');
|
||||
return;
|
||||
}
|
||||
|
||||
const loginModule = this.data.mainPageModule.getLoginModule();
|
||||
const app = getApp<any>();
|
||||
async refreshPageAfterLogin() {
|
||||
if (!this.data.mainPageModule) {
|
||||
console.error('mainPageModule未初始化');
|
||||
return;
|
||||
}
|
||||
|
||||
const loginModule = this.data.mainPageModule.getLoginModule();
|
||||
getApp<any>();
|
||||
|
||||
console.log('🔄 === 全局登录流程完成,执行统一页面刷新 ===');
|
||||
|
||||
// 1. 更新页面登录状态
|
||||
const app = getApp<any>();
|
||||
loginModule.updatePageAfterLogin(app.globalData.userInfo);
|
||||
|
||||
// 2. 更新按钮显示状态
|
||||
@@ -265,7 +275,7 @@ Component<IndexPageComponent>({
|
||||
|
||||
// 4. 登录成功后只加载公开数据(仓库等),不加载业务数据
|
||||
// 业务数据(员工位置等)只有在用户签到后才应该加载
|
||||
if (app.globalData.isLoggedIn) {
|
||||
if (app.globalData.isLoggedIn && this.data.authStatus.userStatus === 'signed_in') {
|
||||
console.log('🔍 登录成功,开始加载公开数据...');
|
||||
await this.data.mainPageModule.loadPublicData();
|
||||
console.log('✅ 公开数据加载完成');
|
||||
@@ -307,6 +317,8 @@ Component<IndexPageComponent>({
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
|
||||
// 处理授权登录 - 已迁移到LoginModule
|
||||
async handleAuthLogin() {
|
||||
if (!this.data.mainPageModule) {
|
||||
@@ -335,107 +347,24 @@ Component<IndexPageComponent>({
|
||||
}
|
||||
},
|
||||
|
||||
// 处理退出登录
|
||||
async handleLogout() {
|
||||
try {
|
||||
wx.showLoading({
|
||||
title: '正在退出...',
|
||||
mask: true
|
||||
});
|
||||
|
||||
// 调用loginModule的logout方法
|
||||
if (!this.data.mainPageModule) {
|
||||
console.error('mainPageModule未初始化');
|
||||
return;
|
||||
}
|
||||
const loginModule = this.data.mainPageModule.getLoginModule();
|
||||
await loginModule.logout();
|
||||
|
||||
// 更新页面状态
|
||||
this.setData({
|
||||
userInfo: null,
|
||||
'authStatus.hasWxCode': false,
|
||||
'authStatus.userStatus': 'unknown',
|
||||
showUserPanel: false
|
||||
});
|
||||
|
||||
|
||||
|
||||
// 处理签退 - 已迁移到LoginModule
|
||||
async handleSignOut() {
|
||||
if (!this.data.mainPageModule) {
|
||||
console.error('mainPageModule未初始化');
|
||||
return;
|
||||
}
|
||||
|
||||
const loginModule = this.data.mainPageModule.getLoginModule();
|
||||
const success = await loginModule.handleSignOut();
|
||||
|
||||
if (success) {
|
||||
// 更新按钮显示状态
|
||||
this.updateButtonDisplayStatus();
|
||||
|
||||
wx.hideLoading();
|
||||
showToast('已退出登录');
|
||||
console.log('用户已退出登录');
|
||||
} catch (error) {
|
||||
wx.hideLoading();
|
||||
console.error('退出登录失败:', error);
|
||||
showToast('退出登录失败');
|
||||
}
|
||||
},
|
||||
|
||||
// 处理签退
|
||||
async handleSignOut() {
|
||||
try {
|
||||
console.log('调试信息 - 开始执行签退流程');
|
||||
wx.showLoading({
|
||||
title: '正在签退...',
|
||||
mask: true
|
||||
});
|
||||
|
||||
// 停止位置追踪服务
|
||||
try {
|
||||
await locationTrackingService.stopTracking();
|
||||
console.log('调试信息 - 位置追踪服务已停止');
|
||||
} catch (trackingError) {
|
||||
console.warn('调试信息 - 停止位置追踪失败:', trackingError);
|
||||
}
|
||||
|
||||
// 停止位置模块的实时跟踪
|
||||
try {
|
||||
if (this.data.mainPageModule) {
|
||||
const locationModule = this.data.mainPageModule.getLocationModule();
|
||||
if (locationModule) {
|
||||
await locationModule.stopRealTimeTracking();
|
||||
console.log('调试信息 - 位置模块实时跟踪已停止');
|
||||
}
|
||||
}
|
||||
} catch (trackingError) {
|
||||
console.warn('调试信息 - 停止位置模块实时跟踪失败:', trackingError);
|
||||
}
|
||||
|
||||
// 保存签退状态到本地存储,防止自动重新登录
|
||||
console.log('调试信息 - 保存签退状态到本地存储');
|
||||
wx.setStorageSync('userStatus', 'signed_out');
|
||||
|
||||
// 验证签退状态是否保存成功
|
||||
const savedStatus = wx.getStorageSync('userStatus');
|
||||
console.log('调试信息 - 验证保存的签退状态:', savedStatus);
|
||||
|
||||
// 更新用户状态为已签退(不调用doGlobalLogout,保持登录状态)
|
||||
this.setData({
|
||||
'authStatus.userStatus': 'signed_out',
|
||||
showSignOutButton: false,
|
||||
showSignInButton: true
|
||||
});
|
||||
|
||||
// 清除所有员工图标
|
||||
if (this.data.mainPageModule) {
|
||||
const employeeModule = this.data.mainPageModule.getEmployeeModule();
|
||||
if (employeeModule) {
|
||||
// 清除员工标记点
|
||||
const { markers } = this.data;
|
||||
const filteredMarkers = markers.filter((marker: any) => marker.type !== 'employee');
|
||||
this.setData({ markers: filteredMarkers });
|
||||
console.log('调试信息 - 已清除所有员工图标');
|
||||
}
|
||||
}
|
||||
|
||||
wx.hideLoading();
|
||||
showToast('签退成功');
|
||||
console.log('调试信息 - 用户签退完成');
|
||||
} catch (error) {
|
||||
wx.hideLoading();
|
||||
console.error('调试信息 - 签退失败:', error);
|
||||
showToast('签退失败');
|
||||
// 刷新页面数据
|
||||
await this.data.mainPageModule.refreshAllData();
|
||||
}
|
||||
},
|
||||
|
||||
@@ -503,6 +432,26 @@ Component<IndexPageComponent>({
|
||||
this.setData({ showUserPanel: true });
|
||||
},
|
||||
|
||||
// 跳转到管理界面(基于用户角色)
|
||||
goToManagementPage() {
|
||||
if (this.data.userInfo && this.data.userInfo.role) {
|
||||
if (this.data.userInfo.role === 'ADMIN') {
|
||||
// 管理员跳转到管理员界面 - 使用navigateTo保持当前页面状态
|
||||
wx.navigateTo({
|
||||
url: '/pages/staff/admin-dashboard'
|
||||
});
|
||||
} else {
|
||||
// 员工跳转到员工界面 - 使用navigateTo保持当前页面状态
|
||||
wx.navigateTo({
|
||||
url: '/pages/staff/employee-dashboard'
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// 未登录用户显示用户面板
|
||||
this.setData({ showUserPanel: true });
|
||||
}
|
||||
},
|
||||
|
||||
// 隐藏用户面板
|
||||
hideUserPanel() {
|
||||
this.setData({ showUserPanel: false });
|
||||
|
||||
Reference in New Issue
Block a user