fix: 星云SDK支持纯web
This commit is contained in:
parent
e5276a3085
commit
21f9bb2c72
2
api.js
2
api.js
@ -1,4 +1,4 @@
|
||||
import request from '@/starCloud/request'
|
||||
import { request } from './export'
|
||||
|
||||
// 创建账号
|
||||
export function starCloudCreateUser(data) {
|
||||
|
||||
184
constant.js
Normal file
184
constant.js
Normal file
@ -0,0 +1,184 @@
|
||||
// 命令ID
|
||||
export const cmdIds = {
|
||||
// 获取公钥
|
||||
getPublicKey: 0x3090,
|
||||
// 获取私钥
|
||||
getCommKey: 0x3091,
|
||||
// 获取锁状态
|
||||
getLockStatus: 0x3040,
|
||||
// 新增用户
|
||||
addUser: 0x3001,
|
||||
// 开门
|
||||
openDoor: 0x3005,
|
||||
// 重置设备
|
||||
resetDevice: 0x3004,
|
||||
// 清理用户
|
||||
cleanUser: 0x300c,
|
||||
// 扩展命令
|
||||
expandCmd: 0x3030
|
||||
}
|
||||
|
||||
// 子命令ID
|
||||
export const subCmdIds = {
|
||||
// 修改管理员密码
|
||||
updateAdminPassword: 2,
|
||||
// 设置开锁密码
|
||||
setLockPassword: 3,
|
||||
// 重置开锁密码
|
||||
resetLockPassword: 19,
|
||||
// 同步开门记录
|
||||
syncOpenRecord: 41
|
||||
}
|
||||
|
||||
export class Result {
|
||||
static codes = {
|
||||
Success: 0,
|
||||
Fail: -1,
|
||||
|
||||
NotMoreData: -10,
|
||||
|
||||
NotAvailableBluetooth: -20,
|
||||
NotAvailableBluetoothPermission: -21,
|
||||
NotAvailableWeChatNearbyDevicesPermission: -22,
|
||||
NotAvailableWeChatLocationPermission: -23,
|
||||
NotAvailableWeChatNearbyDevicesEmpty: -24,
|
||||
NotAvailableWeChatBluetoothPermission: -25,
|
||||
DeviceHasBeenReset: -30,
|
||||
|
||||
NotRegisteredLock: 4,
|
||||
NotTokenLock: 6,
|
||||
NotMoreKeyLock: 12,
|
||||
ReadyHasKeyLock: 15,
|
||||
ReadyHasPassword: 251
|
||||
}
|
||||
|
||||
static resultsMap = new Map([
|
||||
[Result.codes.Success, { message: '成功', data: {} }],
|
||||
[Result.codes.Fail, { message: '失败', data: {} }],
|
||||
|
||||
[Result.codes.NotMoreData, { message: '没有更多数据', data: {} }],
|
||||
|
||||
[Result.codes.NotAvailableBluetooth, { message: '蓝牙尚未打开,请先打开蓝牙', data: {} }],
|
||||
[
|
||||
Result.codes.NotAvailableBluetoothPermission,
|
||||
{ message: '小程序蓝牙功能被禁用,请打开小程序蓝牙权限', data: {} }
|
||||
],
|
||||
[
|
||||
Result.codes.NotAvailableWeChatNearbyDevicesPermission,
|
||||
{ message: '蓝牙功能需要附近设备权限,请前往设置开启微信的附近设备权限后再试', data: {} }
|
||||
],
|
||||
[
|
||||
Result.codes.NotAvailableWeChatLocationPermission,
|
||||
{ message: '蓝牙功能需要定位权限,请前往设置开启微信的定位权限后再试', data: {} }
|
||||
],
|
||||
[
|
||||
Result.codes.NotAvailableWeChatNearbyDevicesEmpty,
|
||||
{
|
||||
message: '蓝牙功能需要定位服务,请前往设置开启定位服务后再试',
|
||||
data: {}
|
||||
}
|
||||
],
|
||||
[
|
||||
Result.codes.NotAvailableWeChatBluetoothPermission,
|
||||
{
|
||||
message: '微信的蓝牙权限被禁用,请前往设置开启微信的蓝牙权限后再试',
|
||||
data: {}
|
||||
}
|
||||
],
|
||||
[Result.codes.DeviceHasBeenReset, { message: '设备已被重置', data: {} }],
|
||||
|
||||
[Result.codes.NotRegisteredLock, { message: '用户在锁端未注册', data: {} }],
|
||||
[Result.codes.NotTokenLock, { message: '用户在锁端token失效', data: {} }],
|
||||
[Result.codes.NotMoreKeyLock, { message: '锁端钥匙数量已达上限', data: {} }],
|
||||
[Result.codes.ReadyHasKeyLock, { message: '用户已是锁端用户', data: {} }],
|
||||
[Result.codes.ReadyHasPassword, { message: '该密码已存在,请更换。', data: {} }]
|
||||
])
|
||||
|
||||
constructor(code, data, message) {
|
||||
const result = Result.resultsMap.get(code)
|
||||
if (result) {
|
||||
this.code = code
|
||||
this.message = message || result.message
|
||||
this.data = data || result.data
|
||||
} else {
|
||||
this.code = code
|
||||
this.message = message || ''
|
||||
this.data = data || {}
|
||||
}
|
||||
}
|
||||
|
||||
// 成功
|
||||
static get Success() {
|
||||
return new Result(Result.codes.Success)
|
||||
}
|
||||
|
||||
// 失败(默认错误)
|
||||
static get Fail() {
|
||||
return new Result(Result.codes.Fail)
|
||||
}
|
||||
|
||||
// 没有更多数据
|
||||
static get NotMoreData() {
|
||||
return new Result(Result.codes.NotMoreData)
|
||||
}
|
||||
|
||||
// 蓝牙未开启
|
||||
static get NotAvailableBluetooth() {
|
||||
return new Result(Result.codes.NotAvailableBluetooth)
|
||||
}
|
||||
|
||||
// 小程序蓝牙权限被禁用
|
||||
static get NotAvailableBluetoothPermission() {
|
||||
return new Result(Result.codes.NotAvailableBluetoothPermission)
|
||||
}
|
||||
|
||||
// 微信附近的设备权限被禁用
|
||||
static get NotAvailableWeChatNearbyDevicesPermission() {
|
||||
return new Result(Result.codes.NotAvailableWeChatNearbyDevicesPermission)
|
||||
}
|
||||
|
||||
// 微信定位权限被禁用
|
||||
static get NotAvailableWeChatLocationPermission() {
|
||||
return new Result(Result.codes.NotAvailableWeChatLocationPermission)
|
||||
}
|
||||
|
||||
// 手机定位服务被关闭
|
||||
static get NotAvailableWeChatNearbyDevicesEmpty() {
|
||||
return new Result(Result.codes.NotAvailableWeChatNearbyDevicesEmpty)
|
||||
}
|
||||
|
||||
// 微信的蓝牙权限被禁用
|
||||
static get NotAvailableWeChatBluetoothPermission() {
|
||||
return new Result(Result.codes.NotAvailableWeChatBluetoothPermission)
|
||||
}
|
||||
|
||||
// 设备已被重置
|
||||
static get DeviceHasBeenReset() {
|
||||
return new Result(Result.codes.DeviceHasBeenReset)
|
||||
}
|
||||
|
||||
// 用户在锁端未注册
|
||||
static get NotRegisteredLock() {
|
||||
return new Result(Result.codes.NotRegisteredLock)
|
||||
}
|
||||
|
||||
// 用户在锁端token失效
|
||||
static get NotTokenLock() {
|
||||
return new Result(Result.codes.NotTokenLock)
|
||||
}
|
||||
|
||||
// 锁端钥匙数量已达上限
|
||||
static get NotMoreKeyLock() {
|
||||
return new Result(Result.codes.NotMoreKeyLock)
|
||||
}
|
||||
|
||||
// 锁端钥匙数量已达上限
|
||||
static get ReadyHasKeyLock() {
|
||||
return new Result(Result.codes.ReadyHasKeyLock)
|
||||
}
|
||||
|
||||
// 密码已存在
|
||||
static get ReadyHasPassword() {
|
||||
return new Result(Result.codes.ReadyHasPassword)
|
||||
}
|
||||
}
|
||||
35
export.js
Normal file
35
export.js
Normal file
@ -0,0 +1,35 @@
|
||||
import { getStorageUni, removeStorageUni, setStorageUni } from './uni/storage'
|
||||
import { getStorageWeb, removeStorageWeb, setStorageWeb } from './web/storage'
|
||||
import requestUni from '@/starCloud/uni/request'
|
||||
import requestWeb from '@/starCloud/web/request'
|
||||
import starCloudInstance from './star-cloud'
|
||||
|
||||
export const setStorage = (key, value) => {
|
||||
if (starCloudInstance.platform === 2) {
|
||||
setStorageWeb(key, value)
|
||||
} else {
|
||||
setStorageUni(key, value)
|
||||
}
|
||||
}
|
||||
|
||||
export const getStorage = key => {
|
||||
if (starCloudInstance.platform === 2) {
|
||||
return getStorageWeb(key)
|
||||
}
|
||||
return getStorageUni(key)
|
||||
}
|
||||
|
||||
export const removeStorage = key => {
|
||||
if (starCloudInstance.platform === 2) {
|
||||
removeStorageWeb(key)
|
||||
} else {
|
||||
removeStorageUni(key)
|
||||
}
|
||||
}
|
||||
|
||||
export const request = async params => {
|
||||
if (starCloudInstance.platform === 2) {
|
||||
return await requestWeb(params)
|
||||
}
|
||||
return await requestUni(params)
|
||||
}
|
||||
187
star-cloud.js
Normal file
187
star-cloud.js
Normal file
@ -0,0 +1,187 @@
|
||||
import { getStorage, setStorage } from './export'
|
||||
import * as device from './star-cloud/device.js'
|
||||
import * as lock from './star-cloud/lock.js'
|
||||
import * as other from './star-cloud/other.js'
|
||||
import * as password from './star-cloud/password.js'
|
||||
import * as record from './star-cloud/record.js'
|
||||
import * as user from './star-cloud/user.js'
|
||||
import * as common from './common.js'
|
||||
import { onBLECharacteristicValueChange } from './uni/basic'
|
||||
|
||||
/**
|
||||
* 账户信息信息
|
||||
* @typedef {Object} AccountInfo
|
||||
* @property {Number} uid 用户ID
|
||||
* @property {String} username 用户名
|
||||
* @property {String} password 密码
|
||||
* @property {String} token token 非必填
|
||||
*/
|
||||
|
||||
/**
|
||||
* 锁信息
|
||||
* @typedef {Object} lockInfo
|
||||
* @property {Number} keyId - 钥匙ID
|
||||
* @property {Number} lockId - 锁ID
|
||||
* @property {String} lockName - 锁名称
|
||||
* @property {String} lockAlias - 锁别名
|
||||
* @property {Number} electricQuantity - 电量
|
||||
* @property {Number} electricQuantityStandby - 备用电量
|
||||
* @property {Number} electricQuantityDate - 电量更新时间
|
||||
* @property {String} fwVersion - 固件版本
|
||||
* @property {String} hwVersion - 硬件版本
|
||||
* @property {Number} keyType - 钥匙类型
|
||||
* @property {Number} passageMode - 常开模式
|
||||
* @property {Number} userType - 用户类型
|
||||
* @property {Number} startDate - 有效期开始时间
|
||||
* @property {Number} endDate - 有效期结束时间
|
||||
* @property {Object} weekDays - 循环周期
|
||||
* @property {Number} remoteEnable - 是否支持远程开锁:0-未知,1-是,2-否
|
||||
* @property {Number} faceAuthentication - 是否实名认证:0-未知,1-是,2-否
|
||||
* @property {Number} lastFaceValidateTime - 最后一次人脸验证时间
|
||||
* @property {Number} nextFaceValidateTime - 下次人脸验证时间
|
||||
* @property {Number} keyRight - 是否授权管理员钥匙: 0-未知,1-是,2-否
|
||||
* @property {Number} keyStatus - 钥匙状态
|
||||
* @property {Object} bluetooth - 蓝牙
|
||||
* @property {Number} sendDate - 发送时间
|
||||
* @property {Number} isLockOwner - 是否是锁主
|
||||
* @property {Object} lockFeature - 锁特征
|
||||
* @property {Object} lockSetting - 锁设置
|
||||
* @property {Number} lockUserNo - 用户编号
|
||||
* @property {Number} senderUserId - 发送者用户ID
|
||||
* @property {Number} isOnlyManageSelf - 如果是授权管理员此字段区分是否仅管理自己发的钥匙
|
||||
* @property {Number} restoreCount - 重置次数
|
||||
* @property {String} model - 模式
|
||||
*/
|
||||
|
||||
/**
|
||||
* 锁蓝牙信息
|
||||
* @typedef {Object} bluetooth
|
||||
* @property {String} bluetoothDeviceId - 设备ID
|
||||
* @property {String} bluetoothDeviceName - 设备名称
|
||||
* @property {String} publicKey - 公钥
|
||||
* @property {String} privateKey - 私钥
|
||||
* @property {String} signKey - 签名密钥
|
||||
* @property {String} passwordKey - 密码密钥
|
||||
*/
|
||||
|
||||
/**
|
||||
* 锁设置信息
|
||||
* @typedef {Object} lockSetting
|
||||
* @property {Number} appUnlockOnline - 开门是否需要联网
|
||||
*/
|
||||
|
||||
/**
|
||||
* 请求返回数据
|
||||
* @typedef {Object} requestData
|
||||
* @property {String} userNos - 设备ID
|
||||
*/
|
||||
|
||||
class StarCloud {
|
||||
constructor(methodModules) {
|
||||
// 环境
|
||||
this.env = null
|
||||
// 客户端Id
|
||||
this.clientId = null
|
||||
// 客户端密码
|
||||
this.clientSecret = null
|
||||
// 小程序环境
|
||||
this.envVersion = ''
|
||||
// 锁信息
|
||||
this.lockInfo = null
|
||||
// 服务器时间
|
||||
this.serverTimestamp = 0
|
||||
// 时间差
|
||||
this.timeDifference = 0
|
||||
// 搜索设备列表
|
||||
this.searchDeviceList = []
|
||||
// 星云用户信息
|
||||
this.userInfo = null
|
||||
// 账户信息
|
||||
this.accountInfo = null
|
||||
// 消息序号
|
||||
this.messageCount = 1
|
||||
// 是否上报日志
|
||||
this.isReportLog = false
|
||||
// 请求参数
|
||||
this.requestParams = null
|
||||
// 特性值回调
|
||||
this.characteristicValueCallback = null
|
||||
// 完整数据
|
||||
this.completeArray = []
|
||||
// 完整内容数据长度
|
||||
this.length = 0
|
||||
// 计时器
|
||||
this.timer = null
|
||||
// 平台
|
||||
this.platform = 1
|
||||
|
||||
this.loadMethods(methodModules)
|
||||
this.loadFromStorage()
|
||||
}
|
||||
|
||||
static saveFieldToStorage(key, value) {
|
||||
setStorage(`StarCloud_${key}`, value)
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化星云
|
||||
* @param params
|
||||
* @param {String} params.clientId 客户端Id
|
||||
* @param {String} params.clientSecret 客户端密码
|
||||
* @param {String} params.env 环境
|
||||
* @param {Number} params.platform 平台 1:uni 2:web 默认为1
|
||||
* @param {Boolean} params.isReportLog 是否上报日志
|
||||
*/
|
||||
init(params) {
|
||||
Object.assign(StarCloud.prototype, device, lock, other, password, record, user, common)
|
||||
|
||||
const { clientId, clientSecret, env, platform } = params
|
||||
this.envVersion = 'release'
|
||||
if (platform !== 2) {
|
||||
const appInfo = uni.getAccountInfoSync()
|
||||
this.envVersion = appInfo.miniProgram.envVersion
|
||||
|
||||
// 监听特性值变化
|
||||
onBLECharacteristicValueChange(this.listenCharacteristicValue.bind(this))
|
||||
}
|
||||
|
||||
this.isReportLog = params.isReportLog
|
||||
|
||||
this.env = env || 'XHJ'
|
||||
this.clientId = clientId
|
||||
this.clientSecret = clientSecret
|
||||
this.platform = platform || 1
|
||||
}
|
||||
|
||||
loadMethods(methodModules) {
|
||||
methodModules.forEach(methods => {
|
||||
Object.entries(methods).forEach(([key, method]) => {
|
||||
if (typeof method === 'function') {
|
||||
this[key] = method.bind(this)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
loadFromStorage() {
|
||||
Object.keys(this).forEach(key => {
|
||||
const value = getStorage(`StarCloud_${key}`)
|
||||
if (value !== undefined) {
|
||||
this[key] = value
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const starCloudInstance = new Proxy(StarCloud.prototype, {
|
||||
get(target, key) {
|
||||
return Reflect.get(target, key)
|
||||
},
|
||||
set(target, key, value) {
|
||||
StarCloud.saveFieldToStorage(key, value)
|
||||
return Reflect.set(target, key, value)
|
||||
}
|
||||
})
|
||||
Object.setPrototypeOf(StarCloud, starCloudInstance)
|
||||
|
||||
export default starCloudInstance
|
||||
164
star-cloud/device.js
Normal file
164
star-cloud/device.js
Normal file
@ -0,0 +1,164 @@
|
||||
import {
|
||||
closeBluetoothAdapter,
|
||||
createBLEConnection,
|
||||
getBluetoothDevices,
|
||||
offBluetoothAdapterStateChange,
|
||||
startBluetoothDevicesDiscovery,
|
||||
stopBluetoothDevicesDiscovery
|
||||
} from '../uni/basic'
|
||||
import { Result } from '../constant'
|
||||
import { bindLockRequest } from '../api'
|
||||
|
||||
// 搜索设备
|
||||
export async function searchDevice(callback) {
|
||||
const result = await startBluetoothDevicesDiscovery()
|
||||
if (result.code === Result.Success.code) {
|
||||
const timestamp = new Date().getTime()
|
||||
let queryFlag = false
|
||||
this.timer = setInterval(async () => {
|
||||
const queryResult = await getBluetoothDevices()
|
||||
if (queryResult.code === Result.Success.code) {
|
||||
const deviceList = queryResult.data
|
||||
if (queryFlag === false && deviceList.length > 0) {
|
||||
queryFlag = true
|
||||
}
|
||||
if (new Date().getTime() - timestamp > 10000 && !queryFlag) {
|
||||
if (this.timer) {
|
||||
clearInterval(this.timer)
|
||||
}
|
||||
callback(Result.NotAvailableWeChatNearbyDevicesEmpty)
|
||||
}
|
||||
|
||||
const list = []
|
||||
|
||||
for (let i = 0; i < deviceList.length; i++) {
|
||||
if (deviceList[i]?.advertisServiceUUIDs) {
|
||||
const uuid = deviceList[i]?.advertisServiceUUIDs[0]
|
||||
if (uuid && uuid.slice(2, 8) === '758824' && uuid.slice(30, 32) === '00') {
|
||||
list.push(deviceList[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.searchDeviceList = list
|
||||
callback(
|
||||
new Result(Result.Success.code, {
|
||||
list
|
||||
})
|
||||
)
|
||||
} else {
|
||||
callback(queryResult)
|
||||
}
|
||||
}, 1000)
|
||||
} else {
|
||||
callback(result)
|
||||
}
|
||||
}
|
||||
|
||||
// 停止搜索
|
||||
export async function stopSearchDevice() {
|
||||
console.log('停止搜索')
|
||||
if (this.timer) {
|
||||
clearInterval(this.timer)
|
||||
}
|
||||
return await stopBluetoothDevicesDiscovery()
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定设备
|
||||
* @param params
|
||||
* @param {AccountInfo} params.accountInfo 账号信息
|
||||
* @param {String} params.name 设备名称
|
||||
* @returns {Promise<Result>}
|
||||
*/
|
||||
export async function bindDevice(params) {
|
||||
const { accountInfo, name } = params
|
||||
// 设置执行账号
|
||||
const result = await this.login(accountInfo)
|
||||
if (result.code !== Result.Success.code) {
|
||||
return result
|
||||
}
|
||||
const device = this.searchDeviceList.find(item => item.name === name)
|
||||
const connectResult = await createBLEConnection(device.deviceId)
|
||||
if (connectResult.code === Result.Success.code) {
|
||||
this.updateLockInfo({
|
||||
...connectResult.data,
|
||||
bluetooth: {
|
||||
bluetoothDeviceId: device.deviceId,
|
||||
bluetoothDeviceName: device.name
|
||||
}
|
||||
})
|
||||
const publicKeyResult = await this.getPublicKey()
|
||||
if (publicKeyResult.code !== Result.Success.code) {
|
||||
return publicKeyResult
|
||||
}
|
||||
const commKeyResult = await this.getCommKey()
|
||||
if (commKeyResult.code !== Result.Success.code) {
|
||||
return commKeyResult
|
||||
}
|
||||
const lockStatusResult = await this.getLockStatus()
|
||||
if (lockStatusResult.code !== Result.Success.code) {
|
||||
return lockStatusResult
|
||||
}
|
||||
const timestamp = Math.ceil(new Date().getTime() / 1000)
|
||||
const password = (Math.floor(Math.random() * 900000) + 100000).toString()
|
||||
const addUserResult = await this.addLockUser({
|
||||
params: {
|
||||
name: this.lockInfo.bluetooth.bluetoothDeviceName,
|
||||
keyId: '0',
|
||||
authUid: this.accountInfo.uid.toString(),
|
||||
uid: this.accountInfo.uid.toString(),
|
||||
openMode: 1,
|
||||
keyType: 0,
|
||||
startDate: timestamp,
|
||||
expireDate: 0xffffffff,
|
||||
useCountLimit: 0xffff,
|
||||
isRound: 0,
|
||||
weekRound: 0,
|
||||
startHour: 0,
|
||||
startMin: 0,
|
||||
endHour: 0,
|
||||
endMin: 0,
|
||||
role: 0xff,
|
||||
password
|
||||
},
|
||||
disconnect: true
|
||||
})
|
||||
if (addUserResult.code !== Result.Success.code) {
|
||||
return addUserResult
|
||||
}
|
||||
|
||||
offBluetoothAdapterStateChange()
|
||||
closeBluetoothAdapter()
|
||||
|
||||
const params = {
|
||||
lockAlias: this.lockInfo.bluetooth.bluetoothDeviceName,
|
||||
lockInfo: {
|
||||
...this.lockInfo.lockConfig,
|
||||
adminPwd: password
|
||||
},
|
||||
bluetooth: this.lockInfo.bluetooth,
|
||||
lockUserNo: this.lockInfo.lockUserNo,
|
||||
pwdTimestamp: this.lockInfo.pwdTimestamp,
|
||||
featureValue: this.lockInfo.featureValue,
|
||||
featureSettingValue: this.lockInfo.featureSettingValue,
|
||||
featureSettingParams: this.lockInfo.featureSettingParams
|
||||
}
|
||||
const bindLockResult = await bindLockRequest(params)
|
||||
if (bindLockResult.code === Result.Success.code) {
|
||||
this.updateLockInfo({
|
||||
lockId: bindLockResult.data.lockId,
|
||||
keyId: bindLockResult.data.keyId,
|
||||
adminPwd: password
|
||||
})
|
||||
}
|
||||
return new Result(
|
||||
bindLockResult.code,
|
||||
{
|
||||
lock: this.lockInfo
|
||||
},
|
||||
bindLockResult.message
|
||||
)
|
||||
}
|
||||
return connectResult
|
||||
}
|
||||
367
star-cloud/lock.js
Normal file
367
star-cloud/lock.js
Normal file
@ -0,0 +1,367 @@
|
||||
import { sm4 } from 'sm-crypto'
|
||||
import { cmdIds, Result } from '../constant'
|
||||
import { searchAndConnectDevice, writeBLECharacteristicValue } from '../uni/basic'
|
||||
import { createPackageEnd, md5Encrypt, timestampToArray } from '../format'
|
||||
import { getLockDetailRequest, getLockSettingDataRequest } from '../api'
|
||||
import { getStorage, setStorage } from '../export'
|
||||
import log from '../uni/log'
|
||||
|
||||
/**
|
||||
* 选择锁
|
||||
* @param params
|
||||
* @param {AccountInfo} params.accountInfo 账号信息
|
||||
* @param {Number} params.lockId 锁ID
|
||||
* @returns {Promise<Result>}
|
||||
*/
|
||||
export async function selectLock(params) {
|
||||
const { accountInfo, lockId } = params
|
||||
const result = await this.login(accountInfo)
|
||||
if (result.code !== Result.Success.code) {
|
||||
return result
|
||||
}
|
||||
|
||||
const { code, data, message } = await getLockDetailRequest({
|
||||
lockId
|
||||
})
|
||||
if (code === Result.Success.code) {
|
||||
this.lockInfo = data
|
||||
let lockList = getStorage('starLockList')
|
||||
if (!lockList) {
|
||||
lockList = {}
|
||||
}
|
||||
if (lockList[accountInfo.uid]) {
|
||||
const index = lockList[accountInfo.uid].findIndex(item => item.lockId === lockId)
|
||||
if (index === -1) {
|
||||
lockList[accountInfo.uid].push(this.lockInfo)
|
||||
} else {
|
||||
this.lockInfo.token = lockList[accountInfo.uid][index].token
|
||||
lockList[accountInfo.uid][index] = this.lockInfo
|
||||
}
|
||||
setStorage('starLockList', lockList)
|
||||
} else {
|
||||
lockList[accountInfo.uid] = [this.lockInfo]
|
||||
setStorage('starLockList', lockList)
|
||||
}
|
||||
} else if (code === Result.Fail.code) {
|
||||
const lockList = getStorage('starLockList')
|
||||
if (lockList[accountInfo.uid]) {
|
||||
const index = lockList[accountInfo.uid].findIndex(item => item.lockId === lockId)
|
||||
if (index !== -1) {
|
||||
this.lockInfo = lockList[accountInfo.uid][index]
|
||||
return new Result(Result.Success.code, this.lockInfo)
|
||||
}
|
||||
}
|
||||
}
|
||||
return new Result(code, data, message)
|
||||
}
|
||||
|
||||
/**
|
||||
* 开门
|
||||
* @param params
|
||||
* @param {AccountInfo} params.accountInfo 账号信息
|
||||
* @param {String} params.type 开门方式 close: 关门 open: 开门
|
||||
* @param {Boolean} params.disconnect 操作后是否断开连接
|
||||
* @returns {Promise<Result>}
|
||||
*/
|
||||
export async function openDoor(params) {
|
||||
const { accountInfo, type } = params
|
||||
|
||||
log.info({
|
||||
...new Result(
|
||||
Result.Success.code,
|
||||
{
|
||||
lockName: this.lockInfo.bluetooth.bluetoothDeviceName,
|
||||
lockId: this.lockInfo.lockId,
|
||||
uid: accountInfo.uid,
|
||||
time: new Date().getTime()
|
||||
},
|
||||
`开始开门`
|
||||
),
|
||||
name: 'openDoor'
|
||||
})
|
||||
|
||||
// 设置执行账号
|
||||
const result = await this.login(accountInfo)
|
||||
if (result.code !== Result.Success.code) {
|
||||
return result
|
||||
}
|
||||
|
||||
log.info({
|
||||
...new Result(
|
||||
result.code,
|
||||
{
|
||||
lockName: this.lockInfo.bluetooth.bluetoothDeviceName,
|
||||
lockId: this.lockInfo.lockId,
|
||||
uid: accountInfo.uid,
|
||||
time: new Date().getTime()
|
||||
},
|
||||
`登录星云账号: ${result.message}`
|
||||
),
|
||||
name: 'openDoor'
|
||||
})
|
||||
|
||||
// 确认设备连接正常
|
||||
if (!params.connected) {
|
||||
const searchResult = await searchAndConnectDevice(this.lockInfo.bluetooth.bluetoothDeviceName)
|
||||
log.info({
|
||||
...new Result(
|
||||
searchResult.code,
|
||||
{
|
||||
lockName: this.lockInfo.bluetooth.bluetoothDeviceName,
|
||||
lockId: this.lockInfo.lockId,
|
||||
uid: accountInfo.uid,
|
||||
time: new Date().getTime()
|
||||
},
|
||||
`连接设备: ${searchResult.message}`
|
||||
),
|
||||
name: 'openDoor'
|
||||
})
|
||||
if (searchResult.code !== Result.Success.code) {
|
||||
return searchResult
|
||||
}
|
||||
this.updateLockInfo(searchResult.data)
|
||||
}
|
||||
|
||||
// 检查是否已添加为用户
|
||||
const checkResult = await this.checkLockUser()
|
||||
if (checkResult.code !== Result.Success.code) {
|
||||
return checkResult
|
||||
}
|
||||
|
||||
log.info({
|
||||
...new Result(
|
||||
checkResult.code,
|
||||
{
|
||||
lockName: this.lockInfo.bluetooth.bluetoothDeviceName,
|
||||
lockId: this.lockInfo.lockId,
|
||||
uid: accountInfo.uid,
|
||||
time: new Date().getTime()
|
||||
},
|
||||
`确认是否为锁用户: ${checkResult.message}`
|
||||
),
|
||||
name: 'openDoor'
|
||||
})
|
||||
|
||||
// 是否需要联网
|
||||
let onlineToken = ''
|
||||
if (this.lockInfo.lockSetting.appUnlockOnline) {
|
||||
const result = await this.getNetToken()
|
||||
if (result.code === Result.Success.code) {
|
||||
onlineToken = result.data.token
|
||||
} else {
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
log.info({
|
||||
...new Result(
|
||||
checkResult.code,
|
||||
{
|
||||
lockName: this.lockInfo.bluetooth.bluetoothDeviceName,
|
||||
lockId: this.lockInfo.lockId,
|
||||
uid: accountInfo.uid,
|
||||
time: new Date().getTime()
|
||||
},
|
||||
`判断是否需要联网token: ${this.lockInfo.lockSetting.appUnlockOnline}`
|
||||
),
|
||||
name: 'openDoor'
|
||||
})
|
||||
|
||||
// 开门方式
|
||||
let openMode
|
||||
if (type === 'close') {
|
||||
openMode = this.lockInfo.lockSetting.appUnlockOnline ? 33 : 32
|
||||
} else {
|
||||
openMode = this.lockInfo.lockSetting.appUnlockOnline ? 1 : 0
|
||||
}
|
||||
|
||||
const name = this.lockInfo.bluetooth.bluetoothDeviceName
|
||||
const uid = this.accountInfo.uid.toString()
|
||||
const openTime = Math.ceil(new Date().getTime() / 1000) + this.timeDifference
|
||||
|
||||
const length = 2 + 40 + 20 + 1 + 4 + 4 + 1 + 16 + 16
|
||||
const headArray = this.createPackageHeader(3, length)
|
||||
|
||||
const contentArray = new Uint8Array(length)
|
||||
contentArray[0] = cmdIds.openDoor / 256
|
||||
contentArray[1] = cmdIds.openDoor % 256
|
||||
|
||||
for (let i = 0; i < name.length; i++) {
|
||||
contentArray[i + 2] = name.charCodeAt(i)
|
||||
}
|
||||
|
||||
for (let i = 0; i < uid.length; i++) {
|
||||
contentArray[i + 42] = uid.charCodeAt(i)
|
||||
}
|
||||
|
||||
contentArray[62] = openMode
|
||||
|
||||
contentArray.set(timestampToArray(openTime), 63)
|
||||
|
||||
console.log('开门时token', this.lockInfo.token)
|
||||
|
||||
contentArray.set(this.lockInfo.token || timestampToArray(openTime), 67)
|
||||
|
||||
contentArray[71] = 16
|
||||
|
||||
const md5Array = md5Encrypt(
|
||||
name + uid,
|
||||
this.lockInfo.token || timestampToArray(openTime),
|
||||
this.lockInfo.bluetooth.signKey
|
||||
)
|
||||
|
||||
contentArray.set(md5Array, 72)
|
||||
|
||||
for (let i = 0; i < onlineToken.length; i++) {
|
||||
contentArray[i + 88] = onlineToken.charCodeAt(i)
|
||||
}
|
||||
|
||||
const cebArray = sm4.encrypt(contentArray, this.lockInfo.bluetooth.privateKey, {
|
||||
mode: 'ecb',
|
||||
output: 'array'
|
||||
})
|
||||
|
||||
const packageArray = createPackageEnd(headArray, cebArray)
|
||||
|
||||
log.info({
|
||||
...new Result(
|
||||
Result.Success.code,
|
||||
{
|
||||
lockName: this.lockInfo.bluetooth.bluetoothDeviceName,
|
||||
lockId: this.lockInfo.lockId,
|
||||
uid: accountInfo.uid,
|
||||
time: new Date().getTime()
|
||||
},
|
||||
`开始写入`
|
||||
),
|
||||
name: 'openDoor'
|
||||
})
|
||||
|
||||
const writeResult = await writeBLECharacteristicValue(
|
||||
this.lockInfo.deviceId,
|
||||
this.lockInfo.serviceId,
|
||||
this.lockInfo.writeCharacteristicId,
|
||||
packageArray
|
||||
)
|
||||
|
||||
if (writeResult.code !== Result.Success.code) {
|
||||
return writeResult
|
||||
}
|
||||
|
||||
log.info({
|
||||
...new Result(
|
||||
writeResult.code,
|
||||
{
|
||||
lockName: this.lockInfo.bluetooth.bluetoothDeviceName,
|
||||
lockId: this.lockInfo.lockId,
|
||||
uid: accountInfo.uid,
|
||||
time: new Date().getTime()
|
||||
},
|
||||
`写入完成:${writeResult.message}`
|
||||
),
|
||||
name: 'openDoor'
|
||||
})
|
||||
|
||||
return this.getWriteResult(this.openDoor, params)
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除锁
|
||||
* @param params
|
||||
* @param {AccountInfo} params.accountInfo 账号信息
|
||||
*/
|
||||
export async function deleteLock(params) {
|
||||
const { accountInfo } = params
|
||||
// 设置执行账号
|
||||
const result = await this.login(accountInfo)
|
||||
if (result.code !== Result.Success.code) {
|
||||
return result
|
||||
}
|
||||
// 确认设备连接正常
|
||||
if (!params.connected) {
|
||||
const searchResult = await searchAndConnectDevice(this.lockInfo.bluetooth.bluetoothDeviceName)
|
||||
if (searchResult.code !== Result.Success.code) {
|
||||
return searchResult
|
||||
}
|
||||
this.updateLockInfo(searchResult.data)
|
||||
}
|
||||
|
||||
// 检查是否已添加为用户
|
||||
const checkResult = await this.checkLockUser()
|
||||
if (checkResult.code !== Result.Success.code) {
|
||||
return checkResult
|
||||
}
|
||||
|
||||
const {
|
||||
token,
|
||||
bluetooth: { publicKey, privateKey }
|
||||
} = this.lockInfo
|
||||
|
||||
const authUid = this.lockInfo.uid.toString()
|
||||
const name = this.lockInfo.bluetooth.bluetoothDeviceName
|
||||
|
||||
const length = 2 + 40 + 20 + 4 + 1 + 16
|
||||
const headArray = this.createPackageHeader(3, length)
|
||||
const contentArray = new Uint8Array(length)
|
||||
|
||||
contentArray[0] = cmdIds.resetDevice / 256
|
||||
contentArray[1] = cmdIds.resetDevice % 256
|
||||
|
||||
for (let i = 0; i < name.length; i++) {
|
||||
contentArray[i + 2] = name.charCodeAt(i)
|
||||
}
|
||||
|
||||
for (let i = 0; i < authUid.length; i++) {
|
||||
contentArray[i + 42] = authUid.charCodeAt(i)
|
||||
}
|
||||
contentArray.set(token || new Uint8Array([0, 0, 0, 0]), 62)
|
||||
contentArray[66] = 16
|
||||
|
||||
const md5Array = md5Encrypt(name, token || new Uint8Array([0, 0, 0, 0]), publicKey)
|
||||
contentArray.set(md5Array, 67)
|
||||
|
||||
const cebArray = sm4.encrypt(contentArray, privateKey, {
|
||||
mode: 'ecb',
|
||||
output: 'array'
|
||||
})
|
||||
|
||||
const packageArray = createPackageEnd(headArray, cebArray)
|
||||
|
||||
const writeResult = await writeBLECharacteristicValue(
|
||||
this.lockInfo.deviceId,
|
||||
this.lockInfo.serviceId,
|
||||
this.lockInfo.writeCharacteristicId,
|
||||
packageArray
|
||||
)
|
||||
|
||||
if (writeResult.code !== Result.Success.code) {
|
||||
return writeResult
|
||||
}
|
||||
|
||||
return this.getWriteResult(this.deleteLock, params)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取锁支持项
|
||||
* @param params
|
||||
* @param {AccountInfo} params.accountInfo 账号信息
|
||||
* @param {Number} params.lockId 锁 Id
|
||||
* @returns {Promise<Result>}
|
||||
*/
|
||||
export async function getLockSupportFeatures(params) {
|
||||
const { accountInfo, lockId } = params
|
||||
|
||||
// 设置执行账号
|
||||
const result = await this.login(accountInfo)
|
||||
if (result.code !== Result.Success.code) {
|
||||
return result
|
||||
}
|
||||
|
||||
const { code, data, message } = await getLockSettingDataRequest({
|
||||
lockId
|
||||
})
|
||||
if (code === Result.Success.code) {
|
||||
return new Result(code, { ...data.lockFeature }, message)
|
||||
}
|
||||
return new Result(code, data, message)
|
||||
}
|
||||
46
star-cloud/other.js
Normal file
46
star-cloud/other.js
Normal file
@ -0,0 +1,46 @@
|
||||
import { getServerDatetimeRequest, removeBadLockRequest } from '../api'
|
||||
import { Result } from '../constant'
|
||||
import { getStorage, setStorage } from '../export'
|
||||
|
||||
/**
|
||||
* 移除坏锁
|
||||
* @param params
|
||||
* @param {AccountInfo} params.accountInfo 账号信息
|
||||
* @param {List[int]} params.lockIds 锁Id列表
|
||||
* @returns {Promise<Result>}
|
||||
*/
|
||||
export async function removeBadLock(params) {
|
||||
const { accountInfo, lockIds } = params
|
||||
// 设置执行账号
|
||||
const result = await this.login(accountInfo)
|
||||
if (result.code !== Result.Success.code) {
|
||||
return result
|
||||
}
|
||||
const { code, message } = await removeBadLockRequest({
|
||||
lockIds: params.lockIds
|
||||
})
|
||||
if (code === Result.Success.code) {
|
||||
const lockList = getStorage('starLockList')
|
||||
if (lockList[accountInfo.uid]) {
|
||||
lockIds.forEach(lockId => {
|
||||
const index = lockList[accountInfo.uid].findIndex(item => item.lockId === lockId)
|
||||
if (index !== -1) {
|
||||
lockList[accountInfo.uid].splice(index, 1)
|
||||
}
|
||||
})
|
||||
setStorage('starLockList', lockList)
|
||||
}
|
||||
}
|
||||
|
||||
return new Result(code, {}, message)
|
||||
}
|
||||
|
||||
// 获取服务器时间
|
||||
export async function getServerTimestamp() {
|
||||
const { code, data, message } = await getServerDatetimeRequest({})
|
||||
if (code === Result.Success.code) {
|
||||
this.serverTimestamp = Math.ceil(data.date / 1000)
|
||||
this.timeDifference = Math.ceil((data.date - new Date().getTime()) / 1000)
|
||||
}
|
||||
return new Result(code, data, message)
|
||||
}
|
||||
295
star-cloud/password.js
Normal file
295
star-cloud/password.js
Normal file
@ -0,0 +1,295 @@
|
||||
import { sm4 } from 'sm-crypto'
|
||||
import { cmdIds, Result, subCmdIds } from '../constant'
|
||||
import { searchAndConnectDevice, writeBLECharacteristicValue } from '../uni/basic'
|
||||
import { createPackageEnd, md5Encrypt, timestampToArray } from '../format'
|
||||
import { checkPasswordRequest, getOfflinePasswordRequest } from '../api'
|
||||
|
||||
/**
|
||||
* 离线密码
|
||||
* 该功能无需蓝牙交互直接请求服务端,详细参数说明请看星云接口文档
|
||||
* @typedef {Object} OfflinePassword
|
||||
* @property {String} keyboardPwdName - 密码名称
|
||||
* @property {Number} keyboardPwdType - 密码类型
|
||||
* @property {Number} lockId - 锁 Id
|
||||
* @property {Number} isCoerced - 胁迫 1:胁迫 2:非胁迫
|
||||
* @property {Number} startDate - 开始时间
|
||||
* @property {Number} endDate - 结束时间
|
||||
* @property {Number} hoursStart - 开始小时
|
||||
* @property {Number} hoursEnd - 结束小时
|
||||
*/
|
||||
|
||||
/**
|
||||
* 自定义密码
|
||||
* @typedef {Object} CustomPassword
|
||||
* @property {String} keyboardPwdName - 密码名称
|
||||
* @property {Number} keyboardPwdType - 密码类型
|
||||
* @property {Number} isCoerced - 胁迫 1:胁迫 2:非胁迫
|
||||
* @property {Number} startDate - 开始时间
|
||||
* @property {Number} endDate - 结束时间
|
||||
* @property {Number} keyboardPwd - 密码
|
||||
* @property {Number} addType - 添加方式,当前仅支持1 1:蓝牙 2:网关
|
||||
* @property {Number} operate - 操作类型,0:注册 1:修改
|
||||
* @property {Number} pwdRight - 是否是管理员密码,0:否 1:是
|
||||
* @property {Number} keyboardPwdId - 密码ID
|
||||
*/
|
||||
|
||||
/**
|
||||
* 获取离线密码
|
||||
* @param params
|
||||
* @param {AccountInfo} params.accountInfo 账号信息
|
||||
* @param {OfflinePassword} params.password 密码信息
|
||||
* @returns {Promise<Result>}
|
||||
*/
|
||||
export async function getOfflinePassword(params) {
|
||||
const { accountInfo, password } = params
|
||||
// 设置执行账号
|
||||
const result = await this.login(accountInfo)
|
||||
if (result.code !== Result.Success.code) {
|
||||
return result
|
||||
}
|
||||
return await getOfflinePasswordRequest(password)
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义密码
|
||||
* @param params
|
||||
* @param {AccountInfo} params.accountInfo 账号信息
|
||||
* @param {CustomPassword} params.password 密码信息
|
||||
* @param {Boolean} params.disconnect 操作后是否断开连接
|
||||
* @returns {Promise<Result>}
|
||||
*/
|
||||
export async function customPassword(params) {
|
||||
const { accountInfo, password } = params
|
||||
// 设置执行账号
|
||||
const result = await this.login(accountInfo)
|
||||
if (result.code !== Result.Success.code) {
|
||||
return result
|
||||
}
|
||||
|
||||
this.requestParams = password
|
||||
let { pwdNo, operate, keyboardPwd, startDate, endDate, pwdRight } = password
|
||||
|
||||
if (operate === 0 || operate === 1) {
|
||||
const checkPasswordParams = {
|
||||
lockId: this.lockInfo.lockId,
|
||||
keyboardPwd
|
||||
}
|
||||
if (password.keyboardPwdId) {
|
||||
checkPasswordParams.keyboardPwdId = password.keyboardPwdId
|
||||
}
|
||||
const checkPasswordResult = await this.checkPassword(checkPasswordParams)
|
||||
if (checkPasswordResult.code === -3) {
|
||||
return Result.ReadyHasPassword
|
||||
}
|
||||
if (checkPasswordResult.code !== Result.Success.code) {
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
// 确认设备连接正常
|
||||
if (!params.connected) {
|
||||
const searchResult = await searchAndConnectDevice(this.lockInfo.bluetooth.bluetoothDeviceName)
|
||||
if (searchResult.code !== Result.Success.code) {
|
||||
return searchResult
|
||||
}
|
||||
this.updateLockInfo(searchResult.data)
|
||||
}
|
||||
|
||||
// 检查是否已添加为用户
|
||||
const checkResult = await this.checkLockUser()
|
||||
if (checkResult.code !== Result.Success.code) {
|
||||
return checkResult
|
||||
}
|
||||
|
||||
const uid = this.accountInfo.uid.toString()
|
||||
const keyId = this.lockInfo.keyId.toString()
|
||||
const isAdmin = pwdRight
|
||||
const userCountLimit = 0xffff
|
||||
startDate = Math.floor(startDate / 1000)
|
||||
endDate = Math.floor(endDate / 1000)
|
||||
keyboardPwd = keyboardPwd.toString()
|
||||
|
||||
if (!pwdNo) {
|
||||
pwdNo = 0
|
||||
}
|
||||
|
||||
const length = 2 + 1 + 1 + 40 + 20 + 2 + 1 + 1 + 20 + 2 + 4 + 4 + 4 + 1 + 16
|
||||
const headArray = this.createPackageHeader(3, length)
|
||||
|
||||
const contentArray = new Uint8Array(length)
|
||||
contentArray[0] = cmdIds.expandCmd / 256
|
||||
contentArray[1] = cmdIds.expandCmd % 256
|
||||
|
||||
// 子命令
|
||||
contentArray[2] = subCmdIds.setLockPassword
|
||||
|
||||
contentArray[3] = length - 3
|
||||
|
||||
for (let i = 0; i < keyId.length; i++) {
|
||||
contentArray[i + 4] = keyId.charCodeAt(i)
|
||||
}
|
||||
|
||||
for (let i = 0; i < uid.length; i++) {
|
||||
contentArray[i + 44] = uid.charCodeAt(i)
|
||||
}
|
||||
|
||||
contentArray[64] = pwdNo / 256
|
||||
contentArray[65] = pwdNo % 256
|
||||
|
||||
contentArray[66] = operate
|
||||
contentArray[67] = isAdmin
|
||||
|
||||
for (let i = 0; i < keyboardPwd.length; i++) {
|
||||
contentArray[i + 68] = keyboardPwd.charCodeAt(i)
|
||||
}
|
||||
|
||||
contentArray[88] = userCountLimit / 256
|
||||
contentArray[89] = userCountLimit % 256
|
||||
|
||||
contentArray.set(this.lockInfo.token || new Uint8Array([0, 0, 0, 0]), 90)
|
||||
|
||||
contentArray.set(timestampToArray(startDate), 94)
|
||||
contentArray.set(timestampToArray(endDate), 98)
|
||||
|
||||
contentArray[102] = 16
|
||||
|
||||
const md5Array = md5Encrypt(
|
||||
keyId + uid,
|
||||
this.lockInfo.token || new Uint8Array([0, 0, 0, 0]),
|
||||
this.lockInfo.bluetooth.signKey
|
||||
)
|
||||
|
||||
contentArray.set(md5Array, 103)
|
||||
|
||||
const cebArray = sm4.encrypt(contentArray, this.lockInfo.bluetooth.privateKey, {
|
||||
mode: 'ecb',
|
||||
output: 'array'
|
||||
})
|
||||
|
||||
const packageArray = createPackageEnd(headArray, cebArray)
|
||||
|
||||
const writeResult = await writeBLECharacteristicValue(
|
||||
this.lockInfo.deviceId,
|
||||
this.lockInfo.serviceId,
|
||||
this.lockInfo.writeCharacteristicId,
|
||||
packageArray
|
||||
)
|
||||
|
||||
if (writeResult.code !== Result.Success.code) {
|
||||
return writeResult
|
||||
}
|
||||
|
||||
return this.getWriteResult(this.customPassword, params)
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改管理员密码
|
||||
* @param params
|
||||
* @param {AccountInfo} params.accountInfo 账号信息
|
||||
* @param {String} params.adminPwd 管理员密码
|
||||
* @param {Boolean} params.disconnect 操作后是否断开连接
|
||||
* @returns {Promise<Result>}
|
||||
*/
|
||||
export async function updateAdminPassword(params) {
|
||||
const { adminPwd, accountInfo } = params
|
||||
|
||||
// 设置执行账号
|
||||
const result = await this.login(accountInfo)
|
||||
if (result.code !== Result.Success.code) {
|
||||
return result
|
||||
}
|
||||
|
||||
// 确认设备连接正常
|
||||
if (!params.connected) {
|
||||
const searchResult = await searchAndConnectDevice(this.lockInfo.bluetooth.bluetoothDeviceName)
|
||||
if (searchResult.code !== Result.Success.code) {
|
||||
return searchResult
|
||||
}
|
||||
this.updateLockInfo(searchResult.data)
|
||||
}
|
||||
|
||||
// 检查是否已添加为用户
|
||||
const checkResult = await this.checkLockUser()
|
||||
if (checkResult.code !== Result.Success.code) {
|
||||
return checkResult
|
||||
}
|
||||
|
||||
this.requestParams = params
|
||||
|
||||
const uid = this.lockInfo.uid.toString()
|
||||
const keyId = this.lockInfo.keyId.toString()
|
||||
const pwdNo = 1
|
||||
const userCountLimit = 0xff
|
||||
const startDate = Math.floor(this.lockInfo.startDate / 1000)
|
||||
const endDate = Math.floor(this.lockInfo.endDate / 1000)
|
||||
|
||||
const length = 2 + 1 + 1 + 40 + 20 + 2 + 20 + 2 + 4 + 4 + 4 + 1 + 16
|
||||
const headArray = this.createPackageHeader(3, length)
|
||||
|
||||
const contentArray = new Uint8Array(length)
|
||||
contentArray[0] = cmdIds.expandCmd / 256
|
||||
contentArray[1] = cmdIds.expandCmd % 256
|
||||
|
||||
// 子命令
|
||||
contentArray[2] = subCmdIds.updateAdminPassword
|
||||
|
||||
contentArray[3] = length - 3
|
||||
|
||||
for (let i = 0; i < keyId.length; i++) {
|
||||
contentArray[i + 4] = keyId.charCodeAt(i)
|
||||
}
|
||||
|
||||
for (let i = 0; i < uid.length; i++) {
|
||||
contentArray[i + 44] = uid.charCodeAt(i)
|
||||
}
|
||||
|
||||
contentArray[64] = pwdNo / 256
|
||||
contentArray[65] = pwdNo % 256
|
||||
|
||||
for (let i = 0; i < adminPwd.length; i++) {
|
||||
contentArray[i + 66] = adminPwd.charCodeAt(i)
|
||||
}
|
||||
|
||||
contentArray[86] = userCountLimit / 256
|
||||
contentArray[87] = userCountLimit % 256
|
||||
|
||||
contentArray.set(this.lockInfo.token || new Uint8Array([0, 0, 0, 0]), 88)
|
||||
|
||||
contentArray.set(timestampToArray(startDate), 92)
|
||||
contentArray.set(timestampToArray(endDate), 96)
|
||||
|
||||
contentArray[100] = 16
|
||||
|
||||
const md5Array = md5Encrypt(
|
||||
keyId + uid,
|
||||
this.lockInfo.token || new Uint8Array([0, 0, 0, 0]),
|
||||
this.lockInfo.bluetooth.signKey
|
||||
)
|
||||
|
||||
contentArray.set(md5Array, 101)
|
||||
|
||||
const cebArray = sm4.encrypt(contentArray, this.lockInfo.bluetooth.privateKey, {
|
||||
mode: 'ecb',
|
||||
output: 'array'
|
||||
})
|
||||
|
||||
const packageArray = createPackageEnd(headArray, cebArray)
|
||||
|
||||
const writeResult = await writeBLECharacteristicValue(
|
||||
this.lockInfo.deviceId,
|
||||
this.lockInfo.serviceId,
|
||||
this.lockInfo.writeCharacteristicId,
|
||||
packageArray
|
||||
)
|
||||
|
||||
if (writeResult.code !== Result.Success.code) {
|
||||
return writeResult
|
||||
}
|
||||
|
||||
return this.getWriteResult(this.updateAdminPassword, params)
|
||||
}
|
||||
|
||||
// 检查密码名称与密码是否已存在
|
||||
export async function checkPassword(params) {
|
||||
return await checkPasswordRequest(params)
|
||||
}
|
||||
31
star-cloud/record.js
Normal file
31
star-cloud/record.js
Normal file
@ -0,0 +1,31 @@
|
||||
import { Result } from '../constant'
|
||||
|
||||
/**
|
||||
* 同步全部开门记录
|
||||
* @param params
|
||||
* @param {AccountInfo} params.accountInfo 账号信息
|
||||
* @param {Boolean} params.disconnect 操作后是否断开连接
|
||||
* @returns {Promise<Result>}
|
||||
*/
|
||||
export async function syncAllOpenRecord(params) {
|
||||
const { accountInfo, disconnect } = params
|
||||
|
||||
const { code, data, message } = await this.syncOpenRecord({
|
||||
accountInfo,
|
||||
disconnect: false
|
||||
})
|
||||
|
||||
if (code === Result.Success.code) {
|
||||
if (data.count === 10) {
|
||||
return await this.syncAllOpenRecord({
|
||||
accountInfo,
|
||||
disconnect
|
||||
})
|
||||
}
|
||||
if (disconnect) {
|
||||
await this.disconnectDevice()
|
||||
}
|
||||
return new Result(code, data, message)
|
||||
}
|
||||
return new Result(code, data, message)
|
||||
}
|
||||
30
star-cloud/user.js
Normal file
30
star-cloud/user.js
Normal file
@ -0,0 +1,30 @@
|
||||
import { removeStorage } from '../export'
|
||||
import { Result } from '../constant'
|
||||
import { starCloudCreateUser } from '../api'
|
||||
|
||||
/**
|
||||
* 注册星云
|
||||
* @returns {Promise<Result>}
|
||||
*/
|
||||
export async function register() {
|
||||
const { code, data, message } = await starCloudCreateUser({
|
||||
clientId: this.clientId,
|
||||
clientSecret: this.clientSecret
|
||||
})
|
||||
return new Result(code, data, message)
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
* @param params
|
||||
* @param {Number} params.uid 用户ID
|
||||
*/
|
||||
export function logout(params) {
|
||||
const { uid } = params
|
||||
if (this.accountInfo?.uid === uid) {
|
||||
this.userInfo = null
|
||||
this.accountInfo = null
|
||||
this.lockInfo = null
|
||||
removeStorage('starCloudToken')
|
||||
}
|
||||
}
|
||||
2107
starCloud.js
2107
starCloud.js
File diff suppressed because it is too large
Load Diff
18
storage.js
18
storage.js
@ -1,18 +0,0 @@
|
||||
import { useStarCloudStore } from '@/starCloud/starCloud'
|
||||
|
||||
export function setStorage(key, value) {
|
||||
return uni.setStorageSync(getPrefix() + key, value)
|
||||
}
|
||||
|
||||
export function getStorage(key) {
|
||||
return uni.getStorageSync(getPrefix() + key)
|
||||
}
|
||||
|
||||
export function removeStorage(key) {
|
||||
return uni.removeStorageSync(getPrefix() + key)
|
||||
}
|
||||
|
||||
function getPrefix() {
|
||||
const starCloud = useStarCloudStore()
|
||||
return `${starCloud.envVersion}:`
|
||||
}
|
||||
@ -1,155 +1,4 @@
|
||||
export class Result {
|
||||
static codes = {
|
||||
Success: 0,
|
||||
Fail: -1,
|
||||
|
||||
NotMoreData: -10,
|
||||
|
||||
NotAvailableBluetooth: -20,
|
||||
NotAvailableBluetoothPermission: -21,
|
||||
NotAvailableWeChatNearbyDevicesPermission: -22,
|
||||
NotAvailableWeChatLocationPermission: -23,
|
||||
NotAvailableWeChatNearbyDevicesEmpty: -24,
|
||||
NotAvailableWeChatBluetoothPermission: -25,
|
||||
DeviceHasBeenReset: -30,
|
||||
|
||||
NotRegisteredLock: 4,
|
||||
NotTokenLock: 6,
|
||||
NotMoreKeyLock: 12,
|
||||
ReadyHasKeyLock: 15,
|
||||
ReadyHasPassword: 251
|
||||
}
|
||||
|
||||
static resultsMap = new Map([
|
||||
[Result.codes.Success, { message: '成功', data: {} }],
|
||||
[Result.codes.Fail, { message: '失败', data: {} }],
|
||||
|
||||
[Result.codes.NotMoreData, { message: '没有更多数据', data: {} }],
|
||||
|
||||
[Result.codes.NotAvailableBluetooth, { message: '蓝牙尚未打开,请先打开蓝牙', data: {} }],
|
||||
[
|
||||
Result.codes.NotAvailableBluetoothPermission,
|
||||
{ message: '小程序蓝牙功能被禁用,请打开小程序蓝牙权限', data: {} }
|
||||
],
|
||||
[
|
||||
Result.codes.NotAvailableWeChatNearbyDevicesPermission,
|
||||
{ message: '蓝牙功能需要附近设备权限,请前往设置开启微信的附近设备权限后再试', data: {} }
|
||||
],
|
||||
[
|
||||
Result.codes.NotAvailableWeChatLocationPermission,
|
||||
{ message: '蓝牙功能需要定位权限,请前往设置开启微信的定位权限后再试', data: {} }
|
||||
],
|
||||
[
|
||||
Result.codes.NotAvailableWeChatNearbyDevicesEmpty,
|
||||
{
|
||||
message: '蓝牙功能需要定位服务,请前往设置开启定位服务后再试',
|
||||
data: {}
|
||||
}
|
||||
],
|
||||
[
|
||||
Result.codes.NotAvailableWeChatBluetoothPermission,
|
||||
{
|
||||
message: '微信的蓝牙权限被禁用,请前往设置开启微信的蓝牙权限后再试',
|
||||
data: {}
|
||||
}
|
||||
],
|
||||
[Result.codes.DeviceHasBeenReset, { message: '设备已被重置', data: {} }],
|
||||
|
||||
[Result.codes.NotRegisteredLock, { message: '用户在锁端未注册', data: {} }],
|
||||
[Result.codes.NotTokenLock, { message: '用户在锁端token失效', data: {} }],
|
||||
[Result.codes.NotMoreKeyLock, { message: '锁端钥匙数量已达上限', data: {} }],
|
||||
[Result.codes.ReadyHasKeyLock, { message: '用户已是锁端用户', data: {} }],
|
||||
[Result.codes.ReadyHasPassword, { message: '该密码已存在,请更换。', data: {} }]
|
||||
])
|
||||
|
||||
constructor(code, data, message) {
|
||||
const result = Result.resultsMap.get(code)
|
||||
if (result) {
|
||||
this.code = code
|
||||
this.message = message || result.message
|
||||
this.data = data || result.data
|
||||
} else {
|
||||
this.code = code
|
||||
this.message = message || ''
|
||||
this.data = data || {}
|
||||
}
|
||||
}
|
||||
|
||||
// 成功
|
||||
static get Success() {
|
||||
return new Result(Result.codes.Success)
|
||||
}
|
||||
|
||||
// 失败(默认错误)
|
||||
static get Fail() {
|
||||
return new Result(Result.codes.Fail)
|
||||
}
|
||||
|
||||
// 没有更多数据
|
||||
static get NotMoreData() {
|
||||
return new Result(Result.codes.NotMoreData)
|
||||
}
|
||||
|
||||
// 蓝牙未开启
|
||||
static get NotAvailableBluetooth() {
|
||||
return new Result(Result.codes.NotAvailableBluetooth)
|
||||
}
|
||||
|
||||
// 小程序蓝牙权限被禁用
|
||||
static get NotAvailableBluetoothPermission() {
|
||||
return new Result(Result.codes.NotAvailableBluetoothPermission)
|
||||
}
|
||||
|
||||
// 微信附近的设备权限被禁用
|
||||
static get NotAvailableWeChatNearbyDevicesPermission() {
|
||||
return new Result(Result.codes.NotAvailableWeChatNearbyDevicesPermission)
|
||||
}
|
||||
|
||||
// 微信定位权限被禁用
|
||||
static get NotAvailableWeChatLocationPermission() {
|
||||
return new Result(Result.codes.NotAvailableWeChatLocationPermission)
|
||||
}
|
||||
|
||||
// 手机定位服务被关闭
|
||||
static get NotAvailableWeChatNearbyDevicesEmpty() {
|
||||
return new Result(Result.codes.NotAvailableWeChatNearbyDevicesEmpty)
|
||||
}
|
||||
|
||||
// 微信的蓝牙权限被禁用
|
||||
static get NotAvailableWeChatBluetoothPermission() {
|
||||
return new Result(Result.codes.NotAvailableWeChatBluetoothPermission)
|
||||
}
|
||||
|
||||
// 设备已被重置
|
||||
static get DeviceHasBeenReset() {
|
||||
return new Result(Result.codes.DeviceHasBeenReset)
|
||||
}
|
||||
|
||||
// 用户在锁端未注册
|
||||
static get NotRegisteredLock() {
|
||||
return new Result(Result.codes.NotRegisteredLock)
|
||||
}
|
||||
|
||||
// 用户在锁端token失效
|
||||
static get NotTokenLock() {
|
||||
return new Result(Result.codes.NotTokenLock)
|
||||
}
|
||||
|
||||
// 锁端钥匙数量已达上限
|
||||
static get NotMoreKeyLock() {
|
||||
return new Result(Result.codes.NotMoreKeyLock)
|
||||
}
|
||||
|
||||
// 锁端钥匙数量已达上限
|
||||
static get ReadyHasKeyLock() {
|
||||
return new Result(Result.codes.ReadyHasKeyLock)
|
||||
}
|
||||
|
||||
// 密码已存在
|
||||
static get ReadyHasPassword() {
|
||||
return new Result(Result.codes.ReadyHasPassword)
|
||||
}
|
||||
}
|
||||
import { Result } from '../constant'
|
||||
|
||||
/**
|
||||
* @typedef {Object} err
|
||||
169
uni/index.js
Normal file
169
uni/index.js
Normal file
@ -0,0 +1,169 @@
|
||||
import starCloudInstance from '../star-cloud'
|
||||
|
||||
/**
|
||||
* 初始化星云
|
||||
* @param params
|
||||
* @param {String} params.clientId 客户端Id
|
||||
* @param {String} params.clientSecret 客户端密码
|
||||
* @param {String} params.env 环境
|
||||
* @param {Boolean} params.isReportLog 是否上报日志
|
||||
*/
|
||||
export const init = params => {
|
||||
starCloudInstance.init({
|
||||
...params,
|
||||
platform: 1
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册星云
|
||||
* @returns Result
|
||||
*/
|
||||
export const register = async () => {
|
||||
return await starCloudInstance.register()
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
* @param params
|
||||
* @param {Number} params.uid 用户ID
|
||||
*/
|
||||
export const logout = params => {
|
||||
starCloudInstance.logout(params)
|
||||
}
|
||||
|
||||
/**
|
||||
* 选择锁
|
||||
* @param params
|
||||
* @param {AccountInfo} params.accountInfo 账号信息
|
||||
* @param {Number} params.lockId 锁ID
|
||||
* @returns Result
|
||||
*/
|
||||
export const selectLock = async params => {
|
||||
return await starCloudInstance.selectLock(params)
|
||||
}
|
||||
|
||||
/**
|
||||
* 开门
|
||||
* @param params
|
||||
* @param {AccountInfo} params.accountInfo 账号信息
|
||||
* @param {String} params.type 开门方式 close: 关门 open: 开门
|
||||
* @param {Boolean} params.disconnect 操作后是否断开连接
|
||||
* @returns Result
|
||||
*/
|
||||
export const openDoor = async params => {
|
||||
return await starCloudInstance.openDoor(params)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取离线密码
|
||||
* @param params
|
||||
* @param {AccountInfo} params.accountInfo 账号信息
|
||||
* @param {OfflinePassword} params.password 密码信息
|
||||
* @returns Result
|
||||
*/
|
||||
export const getOfflinePassword = async params => {
|
||||
return await starCloudInstance.getOfflinePassword(params)
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义密码
|
||||
* @param params
|
||||
* @param {AccountInfo} params.accountInfo 账号信息
|
||||
* @param {CustomPassword} params.password 密码信息
|
||||
* @param {Boolean} params.disconnect 操作后是否断开连接
|
||||
* @returns Result
|
||||
*/
|
||||
export const customPassword = async params => {
|
||||
return await starCloudInstance.customPassword(params)
|
||||
}
|
||||
|
||||
/**
|
||||
* 搜索蓝牙设备
|
||||
* @callback {Function} callback 回调方法
|
||||
* @returns Result
|
||||
*/
|
||||
export const searchDevice = async callback => {
|
||||
return await starCloudInstance.searchDevice(callback)
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止搜索
|
||||
* @returns Result
|
||||
*/
|
||||
export const stopSearchDevice = async () => {
|
||||
return await starCloudInstance.stopSearchDevice()
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定设备
|
||||
* @param params
|
||||
* @param {AccountInfo} params.accountInfo 账号信息
|
||||
* @param {String} params.name 设备名称
|
||||
* @returns Result
|
||||
*/
|
||||
export const bindDevice = async params => {
|
||||
return await starCloudInstance.bindDevice(params)
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除坏锁
|
||||
* @param params
|
||||
* @param {AccountInfo} params.accountInfo 账号信息
|
||||
* @param {List[int]} params.lockIds 锁Id列表
|
||||
* @returns Result
|
||||
*/
|
||||
export const removeBadLock = async params => {
|
||||
return await starCloudInstance.removeBadLock(params)
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除锁
|
||||
* @param params
|
||||
* @param {AccountInfo} params.accountInfo 账号信息
|
||||
*/
|
||||
export const deleteLock = async params => {
|
||||
return await starCloudInstance.deleteLock(params)
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改管理员密码
|
||||
* @param params
|
||||
* @param {AccountInfo} params.accountInfo 账号信息
|
||||
* @param {String} params.adminPwd 管理员密码
|
||||
* @param {Boolean} params.disconnect 操作后是否断开连接
|
||||
* @returns Result
|
||||
*/
|
||||
export const updateAdminPassword = async params => {
|
||||
return await starCloudInstance.updateAdminPassword(params)
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步开门记录
|
||||
* @param params
|
||||
* @param {AccountInfo} params.accountInfo 账号信息
|
||||
* @param {Boolean} params.disconnect 操作后是否断开连接
|
||||
* @returns Result
|
||||
*/
|
||||
export const syncOpenDoorRecord = async params => {
|
||||
return await starCloudInstance.syncAllOpenRecord(params)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取服务器时间
|
||||
* @returns Result
|
||||
*/
|
||||
export const getServerTime = async () => {
|
||||
return await starCloudInstance.getServerTimestamp()
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取锁支持项
|
||||
* @param params
|
||||
* @param {AccountInfo} params.accountInfo 账号信息
|
||||
* @param {Number} params.lockId 锁 Id
|
||||
* @returns Result
|
||||
*/
|
||||
export const getLockSupportFeatures = async params => {
|
||||
return await starCloudInstance.getLockSupportFeatures(params)
|
||||
}
|
||||
@ -1,49 +1,43 @@
|
||||
import { useStarCloudStore } from '@/starCloud/starCloud'
|
||||
import starCloudInstance from '../star-cloud'
|
||||
|
||||
const log = wx.getRealtimeLogManager ? wx.getRealtimeLogManager() : null
|
||||
|
||||
export default {
|
||||
debug() {
|
||||
if (!log) return
|
||||
const $starCloud = useStarCloudStore()
|
||||
if (!$starCloud.isReportLog) return
|
||||
if (!starCloudInstance.isReportLog) return
|
||||
const logger = log.tag(arguments[0].name)
|
||||
logger.info(arguments[0].message, arguments[0])
|
||||
},
|
||||
info() {
|
||||
if (!log) return
|
||||
const $starCloud = useStarCloudStore()
|
||||
if (!$starCloud.isReportLog) return
|
||||
if (!starCloudInstance.isReportLog) return
|
||||
const logger = log.tag(arguments[0].name)
|
||||
logger.info(arguments[0].message, arguments[0])
|
||||
},
|
||||
warn() {
|
||||
if (!log) return
|
||||
const $starCloud = useStarCloudStore()
|
||||
if (!$starCloud.isReportLog) return
|
||||
if (!starCloudInstance.isReportLog) return
|
||||
const logger = log.tag(arguments[0].name)
|
||||
logger.warn(arguments[0].message, arguments[0])
|
||||
},
|
||||
error() {
|
||||
if (!log) return
|
||||
const $starCloud = useStarCloudStore()
|
||||
if (!$starCloud.isReportLog) return
|
||||
if (!starCloudInstance.isReportLog) return
|
||||
const logger = log.tag(arguments[0].name)
|
||||
logger.error(arguments[0].message, arguments[0])
|
||||
},
|
||||
setFilterMsg(msg) {
|
||||
if (!log || !log.setFilterMsg) return
|
||||
if (typeof msg !== 'string') return
|
||||
const $starCloud = useStarCloudStore()
|
||||
if (!$starCloud.isReportLog) return
|
||||
if (!starCloudInstance.isReportLog) return
|
||||
const logger = log.tag(arguments[0].name)
|
||||
logger.setFilterMsg(JSON.stringify(arguments[0]))
|
||||
},
|
||||
addFilterMsg(msg) {
|
||||
if (!log || !log.addFilterMsg) return
|
||||
if (typeof msg !== 'string') return
|
||||
const $starCloud = useStarCloudStore()
|
||||
if (!$starCloud.isReportLog) return
|
||||
if (!starCloudInstance.isReportLog) return
|
||||
const logger = log.tag(arguments[0].name)
|
||||
logger.addFilterMsg(JSON.stringify(arguments[0]))
|
||||
}
|
||||
@ -1,6 +1,6 @@
|
||||
import { getStorage, removeStorage } from '@/starCloud/storage'
|
||||
import { useStarCloudStore } from '@/starCloud/starCloud'
|
||||
import { Result } from '@/starCloud/basic'
|
||||
import { getStorage, removeStorage } from '../export'
|
||||
import starCloudInstance from '../star-cloud'
|
||||
import { Result } from '../constant'
|
||||
|
||||
/*
|
||||
* config
|
||||
@ -13,7 +13,7 @@ import { Result } from '@/starCloud/basic'
|
||||
* */
|
||||
|
||||
const request = config => {
|
||||
const starCloud = useStarCloudStore()
|
||||
const starCloud = starCloudInstance
|
||||
let timer
|
||||
return new Promise(async resolve => {
|
||||
const baseConfig = starCloud.getConfig()
|
||||
18
uni/storage.js
Normal file
18
uni/storage.js
Normal file
@ -0,0 +1,18 @@
|
||||
import starCloudInstance from '../star-cloud'
|
||||
|
||||
export function setStorageUni(key, value) {
|
||||
return uni.setStorageSync(getPrefix() + key, value)
|
||||
}
|
||||
|
||||
export function getStorageUni(key) {
|
||||
return uni.getStorageSync(getPrefix() + key)
|
||||
}
|
||||
|
||||
export function removeStorageUni(key) {
|
||||
return uni.removeStorageSync(getPrefix() + key)
|
||||
}
|
||||
|
||||
function getPrefix() {
|
||||
const starCloud = starCloudInstance
|
||||
return starCloud?.envVersion ? `${starCloud?.envVersion}:` : ''
|
||||
}
|
||||
74
web/index.js
Normal file
74
web/index.js
Normal file
@ -0,0 +1,74 @@
|
||||
import starCloudInstance from '../star-cloud'
|
||||
|
||||
/**
|
||||
* 初始化星云
|
||||
* @param params
|
||||
* @param {String} params.clientId 客户端Id
|
||||
* @param {String} params.clientSecret 客户端密码
|
||||
* @param {String} params.env 环境
|
||||
*/
|
||||
export const init = params => {
|
||||
starCloudInstance.init({
|
||||
...params,
|
||||
isReportLog: false,
|
||||
platform: 2
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册星云
|
||||
* @returns Result
|
||||
*/
|
||||
export const register = async () => {
|
||||
return await starCloudInstance.register()
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
* @param params
|
||||
* @param {Number} params.uid 用户ID
|
||||
*/
|
||||
export const logout = params => {
|
||||
starCloudInstance.logout(params)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取离线密码
|
||||
* @param params
|
||||
* @param {AccountInfo} params.accountInfo 账号信息
|
||||
* @param {OfflinePassword} params.password 密码信息
|
||||
* @returns Result
|
||||
*/
|
||||
export const getOfflinePassword = async params => {
|
||||
return await starCloudInstance.getOfflinePassword(params)
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除坏锁
|
||||
* @param params
|
||||
* @param {AccountInfo} params.accountInfo 账号信息
|
||||
* @param {List[int]} params.lockIds 锁Id列表
|
||||
* @returns Result
|
||||
*/
|
||||
export const removeBadLock = async params => {
|
||||
return await starCloudInstance.removeBadLock(params)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取服务器时间
|
||||
* @returns Result
|
||||
*/
|
||||
export const getServerTime = async () => {
|
||||
return await starCloudInstance.getServerTimestamp()
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取锁支持项
|
||||
* @param params
|
||||
* @param {AccountInfo} params.accountInfo 账号信息
|
||||
* @param {Number} params.lockId 锁 Id
|
||||
* @returns Result
|
||||
*/
|
||||
export const getLockSupportFeatures = async params => {
|
||||
return await starCloudInstance.getLockSupportFeatures(params)
|
||||
}
|
||||
107
web/request.js
Normal file
107
web/request.js
Normal file
@ -0,0 +1,107 @@
|
||||
import { getStorage, removeStorage } from '@/starCloud/uniapp/storage'
|
||||
import starCloudInstance from '@/starCloud/star-cloud'
|
||||
import { Result } from '@/constants/result'
|
||||
|
||||
/*
|
||||
* config
|
||||
* baseUrl: 请求域名
|
||||
* url: 请求路径
|
||||
* method: 请求方法
|
||||
* header: 请求头
|
||||
* token: 请求token
|
||||
* data: 请求参数
|
||||
* */
|
||||
|
||||
const request = config => {
|
||||
const starCloud = starCloudInstance
|
||||
let timer
|
||||
let res = null // 在外部定义res,避免finally块报错
|
||||
return new Promise(async resolve => {
|
||||
const baseConfig = starCloud.getConfig()
|
||||
|
||||
const token = config?.token ? config.token : getStorage('starCloudToken')
|
||||
// 请求地址
|
||||
const URL = config.baseUrl ? config.baseUrl + config.url : baseConfig.baseUrl + config.url
|
||||
|
||||
// 默认请求头
|
||||
const headerDefault = {
|
||||
version: baseConfig.version + '+' + baseConfig.buildNumber
|
||||
}
|
||||
const header = {
|
||||
...headerDefault,
|
||||
...config.header
|
||||
}
|
||||
const method = config.method || 'POST'
|
||||
const data = {
|
||||
...config.data,
|
||||
accessToken: token,
|
||||
clientId: starCloud.clientId
|
||||
}
|
||||
const timestamp = new Date().getTime()
|
||||
|
||||
// 超时处理
|
||||
timer = setTimeout(() => {
|
||||
resolve(new Result(Result.Fail.code, {}, '网络访问失败,请检查网络是否正常'))
|
||||
}, 3200)
|
||||
|
||||
try {
|
||||
const response = await fetch(URL, {
|
||||
method,
|
||||
headers: {
|
||||
...header,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
})
|
||||
|
||||
res = await response.json() // 在这里将res赋值
|
||||
if (timer) {
|
||||
clearTimeout(timer)
|
||||
}
|
||||
|
||||
if (response.ok) {
|
||||
const { errcode, errmsg, data } = res
|
||||
if (errcode === 10003) {
|
||||
removeStorage('starCloudToken')
|
||||
removeStorage('starCloudUser')
|
||||
const { code } = await starCloud.login({
|
||||
username: starCloud.starCloudAccountInfo.username,
|
||||
password: starCloud.starCloudAccountInfo.password,
|
||||
uid: starCloud.starCloudAccountInfo.uid
|
||||
})
|
||||
if (code === Result.Success.code) {
|
||||
resolve(await request(config))
|
||||
}
|
||||
} else {
|
||||
resolve({
|
||||
code: errcode,
|
||||
data,
|
||||
message: errmsg
|
||||
})
|
||||
}
|
||||
} else {
|
||||
resolve(new Result(Result.Fail.code, {}, '网络访问失败,请检查网络是否正常'))
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('网络访问失败', error)
|
||||
if (timer) {
|
||||
clearTimeout(timer)
|
||||
}
|
||||
resolve(new Result(Result.Fail.code, {}, '网络访问失败,请检查网络是否正常'))
|
||||
} finally {
|
||||
// 访问res时确保它已定义
|
||||
console.log(URL.substring(baseConfig.baseUrl.length + 1), {
|
||||
env: baseConfig.name,
|
||||
url: URL.substring(baseConfig.baseUrl.length + 1),
|
||||
req: config?.data || {},
|
||||
code: res?.errcode || null, // 如果res未定义,fallback为null
|
||||
res: res?.data || null, // 如果res未定义,fallback为null
|
||||
token: header?.authorization || '',
|
||||
message: res?.errmsg || '', // 如果res未定义,fallback为空字符串
|
||||
duration: new Date().getTime() - timestamp
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export default request
|
||||
19
web/storage.js
Normal file
19
web/storage.js
Normal file
@ -0,0 +1,19 @@
|
||||
import starCloudInstance from '../star-cloud'
|
||||
|
||||
export function setStorageWeb(key, value) {
|
||||
return localStorage.setItem(getPrefix() + key, JSON.stringify(value))
|
||||
}
|
||||
|
||||
export function getStorageWeb(key) {
|
||||
const value = localStorage.getItem(getPrefix() + key)
|
||||
return value ? JSON.parse(value) : null
|
||||
}
|
||||
|
||||
export function removeStorageWeb(key) {
|
||||
return localStorage.removeItem(getPrefix() + key)
|
||||
}
|
||||
|
||||
function getPrefix() {
|
||||
const starCloud = starCloudInstance
|
||||
return starCloud?.envVersion ? `${starCloud?.envVersion}:` : ''
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user