fastbee/yack-mini/miniprogram/utils/auth.js
2026-09-05 10:19:07 +08:00

116 lines
3.0 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.

/**
* 认证工具 - 获取 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,
};
}
/**
* 从设备配置获取 MQTT 凭证
* @param {Object} device - 设备配置对象
* @returns {Object|null}
*/
function getMqttCredentialsFromDevice(device) {
if (!device || !device.serialNumber || !device.productId || !device.productSecret) {
console.error('[Auth] 设备配置不完整');
return null;
}
return getMqttCredentials(device.serialNumber, device.productId, device.productSecret);
}
/**
* 构建属性上报 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,
getMqttCredentialsFromDevice,
buildPropertyPostTopic,
buildFunctionGetTopic,
buildPropertyGetTopic,
checkLoginStatus,
getCurrentUserId,
};