604 lines
18 KiB
TypeScript
604 lines
18 KiB
TypeScript
// 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;
|
||
};
|
||
}
|
||
|
||
// 主页面组件
|
||
Component<IndexPageComponent>({
|
||
data: {
|
||
mainPageModule: null as MainPageModule | null,
|
||
// 核心UI状态
|
||
showUserPanel: false,
|
||
showOrderPanel: false,
|
||
showDeliveryPersonModal: false,
|
||
showWarehouseModal: false
|
||
},
|
||
|
||
lifetimes: {
|
||
attached() {
|
||
console.log('index page attached');
|
||
this.initPage();
|
||
},
|
||
|
||
detached() {
|
||
console.log('index page detached');
|
||
// 清理资源
|
||
if (this.data.mainPageModule) {
|
||
this.data.mainPageModule.cleanup();
|
||
}
|
||
},
|
||
|
||
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();
|
||
}
|
||
}
|
||
},
|
||
|
||
methods: {
|
||
|
||
|
||
|
||
// 处理货运人员底部弹窗关闭
|
||
onDeliveryPersonModalClose() {
|
||
this.setData({
|
||
showDeliveryPersonModal: false
|
||
});
|
||
},
|
||
|
||
// 处理仓库底部弹窗关闭
|
||
onWarehouseModalClose() {
|
||
this.setData({
|
||
showWarehouseModal: false
|
||
});
|
||
},
|
||
|
||
// 初始化页面
|
||
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) {
|
||
console.error('mainPageModule未初始化');
|
||
return;
|
||
}
|
||
const loginModule = this.data.mainPageModule.getLoginModule();
|
||
// 显示加载状态
|
||
wx.showLoading({
|
||
title: '检查登录状态...',
|
||
mask: true
|
||
});
|
||
|
||
// 异步检查登录状态
|
||
const isLoggedIn = await this.asyncCheckLoginStatus(loginModule);
|
||
|
||
if (isLoggedIn) {
|
||
// 已登录状态 - 按钮状态会在refreshPageAfterLogin中统一更新
|
||
console.log('✅ 登录检查成功,等待统一页面刷新');
|
||
} else {
|
||
// 未登录状态
|
||
this.setData({
|
||
userInfo: null,
|
||
'authStatus.hasWxCode': false,
|
||
'authStatus.userStatus': 'unknown',
|
||
// 初始化按钮显示状态
|
||
showSignInButton: false,
|
||
showRegisterButton: false,
|
||
showAuthButton: true
|
||
});
|
||
}
|
||
} catch (error) {
|
||
console.error('检查登录状态失败:', error);
|
||
// 出错时设置为未登录状态
|
||
this.setData({
|
||
userInfo: null,
|
||
'authStatus.hasWxCode': false,
|
||
'authStatus.userStatus': 'unknown',
|
||
showSignInButton: false,
|
||
showRegisterButton: false,
|
||
showAuthButton:true
|
||
});
|
||
|
||
console.log('❌ 登录检查失败,重置为未登录状态');
|
||
console.log(' - authStatus.hasWxCode 设置为 false');
|
||
console.log(' - showRegisterButton 设置为 false');
|
||
} finally {
|
||
wx.hideLoading();
|
||
}
|
||
},
|
||
|
||
// 异步检查登录状态
|
||
async asyncCheckLoginStatus(loginModule: any): Promise<boolean> {
|
||
const app = getApp<any>();
|
||
|
||
// 首先检查本地是否有登录状态
|
||
if (app.globalData.isLoggedIn && app.globalData.userInfo) {
|
||
// 本地已有登录状态,此时可以安全获取用户状态(因为有token)
|
||
const userStatus = await loginModule.determineUserStatus(app.globalData.userInfo);
|
||
|
||
// 更新页面状态
|
||
this.setData({
|
||
userInfo: app.globalData.userInfo,
|
||
'authStatus.hasWxCode': true,
|
||
'authStatus.userStatus': userStatus
|
||
});
|
||
|
||
console.log('✅ 使用本地登录状态,用户状态已更新:', userStatus);
|
||
|
||
// 登录成功后统一刷新页面
|
||
this.refreshPageAfterLogin();
|
||
|
||
return true;
|
||
}
|
||
|
||
// 如果本地没有登录状态,尝试执行静默登录
|
||
try {
|
||
const loginResult = await userService.wxLogin();
|
||
if (loginResult.success && loginResult.userInfo) {
|
||
// 登录成功,更新全局用户信息
|
||
app.globalData.userInfo = loginResult.userInfo;
|
||
app.globalData.isLoggedIn = true;
|
||
|
||
// 登录成功后,此时已经有token,可以安全获取用户状态
|
||
const userStatus = await loginModule.determineUserStatus(loginResult.userInfo);
|
||
|
||
// 更新页面状态
|
||
this.setData({
|
||
userInfo: loginResult.userInfo,
|
||
'authStatus.hasWxCode': true,
|
||
'authStatus.userStatus': userStatus
|
||
});
|
||
|
||
console.log('✅ 静默登录成功,用户状态已更新:', userStatus);
|
||
|
||
// === 全局登录流程完成,登录成功 ===
|
||
// 统一在此处执行一次完整的页面刷新
|
||
this.refreshPageAfterLogin();
|
||
|
||
return true;
|
||
}
|
||
} catch (error) {
|
||
console.warn('静默登录失败:', error);
|
||
}
|
||
|
||
return false;
|
||
},
|
||
|
||
|
||
|
||
// 更新按钮显示状态
|
||
updateButtonDisplayStatus() {
|
||
if (!this.data.mainPageModule) {
|
||
console.error('mainPageModule未初始化');
|
||
return;
|
||
}
|
||
|
||
const loginModule = this.data.mainPageModule.getLoginModule();
|
||
const showSignInButton = loginModule.shouldShowSignInButton();
|
||
const showRegisterButton = loginModule.shouldShowRegisterButton();
|
||
|
||
// 签退按钮显示逻辑:已签到用户显示签退按钮
|
||
const showSignOutButton = this.data.authStatus && this.data.authStatus.userStatus === 'signed_in';
|
||
|
||
console.log('🔄 更新按钮显示状态:');
|
||
console.log(' - showSignInButton:', showSignInButton);
|
||
console.log(' - showRegisterButton:', showRegisterButton);
|
||
console.log(' - showSignOutButton:', showSignOutButton);
|
||
console.log(' - 当前用户状态:', this.data.authStatus ? this.data.authStatus.userStatus : 'undefined');
|
||
console.log(' - 当前hasWxCode:', this.data.authStatus ? this.data.authStatus.hasWxCode : 'undefined');
|
||
|
||
this.setData({
|
||
showSignInButton,
|
||
showRegisterButton,
|
||
showSignOutButton
|
||
});
|
||
|
||
console.log('✅ 按钮状态已更新到页面');
|
||
},
|
||
|
||
// 登录成功后统一刷新页面
|
||
async refreshPageAfterLogin() {
|
||
if (!this.data.mainPageModule) {
|
||
console.error('mainPageModule未初始化');
|
||
return;
|
||
}
|
||
|
||
const loginModule = this.data.mainPageModule.getLoginModule();
|
||
const app = getApp<any>();
|
||
|
||
console.log('🔄 === 全局登录流程完成,执行统一页面刷新 ===');
|
||
|
||
// 1. 更新页面登录状态
|
||
loginModule.updatePageAfterLogin(app.globalData.userInfo);
|
||
|
||
// 2. 更新按钮显示状态
|
||
this.updateButtonDisplayStatus();
|
||
|
||
// 3. 初始化主页面模块
|
||
await this.data.mainPageModule.onLoad();
|
||
|
||
// 4. 登录成功后只加载公开数据(仓库等),不加载业务数据
|
||
// 业务数据(员工位置等)只有在用户签到后才应该加载
|
||
if (app.globalData.isLoggedIn) {
|
||
console.log('🔍 登录成功,开始加载公开数据...');
|
||
await this.data.mainPageModule.loadPublicData();
|
||
console.log('✅ 公开数据加载完成');
|
||
}
|
||
|
||
console.log('✅ 统一页面刷新完成');
|
||
},
|
||
|
||
// 处理仓库底部弹窗状态变化
|
||
onWarehouseModalStateChange(e: any) {
|
||
const state = e.detail.state;
|
||
this.setData({ warehouseModalState: state });
|
||
},
|
||
|
||
// 处理货运人员底部弹窗状态变化
|
||
onDeliveryPersonModalStateChange(e: any) {
|
||
const state = e.detail.state;
|
||
this.setData({ deliveryPersonModalState: state });
|
||
},
|
||
|
||
|
||
|
||
// 处理签到 - 已迁移到LoginModule
|
||
async handleSignIn() {
|
||
if (!this.data.mainPageModule) {
|
||
console.error('mainPageModule未初始化');
|
||
return;
|
||
}
|
||
|
||
const loginModule = this.data.mainPageModule.getLoginModule();
|
||
const success = await loginModule.handleSignIn();
|
||
|
||
if (success) {
|
||
// 更新按钮显示状态
|
||
this.updateButtonDisplayStatus();
|
||
|
||
// 刷新页面数据
|
||
await this.data.mainPageModule.refreshAllData();
|
||
}
|
||
},
|
||
|
||
// 处理授权登录 - 已迁移到LoginModule
|
||
async handleAuthLogin() {
|
||
if (!this.data.mainPageModule) {
|
||
console.error('mainPageModule未初始化');
|
||
return;
|
||
}
|
||
|
||
const loginModule = this.data.mainPageModule.getLoginModule();
|
||
const success = await loginModule.handleAuthLogin();
|
||
|
||
if (success) {
|
||
const app = getApp<any>();
|
||
const userStatus = loginModule.determineUserStatus(app.globalData.userInfo);
|
||
this.setData({
|
||
userInfo: app.globalData.userInfo,
|
||
'authStatus.hasWxCode': true,
|
||
'authStatus.userStatus': userStatus
|
||
});
|
||
|
||
console.log('✅ 手动登录成功,authStatus.hasWxCode 设置为 true');
|
||
console.log('✅ 用户状态已更新:', userStatus);
|
||
|
||
// === 全局登录流程完成,登录成功 ===
|
||
// 统一在此处执行一次完整的页面刷新
|
||
this.refreshPageAfterLogin();
|
||
}
|
||
},
|
||
|
||
// 处理退出登录
|
||
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
|
||
});
|
||
|
||
// 更新按钮显示状态
|
||
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('签退失败');
|
||
}
|
||
},
|
||
|
||
// 显示申请加入货运人员表单 - 跳转到独立页面
|
||
showApplyForm() {
|
||
wx.navigateTo({
|
||
url: '/pages/apply/apply'
|
||
});
|
||
},
|
||
|
||
// 跳转到管理员页面
|
||
goToAdminPage() {
|
||
wx.navigateTo({
|
||
url: '/pages/admin/admin'
|
||
});
|
||
},
|
||
|
||
// 跳转到员工管理页面
|
||
goToEmployeeManagement() {
|
||
wx.navigateTo({
|
||
url: '/pages/employee/employee'
|
||
});
|
||
},
|
||
|
||
// 阻止事件冒泡
|
||
stopPropagation(e: any) {
|
||
if (e && typeof e.stopPropagation === 'function') {
|
||
e.stopPropagation();
|
||
}
|
||
},
|
||
|
||
|
||
|
||
// 获取性别文本表示
|
||
getGenderText(gender: number): string {
|
||
if (gender === 1) return '男';
|
||
if (gender === 2) return '女';
|
||
return '未知';
|
||
},
|
||
|
||
// 格式化坐标对
|
||
formatCoordinatePair(longitude: number, latitude: number): string {
|
||
return `${longitude.toFixed(6)}, ${latitude.toFixed(6)}`;
|
||
},
|
||
|
||
// 用户登出
|
||
userLogout() {
|
||
if (this.data.mainPageModule) {
|
||
const loginModule = this.data.mainPageModule.getLoginModule();
|
||
loginModule.logout();
|
||
|
||
// 更新页面状态
|
||
this.setData({
|
||
isLoggedIn: false,
|
||
userInfo: null
|
||
});
|
||
|
||
// 更新按钮显示状态
|
||
this.updateButtonDisplayStatus();
|
||
}
|
||
},
|
||
|
||
// 显示用户面板
|
||
showUserPanel() {
|
||
this.setData({ showUserPanel: true });
|
||
},
|
||
|
||
// 隐藏用户面板
|
||
hideUserPanel() {
|
||
this.setData({ showUserPanel: false });
|
||
},
|
||
|
||
// 隐藏所有面板
|
||
hideAllPanels() {
|
||
if (this.data.mainPageModule) {
|
||
this.data.mainPageModule.hideAllPanels();
|
||
}
|
||
},
|
||
|
||
// 重置标记点状态
|
||
resetMarkers() {
|
||
if (this.data.mainPageModule) {
|
||
this.data.mainPageModule.resetMarkers();
|
||
}
|
||
},
|
||
|
||
// 地图点击事件
|
||
onMapTap(e: any) {
|
||
if (this.data.mainPageModule) {
|
||
this.data.mainPageModule.onMapTap(e);
|
||
}
|
||
},
|
||
|
||
// 标记点点击事件
|
||
onMarkerTap(e: any) {
|
||
if (this.data.mainPageModule) {
|
||
this.data.mainPageModule.onMarkerTap(e);
|
||
}
|
||
},
|
||
|
||
// 分配订单
|
||
async assignOrder(orderId: number, deliveryPersonId: number) {
|
||
if (this.data.mainPageModule) {
|
||
const orderModule = this.data.mainPageModule.getOrderModule();
|
||
await orderModule.assignOrder(orderId, deliveryPersonId);
|
||
}
|
||
},
|
||
|
||
// 更新订单状态
|
||
async updateOrderStatus(orderId: number, status: 'pending' | 'assigned' | 'in_transit' | 'delivered') {
|
||
if (this.data.mainPageModule) {
|
||
const orderModule = this.data.mainPageModule.getOrderModule();
|
||
await orderModule.updateOrderStatus(orderId, status);
|
||
}
|
||
},
|
||
|
||
// 展开仓库面板
|
||
expandWarehousePanel() {
|
||
if (this.data.mainPageModule) {
|
||
const warehouseModule = this.data.mainPageModule.getWarehouseModule();
|
||
warehouseModule.expandWarehousePanel();
|
||
}
|
||
},
|
||
|
||
// 收起仓库面板
|
||
collapseWarehousePanel() {
|
||
if (this.data.mainPageModule) {
|
||
const warehouseModule = this.data.mainPageModule.getWarehouseModule();
|
||
warehouseModule.collapseWarehousePanel();
|
||
}
|
||
},
|
||
|
||
// 展开员工面板
|
||
expandDeliveryPersonPanel() {
|
||
if (this.data.mainPageModule) {
|
||
const employeeModule = this.data.mainPageModule.getEmployeeModule();
|
||
employeeModule.expandDeliveryPersonPanel();
|
||
}
|
||
},
|
||
|
||
// 收起员工面板
|
||
collapseDeliveryPersonPanel() {
|
||
if (this.data.mainPageModule) {
|
||
const employeeModule = this.data.mainPageModule.getEmployeeModule();
|
||
employeeModule.collapseDeliveryPersonPanel();
|
||
}
|
||
},
|
||
|
||
// 刷新所有数据
|
||
async refreshAllData() {
|
||
if (this.data.mainPageModule) {
|
||
await this.data.mainPageModule.refreshAllData();
|
||
}
|
||
},
|
||
|
||
// 开始定位(处理地图控制按钮点击)
|
||
async startLocation() {
|
||
if (this.data.mainPageModule) {
|
||
const mapModule = this.data.mainPageModule.getMapModule();
|
||
await mapModule.startLocation();
|
||
}
|
||
},
|
||
|
||
|
||
}
|
||
}); |