|
|
@ -1,13 +1,29 @@ |
|
|
// pages/message-list/index.js
|
|
|
// pages/message-list/index.js
|
|
|
|
|
|
const API = require('../../utils/api.js'); |
|
|
Page({ |
|
|
Page({ |
|
|
data: { |
|
|
data: { |
|
|
messageList: [] |
|
|
messageList: [], |
|
|
|
|
|
userInfo: {}, |
|
|
|
|
|
needPhoneAuth: false |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
onLoad: function (options) { |
|
|
onLoad: function (options) { |
|
|
|
|
|
// 检查本地缓存并恢复登录状态
|
|
|
|
|
|
this.checkAndRestoreLoginStatus(); |
|
|
this.loadMessageList(); |
|
|
this.loadMessageList(); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
onShow: function () { |
|
|
|
|
|
// 页面显示时再次检查登录状态
|
|
|
|
|
|
this.checkAndRestoreLoginStatus(); |
|
|
|
|
|
// 更新自定义tabBar状态
|
|
|
|
|
|
if (typeof this.getTabBar === 'function' && this.getTabBar()) { |
|
|
|
|
|
this.getTabBar().setData({ |
|
|
|
|
|
selected: 3 // 假设消息中心是第4个tab(索引3)
|
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
loadMessageList: function () { |
|
|
loadMessageList: function () { |
|
|
// 模拟加载消息列表
|
|
|
// 模拟加载消息列表
|
|
|
const messageList = [ |
|
|
const messageList = [ |
|
|
@ -55,5 +71,154 @@ Page({ |
|
|
onPullDownRefresh: function () { |
|
|
onPullDownRefresh: function () { |
|
|
this.loadMessageList(); |
|
|
this.loadMessageList(); |
|
|
wx.stopPullDownRefresh(); |
|
|
wx.stopPullDownRefresh(); |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
// 检查本地缓存并恢复登录状态
|
|
|
|
|
|
checkAndRestoreLoginStatus() { |
|
|
|
|
|
console.log('开始检查并恢复登录状态') |
|
|
|
|
|
const app = getApp() |
|
|
|
|
|
|
|
|
|
|
|
// 从本地存储获取用户信息
|
|
|
|
|
|
const localUserInfo = wx.getStorageSync('userInfo') || {} |
|
|
|
|
|
const userId = wx.getStorageSync('userId') |
|
|
|
|
|
const openid = wx.getStorageSync('openid') |
|
|
|
|
|
console.log('恢复登录状态 - userId:', userId, 'openid:', openid ? '已获取' : '未获取') |
|
|
|
|
|
|
|
|
|
|
|
// 优先使用全局用户信息,如果没有则使用本地存储的用户信息
|
|
|
|
|
|
if (app.globalData.userInfo) { |
|
|
|
|
|
this.setData({ |
|
|
|
|
|
userInfo: app.globalData.userInfo, |
|
|
|
|
|
needPhoneAuth: !app.globalData.userInfo.phoneNumber |
|
|
|
|
|
}) |
|
|
|
|
|
} else { |
|
|
|
|
|
app.globalData.userInfo = localUserInfo |
|
|
|
|
|
this.setData({ |
|
|
|
|
|
userInfo: localUserInfo, |
|
|
|
|
|
needPhoneAuth: !localUserInfo.phoneNumber |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (userId && openid) { |
|
|
|
|
|
// 确保users存储结构存在
|
|
|
|
|
|
let users = wx.getStorageSync('users') |
|
|
|
|
|
if (!users) { |
|
|
|
|
|
users = {} |
|
|
|
|
|
wx.setStorageSync('users', users) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!users[userId]) { |
|
|
|
|
|
users[userId] = { type: '' } |
|
|
|
|
|
wx.setStorageSync('users', users) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 先显示本地存储的用户类型,但会被服务器返回的最新值覆盖
|
|
|
|
|
|
const user = users[userId] |
|
|
|
|
|
const currentType = user.type |
|
|
|
|
|
this.setData({ userType: currentType }) |
|
|
|
|
|
console.log('恢复登录状态 - 当前本地存储的用户类型:', currentType) |
|
|
|
|
|
|
|
|
|
|
|
// 从服务器获取最新的用户信息,确保身份由数据库决定
|
|
|
|
|
|
this.refreshUserInfoFromServer(openid, userId) |
|
|
|
|
|
} else { |
|
|
|
|
|
console.log('未找到有效的本地登录信息') |
|
|
|
|
|
} |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
// 从服务器刷新用户信息并同步身份数据
|
|
|
|
|
|
refreshUserInfoFromServer(openid, userId) { |
|
|
|
|
|
const API = require('../../utils/api.js') |
|
|
|
|
|
|
|
|
|
|
|
API.getUserInfo(openid).then(res => { |
|
|
|
|
|
console.log('从服务器获取用户信息成功:', res) |
|
|
|
|
|
|
|
|
|
|
|
if (res.success && res.data) { |
|
|
|
|
|
const serverUserInfo = res.data |
|
|
|
|
|
|
|
|
|
|
|
// 更新本地用户信息
|
|
|
|
|
|
const app = getApp() |
|
|
|
|
|
const updatedUserInfo = { |
|
|
|
|
|
...app.globalData.userInfo, |
|
|
|
|
|
...serverUserInfo |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
app.globalData.userInfo = updatedUserInfo |
|
|
|
|
|
wx.setStorageSync('userInfo', updatedUserInfo) |
|
|
|
|
|
this.setData({ userInfo: updatedUserInfo }) |
|
|
|
|
|
|
|
|
|
|
|
// 同步更新用户身份信息(当前身份由数据库决定)
|
|
|
|
|
|
if (serverUserInfo.type) { |
|
|
|
|
|
this.syncUserTypeFromServer(userId, serverUserInfo.type) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
console.log('用户信息已更新,昵称:', updatedUserInfo.name, '手机号:', updatedUserInfo.phoneNumber, '身份:', serverUserInfo.type) |
|
|
|
|
|
} |
|
|
|
|
|
}).catch(err => { |
|
|
|
|
|
console.error('从服务器获取用户信息失败:', err) |
|
|
|
|
|
// 如果getUserInfo失败,尝试使用validateUserLogin作为备选
|
|
|
|
|
|
API.validateUserLogin().then(res => { |
|
|
|
|
|
console.log('使用validateUserLogin获取用户信息成功:', res) |
|
|
|
|
|
|
|
|
|
|
|
if (res.success && res.data) { |
|
|
|
|
|
const serverUserInfo = res.data |
|
|
|
|
|
|
|
|
|
|
|
// 更新本地用户信息
|
|
|
|
|
|
const app = getApp() |
|
|
|
|
|
const updatedUserInfo = { |
|
|
|
|
|
...app.globalData.userInfo, |
|
|
|
|
|
...serverUserInfo |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
app.globalData.userInfo = updatedUserInfo |
|
|
|
|
|
wx.setStorageSync('userInfo', updatedUserInfo) |
|
|
|
|
|
this.setData({ userInfo: updatedUserInfo }) |
|
|
|
|
|
|
|
|
|
|
|
// 同步更新用户身份信息(当前身份由数据库决定)
|
|
|
|
|
|
if (serverUserInfo.type) { |
|
|
|
|
|
this.syncUserTypeFromServer(userId, serverUserInfo.type) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
console.log('用户信息已更新(备选方案):', updatedUserInfo) |
|
|
|
|
|
} |
|
|
|
|
|
}).catch(validateErr => { |
|
|
|
|
|
console.error('从服务器获取用户信息失败(包括备选方案):', validateErr) |
|
|
|
|
|
// 如果服务器请求失败,继续使用本地缓存的信息
|
|
|
|
|
|
}) |
|
|
|
|
|
}) |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
// 从服务器同步用户身份信息
|
|
|
|
|
|
syncUserTypeFromServer(userId, serverType) { |
|
|
|
|
|
if (!userId || !serverType) { |
|
|
|
|
|
console.error('同步用户身份信息失败: 参数不完整') |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
console.log('从服务器同步用户身份信息:', { userId, serverType }) |
|
|
|
|
|
|
|
|
|
|
|
// 更新本地存储的用户身份
|
|
|
|
|
|
let users = wx.getStorageSync('users') || {} |
|
|
|
|
|
if (!users[userId]) { |
|
|
|
|
|
users[userId] = {} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 移除serverType中的customer(如果存在)
|
|
|
|
|
|
let processedServerType = serverType.replace(/,?customer/g, '').replace(/^,|,$/g, '') |
|
|
|
|
|
|
|
|
|
|
|
// 构建新的用户类型
|
|
|
|
|
|
let newUserType = processedServerType |
|
|
|
|
|
|
|
|
|
|
|
// 只有当新构建的用户类型与本地不同时才更新
|
|
|
|
|
|
if (users[userId].type !== newUserType) { |
|
|
|
|
|
users[userId].type = newUserType |
|
|
|
|
|
wx.setStorageSync('users', users) |
|
|
|
|
|
|
|
|
|
|
|
// 更新全局用户类型
|
|
|
|
|
|
const app = getApp() |
|
|
|
|
|
app.globalData.userType = newUserType |
|
|
|
|
|
|
|
|
|
|
|
console.log('用户身份已从服务器同步并保留客服标识:', newUserType) |
|
|
|
|
|
} else { |
|
|
|
|
|
console.log('用户身份与服务器一致,无需更新:', newUserType) |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |