user.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // 用户相关工具函数
  2. // 检查是否登录
  3. function isLogin() {
  4. const token = wx.getStorageSync('token');
  5. return !!token;
  6. }
  7. // 获取用户信息
  8. function getUserInfo() {
  9. return wx.getStorageSync('userInfo') || null;
  10. }
  11. // 保存用户信息
  12. function saveUserInfo(userInfo, token) {
  13. wx.setStorageSync('userInfo', userInfo);
  14. wx.setStorageSync('token', token);
  15. }
  16. // 清除用户信息
  17. function clearUserInfo() {
  18. const userInfo = getUserInfo();
  19. // 清除用户信息
  20. wx.removeStorageSync('userInfo');
  21. wx.removeStorageSync('token');
  22. // 清除该用户的聊天会话数据
  23. if (userInfo && userInfo.userId) {
  24. const chatSessionKey = `chat_sessionId_${userInfo.userId}`;
  25. wx.removeStorageSync(chatSessionKey);
  26. console.log('已清除用户聊天会话数据:', chatSessionKey);
  27. }
  28. }
  29. // 检查是否为管理员
  30. function isAdmin() {
  31. const userInfo = getUserInfo();
  32. return userInfo && userInfo.userRole === 1;
  33. }
  34. module.exports = {
  35. isLogin,
  36. getUserInfo,
  37. saveUserInfo,
  38. clearUserInfo,
  39. isAdmin
  40. };