init
This commit is contained in:
commit
cbf8505bae
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
.idea
|
||||||
6
README.md
Normal file
6
README.md
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
## 星云SDK
|
||||||
|
|
||||||
|
### 1. 安装
|
||||||
|
|
||||||
|
### 2. 需安装的npm包
|
||||||
|
`npm install buffer crc js-md5 pinia sm-crypto`
|
||||||
91
api.js
Normal file
91
api.js
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
import request from '@/starCloud/request'
|
||||||
|
|
||||||
|
// 创建账号
|
||||||
|
export function starCloudCreateUser(data) {
|
||||||
|
return request({
|
||||||
|
url: '/createUser',
|
||||||
|
method: 'POST',
|
||||||
|
data
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取访问令牌
|
||||||
|
export function getStarCloudToken(data) {
|
||||||
|
return request({
|
||||||
|
url: '/oauth2/token',
|
||||||
|
method: 'POST',
|
||||||
|
data
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新锁用户
|
||||||
|
export function updateLockUserNoRequest(data) {
|
||||||
|
return request({
|
||||||
|
url: '/v1/key/updateLockUserNo',
|
||||||
|
method: 'POST',
|
||||||
|
data
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取所有锁用户
|
||||||
|
export function getUserNoListRequest(data) {
|
||||||
|
return request({
|
||||||
|
url: '/v1/key/getUserNoList',
|
||||||
|
method: 'POST',
|
||||||
|
data
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取手机联网token
|
||||||
|
export function getLockNetTokenRequest(data) {
|
||||||
|
return request({
|
||||||
|
url: '/v1/lock/getLockNetToken',
|
||||||
|
method: 'POST',
|
||||||
|
data
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取服务器时间
|
||||||
|
export function getServerDatetime(data) {
|
||||||
|
return request({
|
||||||
|
url: '/v1/lock/queryDate',
|
||||||
|
method: 'POST',
|
||||||
|
data
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取锁详情
|
||||||
|
export function getLockDetail(data) {
|
||||||
|
return request({
|
||||||
|
url: '/v1/lock/detail',
|
||||||
|
method: 'POST',
|
||||||
|
data
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取离线密码
|
||||||
|
export function getOfflinePassword(data) {
|
||||||
|
return request({
|
||||||
|
url: '/v1/keyboardPwd/get',
|
||||||
|
method: 'POST',
|
||||||
|
data
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加自定义密码
|
||||||
|
export function addCustomPassword(data) {
|
||||||
|
return request({
|
||||||
|
url: '/v1/keyboardPwd/add',
|
||||||
|
method: 'POST',
|
||||||
|
data
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新密码
|
||||||
|
export function updatePassword(data) {
|
||||||
|
return request({
|
||||||
|
url: '/v1/keyboardPwd/update',
|
||||||
|
method: 'POST',
|
||||||
|
data
|
||||||
|
})
|
||||||
|
}
|
||||||
442
basic.js
Normal file
442
basic.js
Normal file
@ -0,0 +1,442 @@
|
|||||||
|
export class Result {
|
||||||
|
static codes = {
|
||||||
|
Success: 0,
|
||||||
|
Fail: -1,
|
||||||
|
|
||||||
|
NotAvailableBluetooth: -20,
|
||||||
|
NotAvailableBluetoothPermission: -21,
|
||||||
|
NotAvailableWeChatNearbyDevicesPermission: -22,
|
||||||
|
NotAvailableWeChatLocationPermission: -23,
|
||||||
|
NotAvailableWeChatNearbyDevicesEmpty: -24,
|
||||||
|
DeviceHasBeenReset: -30,
|
||||||
|
|
||||||
|
NotRegisteredLock: 4,
|
||||||
|
NotTokenLock: 6,
|
||||||
|
NotMoreKeyLock: 12,
|
||||||
|
ReadyHasKeyLock: 15,
|
||||||
|
}
|
||||||
|
|
||||||
|
static resultsMap = new Map([
|
||||||
|
[Result.codes.Success, { message: '成功', data: {} }],
|
||||||
|
[Result.codes.Fail, { 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.DeviceHasBeenReset, { message: '设备已被重置', data: {} }],
|
||||||
|
|
||||||
|
[Result.codes.NotRegisteredLock, { message: '用户在锁端未注册', data: {} }],
|
||||||
|
[Result.codes.NotTokenLock, { message: '用户在锁端token失效', data: {} }],
|
||||||
|
[Result.codes.NotMoreKeyLock, { message: '锁端钥匙数量已达上限', data: {} }],
|
||||||
|
[Result.codes.ReadyHasKeyLock, { 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 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 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {Object} err
|
||||||
|
* @property {number} errno - 错误代码
|
||||||
|
* @property {number} errCode - 错误代码
|
||||||
|
* @property {String} errMsg - 错误信息
|
||||||
|
*/
|
||||||
|
|
||||||
|
// 查找设备并连接
|
||||||
|
export function searchAndConnectDevice(name) {
|
||||||
|
// 循环查找设备
|
||||||
|
let timer
|
||||||
|
// 超时计时器
|
||||||
|
let timeoutTimer
|
||||||
|
|
||||||
|
return new Promise(async (resolve) => {
|
||||||
|
const result = await startBluetoothDevicesDiscovery()
|
||||||
|
if(result.code === Result.Success.code) {
|
||||||
|
let searchFlag = false
|
||||||
|
timeoutTimer = setTimeout(async () => {
|
||||||
|
await stopBluetoothDevicesDiscovery()
|
||||||
|
clearInterval(timer)
|
||||||
|
if (!searchFlag) {
|
||||||
|
resolve(Result.NotAvailableWeChatNearbyDevicesEmpty)
|
||||||
|
} else {
|
||||||
|
resolve(Result.Fail)
|
||||||
|
}
|
||||||
|
}, 10500)
|
||||||
|
timer = setInterval(async () => {
|
||||||
|
const queryResult = await getBluetoothDevices()
|
||||||
|
if(queryResult.code === Result.Success.code) {
|
||||||
|
const deviceList = queryResult.data
|
||||||
|
if (searchFlag === false && deviceList.length > 0) {
|
||||||
|
searchFlag = true
|
||||||
|
}
|
||||||
|
for (let i = 0; i < deviceList.length; i++) {
|
||||||
|
if (deviceList[i]?.name === name) {
|
||||||
|
const uuid = deviceList[i]?.advertisServiceUUIDs[0]
|
||||||
|
if(uuid && uuid.slice(2, 8) === '758824') {
|
||||||
|
await stopBluetoothDevicesDiscovery()
|
||||||
|
clearTimeout(timeoutTimer)
|
||||||
|
clearInterval(timer)
|
||||||
|
if(uuid.slice(30, 32) === '00') {
|
||||||
|
resolve(Result.DeviceHasBeenReset)
|
||||||
|
} else if (uuid.slice(30, 32) === '01') {
|
||||||
|
const connectResult = await createBLEConnection(deviceList[i].deviceId)
|
||||||
|
resolve(connectResult)
|
||||||
|
} else {
|
||||||
|
resolve(Result.Fail)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
resolve(queryResult)
|
||||||
|
}
|
||||||
|
}, 1000)
|
||||||
|
} else {
|
||||||
|
resolve(result)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 蓝牙操作报错处理
|
||||||
|
async function handleError (err, event) {
|
||||||
|
if (err.errCode === 10000) {
|
||||||
|
const result = await openBluetoothAdapter()
|
||||||
|
if (result.code === Result.Success.code) {
|
||||||
|
return await event()
|
||||||
|
} else {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
} else if (err.errCode === 10001) {
|
||||||
|
return Result.NotAvailableBluetooth
|
||||||
|
} else if (err.errno === 3) {
|
||||||
|
return Result.NotAvailableWeChatNearbyDevicesPermission
|
||||||
|
} else if (err.errno === 103) {
|
||||||
|
return Result.NotAvailableBluetoothPermission
|
||||||
|
} else if (err.errno === 1509008) {
|
||||||
|
return Result.NotAvailableWeChatLocationPermission
|
||||||
|
} else if (err.errMsg === 'openBluetoothAdapter:fail already opened') {
|
||||||
|
return Result.Success
|
||||||
|
} else {
|
||||||
|
return Result.Fail
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化蓝牙模块
|
||||||
|
function openBluetoothAdapter() {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
uni.openBluetoothAdapter({
|
||||||
|
success() {
|
||||||
|
resolve(Result.Success)
|
||||||
|
},
|
||||||
|
async fail (err) {
|
||||||
|
resolve(await handleError(err))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 关闭蓝牙模块
|
||||||
|
function closeBluetoothAdapter() {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
uni.closeBluetoothAdapter({
|
||||||
|
success() {
|
||||||
|
resolve(Result.Success)
|
||||||
|
},
|
||||||
|
async fail (err) {
|
||||||
|
resolve(await handleError(err))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 移除蓝牙适配器的全部监听
|
||||||
|
function offBluetoothAdapterStateChange() {
|
||||||
|
uni.offBluetoothAdapterStateChange()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 监听蓝牙特征值改变
|
||||||
|
export function onBLECharacteristicValueChange(callback) {
|
||||||
|
uni.onBLECharacteristicValueChange((res) => {
|
||||||
|
callback(res)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 开始搜索附近的蓝牙设备
|
||||||
|
function startBluetoothDevicesDiscovery() {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
uni.startBluetoothDevicesDiscovery({
|
||||||
|
success() {
|
||||||
|
resolve(Result.Success)
|
||||||
|
},
|
||||||
|
async fail (err) {
|
||||||
|
resolve(await handleError(err, startBluetoothDevicesDiscovery))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取所有已发现的蓝牙设备
|
||||||
|
function getBluetoothDevices() {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
uni.getBluetoothDevices({
|
||||||
|
success(res) {
|
||||||
|
resolve(new Result(Result.Success.code, res.devices))
|
||||||
|
},
|
||||||
|
async fail () {
|
||||||
|
resolve(await handleError(err, getBluetoothDevices))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 停止搜索附近的蓝牙设备
|
||||||
|
function stopBluetoothDevicesDiscovery() {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
uni.stopBluetoothDevicesDiscovery({
|
||||||
|
success() {
|
||||||
|
resolve(Result.Success)
|
||||||
|
},
|
||||||
|
async fail () {
|
||||||
|
resolve(await handleError(err))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 连接低功耗蓝牙设备
|
||||||
|
function createBLEConnection(deviceId, reconnectNumber = 0) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
uni.createBLEConnection({
|
||||||
|
deviceId,
|
||||||
|
timeout: 10000,
|
||||||
|
async success () {
|
||||||
|
const res = await getBLEDeviceServicesAndCharacteristics(deviceId)
|
||||||
|
await notifyBLECharacteristicValueChange(deviceId, res.data.serviceId, res.data.notifyCharacteristicId)
|
||||||
|
resolve(res)
|
||||||
|
},
|
||||||
|
async fail (err) {
|
||||||
|
if (err.errno === 1509007) {
|
||||||
|
const res = await getBLEDeviceServicesAndCharacteristics(deviceId)
|
||||||
|
await notifyBLECharacteristicValueChange(deviceId, res.data.serviceId, res.data.notifyCharacteristicId)
|
||||||
|
resolve(res)
|
||||||
|
} else if (err.errno === 1509001 && reconnectNumber < 1) {
|
||||||
|
resolve(Result.Fail)
|
||||||
|
} else if (reconnectNumber < 1) {
|
||||||
|
resolve(await createBLEConnection(deviceId, reconnectNumber + 1))
|
||||||
|
} else {
|
||||||
|
resolve(Result.Fail)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取服务及对应特征值
|
||||||
|
async function getBLEDeviceServicesAndCharacteristics (deviceId) {
|
||||||
|
const { code, data } = await getBLEDeviceServices(deviceId)
|
||||||
|
if (code === Result.Success.code) {
|
||||||
|
const { serviceId } = data
|
||||||
|
const {
|
||||||
|
code,
|
||||||
|
data: { notifyCharacteristicId, writeCharacteristicId }
|
||||||
|
} = await getBLEDeviceCharacteristics(deviceId, serviceId)
|
||||||
|
if (code === Result.Success.code) {
|
||||||
|
return new Result(Result.Success.code, { deviceId, serviceId, notifyCharacteristicId, writeCharacteristicId })
|
||||||
|
} else {
|
||||||
|
return Result.Fail
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Result.Fail
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取设备的服务
|
||||||
|
function getBLEDeviceServices(deviceId) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
uni.getBLEDeviceServices({
|
||||||
|
deviceId,
|
||||||
|
success(res) {
|
||||||
|
let serviceId
|
||||||
|
for(let i = 0; i < res.services.length; i++) {
|
||||||
|
if(res.services[i].uuid.indexOf('FFF0') !== -1) {
|
||||||
|
serviceId = res.services[i].uuid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!serviceId) {
|
||||||
|
resolve(Result.Fail)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
resolve(new Result(Result.Success.code, { serviceId }))
|
||||||
|
},
|
||||||
|
fail() {
|
||||||
|
resolve(Result.Fail)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取服务的特征值
|
||||||
|
function getBLEDeviceCharacteristics(deviceId, serviceId) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
uni.getBLEDeviceCharacteristics({
|
||||||
|
deviceId,
|
||||||
|
serviceId,
|
||||||
|
success(res) {
|
||||||
|
let notifyCharacteristicId
|
||||||
|
let writeCharacteristicId
|
||||||
|
for(let i = 0; i < res.characteristics.length; i++) {
|
||||||
|
const characteristic = res.characteristics[i]
|
||||||
|
if(characteristic.properties.notify) {
|
||||||
|
notifyCharacteristicId = characteristic.uuid
|
||||||
|
}
|
||||||
|
if(characteristic.properties.write) {
|
||||||
|
writeCharacteristicId = characteristic.uuid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(notifyCharacteristicId && writeCharacteristicId) {
|
||||||
|
resolve(new Result(Result.Success.code, { notifyCharacteristicId, writeCharacteristicId }))
|
||||||
|
} else {
|
||||||
|
resolve(Result.Fail)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
fail() {
|
||||||
|
resolve(Result.Fail)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 订阅特征值
|
||||||
|
function notifyBLECharacteristicValueChange(deviceId, serviceId, characteristicId) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
uni.notifyBLECharacteristicValueChange({
|
||||||
|
deviceId,
|
||||||
|
serviceId,
|
||||||
|
characteristicId,
|
||||||
|
state: true,
|
||||||
|
success() {
|
||||||
|
resolve(Result.Success)
|
||||||
|
},
|
||||||
|
fail() {
|
||||||
|
resolve(Result.Fail)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 断开与低功耗蓝牙设备的连接
|
||||||
|
export function closeBLEConnection(deviceId) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
uni.closeBLEConnection({
|
||||||
|
deviceId,
|
||||||
|
success() {
|
||||||
|
console.log('断开连接成功')
|
||||||
|
resolve(Result.Success)
|
||||||
|
},
|
||||||
|
fail() {
|
||||||
|
console.log('断开连接失败')
|
||||||
|
resolve(Result.Fail)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 写入特征值
|
||||||
|
export function writeBLECharacteristicValue(deviceId, serviceId, characteristicId, binaryData) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
const count = Math.ceil(binaryData.length / 20)
|
||||||
|
let successCount = 0
|
||||||
|
for(let i = 0; i < count; i++) {
|
||||||
|
const writeData = binaryData.slice(i * 20, i === (count - 1) ? binaryData.length : (i + 1) * 20)
|
||||||
|
uni.writeBLECharacteristicValue({
|
||||||
|
deviceId,
|
||||||
|
serviceId,
|
||||||
|
characteristicId,
|
||||||
|
value: writeData.buffer,
|
||||||
|
success() {
|
||||||
|
successCount++
|
||||||
|
if(successCount === count) {
|
||||||
|
resolve(Result.Success)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
fail() {
|
||||||
|
resolve(Result.Fail)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
24
env.js
Normal file
24
env.js
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
// 版本号
|
||||||
|
export const version = '1.0.0'
|
||||||
|
// 构建号
|
||||||
|
export const buildNumber = 1
|
||||||
|
|
||||||
|
// 环境配置
|
||||||
|
export const configs = {
|
||||||
|
DEV: {
|
||||||
|
name: 'DEV',
|
||||||
|
baseUrl: 'https://dev.cloud.star-lock.cn/sdk'
|
||||||
|
},
|
||||||
|
PRE: {
|
||||||
|
name: 'PRE',
|
||||||
|
baseUrl: 'https://pre.cloud.star-lock.cn'
|
||||||
|
},
|
||||||
|
XHJ: {
|
||||||
|
name: 'XHJ',
|
||||||
|
baseUrl: 'https://cloud.star-lock.cn'
|
||||||
|
},
|
||||||
|
SKY: {
|
||||||
|
name: 'SKY',
|
||||||
|
baseUrl: 'https://cloud.star-lock.cn'
|
||||||
|
}
|
||||||
|
}
|
||||||
83
format.js
Normal file
83
format.js
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
import {
|
||||||
|
md5
|
||||||
|
} from 'js-md5'
|
||||||
|
import crc from 'crc'
|
||||||
|
|
||||||
|
// 周数组转换
|
||||||
|
export function convertWeekdaysToNumber(weekDay) {
|
||||||
|
let weekStr = '00000000'
|
||||||
|
|
||||||
|
for (const day of weekDay) {
|
||||||
|
const index = day % 7 // 将周日的索引转换为0
|
||||||
|
weekStr = weekStr.substring(0, index) + '1' + weekStr.substring(index + 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 倒序 weekStr
|
||||||
|
weekStr = weekStr.split('')
|
||||||
|
.reverse()
|
||||||
|
.join('')
|
||||||
|
|
||||||
|
return parseInt(weekStr, 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 时间戳转二进制
|
||||||
|
export function timestampToArray(timestamp) {
|
||||||
|
const array = new Uint8Array(4)
|
||||||
|
array[0] = (timestamp & 0xff000000) >> 24
|
||||||
|
array[1] = (timestamp & 0xff0000) >> 16
|
||||||
|
array[2] = (timestamp & 0xff00) >> 8
|
||||||
|
array[3] = (timestamp & 0xff)
|
||||||
|
return array
|
||||||
|
}
|
||||||
|
|
||||||
|
// md5加密
|
||||||
|
export function md5Encrypt(text, token, key) {
|
||||||
|
const length = text.length + 4 + 16
|
||||||
|
const md5Array = new Uint8Array(length)
|
||||||
|
|
||||||
|
for (let i = 0; i < text.length; i++) {
|
||||||
|
md5Array[i] = text.charCodeAt(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
md5Array.set(token, text.length)
|
||||||
|
md5Array.set(key, text.length + 4)
|
||||||
|
|
||||||
|
const md5Text = md5(md5Array)
|
||||||
|
return new Uint8Array(md5Text.match(/.{1,2}/g)
|
||||||
|
.map(byte => parseInt(byte, 16)))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成包尾 头部数据+内容数据
|
||||||
|
export function createPackageEnd(headArray, contentArray) {
|
||||||
|
// 拼接头部和内容
|
||||||
|
let mergerArray = new Uint8Array(headArray.length + contentArray.length)
|
||||||
|
mergerArray.set(headArray)
|
||||||
|
mergerArray.set(contentArray, headArray.length)
|
||||||
|
|
||||||
|
// crc加密
|
||||||
|
const crcResult = crc.crc16kermit(mergerArray)
|
||||||
|
|
||||||
|
// 拼接crc
|
||||||
|
let newArray = new Uint8Array(mergerArray.length + 2)
|
||||||
|
newArray.set(mergerArray)
|
||||||
|
newArray.set([crcResult / 256, crcResult % 256], mergerArray.length)
|
||||||
|
|
||||||
|
return newArray
|
||||||
|
}
|
||||||
|
|
||||||
|
// 二进制转时间戳
|
||||||
|
export function arrayToTimestamp(array) {
|
||||||
|
const timestamp = (array[0] << 24) | (array[1] << 16) | (array[2] << 8) | array[3]
|
||||||
|
return timestamp >>> 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// 二进制转字符串
|
||||||
|
export function uint8ArrayToString(uint8Array) {
|
||||||
|
let str = ''
|
||||||
|
for (let i = 0; i < uint8Array.length; i++) {
|
||||||
|
if (uint8Array[i] !== 0) {
|
||||||
|
str += String.fromCharCode(uint8Array[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return str
|
||||||
|
}
|
||||||
104
request.js
Normal file
104
request.js
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
import { getStorage, removeStorage } from '@/starCloud/storage'
|
||||||
|
import { useStarCloudStore } from '@/starCloud/starCloud'
|
||||||
|
import { Result } from '@/starCloud/basic'
|
||||||
|
|
||||||
|
/*
|
||||||
|
* config
|
||||||
|
* baseUrl: 请求域名
|
||||||
|
* url: 请求路径
|
||||||
|
* method: 请求方法
|
||||||
|
* header: 请求头
|
||||||
|
* token: 请求token
|
||||||
|
* data: 请求参数
|
||||||
|
* */
|
||||||
|
|
||||||
|
const request = (config) => {
|
||||||
|
const starCloud = useStarCloudStore()
|
||||||
|
let timer
|
||||||
|
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)
|
||||||
|
|
||||||
|
uni.request({
|
||||||
|
url: URL,
|
||||||
|
method,
|
||||||
|
header,
|
||||||
|
data,
|
||||||
|
timeout: 3000,
|
||||||
|
async success (res) {
|
||||||
|
const { statusCode, data } = res
|
||||||
|
if (timer) {
|
||||||
|
clearTimeout(timer)
|
||||||
|
}
|
||||||
|
if (statusCode === 200) {
|
||||||
|
const code = data.errcode
|
||||||
|
const message = data.errmsg
|
||||||
|
if (code === 10003) {
|
||||||
|
removeStorage('starCloudToken')
|
||||||
|
removeStorage('starCloudUser')
|
||||||
|
const { code } = await starCloud.login(
|
||||||
|
starCloud.accountInfo.username,
|
||||||
|
starCloud.accountInfo.password
|
||||||
|
)
|
||||||
|
if(code === Result.Success.code) {
|
||||||
|
resolve(await request(config))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
resolve({
|
||||||
|
code,
|
||||||
|
data: data.data,
|
||||||
|
message
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
resolve(new Result(Result.Fail.code, {},'网络访问失败,请检查网络是否正常'))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
async fail (res) {
|
||||||
|
console.log('网络访问失败', res)
|
||||||
|
if (timer) {
|
||||||
|
clearTimeout(timer)
|
||||||
|
}
|
||||||
|
resolve(new Result(Result.Fail.code, {},'网络访问失败,请检查网络是否正常'))
|
||||||
|
},
|
||||||
|
async complete (res) {
|
||||||
|
console.log(URL.substring(baseConfig.baseUrl.length + 1), {
|
||||||
|
env: baseConfig.name,
|
||||||
|
url: URL.substring(baseConfig.baseUrl.length + 1),
|
||||||
|
req: config?.data || {},
|
||||||
|
code: res?.data?.errcode,
|
||||||
|
res: res?.data?.data,
|
||||||
|
token: header?.authorization || '',
|
||||||
|
message: res?.data?.errmsg,
|
||||||
|
duration: new Date().getTime() - timestamp
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export default request
|
||||||
1013
starCloud.js
Normal file
1013
starCloud.js
Normal file
File diff suppressed because it is too large
Load Diff
18
storage.js
Normal file
18
storage.js
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
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 }:`
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user