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.

248 lines
7.8 KiB

// pages/message-list/index.js
const API = require('../../utils/api.js');
Page({
// 分享给朋友/群聊
onShareAppMessage() {
return {
title: '鸡蛋贸易平台 - 消息通知,及时沟通',
path: '/pages/message-list/index',
imageUrl: '/images/你有好蛋.png'
}
},
// 分享到朋友圈
onShareTimeline() {
return {
title: '鸡蛋贸易平台 - 消息通知,及时沟通',
query: '',
imageUrl: '/images/你有好蛋.png'
}
},
data: {
messageList: [],
userInfo: {},
needPhoneAuth: false
},
onLoad: function (options) {
// 检查本地缓存并恢复登录状态
this.checkAndRestoreLoginStatus();
this.loadMessageList();
},
onShow: function () {
// 页面显示时再次检查登录状态
this.checkAndRestoreLoginStatus();
// 更新自定义tabBar状态
if (typeof this.getTabBar === 'function' && this.getTabBar()) {
this.getTabBar().setData({
selected: 3 // 假设消息中心是第4个tab(索引3)
});
}
},
loadMessageList: function () {
// 模拟加载消息列表
const messageList = [
{
id: 1,
type: 'system',
name: '系统通知',
avatar: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
content: '您有一条新的系统通知',
time: '刚刚',
unread: 1
},
{
id: 2,
type: 'service',
name: '客服中心',
avatar: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
content: '您好,请问有什么可以帮助您的?',
time: '5分钟前',
unread: 0
},
{
id: 3,
type: 'order',
name: '订单通知',
avatar: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
content: '您的订单已发货',
time: '1小时前',
unread: 0
}
];
this.setData({
messageList: messageList
});
},
onMessageTap: function (e) {
// 跳转到消息详情页
wx.navigateTo({
url: '/pages/chat-detail/index?id=' + e.currentTarget.dataset.id
});
},
onPullDownRefresh: function () {
this.loadMessageList();
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] = {}
}
// 如果当前类型是 Colleague,直接返回,不允许从服务器同步覆盖
if (users[userId].type === 'Colleague') {
console.log('当前用户是 Colleague 类型,不允许从服务器同步覆盖用户类型')
return
}
// 移除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)
}
}
});