You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

143 lines
4.3 KiB

App({
onLaunch: function () {
// 初始化应用
console.log('App Launch')
// 初始化WebSocket管理器
const wsManager = require('./utils/websocket').default;
this.globalData.webSocketManager = wsManager;
// 连接WebSocket服务器
wsManager.connect('ws://localhost:3003', {
maxReconnectAttempts: 5,
reconnectInterval: 3000,
heartbeatTime: 30000
});
// 初始化本地存储的标签和用户数据
if (!wx.getStorageSync('users')) {
wx.setStorageSync('users', {})
}
if (!wx.getStorageSync('tags')) {
wx.setStorageSync('tags', {})
}
if (!wx.getStorageSync('goods')) {
// 初始化空的商品列表,不预置默认数据,由服务器获取
wx.setStorageSync('goods', [])
}
if (!wx.getStorageSync('supplies')) {
// 初始化空的供应列表,不预置默认数据,由服务器获取
wx.setStorageSync('supplies', [])
}
// 检查是否是首次启动
const isFirstLaunch = !wx.getStorageSync('hasLaunched')
if (isFirstLaunch) {
// 标记应用已经启动过
wx.setStorageSync('hasLaunched', true)
// 只有在首次启动时才检查用户身份并可能跳转
const userId = wx.getStorageSync('userId')
if (userId) {
const users = wx.getStorageSync('users')
const user = users[userId]
if (user && user.type) {
// 延迟跳转,确保页面加载完成
setTimeout(() => {
try {
if (user.type === 'buyer') {
wx.switchTab({ url: '/pages/buyer/index' })
} else if (user.type === 'seller') {
wx.switchTab({ url: '/pages/seller/index' })
}
} catch (e) {
console.error('启动时页面跳转异常:', e)
// 即使跳转失败,也不影响应用正常启动
}
}, 100)
}
}
}
// 获取本地存储的用户信息和用户类型
const storedUserInfo = wx.getStorageSync('userInfo');
const storedUserType = wx.getStorageSync('userType');
if (storedUserInfo) {
this.globalData.userInfo = storedUserInfo;
}
if (storedUserType) {
this.globalData.userType = storedUserType;
}
console.log('App初始化 - 用户类型:', this.globalData.userType);
console.log('App初始化 - 用户信息:', this.globalData.userInfo);
// 获取用户信息
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称
wx.getUserInfo({
success: res => {
this.globalData.userInfo = res.userInfo
// 存储用户ID(实际项目中使用openid)
if (!wx.getStorageSync('userId')) {
const userId = 'user_' + Date.now()
wx.setStorageSync('userId', userId)
// 初始化用户数据
const users = wx.getStorageSync('users')
users[userId] = {
info: res.userInfo,
type: null
}
wx.setStorageSync('users', users)
}
}
})
}
}
})
},
onShow: function () {
console.log('App Show')
},
onHide: function () {
console.log('App Hide')
},
// 更新当前选中的tab
updateCurrentTab(tabKey) {
if (this.globalData) {
this.globalData.currentTab = tabKey
}
},
// 跳转到估价页面
goToEvaluatePage() {
wx.navigateTo({
url: '/pages/evaluate/index'
})
},
// 上传手机号数据
async uploadPhoneNumberData(phoneData) {
const API = require('./utils/api.js')
return await API.uploadPhoneNumberData(phoneData)
},
globalData: {
userInfo: null,
userType: 'customer', // 默认客户类型
currentTab: 'index', // 当前选中的tab
showTabBar: true, // 控制底部tab-bar显示状态
onNewMessage: null, // 全局新消息处理回调函数
isConnected: false,
unreadMessages: 0,
// 测试环境配置
isTestMode: false,
// 全局WebSocket连接状态
wsConnectionState: 'disconnected', // disconnected, connecting, connected, error
// 客服相关状态
isServiceOnline: false,
onlineServiceCount: 0
}
})