Files
xingque/utils/imAuth/jianquanxian.js
2026-06-29 17:59:23 +08:00

57 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// utils/imAuth/jianquanxian.js
// 原require('./laobanjianquan')
// 改:
const { laobanjianquan } = require('./laobanjianquan.js');
const { dashoujianquan } = require('./dashoujianquan.js');
/**
* IM 长连接权限统一入口
* 按顺序进行:
* 1. 检查本地 token/uid 是否存在
* 2. 若不存在,强制断开已有连接,弹窗提示未登录(强制阅读 2 秒)
* 3. 若存在,根据当前角色调用具体的鉴权模块
*
* @param {Object} app 全局 App 实例
* @returns {Promise<{ allowed: boolean, reason?: string }>}
*/
async function jianquanxian(app, options = {}) {
const silent = options && options.silent === true;
// 1. 检测登录态(用 uid 或 token
const token = wx.getStorageSync('token') || wx.getStorageSync('uid');
if (!token) {
if (!silent) {
await app.disconnectGoEasy();
wx.showToast({
title: '您当前处于未登录状态,消息功能暂不可用',
icon: 'none',
duration: 2000,
mask: true
});
await new Promise(resolve => setTimeout(resolve, 2100));
}
return { allowed: false, reason: '未登录' };
}
// 2. 获取当前选中的角色
const role = app.globalData.currentRole || 'normal';
// 3. 根据角色分发到具体鉴权模块
switch (role) {
case 'normal':
return laobanjianquan(app, { silent });
case 'dashou':
return dashoujianquan(app, { silent });
case 'shangjia':
case 'guanshi':
case 'zuzhang':
case 'kaoheguan':
return { allowed: true };
default:
return { allowed: false, reason: '未知角色' };
}
}
module.exports = { jianquanxian };