// 统一身份验证工具函数 const API = require('./api.js'); /** * 统一的身份验证工具 * 用于在聊天、电话、消息中心等操作前检查用户登录状态 */ const AuthManager = { /** * 检查用户是否已登录 * @returns {boolean} 是否已登录 */ isLoggedIn: function() { const userInfo = wx.getStorageSync('userInfo'); const openid = userInfo && userInfo.openid; return !!openid; }, /** * 获取当前用户ID * @returns {string|null} 用户ID */ getUserId: function() { const userInfo = wx.getStorageSync('userInfo'); return userInfo && userInfo.userId ? String(userInfo.userId) : null; }, /** * 获取当前用户类型 * @returns {string} 用户类型 */ getUserType: function() { return wx.getStorageSync('userType') || ''; }, /** * 判断是否为客服 * @returns {boolean} 是否为客服 */ isCustomerService: function() { const userType = this.getUserType(); return userType.includes('manager') || userType === 'manager'; }, /** * 获取客服managerId * @returns {string|null} managerId */ getManagerId: function() { return wx.getStorageSync('managerId') || null; }, /** * 执行统一的身份验证 * @param {Function} successCallback - 验证成功后的回调 * @param {Function} failCallback - 验证失败后的回调 */ authenticate: function(successCallback, failCallback) { console.log('执行统一身份验证...'); // 检查是否已登录 if (!this.isLoggedIn()) { console.log('用户未登录,执行授权登录'); // 显示登录模态框 wx.showModal({ title: '需要登录', content: '请先授权登录后再继续操作', showCancel: true, cancelText: '取消', confirmText: '去登录', success: (res) => { if (res.confirm) { // 执行登录操作 this.doLogin((loginRes) => { if (loginRes.success) { this.handlePostLogin(successCallback); } else { if (failCallback) { failCallback(new Error('登录失败')); } else { wx.showToast({ title: '登录失败', icon: 'none' }); } } }); } else if (failCallback) { failCallback(new Error('用户取消登录')); } } }); return; } // 已登录,继续后续处理 this.handlePostLogin(successCallback); }, /** * 执行登录操作 * @param {Function} callback - 登录回调 */ doLogin: function(callback) { console.log('执行微信登录...'); API.login() .then(res => { if (callback) { callback(res); } }) .catch(err => { console.error('登录失败:', err); if (callback) { callback({ success: false, error: err.message }); } }); }, /** * 登录后的处理逻辑 * @param {Function} successCallback - 成功回调 */ handlePostLogin: function(successCallback) { const userId = this.getUserId(); const userType = this.getUserType(); console.log('登录后信息:', { userId, userType }); // 如果是客服,确保有managerId if (this.isCustomerService() && !this.getManagerId()) { console.warn('客服身份但缺少managerId,尝试重新获取'); this.syncCustomerServiceInfo(() => { if (successCallback) { successCallback({ userId, userType, managerId: this.getManagerId() }); } }); } else { if (successCallback) { successCallback({ userId, userType, managerId: this.getManagerId() }); } } }, /** * 同步客服信息 * @param {Function} callback - 完成回调 */ syncCustomerServiceInfo: function(callback) { const userInfo = wx.getStorageSync('userInfo'); const phoneNumber = userInfo && userInfo.phoneNumber; if (!phoneNumber) { console.warn('没有手机号,无法同步客服信息'); if (callback) callback(); return; } // 重新获取managerId Promise.all([ API.checkIfUserIsCustomerService(phoneNumber), API.getManagerIdByPhone(phoneNumber) ]).then(([isCustomerService, managerId]) => { if (isCustomerService && managerId) { console.log('同步客服信息成功:', { managerId }); wx.setStorageSync('managerId', managerId); } if (callback) callback(); }).catch(err => { console.error('同步客服信息失败:', err); if (callback) callback(); }); }, /** * 清理登录状态 */ clearLoginStatus: function() { wx.removeStorageSync('userInfo'); wx.removeStorageSync('userType'); wx.removeStorageSync('managerId'); wx.removeStorageSync('phoneNumber'); console.log('登录状态已清理'); } }; module.exports = AuthManager;