| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- /**
- * 用户认证和ID管理工具类
- */
- export default {
- /**
- * 智能获取用户ID(修复版)
- * @returns {number|null} 用户ID或null
- */
- getUserId() {
-
-
- // 首先检查是否真的已登录(通过token和userInfo判断)
- const token = uni.getStorageSync('token')
- const userInfo = uni.getStorageSync('userInfo')
- const storedUserId = uni.getStorageSync('userId')
-
-
-
- // 如果有token和userInfo,说明确实已登录
- if (token && userInfo) {
- // 1. 优先从直接存储的userId获取(包括ID为1的情况)
- if (storedUserId !== null && storedUserId !== undefined && storedUserId !== '') {
- const finalUserId = parseInt(storedUserId)
-
- return finalUserId
- }
-
- // 2. 从userInfo中获取
- const userIdFromInfo = userInfo.userId || userInfo.id || userInfo.user_id
- if (userIdFromInfo !== null && userIdFromInfo !== undefined) {
- const finalUserId = parseInt(userIdFromInfo)
-
- // 同步更新userId存储
- uni.setStorageSync('userId', finalUserId)
- return finalUserId
- }
-
- // 3. 如果有登录信息但无法提取userId,使用默认值1
-
- uni.setStorageSync('userId', 1)
- return 1
- }
-
- // 4. 未登录状态
-
- return null
- },
- /**
- * 检查用户是否已登录
- * @returns {boolean} 是否已登录
- */
- isLoggedIn() {
- const token = uni.getStorageSync('token')
- const userInfo = uni.getStorageSync('userInfo')
- const userId = this.getUserId()
-
- // 修复:允许userId为1的用户(测试用户)也被认为是已登录状态
- return !!(token && userInfo && userId)
- },
- /**
- * 获取用户信息
- * @returns {object|null} 用户信息对象或null
- */
- getUserInfo() {
- return uni.getStorageSync('userInfo') || null
- },
- /**
- * 获取用户token
- * @returns {string|null} token或null
- */
- getToken() {
- return uni.getStorageSync('token') || null
- },
- /**
- * 保存登录信息
- * @param {string} token - 登录token
- * @param {object} userInfo - 用户信息
- */
- saveLoginInfo(token, userInfo) {
-
-
- // 保存token和用户信息
- uni.setStorageSync('token', token)
- uni.setStorageSync('userInfo', userInfo)
-
- // 提取并保存用户ID - 支持多种字段名和数据类型
- const userId = userInfo?.userId || userInfo?.id || userInfo?.user_id
- if (userId !== null && userId !== undefined) {
- // 确保userId是数字类型
- const finalUserId = parseInt(userId)
- uni.setStorageSync('userId', finalUserId)
-
- } else {
- console.warn('⚠️ 用户信息中未找到userId字段')
- console.warn('userInfo keys:', Object.keys(userInfo || {}))
- }
- },
- /**
- * 清除登录信息
- */
- clearLoginInfo() {
- uni.removeStorageSync('token')
- uni.removeStorageSync('userInfo')
- uni.removeStorageSync('userId')
- uni.removeStorageSync('rememberedAccount')
-
- },
- /**
- * 检查并处理登录状态
- * @param {object} options - 配置选项
- * @param {boolean} options.requireLogin - 是否要求必须登录
- * @param {boolean} options.allowTestUser - 是否允许使用测试用户(ID=1)
- * @param {string} options.redirectUrl - 登录后重定向的URL
- * @returns {Promise<number|null>} 返回用户ID或null
- */
- async checkLoginStatus(options = {}) {
- const {
- requireLogin = true,
- allowTestUser = false,
- redirectUrl = ''
- } = options
- const token = this.getToken()
- const userInfo = this.getUserInfo()
- const userId = this.getUserId()
- // 未登录情况
- if (!token || !userInfo) {
- if (requireLogin) {
- return new Promise((resolve) => {
- uni.showModal({
- title: '需要登录',
- content: '请先登录后继续使用',
- showCancel: !requireLogin,
- cancelText: '取消',
- confirmText: '去登录',
- success: (res) => {
- if (res.confirm) {
- const loginUrl = `/pages/page3/page3${redirectUrl ? '?redirect=' + encodeURIComponent(redirectUrl) : ''}`
- uni.navigateTo({ url: loginUrl })
- }
- resolve(null)
- }
- })
- })
- }
- return null
- }
- // 登录信息异常情况
- if (!userId || userId === 1) {
- if (allowTestUser && userId === 1) {
-
- return 1
- }
- return new Promise((resolve) => {
- uni.showModal({
- title: '登录信息异常',
- content: '登录信息不完整,是否重新登录?',
- showCancel: allowTestUser,
- cancelText: '使用测试账号',
- confirmText: '重新登录',
- success: (res) => {
- if (res.confirm) {
- this.clearLoginInfo()
- const loginUrl = `/pages/page3/page3${redirectUrl ? '?redirect=' + encodeURIComponent(redirectUrl) : ''}`
- uni.navigateTo({ url: loginUrl })
- resolve(null)
- } else if (allowTestUser) {
-
- resolve(1)
- } else {
- resolve(null)
- }
- }
- })
- })
- }
- // 登录正常
- return userId
- }
- }
|