123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- import CryptoJS from "crypto-js";
- export function genReqSecretPkg() {
- const requet = packetData("REQUEST_SECRET_CMD", 0);
- return requet;
- }
- export function genOpDoorPkg() {
- var sessionKey = uni.getStorageSync("sessionKey");
- sessionKey = hexStringToUint8Array(sessionKey);
- const requet = packetData("OPEN_DOOR_CMD", 0, sessionKey);
- return requet;
- }
- export function genQuPowerPkg() {
- var sessionKey = uni.getStorageSync("sessionKey");
- sessionKey = hexStringToUint8Array(sessionKey);
- const requet = packetData("QUERY_POWER_CMD", 3, sessionKey);
- return requet;
- }
- export function gnQuDoorLogPkg() {
- var sessionKey = uni.getStorageSync("sessionKey");
- sessionKey = hexStringToUint8Array(sessionKey);
- const requet = packetData("QUERY_DOOR_LOG_CMD", 2, sessionKey);
- return requet;
- }
- //需要根据命令字段进行不同的包封装
- function packetData(command, id, sessionKey) {
- var cmd;
- var data;
- if (command == "REQUEST_SECRET_CMD") {
- cmd = 0x01;
- data = new Uint8Array();
- }
- if (command == "OPEN_DOOR_CMD") {
- cmd = 0x11;
- data = sessionKey;
- }
- if (command == "QUERY_POWER_CMD") {
- cmd = 0x04;
- data = sessionKey;
- }
- if (command == "QUERY_DOOR_LOG_CMD") {
- cmd = 0x12;
- data = sessionKey;
- }
- var packet = new Uint8Array(6 + data.length);
- // 设置开始码
- packet[0] = 0xa5;
- // 设置命令号
- packet[1] = cmd;
- // 设置包序号
- packet[2] = id;
- // 设置实际数据
- for (var j = 0; j < data.length; j++) {
- packet[6 + j] = data[j];
- }
- // 计算负载的校验和
- var chksum = 0;
- for (var i = 6; i < packet.length; i++) {
- chksum ^= packet[i];
- }
- packet[3] = chksum;
- // 设置负载长度的低位和高位
- var dataLength = data.length;
- packet[4] = dataLength & 0xff; // 低位
- packet[5] = (dataLength >> 8) & 0xff; // 高位
- return packet.buffer;
- }
- //分包延迟定时器
- export function Delayed(ms, res) {
- return new Promise(function (Resolve, Reject) {
- setTimeout(function () {
- Resolve(res);
- }, ms);
- });
- }
- //AES解密
- export function decryptAes(originalKey, encryptedData) {
- let key = hexStringToUint8Array(originalKey);
- let data = encryptedData;
- try {
- // 将密文和密钥转换为 WordArray 对象
- const ciphertext = CryptoJS.lib.WordArray.create(data);
- const keyHex = CryptoJS.lib.WordArray.create(key);
- // 进行解密
- const decrypted = CryptoJS.AES.decrypt({ ciphertext }, keyHex, {
- mode: CryptoJS.mode.ECB,
- padding: CryptoJS.pad.NoPadding,
- });
- const decryptedUint8Array = new Uint8Array(decrypted.words.length * 4);
- for (let i = 0; i < decrypted.words.length; i++) {
- decryptedUint8Array[i * 4] = (decrypted.words[i] >> 24) & 0xff;
- decryptedUint8Array[i * 4 + 1] = (decrypted.words[i] >> 16) & 0xff;
- decryptedUint8Array[i * 4 + 2] = (decrypted.words[i] >> 8) & 0xff;
- decryptedUint8Array[i * 4 + 3] = decrypted.words[i] & 0xff;
- }
- // 返回解密后的数据
- return decryptedUint8Array;
- } catch (e) {
- console.error("解密异常", e);
- return null;
- }
- }
- //字符串转Uint8
- export function hexStringToUint8Array(hexString) {
- // 去除字符串中的空格
- hexString = hexString.replace(/\s/g, "");
- // 使用正则表达式将字符串拆分成两个字符一组的数组
- const match = hexString.match(/.{1,2}/g);
- // 将每两个字符转换为一个字节的十六进制数
- const uint8Array = new Uint8Array(match.map((byte) => parseInt(byte, 16)));
- return uint8Array;
- }
- // 将后端传过来的蓝牙设备id转化为符合接口要求的id
- export function parseBELDeviceId(deviceId = "") {
- const arrayOfDeviceId = deviceId.toUpperCase().split("");
- let targetId = arrayOfDeviceId[0] + arrayOfDeviceId[1];
- for (let i = 2; i < arrayOfDeviceId.length; i += 2) {
- targetId = targetId + ":" + arrayOfDeviceId[i] + arrayOfDeviceId[i + 1];
- }
- return targetId;
- }
|