userStatusCheck.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * 用户状态检查工具类
  3. * 用于检查用户是否被禁用,并在被禁用时显示提示
  4. * 支持实时刷新状态,当后台修改用户状态后能及时响应
  5. */
  6. const BASE_URL = 'https://api.zhongruanke.cn/api'
  7. export default {
  8. /**
  9. * 检查用户是否被禁用(每次操作都实时请求后端)
  10. * @param {boolean} useCache - 是否使用缓存,默认false(实时请求)
  11. * @returns {Promise<boolean>} true表示用户正常,false表示用户被禁用
  12. */
  13. async checkUserStatus(useCache = false) {
  14. try {
  15. const userId = uni.getStorageSync('userId')
  16. if (!userId) {
  17. return true // 未登录用户不检查
  18. }
  19. // 如果允许使用缓存,检查缓存(缓存有效期30秒,用于短时间内连续操作)
  20. if (useCache) {
  21. const cachedStatus = uni.getStorageSync('userStatus')
  22. const cacheTime = uni.getStorageSync('userStatusCacheTime')
  23. const now = Date.now()
  24. // 缓存有效期30秒(短时间内连续操作不重复请求)
  25. if (cachedStatus !== undefined && cacheTime && (now - cacheTime) < 30 * 1000) {
  26. return cachedStatus !== 0
  27. }
  28. }
  29. // 调用后端接口获取用户状态(实时请求)
  30. const [error, res] = await uni.request({
  31. url: `${BASE_URL}/user/status/${userId}`,
  32. method: 'GET',
  33. timeout: 5000
  34. })
  35. if (error) {
  36. console.error('检查用户状态失败:', error)
  37. return true // 网络错误时默认允许操作
  38. }
  39. if (res.statusCode === 200 && res.data && res.data.code === 200) {
  40. const status = res.data.data?.status
  41. const now = Date.now()
  42. // 更新缓存
  43. uni.setStorageSync('userStatus', status)
  44. uni.setStorageSync('userStatusCacheTime', now)
  45. if (status === 0) {
  46. return false // 用户被禁用
  47. }
  48. }
  49. return true
  50. } catch (e) {
  51. console.error('检查用户状态异常:', e)
  52. return true // 异常时默认允许操作
  53. }
  54. },
  55. /**
  56. * 显示用户被禁用的提示
  57. */
  58. showDisabledTip() {
  59. uni.showModal({
  60. title: '账号已被禁用',
  61. content: '您的当前账号违反规定已被禁用,如有疑问请联系客服',
  62. showCancel: false,
  63. confirmText: '我知道了'
  64. })
  65. },
  66. /**
  67. * 检查用户状态,如果被禁用则显示提示并返回false
  68. * 每次调用都会实时请求后端,确保状态最新
  69. * @returns {Promise<boolean>} true表示可以继续操作,false表示被禁用
  70. */
  71. async checkAndTip() {
  72. // 实时请求后端,不使用缓存,确保状态最新
  73. const isNormal = await this.checkUserStatus(false)
  74. if (!isNormal) {
  75. this.showDisabledTip()
  76. return false
  77. }
  78. return true
  79. },
  80. /**
  81. * 带缓存的状态检查(用于短时间内连续操作,减少请求)
  82. * 缓存有效期30秒
  83. * @returns {Promise<boolean>} true表示可以继续操作,false表示被禁用
  84. */
  85. async checkAndTipWithCache() {
  86. const isNormal = await this.checkUserStatus(true)
  87. if (!isNormal) {
  88. this.showDisabledTip()
  89. return false
  90. }
  91. return true
  92. },
  93. /**
  94. * 清除用户状态缓存(用于登录/登出时)
  95. */
  96. clearStatusCache() {
  97. uni.removeStorageSync('userStatus')
  98. uni.removeStorageSync('userStatusCacheTime')
  99. },
  100. /**
  101. * 强制刷新用户状态(绕过缓存)
  102. * @returns {Promise<boolean>} true表示用户正常,false表示用户被禁用
  103. */
  104. async refreshUserStatus() {
  105. this.clearStatusCache()
  106. return await this.checkUserStatus(false)
  107. }
  108. }