添加日志上报

This commit is contained in:
范鹏 2024-10-24 15:44:42 +08:00
parent 72f0362810
commit cb46da07a0
6 changed files with 448 additions and 74 deletions

View File

@ -2,8 +2,8 @@
"name" : "wx-starlock",
"appid" : "__UNI__933D519",
"description" : "",
"versionName" : "1.1.1",
"versionCode" : "28",
"versionName" : "1.1.2",
"versionCode" : "29",
"transformPx" : false,
/* */
"mp-weixin" : {

View File

@ -13,7 +13,7 @@
<LockInput :value="permanentAccount" title="接收者" placeholder="请输入手机号或邮箱"
@changeInput="changePermanentAccountInput"></LockInput>
<LockInput :value="permanentName" title="姓名" placeholder="请输入姓名"
@changeInput="changePermanentNmaeInput"></LockInput>
@changeInput="changePermanentNameInput"></LockInput>
<view class="text" style="margin-top: 40rpx;">接收者可使用此小程序开关锁</view>
<view class="text" style="margin-bottom: 50rpx;">小程序暂不支持邮箱登录若接收者账号为邮箱可先使用星星锁APP登录绑定手机号后可使用小程序登录</view>
<view class="button" @click="createKey('permanent')">发送</view>
@ -149,6 +149,8 @@ export default {
}
const { code, message } = await createKeyRequest(params)
if (code === 0) {
uni.reportEvent("create_key", {
})
this.updateKeySearch({
...this.keySearch,
pageNo: 1
@ -177,7 +179,7 @@ export default {
changePermanentAccountInput (e) {
this.permanentAccount = e
},
changePermanentNmaeInput (e) {
changePermanentNameInput (e) {
this.permanentName = e
},
changeTemporaryNameInput (e) {

View File

@ -145,6 +145,9 @@ export default {
}
const { code, data, message } = await createPsaawordRequest(params)
if(code === 0) {
uni.reportEvent("create_password", {
})
this.updatePasswordSearch({
...this.passwordSearch,
pageNo: 1

View File

@ -137,6 +137,7 @@ export default {
}
},
async openDoorOperate(type) {
const timestamp = new Date().getTime()
if(this.currentLockInfo.faceAuthentication === 1 &&
((this.currentLockInfo.nextFaceValidateTime <= new Date().getTime() + this.time * 1000) &&
this.currentLockInfo.nextFaceValidateTime !== 0)) {
@ -187,6 +188,17 @@ export default {
onlineToken: this.onlineToken
})
this.closeBluetoothConnection()
if(type === 'open') {
uni.reportEvent("open_door", {
result: code,
duration: new Date().getTime() - timestamp
})
} else if(type === 'close') {
uni.reportEvent("close_door", {
result: code,
duration: new Date().getTime() - timestamp
})
}
if(code === 0) {
this.show = true
this.type = type

View File

@ -10,6 +10,8 @@ import { getUserNoListRequest, updateLockUserNoRequest } from '@/api/key'
import { updateElectricQuantityRequest } from '@/api/room'
import { reportOpenDoorRequest } from '@/api/lockRecords'
import { updateTimezoneOffsetRequest } from '@/api/user'
import log from '@/utils/log'
import { useUserStore } from '@/stores/user'
// 定时器
let timer
@ -228,6 +230,7 @@ export const useBluetoothStore = defineStore('ble', {
},
// 解析特征值
parsingCharacteristicValue(binaryData) {
const $user = useUserStore()
const that = this
// 0x20 明文 0x22 SM4事先约定密钥 0x23 SM4设备指定密钥
if(binaryData[7] === 0x20) {
@ -363,10 +366,34 @@ export const useBluetoothStore = defineStore('ble', {
token: decrypted.slice(2,6)
})
console.log('开门', decrypted[6], that.currentLockInfo.token)
log.info({
code: decrypted[6],
message: `锁端返回状态码`,
data: {
lockName: this.currentLockInfo.name,
lockId: this.currentLockInfo.lockId,
uid: $user.userInfo.uid,
nickname: $user.userInfo.nickname,
mobile: $user.userInfo.mobile,
email: $user.userInfo.email
}
})
characteristicValueCallback({
code: decrypted[6]
})
if(decrypted[6] === 0) {
log.info({
code: decrypted[6],
message: `开门成功`,
data: {
lockName: this.currentLockInfo.name,
lockId: this.currentLockInfo.lockId,
uid: $user.userInfo.uid,
nickname: $user.userInfo.nickname,
mobile: $user.userInfo.mobile,
email: $user.userInfo.email
}
})
updateElectricQuantityRequest({
lockId: that.currentLockInfo.lockId,
electricQuantity: decrypted[7],
@ -773,8 +800,10 @@ export const useBluetoothStore = defineStore('ble', {
})
}
})
resolve({ code: -22 })
} else {
resolve({ code: -4 })
}
resolve({ code: -1 })
}, 10500)
timer1 = setInterval(() => {
uni.getBluetoothDevices({
@ -855,7 +884,7 @@ export const useBluetoothStore = defineStore('ble', {
}
})
resolve({
code: -1
code: -23
})
} else {
resolve({
@ -1308,18 +1337,57 @@ export const useBluetoothStore = defineStore('ble', {
},
// 获取写入结果
getWriteResult(request, params) {
const $user = useUserStore()
const that = this
return new Promise(resolve => {
const getWriteResultTimer = setTimeout(() => {
log.info({
code: -1,
message: `操作失败,蓝牙操作超时`,
data: {
lockName: this.currentLockInfo.name,
lockId: this.currentLockInfo.lockId,
uid: $user.userInfo.uid,
nickname: $user.userInfo.nickname,
mobile: $user.userInfo.mobile,
email: $user.userInfo.email
}
})
resolve({ code: -1 })
}, 20000)
characteristicValueCallback = async (data) => {
// code 6 token过期,重新获取
if(data.code === 6) {
log.info({
code: 0,
message: `开门中更换过期token开始重试`,
data: {
lockName: this.currentLockInfo.name,
lockId: this.currentLockInfo.lockId,
uid: $user.userInfo.uid,
nickname: $user.userInfo.nickname,
mobile: $user.userInfo.mobile,
email: $user.userInfo.email
}
})
clearTimeout(getWriteResultTimer)
resolve(await request(params))
} else if(data.code === 4) {
log.info({
code: 4,
message: `锁端数据返回,无操作权限,添加用户后重新执行`,
data: {
lockName: this.currentLockInfo.name,
lockId: this.currentLockInfo.lockId,
uid: $user.userInfo.uid,
nickname: $user.userInfo.nickname,
mobile: $user.userInfo.mobile,
email: $user.userInfo.email
}
})
const checkResult = await that.checkLockUser(true)
if(checkResult) {
clearTimeout(getWriteResultTimer)
resolve(await request(params))
} else {
clearTimeout(getWriteResultTimer)
@ -1334,84 +1402,343 @@ export const useBluetoothStore = defineStore('ble', {
},
// 开门
async openDoor(data) {
// 确认蓝牙状态正常
if(this.bluetoothStatus !== 0) {
console.log('写入未执行', this.bluetoothStatus)
this.getBluetoothStatus()
return {
code: -1
}
}
// 确认设备连接正常
if(!this.currentLockInfo.connected) {
const srerchResult = await this.searchAndConnectDevice()
if(srerchResult.code !== 0) {
return srerchResult
}
this.updateCurrentLockInfo({
...this.currentLockInfo,
deviceId: srerchResult.data.deviceId
const $user = useUserStore()
try {
log.info({
code: 0,
message: `开始开门`,
data: {
lockName: this.currentLockInfo.name,
lockId: this.currentLockInfo.lockId,
uid: $user.userInfo.uid,
nickname: $user.userInfo.nickname,
mobile: $user.userInfo.mobile,
email: $user.userInfo.email
}
})
console.log('设备ID', this.currentLockInfo.deviceId)
const result = await this.connectBluetoothDevice()
console.log('连接结果', result)
if(!result) {
// 确认蓝牙状态正常
if(this.bluetoothStatus !== 0) {
console.log('写入未执行', this.bluetoothStatus)
this.getBluetoothStatus()
if(this.bluetoothStatus === -1) {
log.info({
code: -25,
message: `开门失败,未知蓝牙状态, ${this.bluetoothStatus}`,
data: {
lockName: this.currentLockInfo.name,
lockId: this.currentLockInfo.lockId,
uid: $user.userInfo.uid,
nickname: $user.userInfo.nickname,
mobile: $user.userInfo.mobile,
email: $user.userInfo.email
}
})
} else if(this.bluetoothStatus === 1) {
log.info({
code: -20,
message: `开门失败,蓝牙未开启, ${this.bluetoothStatus}`,
data: {
lockName: this.currentLockInfo.name,
lockId: this.currentLockInfo.lockId,
uid: $user.userInfo.uid,
nickname: $user.userInfo.nickname,
mobile: $user.userInfo.mobile,
email: $user.userInfo.email
}
})
} else if(this.bluetoothStatus === 2) {
log.info({
code: -21,
message: `开门失败,小程序蓝牙权限被禁用, ${this.bluetoothStatus}`,
data: {
lockName: this.currentLockInfo.name,
lockId: this.currentLockInfo.lockId,
uid: $user.userInfo.uid,
nickname: $user.userInfo.nickname,
mobile: $user.userInfo.mobile,
email: $user.userInfo.email
}
})
} else if(this.bluetoothStatus === 3) {
log.info({
code: -26,
message: `开门失败,蓝牙未开启且小程序蓝牙功能被禁用, ${this.bluetoothStatus}`,
data: {
lockName: this.currentLockInfo.name,
lockId: this.currentLockInfo.lockId,
uid: $user.userInfo.uid,
nickname: $user.userInfo.nickname,
mobile: $user.userInfo.mobile,
email: $user.userInfo.email
}
})
}
return {
code: -1
}
}
}
// 检查是否已添加为用户
const checkResult = await this.checkLockUser()
if (!checkResult) {
log.info({
code: 0,
message: `开门中,蓝牙权限正常, ${this.bluetoothStatus}`,
data: {
lockName: this.currentLockInfo.name,
lockId: this.currentLockInfo.lockId,
uid: $user.userInfo.uid,
nickname: $user.userInfo.nickname,
mobile: $user.userInfo.mobile,
email: $user.userInfo.email
}
})
// 确认设备连接正常
if(!this.currentLockInfo.connected) {
const srerchResult = await this.searchAndConnectDevice()
if(srerchResult.code === 0) {
log.info({
code: srerchResult.code,
message: `开门中,已搜索到设备`,
data: {
lockName: this.currentLockInfo.name,
lockId: this.currentLockInfo.lockId,
uid: $user.userInfo.uid,
nickname: $user.userInfo.nickname,
mobile: $user.userInfo.mobile,
email: $user.userInfo.email
}
})
} else if(srerchResult.code === -1) {
log.info({
code: srerchResult.code,
message: `开门失败,搜索失败`,
data: {
lockName: this.currentLockInfo.name,
lockId: this.currentLockInfo.lockId,
uid: $user.userInfo.uid,
nickname: $user.userInfo.nickname,
mobile: $user.userInfo.mobile,
email: $user.userInfo.email
}
})
} else if(srerchResult.code === -2) {
log.info({
code: srerchResult.code,
message: `开门失败,锁已被重置`,
data: {
lockName: this.currentLockInfo.name,
lockId: this.currentLockInfo.lockId,
uid: $user.userInfo.uid,
nickname: $user.userInfo.nickname,
mobile: $user.userInfo.mobile,
email: $user.userInfo.email
}
})
} else if(srerchResult.code === -3) {
log.info({
code: srerchResult.code,
message: `开门失败设备已找到但uuid异常`,
data: {
lockName: this.currentLockInfo.name,
lockId: this.currentLockInfo.lockId,
uid: $user.userInfo.uid,
nickname: $user.userInfo.nickname,
mobile: $user.userInfo.mobile,
email: $user.userInfo.email
}
})
} else if(srerchResult.code === -4) {
log.info({
code: srerchResult.code,
message: `开门失败,未搜索到操作设备`,
data: {
lockName: this.currentLockInfo.name,
lockId: this.currentLockInfo.lockId,
uid: $user.userInfo.uid,
nickname: $user.userInfo.nickname,
mobile: $user.userInfo.mobile,
email: $user.userInfo.email
}
})
} else if(srerchResult.code === -22) {
log.info({
code: srerchResult.code,
message: `开门失败,微信附近设备权限未开启`,
data: {
lockName: this.currentLockInfo.name,
lockId: this.currentLockInfo.lockId,
uid: $user.userInfo.uid,
nickname: $user.userInfo.nickname,
mobile: $user.userInfo.mobile,
email: $user.userInfo.email
}
})
} else if(srerchResult.code === -23) {
log.info({
code: srerchResult.code,
message: `开门失败,微信定位权限未开启`,
data: {
lockName: this.currentLockInfo.name,
lockId: this.currentLockInfo.lockId,
uid: $user.userInfo.uid,
nickname: $user.userInfo.nickname,
mobile: $user.userInfo.mobile,
email: $user.userInfo.email
}
})
}
if(srerchResult.code !== 0) {
return { code: -1 }
}
this.updateCurrentLockInfo({
...this.currentLockInfo,
deviceId: srerchResult.data.deviceId
})
console.log('设备ID', this.currentLockInfo.deviceId)
const result = await this.connectBluetoothDevice()
if(result) {
log.info({
code: 0,
message: `开门中,设备连接成功`,
data: {
lockName: this.currentLockInfo.name,
lockId: this.currentLockInfo.lockId,
uid: $user.userInfo.uid,
nickname: $user.userInfo.nickname,
mobile: $user.userInfo.mobile,
email: $user.userInfo.email
}
})
} else {
log.info({
code: -1,
message: `开门失败,设备连接失败`,
data: {
lockName: this.currentLockInfo.name,
lockId: this.currentLockInfo.lockId,
uid: $user.userInfo.uid,
nickname: $user.userInfo.nickname,
mobile: $user.userInfo.mobile,
email: $user.userInfo.email
}
})
}
console.log('连接结果', result)
if(!result) {
return {
code: -1
}
}
}
log.info({
code: 0,
message: `开门中,设备已连接`,
data: {
lockName: this.currentLockInfo.name,
lockId: this.currentLockInfo.lockId,
uid: $user.userInfo.uid,
nickname: $user.userInfo.nickname,
mobile: $user.userInfo.mobile,
email: $user.userInfo.email
}
})
// 检查是否已添加为用户
const checkResult = await this.checkLockUser()
if (!checkResult) {
log.info({
code: -1,
message: `开门失败,用户添加失败`,
data: {
lockName: this.currentLockInfo.name,
lockId: this.currentLockInfo.lockId,
uid: $user.userInfo.uid,
nickname: $user.userInfo.nickname,
mobile: $user.userInfo.mobile,
email: $user.userInfo.email
}
})
return {
code: -1
}
}
log.info({
code: 0,
message: `开门中,用户已添加`,
data: {
lockName: this.currentLockInfo.name,
lockId: this.currentLockInfo.lockId,
uid: $user.userInfo.uid,
nickname: $user.userInfo.nickname,
mobile: $user.userInfo.mobile,
email: $user.userInfo.email
}
})
const { name, uid, openMode, openTime, onlineToken } = data
const length = 2 + 40 + 20 + 1 + 4 + 4 + 1 + 16 + 16
const headArray = this.createPackageHeader(3, length)
const conentArray = new Uint8Array(length)
conentArray[0] = cmdIds.openDoor / 256
conentArray[1] = cmdIds.openDoor % 256
for(let i = 0; i < name.length; i++) {
conentArray[i + 2] = name.charCodeAt(i)
}
for (let i = 0; i < uid.length; i++) {
conentArray[i + 42] = uid.charCodeAt(i)
}
conentArray[62] = openMode
conentArray.set(this.timestampToArray(openTime), 63)
console.log('开门时token', this.currentLockInfo.token)
conentArray.set(this.currentLockInfo.token || this.timestampToArray(openTime), 67)
conentArray[71] = 16
const md5Array = this.md5Encrypte(name + uid, this.currentLockInfo.token || this.timestampToArray(openTime), this.currentLockInfo.signKey)
conentArray.set(md5Array, 72)
for (let i = 0; i < onlineToken.length; i++) {
conentArray[i + 88] = onlineToken.charCodeAt(i)
}
const cebArray = sm4.encrypt(conentArray, this.currentLockInfo.commKey, { mode: 'ecb', output: 'array' })
const packageArray = this.createPackageEnd(headArray, cebArray)
log.info({
code: 0,
message: `开门中,开始写入`,
data: {
lockName: this.currentLockInfo.name,
lockId: this.currentLockInfo.lockId,
uid: $user.userInfo.uid,
nickname: $user.userInfo.nickname,
mobile: $user.userInfo.mobile,
email: $user.userInfo.email
}
})
this.writeBLECharacteristicValue(packageArray)
return this.getWriteResult(this.openDoor, data)
} catch (e) {
log.error({
code: -1,
error: e,
message: `开门失败,未知错误`
})
return {
code: -1
}
}
const { name, uid, openMode, openTime, onlineToken } = data
const length = 2 + 40 + 20 + 1 + 4 + 4 + 1 + 16 + 16
const headArray = this.createPackageHeader(3, length)
const conentArray = new Uint8Array(length)
conentArray[0] = cmdIds.openDoor / 256
conentArray[1] = cmdIds.openDoor % 256
for(let i = 0; i < name.length; i++) {
conentArray[i + 2] = name.charCodeAt(i)
}
for (let i = 0; i < uid.length; i++) {
conentArray[i + 42] = uid.charCodeAt(i)
}
conentArray[62] = openMode
conentArray.set(this.timestampToArray(openTime), 63)
console.log('开门时token', this.currentLockInfo.token)
conentArray.set(this.currentLockInfo.token || this.timestampToArray(openTime), 67)
conentArray[71] = 16
const md5Array = this.md5Encrypte(name + uid, this.currentLockInfo.token || this.timestampToArray(openTime), this.currentLockInfo.signKey)
conentArray.set(md5Array, 72)
for (let i = 0; i < onlineToken.length; i++) {
conentArray[i + 88] = onlineToken.charCodeAt(i)
}
const cebArray = sm4.encrypt(conentArray, this.currentLockInfo.commKey, { mode: 'ecb', output: 'array' })
const packageArray = this.createPackageEnd(headArray, cebArray)
this.writeBLECharacteristicValue(packageArray)
return this.getWriteResult(this.openDoor, data)
},
// 清理用户
async cleanLockUser(data) {

30
utils/log.js Normal file
View File

@ -0,0 +1,30 @@
var log = wx.getRealtimeLogManager ? wx.getRealtimeLogManager() : null
export default {
debug() {
if (!log) return
log.debug.apply(log, arguments)
},
info() {
if (!log) return
log.info.apply(log, arguments)
},
warn() {
if (!log) return
log.warn.apply(log, arguments)
},
error() {
if (!log) return
log.error.apply(log, arguments)
},
setFilterMsg(msg) { // 从基础库2.7.3开始支持
if (!log || !log.setFilterMsg) return
if (typeof msg !== 'string') return
log.setFilterMsg(msg)
},
addFilterMsg(msg) { // 从基础库2.8.1开始支持
if (!log || !log.addFilterMsg) return
if (typeof msg !== 'string') return
log.addFilterMsg(msg)
}
}