|
|
|
|
App({
|
|
|
|
|
onLaunch: function () {
|
|
|
|
|
// 初始化应用
|
|
|
|
|
console.log('App Launch')
|
|
|
|
|
// 初始化WebSocket管理器
|
|
|
|
|
try {
|
|
|
|
|
const wsManager = require('./utils/websocket');
|
|
|
|
|
this.globalData.webSocketManager = wsManager;
|
|
|
|
|
// 连接WebSocket服务器
|
|
|
|
|
wsManager.connect('ws://localhost:3003', {
|
|
|
|
|
maxReconnectAttempts: 5,
|
|
|
|
|
reconnectInterval: 3000,
|
|
|
|
|
heartbeatTime: 30000
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('初始化WebSocket管理器失败:', e);
|
|
|
|
|
}
|
|
|
|
|
// 初始化本地存储的标签和用户数据
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
// 异步获取客服列表并缓存
|
|
|
|
|
this.fetchAndCacheCustomerServices();
|
|
|
|
|
|
|
|
|
|
// 获取用户信息
|
|
|
|
|
wx.getSetting({
|
|
|
|
|
success: res => {
|
|
|
|
|
if (res.authSetting['scope.userInfo']) {
|
|
|
|
|
// 已经授权,可以直接调用 getUserInfo 获取头像昵称
|
|
|
|
|
wx.getUserInfo({
|
|
|
|
|
success: res => {
|
|
|
|
|
// 调用API获取服务器users表中的真实userId
|
|
|
|
|
const API = require('./utils/api.js');
|
|
|
|
|
API.login().then(serverUserInfo => {
|
|
|
|
|
// 确保获取到服务器返回的真实userId
|
|
|
|
|
if (serverUserInfo && serverUserInfo.data && serverUserInfo.data.userId) {
|
|
|
|
|
const userId = String(serverUserInfo.data.userId);
|
|
|
|
|
console.log('从服务器获取到真实用户ID:', userId);
|
|
|
|
|
|
|
|
|
|
// 存储服务器返回的真实用户ID
|
|
|
|
|
wx.setStorageSync('userId', userId);
|
|
|
|
|
|
|
|
|
|
// 获取用户类型,默认customer
|
|
|
|
|
const userType = serverUserInfo.data.userType || 'customer';
|
|
|
|
|
|
|
|
|
|
// 更新全局用户信息,确保使用服务器返回的ID
|
|
|
|
|
const userInfoWithId = {
|
|
|
|
|
...res.userInfo,
|
|
|
|
|
...serverUserInfo.data.userInfo, // 确保包含服务器返回的所有用户信息
|
|
|
|
|
userId: userId
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.globalData.userInfo = userInfoWithId;
|
|
|
|
|
this.globalData.userType = userType;
|
|
|
|
|
|
|
|
|
|
// 更新本地存储的用户信息
|
|
|
|
|
wx.setStorageSync('userInfo', userInfoWithId);
|
|
|
|
|
wx.setStorageSync('userType', userType);
|
|
|
|
|
|
|
|
|
|
// 更新用户数据
|
|
|
|
|
const users = wx.getStorageSync('users');
|
|
|
|
|
users[userId] = {
|
|
|
|
|
info: userInfoWithId,
|
|
|
|
|
type: userType
|
|
|
|
|
};
|
|
|
|
|
wx.setStorageSync('users', users);
|
|
|
|
|
|
|
|
|
|
// 用户授权登录后重新认证WebSocket连接,使用服务器返回的真实userId
|
|
|
|
|
if (this.globalData.webSocketManager) {
|
|
|
|
|
console.log('用户授权后重新认证WebSocket,使用服务器返回的真实用户ID:', userId);
|
|
|
|
|
this.globalData.webSocketManager.authenticate(userType, userId);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 从本地存储获取备用userId
|
|
|
|
|
const localUserId = wx.getStorageSync('userId');
|
|
|
|
|
if (localUserId) {
|
|
|
|
|
console.log('使用本地存储的备用用户ID:', localUserId);
|
|
|
|
|
const userId = String(localUserId);
|
|
|
|
|
|
|
|
|
|
// 更新全局用户信息,使用本地userId
|
|
|
|
|
const userInfoWithId = {
|
|
|
|
|
...res.userInfo,
|
|
|
|
|
userId: userId
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.globalData.userInfo = userInfoWithId;
|
|
|
|
|
this.globalData.userType = wx.getStorageSync('userType') || 'customer';
|
|
|
|
|
|
|
|
|
|
// 更新本地存储的用户信息
|
|
|
|
|
wx.setStorageSync('userInfo', userInfoWithId);
|
|
|
|
|
|
|
|
|
|
// 更新用户数据
|
|
|
|
|
const users = wx.getStorageSync('users');
|
|
|
|
|
users[userId] = {
|
|
|
|
|
info: userInfoWithId,
|
|
|
|
|
type: this.globalData.userType
|
|
|
|
|
};
|
|
|
|
|
wx.setStorageSync('users', users);
|
|
|
|
|
} else {
|
|
|
|
|
console.error('登录失败:未获取到有效的用户ID');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}).catch(error => {
|
|
|
|
|
console.error('登录API调用失败:', error);
|
|
|
|
|
// 登录失败时提示用户
|
|
|
|
|
wx.showToast({
|
|
|
|
|
title: '登录失败,请重试',
|
|
|
|
|
icon: 'none'
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
fail: error => {
|
|
|
|
|
console.error('获取用户信息失败:', error);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
fail: error => {
|
|
|
|
|
console.error('获取用户设置失败:', error);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
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,
|
|
|
|
|
// 客服列表,从服务器获取并缓存
|
|
|
|
|
customerServiceList: []
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 获取客服列表并存储到globalData和本地存储
|
|
|
|
|
async fetchAndCacheCustomerServices() {
|
|
|
|
|
try {
|
|
|
|
|
console.log('开始获取客服列表...');
|
|
|
|
|
|
|
|
|
|
// 使用wx.request直接获取客服列表
|
|
|
|
|
const res = await new Promise((resolve, reject) => {
|
|
|
|
|
wx.request({
|
|
|
|
|
url: 'http://localhost:3003/api/managers',
|
|
|
|
|
method: 'GET',
|
|
|
|
|
timeout: 15000,
|
|
|
|
|
header: {
|
|
|
|
|
'content-type': 'application/json'
|
|
|
|
|
},
|
|
|
|
|
success: resolve,
|
|
|
|
|
fail: reject
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (res && res.statusCode === 200 && res.data) {
|
|
|
|
|
const dataSource = res.data.data || res.data;
|
|
|
|
|
if (Array.isArray(dataSource)) {
|
|
|
|
|
// 处理客服数据,确保包含必要字段
|
|
|
|
|
const processedData = dataSource.map(item => ({
|
|
|
|
|
id: item.id || `id_${Date.now()}_${Math.random()}`,
|
|
|
|
|
managerId: item.managerId || '',
|
|
|
|
|
name: item.name || '未知',
|
|
|
|
|
alias: item.alias || item.name || '未知',
|
|
|
|
|
phoneNumber: item.phoneNumber || '',
|
|
|
|
|
avatarUrl: item.avatar || item.avatarUrl || '',
|
|
|
|
|
isOnline: !!item.online
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// 更新全局客服列表
|
|
|
|
|
this.globalData.customerServiceList = processedData;
|
|
|
|
|
|
|
|
|
|
// 存储到本地存储
|
|
|
|
|
wx.setStorageSync('cached_customer_services', processedData);
|
|
|
|
|
|
|
|
|
|
console.log('客服列表获取成功,共', processedData.length, '条数据');
|
|
|
|
|
return processedData;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('获取客服列表失败:', error);
|
|
|
|
|
}
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
})
|