115 lines
3.1 KiB
JavaScript
115 lines
3.1 KiB
JavaScript
/**
|
||
* 认证工具 - 获取 MQTT 凭证
|
||
*/
|
||
const storage = require('./storage.js');
|
||
|
||
/**
|
||
* 生成 MQTT clientId
|
||
* 格式:S&{设备编号}&{产品ID}&{用户ID}
|
||
* @param {string} serialNumber - 设备编号
|
||
* @param {number|string} productId - 产品ID
|
||
* @param {number|string} userId - 用户ID
|
||
* @returns {string}
|
||
*/
|
||
function generateClientId(serialNumber, productId, userId) {
|
||
return `S&${serialNumber}&${productId}&${userId}`;
|
||
}
|
||
|
||
/**
|
||
* 获取 MQTT 连接凭证
|
||
* @param {string} serialNumber - 设备编号
|
||
* @param {number|string} productId - 产品ID
|
||
* @param {string} productSecret - 产品密钥
|
||
* @returns {Object|null} { clientId, username, password }
|
||
*/
|
||
function getMqttCredentials(serialNumber, productId, productSecret) {
|
||
const userInfo = storage.getUserInfo();
|
||
if (!userInfo || !userInfo.userId) {
|
||
console.error('[Auth] 未登录,无法获取 MQTT 凭证');
|
||
return null;
|
||
}
|
||
|
||
const userId = userInfo.userId;
|
||
const clientId = generateClientId(serialNumber, productId, userId);
|
||
|
||
return {
|
||
clientId: clientId,
|
||
username: serialNumber,
|
||
password: productSecret,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 构建测点数据上报 Topic(蓝牙网关测点链路)
|
||
* 格式:/{productId}/{serialNumber}/{measurePointId}/measure/post
|
||
* @param {number|string} productId - 产品ID
|
||
* @param {string} serialNumber - 设备编号
|
||
* @param {string} measurePointId - 测点唯一识别号
|
||
* @returns {string}
|
||
*/
|
||
function buildMeasurePostTopic(productId, serialNumber, measurePointId) {
|
||
return `/${productId}/${serialNumber}/${measurePointId}/measure/post`;
|
||
}
|
||
|
||
/**
|
||
* 构建属性上报 Topic
|
||
* 格式:/{productId}/{serialNumber}/property/post
|
||
* @param {number|string} productId - 产品ID
|
||
* @param {string} serialNumber - 设备编号
|
||
* @returns {string}
|
||
*/
|
||
function buildPropertyPostTopic(productId, serialNumber) {
|
||
return `/${productId}/${serialNumber}/property/post`;
|
||
}
|
||
|
||
/**
|
||
* 构建功能下发 Topic
|
||
* 格式:/{productId}/{serialNumber}/function/get
|
||
* @param {number|string} productId - 产品ID
|
||
* @param {string} serialNumber - 设备编号
|
||
* @returns {string}
|
||
*/
|
||
function buildFunctionGetTopic(productId, serialNumber) {
|
||
return `/${productId}/${serialNumber}/function/get`;
|
||
}
|
||
|
||
/**
|
||
* 构建属性读取 Topic
|
||
* 格式:/{productId}/{serialNumber}/property/get
|
||
* @param {number|string} productId - 产品ID
|
||
* @param {string} serialNumber - 设备编号
|
||
* @returns {string}
|
||
*/
|
||
function buildPropertyGetTopic(productId, serialNumber) {
|
||
return `/${productId}/${serialNumber}/property/get`;
|
||
}
|
||
|
||
/**
|
||
* 检查登录状态
|
||
* @returns {boolean}
|
||
*/
|
||
function checkLoginStatus() {
|
||
const token = storage.getToken();
|
||
const userInfo = storage.getUserInfo();
|
||
return !!(token && userInfo && userInfo.userId);
|
||
}
|
||
|
||
/**
|
||
* 获取当前用户ID
|
||
* @returns {number|null}
|
||
*/
|
||
function getCurrentUserId() {
|
||
const userInfo = storage.getUserInfo();
|
||
return userInfo ? userInfo.userId : null;
|
||
}
|
||
|
||
module.exports = {
|
||
generateClientId,
|
||
getMqttCredentials,
|
||
buildMeasurePostTopic,
|
||
buildPropertyPostTopic,
|
||
buildFunctionGetTopic,
|
||
buildPropertyGetTopic,
|
||
checkLoginStatus,
|
||
getCurrentUserId,
|
||
}; |