starcloud-sdk-uniapp/star-cloud.js

215 lines
6.7 KiB
JavaScript
Raw Permalink Normal View History

2024-12-19 16:01:45 +08:00
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'
2024-12-31 11:12:02 +08:00
import * as elec from './star-cloud/elec.js'
import * as coldWater from './star-cloud/coldWater.js'
import * as hotWater from './star-cloud/hotWater.js'
import * as generalExtend from './star-cloud/generalExtend.js'
2025-06-11 17:29:21 +08:00
import * as configureNetwork from './star-cloud/configureNetwork.js'
2024-12-19 16:01:45 +08:00
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 {
2024-12-20 17:36:14 +08:00
constructor() {
2024-12-19 16:01:45 +08:00
// 环境
this.env = null
2024-12-26 15:04:30 +08:00
//sdk服务器地址
2024-12-26 15:34:42 +08:00
this.clientUrl = ''
2024-12-19 16:01:45 +08:00
// 客户端Id
this.clientId = null
// 客户端密码
this.clientSecret = null
// 小程序环境
this.envVersion = ''
// 锁信息
this.lockInfo = null
// 服务器时间
this.serverTimestamp = 0
// 时间差
this.timeDifference = 0
// 搜索设备列表
this.searchDeviceList = []
// 星云用户信息
this.userInfo = null
2024-12-20 17:36:14 +08:00
// 账号列表
this.accounts = null
2024-12-19 16:01:45 +08:00
// 账户信息
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.loadFromStorage()
}
static saveFieldToStorage(key, value) {
setStorage(`StarCloud_${key}`, value)
}
/**
* 初始化星云
2024-12-20 17:36:14 +08:00
* @param {Object} params
2024-12-19 16:01:45 +08:00
* @param {String} params.clientId 客户端Id
* @param {String} params.clientSecret 客户端密码
* @param {String} params.env 环境
2024-12-20 17:36:14 +08:00
* @param {Array<AccountInfo>} params.accounts 账号列表后续方法中需传入uid若未传入则默认使用第一个账号
2024-12-19 16:01:45 +08:00
* @param {Number} params.platform 平台 1:uni 2:web 默认为1
* @param {Boolean} params.isReportLog 是否上报日志
*/
init(params) {
2025-06-11 17:29:21 +08:00
Object.assign(StarCloud.prototype, device, lock, other, password, record, user, common, elec,coldWater, hotWater,generalExtend,configureNetwork)
2024-12-19 16:01:45 +08:00
2024-12-26 15:34:42 +08:00
const { clientId, clientSecret, env, platform, accounts,clientUrl } = params
2024-12-19 16:01:45 +08:00
this.envVersion = 'release'
if (platform !== 2) {
const appInfo = uni.getAccountInfoSync()
this.envVersion = appInfo.miniProgram.envVersion
// 监听特性值变化
onBLECharacteristicValueChange(this.listenCharacteristicValue.bind(this))
}
2024-12-26 15:34:42 +08:00
this.clientUrl = clientUrl
2024-12-19 16:01:45 +08:00
this.isReportLog = params.isReportLog
this.env = env || 'XHJ'
this.clientId = clientId
this.clientSecret = clientSecret
this.platform = platform || 1
2024-12-20 17:36:14 +08:00
this.accounts = accounts
2024-12-19 16:01:45 +08:00
2024-12-20 17:36:14 +08:00
this.accountSave(accounts)
2024-12-19 16:01:45 +08:00
}
loadFromStorage() {
Object.keys(this).forEach(key => {
const value = getStorage(`StarCloud_${key}`)
if (value !== undefined) {
this[key] = value
}
})
}
2024-12-20 17:36:14 +08:00
/**
* 账号保存
* @param {Array<AccountInfo>} accounts
*/
// eslint-disable-next-line class-methods-use-this
accountSave(accounts) {
let starCloudAccount = getStorage('starCloudAccount')
if (!starCloudAccount) {
starCloudAccount = {}
}
accounts.forEach(account => {
2024-12-31 15:15:35 +08:00
const accountInfo = starCloudAccount[account.uid]
2024-12-20 17:36:14 +08:00
if (accountInfo) {
starCloudAccount[account.uid] = {
...accountInfo,
...account
}
} else {
starCloudAccount[account.uid] = account
}
})
setStorage('starCloudAccount', starCloudAccount)
}
2024-12-19 16:01:45 +08:00
}
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