fix: 星云SDK支持纯web

This commit is contained in:
范鹏 2024-12-20 17:36:14 +08:00
parent 8702c936a5
commit 50954e4158
14 changed files with 415 additions and 144 deletions

View File

@ -34,15 +34,13 @@ import log from './log'
/**
* 同步开门记录
* @param params
* @param {AccountInfo} params.accountInfo 账号信息
* @param {Boolean} params.disconnect 操作后是否断开连接
* @param {Number} [params.uid] 用户ID
* @param {Boolean} [params.disconnect] 操作后是否断开连接, 默认断开
* @returns {Promise<Result>}
*/
export async function syncOpenRecord(params) {
const { accountInfo } = params
// 设置执行账号
const result = await this.login(accountInfo)
const result = await this.login(params.uid)
if (result.code !== Result.Success.code) {
return result
}
@ -228,10 +226,10 @@ export async function cleanLockUser() {
/**
* 登录星云
* @param {Object} accountInfo
* @returns {Promise<Result>}
* @param {Number} [uid] 用户ID
* @returns Result
*/
export async function login(accountInfo) {
export async function login(uid) {
let accounts = getStorage('starCloudAccount')
let userInfos = getStorage('starCloudUser')
if (!accounts) {
@ -241,50 +239,52 @@ export async function login(accountInfo) {
userInfos = {}
}
this.accountInfo = accounts[accountInfo.uid]
if (this.accountInfo && this.accountInfo.token) {
this.userInfo = userInfos[accountInfo.uid]
const id = uid || this.accounts[0].uid
this.accountInfo = accounts[id]
setStorage('starCloudToken', this.accountInfo.token)
if (this.accountInfo) {
if (this.accountInfo.token) {
this.userInfo = userInfos[id]
return Result.Success
}
setStorage('starCloudToken', this.accountInfo.token)
const {
code,
data: userInfo,
message
} = await getStarCloudToken({
username: accountInfo.username,
password: accountInfo.password,
clientId: this.clientId,
clientSecret: this.clientSecret
})
if (code === Result.Success.code) {
this.userInfo = userInfo
this.accountInfo = {
username: accountInfo.username,
password: accountInfo.password,
token: userInfo.access_token,
uid: userInfo.uid
return Result.Success
}
setStorage('starCloudToken', userInfo.access_token)
const {
code,
data: userInfo,
message
} = await getStarCloudToken({
username: this.accountInfo.username,
password: this.accountInfo.password,
clientId: this.clientId,
clientSecret: this.clientSecret
})
if (code === Result.Success.code) {
this.userInfo = userInfo
accounts[userInfo.uid] = {
uid: userInfo.uid,
username: accountInfo.username,
password: accountInfo.password,
token: userInfo.access_token
this.accountInfo = {
username: this.accountInfo.username,
password: this.accountInfo.password,
token: userInfo.access_token,
uid: userInfo.uid
}
setStorage('starCloudToken', userInfo.access_token)
accounts[userInfo.uid] = {
uid: userInfo.uid,
username: this.accountInfo.username,
password: this.accountInfo.password,
token: userInfo.access_token
}
setStorage('starCloudAccount', accounts)
userInfos[userInfo.uid] = userInfo
setStorage('starCloudUser', userInfos)
}
setStorage('starCloudAccount', accounts)
userInfos[userInfo.uid] = userInfo
setStorage('starCloudUser', userInfo)
// 获取服务器时间
this.getServerTimestamp().then(() => {})
return new Result(code, {}, message)
}
return new Result(code, {}, message)
return Result.Fail
}
// 获取公钥
@ -565,18 +565,18 @@ export function getWriteResult(request, params) {
),
name: 'openDoor'
})
resolve(await request({ ...params, connected: true }).bind(this))
resolve(await request.call(this, { ...params, connected: true }))
} else if (data.code === Result.NotRegisteredLock.code) {
const checkResult = await this.checkLockUser(true)
if (checkResult.code === Result.Success.code) {
resolve(await request({ ...params, connected: true }).bind(this))
resolve(await request.call(this, { ...params, connected: true }))
} else {
clearTimeout(getWriteResultTimer)
resolve(checkResult)
}
} else {
clearTimeout(getWriteResultTimer)
if (params.disconnect) {
if (params.disconnect !== false) {
await this.disconnectDevice()
}
console.log('写入结果', data, request, params)

View File

@ -6,27 +6,28 @@ import starCloudInstance from './star-cloud'
export const setStorage = (key, value) => {
if (starCloudInstance.platform === 1) {
setStorageUni(key, value)
} else if (starCloudInstance.platform === 2) {
setStorageWeb(key, value)
return setStorageUni(key, value)
}
if (starCloudInstance.platform === 2) {
return setStorageWeb(key, value)
}
}
export const getStorage = key => {
if (starCloudInstance.platform === 1) {
getStorageUni(key)
return getStorageUni(key)
}
if (starCloudInstance.platform === 2) {
getStorageWeb(key)
return getStorageWeb(key)
}
}
export const removeStorage = key => {
if (starCloudInstance.platform === 1) {
removeStorageUni(key)
return removeStorageUni(key)
}
if (starCloudInstance.platform === 2) {
removeStorageWeb(key)
return removeStorageWeb(key)
}
}

101
obfuscate-uni.js Normal file
View File

@ -0,0 +1,101 @@
const JavaScriptObfuscator = require('javascript-obfuscator')
const fs = require('fs')
const path = require('path')
const ignore = require('ignore')
const sourceDir = __dirname
const distDir = path.join(__dirname, 'dist')
const gitignorePath = path.join(__dirname, '.gitignore')
// 定义需要排除的文件和文件夹
const excludedFilesAndDirs = [distDir, 'node_modules']
// 将 .gitignore 路径转换为相对路径
const relativeGitignorePath = path.relative(sourceDir, gitignorePath)
// 读取 .gitignore 文件并解析
const ig = ignore().add(fs.readFileSync(gitignorePath, 'utf-8'))
// 创建目录
const createDir = dir => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true })
}
}
// 处理文件
const processFile = (filePath, targetPath) => {
const code = fs.readFileSync(filePath, 'utf-8')
if (!code.trim()) {
console.log(`跳过空文件: ${filePath}`)
return
}
try {
const obfuscatedCode = JavaScriptObfuscator.obfuscate(code, {
compact: true,
controlFlowFlattening: true,
deadCodeInjection: true,
stringArray: true,
stringArrayEncoding: ['base64'],
stringArrayThreshold: 0.75
}).getObfuscatedCode()
fs.writeFileSync(targetPath, obfuscatedCode)
console.log(`混淆完成: ${filePath}`)
} catch (error) {
console.error(`混淆失败: ${filePath}`)
console.error(error.message)
}
}
// 遍历并处理文件
const traverseAndProcess = (currentDir, targetDir) => {
createDir(targetDir)
fs.readdirSync(currentDir).forEach(file => {
const sourcePath = path.join(currentDir, file)
const targetPath = path.join(targetDir, file)
const stats = fs.statSync(sourcePath)
// 如果是 node_modules 文件夹或者被 .gitignore 忽略的文件或文件夹,跳过
if (
sourcePath.includes('node_modules') || // 排除 node_modules 文件夹
ig.ignores(path.relative(sourceDir, sourcePath)) || // 过滤 .gitignore 忽略的文件
excludedFilesAndDirs.some(excludedPath => sourcePath.startsWith(excludedPath)) // 排除其他指定路径
) {
console.log(`跳过文件或文件夹: ${sourcePath}`)
return
}
// 针对 web/index.js 文件,不进行混淆,直接复制到 dist 目录
if (sourcePath === path.join(__dirname, 'uni', 'index.js')) {
console.log(`复制入口文件: ${sourcePath}`)
fs.copyFileSync(sourcePath, targetPath)
return
}
// 对其他 JS 文件进行混淆
if (stats.isDirectory()) {
traverseAndProcess(sourcePath, targetPath)
} else if (sourcePath.endsWith('.js')) {
processFile(sourcePath, targetPath)
} else if (sourcePath.endsWith('.json')) {
console.log(`跳过 JSON 文件: ${sourcePath}`)
fs.copyFileSync(sourcePath, targetPath)
} else {
console.log(`跳过非 JS 文件: ${sourcePath}`)
}
})
}
// 清理已存在的 dist 目录
if (fs.existsSync(distDir)) {
fs.rmSync(distDir, { recursive: true, force: true })
}
createDir(distDir)
traverseAndProcess(sourceDir, distDir)
console.log('混淆完成,输出目录为 dist')

View File

@ -3,9 +3,9 @@
"version": "1.0.2",
"main": "./web/index.js",
"author": "zzc059",
"license": "ISC",
"scripts": {
"build": "node obfuscate.js"
"web-build": "node obfuscate-web.js",
"uni-build": "node obfuscate-uni.js"
},
"dependencies": {
"buffer": "^6.0.3",

View File

@ -77,7 +77,7 @@ import { onBLECharacteristicValueChange } from './uni/basic'
*/
class StarCloud {
constructor(methodModules) {
constructor() {
// 环境
this.env = null
// 客户端Id
@ -96,6 +96,8 @@ class StarCloud {
this.searchDeviceList = []
// 星云用户信息
this.userInfo = null
// 账号列表
this.accounts = null
// 账户信息
this.accountInfo = null
// 消息序号
@ -115,7 +117,6 @@ class StarCloud {
// 平台
this.platform = 1
this.loadMethods(methodModules)
this.loadFromStorage()
}
@ -125,17 +126,18 @@ class StarCloud {
/**
* 初始化星云
* @param params
* @param {Object} params
* @param {String} params.clientId 客户端Id
* @param {String} params.clientSecret 客户端密码
* @param {String} params.env 环境
* @param {Array<AccountInfo>} params.accounts 账号列表后续方法中需传入uid若未传入则默认使用第一个账号
* @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
const { clientId, clientSecret, env, platform, accounts } = params
this.envVersion = 'release'
if (platform !== 2) {
const appInfo = uni.getAccountInfoSync()
@ -151,16 +153,9 @@ class StarCloud {
this.clientId = clientId
this.clientSecret = clientSecret
this.platform = platform || 1
}
this.accounts = accounts
loadMethods(methodModules) {
methodModules.forEach(methods => {
Object.entries(methods).forEach(([key, method]) => {
if (typeof method === 'function') {
this[key] = method.bind(this)
}
})
})
this.accountSave(accounts)
}
loadFromStorage() {
@ -171,6 +166,30 @@ class StarCloud {
}
})
}
/**
* 账号保存
* @param {Array<AccountInfo>} accounts
*/
// eslint-disable-next-line class-methods-use-this
accountSave(accounts) {
let starCloudAccount = getStorage('starCloudAccount')
if (!starCloudAccount) {
starCloudAccount = {}
}
accounts.forEach(account => {
const accountInfo = accounts[account.uid]
if (accountInfo) {
starCloudAccount[account.uid] = {
...accountInfo,
...account
}
} else {
starCloudAccount[account.uid] = account
}
})
setStorage('starCloudAccount', starCloudAccount)
}
}
const starCloudInstance = new Proxy(StarCloud.prototype, {

View File

@ -67,14 +67,14 @@ export async function stopSearchDevice() {
/**
* 绑定设备
* @param params
* @param {AccountInfo} params.accountInfo 账号信息
* @param {Number} [params.uid] 用户ID
* @param {String} params.name 设备名称
* @returns {Promise<Result>}
*/
export async function bindDevice(params) {
const { accountInfo, name } = params
const { name } = params
// 设置执行账号
const result = await this.login(accountInfo)
const result = await this.login(params.uid)
if (result.code !== Result.Success.code) {
return result
}

View File

@ -9,13 +9,13 @@ import log from '../log'
/**
* 选择锁
* @param params
* @param {AccountInfo} params.accountInfo 账号信息
* @param {Number} [params.uid] 用户ID
* @param {Number} params.lockId 锁ID
* @returns {Promise<Result>}
*/
export async function selectLock(params) {
const { accountInfo, lockId } = params
const result = await this.login(accountInfo)
const { lockId } = params
const result = await this.login(params.uid)
if (result.code !== Result.Success.code) {
return result
}
@ -29,25 +29,25 @@ export async function selectLock(params) {
if (!lockList) {
lockList = {}
}
if (lockList[accountInfo.uid]) {
const index = lockList[accountInfo.uid].findIndex(item => item.lockId === lockId)
if (lockList[this.accountInfo.uid]) {
const index = lockList[this.accountInfo.uid].findIndex(item => item.lockId === lockId)
if (index === -1) {
lockList[accountInfo.uid].push(this.lockInfo)
lockList[this.accountInfo.uid].push(this.lockInfo)
} else {
this.lockInfo.token = lockList[accountInfo.uid][index].token
lockList[accountInfo.uid][index] = this.lockInfo
this.lockInfo.token = lockList[this.accountInfo.uid][index].token
lockList[this.accountInfo.uid][index] = this.lockInfo
}
setStorage('starLockList', lockList)
} else {
lockList[accountInfo.uid] = [this.lockInfo]
lockList[this.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 (lockList[this.accountInfo.uid]) {
const index = lockList[this.accountInfo.uid].findIndex(item => item.lockId === lockId)
if (index !== -1) {
this.lockInfo = lockList[accountInfo.uid][index]
this.lockInfo = lockList[this.accountInfo.uid][index]
return new Result(Result.Success.code, this.lockInfo)
}
}
@ -58,21 +58,20 @@ export async function selectLock(params) {
/**
* 开门
* @param params
* @param {AccountInfo} params.accountInfo 账号信息
* @param {Number} [params.uid] 用户ID
* @param {String} params.type 开门方式 close: 关门 open: 开门
* @param {Boolean} params.disconnect 操作后是否断开连接
* @param {Boolean} [params.disconnect] 操作后是否断开连接默认断开
* @returns {Promise<Result>}
*/
export async function openDoor(params) {
const { accountInfo, type } = params
const { type } = params
log.info({
...new Result(
Result.Success.code,
{
lockName: this.lockInfo.bluetooth.bluetoothDeviceName,
lockId: this.lockInfo.lockId,
uid: accountInfo.uid,
uid: params.uid,
time: new Date().getTime()
},
`开始开门`
@ -81,7 +80,7 @@ export async function openDoor(params) {
})
// 设置执行账号
const result = await this.login(accountInfo)
const result = await this.login(params.uid)
if (result.code !== Result.Success.code) {
return result
}
@ -92,7 +91,7 @@ export async function openDoor(params) {
{
lockName: this.lockInfo.bluetooth.bluetoothDeviceName,
lockId: this.lockInfo.lockId,
uid: accountInfo.uid,
uid: this.accountInfo.uid,
time: new Date().getTime()
},
`登录星云账号: ${result.message}`
@ -109,7 +108,7 @@ export async function openDoor(params) {
{
lockName: this.lockInfo.bluetooth.bluetoothDeviceName,
lockId: this.lockInfo.lockId,
uid: accountInfo.uid,
uid: this.accountInfo.uid,
time: new Date().getTime()
},
`连接设备: ${searchResult.message}`
@ -134,7 +133,7 @@ export async function openDoor(params) {
{
lockName: this.lockInfo.bluetooth.bluetoothDeviceName,
lockId: this.lockInfo.lockId,
uid: accountInfo.uid,
uid: this.accountInfo.uid,
time: new Date().getTime()
},
`确认是否为锁用户: ${checkResult.message}`
@ -159,7 +158,7 @@ export async function openDoor(params) {
{
lockName: this.lockInfo.bluetooth.bluetoothDeviceName,
lockId: this.lockInfo.lockId,
uid: accountInfo.uid,
uid: this.accountInfo.uid,
time: new Date().getTime()
},
`判断是否需要联网token: ${this.lockInfo.lockSetting.appUnlockOnline}`
@ -229,7 +228,7 @@ export async function openDoor(params) {
{
lockName: this.lockInfo.bluetooth.bluetoothDeviceName,
lockId: this.lockInfo.lockId,
uid: accountInfo.uid,
uid: this.accountInfo.uid,
time: new Date().getTime()
},
`开始写入`
@ -254,7 +253,7 @@ export async function openDoor(params) {
{
lockName: this.lockInfo.bluetooth.bluetoothDeviceName,
lockId: this.lockInfo.lockId,
uid: accountInfo.uid,
uid: this.accountInfo.uid,
time: new Date().getTime()
},
`写入完成:${writeResult.message}`
@ -268,12 +267,11 @@ export async function openDoor(params) {
/**
* 删除锁
* @param params
* @param {AccountInfo} params.accountInfo 账号信息
* @param {Number} [params.uid] 用户ID
*/
export async function deleteLock(params) {
const { accountInfo } = params
// 设置执行账号
const result = await this.login(accountInfo)
const result = await this.login(params.uid)
if (result.code !== Result.Success.code) {
return result
}
@ -344,15 +342,15 @@ export async function deleteLock(params) {
/**
* 获取锁支持项
* @param params
* @param {AccountInfo} params.accountInfo 账号信息
* @param {Number} [params.uid] 用户ID
* @param {Number} params.lockId Id
* @returns {Promise<Result>}
*/
export async function getLockSupportFeatures(params) {
const { accountInfo, lockId } = params
const { lockId } = params
// 设置执行账号
const result = await this.login(accountInfo)
const result = await this.login(params.uid)
if (result.code !== Result.Success.code) {
return result
}

View File

@ -5,14 +5,14 @@ import { getStorage, setStorage } from '../export'
/**
* 移除坏锁
* @param params
* @param {AccountInfo} params.accountInfo 账号信息
* @param {Number} [params.uid] 用户ID
* @param {List[int]} params.lockIds 锁Id列表
* @returns {Promise<Result>}
*/
export async function removeBadLock(params) {
const { accountInfo, lockIds } = params
const { lockIds } = params
// 设置执行账号
const result = await this.login(accountInfo)
const result = await this.login(params.uid)
if (result.code !== Result.Success.code) {
return result
}
@ -21,11 +21,11 @@ export async function removeBadLock(params) {
})
if (code === Result.Success.code) {
const lockList = getStorage('starLockList')
if (lockList[accountInfo.uid]) {
if (lockList[this.accountInfo.uid]) {
lockIds.forEach(lockId => {
const index = lockList[accountInfo.uid].findIndex(item => item.lockId === lockId)
const index = lockList[this.accountInfo.uid].findIndex(item => item.lockId === lockId)
if (index !== -1) {
lockList[accountInfo.uid].splice(index, 1)
lockList[this.accountInfo.uid].splice(index, 1)
}
})
setStorage('starLockList', lockList)

View File

@ -36,14 +36,14 @@ import { checkPasswordRequest, getOfflinePasswordRequest } from '../api'
/**
* 获取离线密码
* @param params
* @param {AccountInfo} params.accountInfo 账号信息
* @param {Number} [params.uid] 用户ID
* @param {OfflinePassword} params.password 密码信息
* @returns {Promise<Result>}
*/
export async function getOfflinePassword(params) {
const { accountInfo, password } = params
const { password } = params
// 设置执行账号
const result = await this.login(accountInfo)
const result = await this.login(params.uid)
if (result.code !== Result.Success.code) {
return result
}
@ -53,15 +53,15 @@ export async function getOfflinePassword(params) {
/**
* 自定义密码
* @param params
* @param {AccountInfo} params.accountInfo 账号信息
* @param {Number} [params.uid] 用户ID
* @param {CustomPassword} params.password 密码信息
* @param {Boolean} params.disconnect 操作后是否断开连接
* @param {Boolean} [params.disconnect] 操作后是否断开连接默认断开
* @returns {Promise<Result>}
*/
export async function customPassword(params) {
const { accountInfo, password } = params
const { password } = params
// 设置执行账号
const result = await this.login(accountInfo)
const result = await this.login(params.uid)
if (result.code !== Result.Success.code) {
return result
}
@ -185,16 +185,16 @@ export async function customPassword(params) {
/**
* 修改管理员密码
* @param params
* @param {AccountInfo} params.accountInfo 账号信息
* @param {Number} [params.uid] 用户ID
* @param {String} params.adminPwd 管理员密码
* @param {Boolean} params.disconnect 操作后是否断开连接
* @param {Boolean} [params.disconnect] 操作后是否断开连接默认断开
* @returns {Promise<Result>}
*/
export async function updateAdminPassword(params) {
const { adminPwd, accountInfo } = params
const { adminPwd } = params
// 设置执行账号
const result = await this.login(accountInfo)
const result = await this.login(params.uid)
if (result.code !== Result.Success.code) {
return result
}

View File

@ -3,22 +3,22 @@ import { Result } from '../constant'
/**
* 同步全部开门记录
* @param params
* @param {AccountInfo} params.accountInfo 账号信息
* @param {Boolean} params.disconnect 操作后是否断开连接
* @param {Number} [params.uid] 用户ID
* @param {Boolean} [params.disconnect] 操作后是否断开连接默认断开
* @returns {Promise<Result>}
*/
export async function syncAllOpenRecord(params) {
const { accountInfo, disconnect } = params
const { disconnect } = params
const { code, data, message } = await this.syncOpenRecord({
accountInfo,
uid: params.uid,
disconnect: false
})
if (code === Result.Success.code) {
if (data.count === 10) {
return await this.syncAllOpenRecord({
accountInfo,
uid: params.uid,
disconnect
})
}

View File

@ -1,10 +1,23 @@
import starCloudInstance from '../star-cloud'
import { Result } from '../constant'
export { Result }
/**
* 账户信息
* @typedef {Object} AccountInfo
* @property {Number} uid 用户ID
* @property {String} username 用户名
* @property {String} password 密码
* @property {String} token token 非必填
*/
/**
* 初始化星云
* @param params
* @param {String} params.clientId 客户端Id
* @param {String} params.clientSecret 客户端密码
* @param {Array<AccountInfo>} params.accounts 账号列表后续方法中需传入uid若未传入则默认使用第一个账号
* @param {String} params.env 环境
* @param {Boolean} params.isReportLog 是否上报日志
*/
@ -35,7 +48,7 @@ export const logout = params => {
/**
* 选择锁
* @param params
* @param {AccountInfo} params.accountInfo 账号信息
* @param {Number} [params.uid] 用户ID
* @param {Number} params.lockId 锁ID
* @returns Result
*/
@ -46,19 +59,32 @@ export const selectLock = async params => {
/**
* 开门
* @param params
* @param {AccountInfo} params.accountInfo 账号信息
* @param {Number} [params.uid] 用户ID
* @param {String} params.type 开门方式 close: 关门 open: 开门
* @param {Boolean} params.disconnect 操作后是否断开连接
* @param {Boolean} [params.disconnect] 操作后是否断开连接默认断开
* @returns Result
*/
export const openDoor = async params => {
return await starCloudInstance.openDoor(params)
}
/**
* 离线密码
* @typedef {Object} OfflinePassword
* @property {String} keyboardPwdName - 密码名称
* @property {Number} keyboardPwdType - 密码类型
* @property {Number} lockId - Id
* @property {Number} isCoerced - 是否胁迫模式 1:胁迫 2:非胁迫
* @property {Number} startDate - 开始日期时间戳(毫秒永久默认为0)
* @property {Number} endDate - 结束日期时间戳(毫秒永久默认为0)
* @property {Number} hoursStart - 开始时间小时, 不需要时传0
* @property {Number} hoursEnd - 结束时间小时, 不需要时传0
*/
/**
* 获取离线密码
* @param params
* @param {AccountInfo} params.accountInfo 账号信息
* @param {Number} [params.uid] 用户ID
* @param {OfflinePassword} params.password 密码信息
* @returns Result
*/
@ -66,12 +92,28 @@ export const getOfflinePassword = async params => {
return await starCloudInstance.getOfflinePassword(params)
}
/**
* 自定义密码
* @typedef {Object} CustomPassword
* @property {String} keyboardPwdName - 密码名称
* @property {Number} keyboardPwdType - 密码类型2永久 3限期
* @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修改 2删除自定义密码 3删除离线密码
* @property {Number} pwdRight - 是否是管理员密码 0不是 1
* @property {Number} [keyboardPwdId] - 密码Id创建无需传
* @property {Number} [pwdNo] - 密码编号创建无需传
*/
/**
* 自定义密码
* @param params
* @param {AccountInfo} params.accountInfo 账号信息
* @param {Number} [params.uid] 用户ID
* @param {CustomPassword} params.password 密码信息
* @param {Boolean} params.disconnect 操作后是否断开连接
* @param {Boolean} [params.disconnect] 操作后是否断开连接默认断开
* @returns Result
*/
export const customPassword = async params => {
@ -98,7 +140,7 @@ export const stopSearchDevice = async () => {
/**
* 绑定设备
* @param params
* @param {AccountInfo} params.accountInfo 账号信息
* @param {Number} [params.uid] 用户ID
* @param {String} params.name 设备名称
* @returns Result
*/
@ -109,8 +151,8 @@ export const bindDevice = async params => {
/**
* 移除坏锁
* @param params
* @param {AccountInfo} params.accountInfo 账号信息
* @param {List[int]} params.lockIds 锁Id列表
* @param {Number} [params.uid] 用户ID
* @param {List[Number]} params.lockIds 锁Id列表
* @returns Result
*/
export const removeBadLock = async params => {
@ -120,7 +162,7 @@ export const removeBadLock = async params => {
/**
* 删除锁
* @param params
* @param {AccountInfo} params.accountInfo 账号信息
* @param {Number} [params.uid] 用户ID
*/
export const deleteLock = async params => {
return await starCloudInstance.deleteLock(params)
@ -129,9 +171,9 @@ export const deleteLock = async params => {
/**
* 修改管理员密码
* @param params
* @param {AccountInfo} params.accountInfo 账号信息
* @param {Number} [params.uid] 用户ID
* @param {String} params.adminPwd 管理员密码
* @param {Boolean} params.disconnect 操作后是否断开连接
* @param {Boolean} [params.disconnect] 操作后是否断开连接默认断开
* @returns Result
*/
export const updateAdminPassword = async params => {
@ -141,8 +183,8 @@ export const updateAdminPassword = async params => {
/**
* 同步开门记录
* @param params
* @param {AccountInfo} params.accountInfo 账号信息
* @param {Boolean} params.disconnect 操作后是否断开连接
* @param {Number} [params.uid] 用户ID
* @param {Boolean} [params.disconnect] 操作后是否断开连接默认断开
* @returns Result
*/
export const syncOpenDoorRecord = async params => {
@ -160,8 +202,8 @@ export const getServerTime = async () => {
/**
* 获取锁支持项
* @param params
* @param {AccountInfo} params.accountInfo 账号信息
* @param {Number} params.lockId Id
* @param {Number} [params.uid] 用户ID
* @param {Number} params.lockId ID
* @returns Result
*/
export const getLockSupportFeatures = async params => {

84
web.md Normal file
View File

@ -0,0 +1,84 @@
## 星云SDK
### 1. 安装
```git
npm install star-cloud-web
```
### 2. 使用
```javascript
import {
init,
Result,
register,
logout,
getOfflinePassword,
removeBadLock,
getLockSupportFeatures,
getServerTimestamp
} from 'star-cloud-web'
/**
* 初始化星云
* @param params
* @param {String} params.clientId 客户端Id
* @param {String} params.clientSecret 客户端密码
* @param {Array<AccountInfo>} params.accounts 账号列表后续方法中需传入uid若未传入则默认使用第一个账号
* @param {String} params.env 环境
*/
init(params)
/**
* 注册星云
* @returns Result
*/
const { code, data, message } = await register()
if (code === Result.Success.code) {
// 逻辑代码
} else {
// 错误处理
}
/**
* 退出登录
* @param params
* @param {Number} params.uid 用户ID
* @returns Result
*/
const { code, data, message } = await logout(params)
/**
* 获取离线密码
* @param params
* @param {Number} [params.uid] 用户ID
* @param {OfflinePassword} params.password 密码信息
* @returns Result
*/
const data = await getOfflinePassword(params)
/**
* 移除坏锁
* @param params
* @param {Number} [params.uid] 用户ID
* @param {List[Number]} params.lockIds 锁Id列表
* @returns Result
*/
const data = await removeBadLock(params)
/**
* 获取服务器时间
* @returns Result
*/
const data = await getServerTimestamp()
/**
* 获取锁支持项
* @param params
* @param {Number} [params.uid] 用户ID
* @param {Number} params.lockId 锁 Id
* @returns Result
*/
const data = await getLockSupportFeatures(params)
```

View File

@ -1,10 +1,23 @@
import starCloudInstance from '../star-cloud'
import { Result } from '../constant'
export { Result }
/**
* 账户信息
* @typedef {Object} AccountInfo
* @property {Number} uid 用户ID
* @property {String} username 用户名
* @property {String} password 密码
* @property {String} token token 非必填
*/
/**
* 初始化星云
* @param params
* @param {String} params.clientId 客户端Id
* @param {String} params.clientSecret 客户端密码
* @param {Array<AccountInfo>} params.accounts 账号列表后续方法中需传入uid若未传入则默认使用第一个账号
* @param {String} params.env 环境
*/
export const init = params => {
@ -32,10 +45,23 @@ export const logout = params => {
starCloudInstance.logout(params)
}
/**
* 离线密码
* @typedef {Object} OfflinePassword
* @property {String} keyboardPwdName - 密码名称
* @property {Number} keyboardPwdType - 密码类型
* @property {Number} lockId - Id
* @property {Number} isCoerced - 是否胁迫模式 1:胁迫 2:非胁迫
* @property {Number} startDate - 开始日期时间戳(毫秒永久默认为0)
* @property {Number} endDate - 结束日期时间戳(毫秒永久默认为0)
* @property {Number} hoursStart - 开始时间小时, 不需要时传0
* @property {Number} hoursEnd - 结束时间小时, 不需要时传0
*/
/**
* 获取离线密码
* @param params
* @param {AccountInfo} params.accountInfo 账号信息
* @param {Number} [params.uid] 用户ID
* @param {OfflinePassword} params.password 密码信息
* @returns Result
*/
@ -46,7 +72,7 @@ export const getOfflinePassword = async params => {
/**
* 移除坏锁
* @param params
* @param {AccountInfo} params.accountInfo 账号信息
* @param {Number} [params.uid] 用户ID
* @param {Array[int]} params.lockIds 锁Id列表
* @returns Result
*/
@ -65,7 +91,7 @@ export const getServerTime = async () => {
/**
* 获取锁支持项
* @param params
* @param {AccountInfo} params.accountInfo 账号信息
* @param {Number} [params.uid] 用户ID
* @param {Number} params.lockId Id
* @returns Result
*/