2 lines
28 KiB
JavaScript
2 lines
28 KiB
JavaScript
|
|
"use strict";const e=require("../common/vendor.js"),t=require("./env.js"),o=require("./api.js"),c=require("./storage.js"),s=require("./basic.js"),i=require("./format.js"),n=require("./log.js"),a=12432,r=12433,l=12352,u=12289,d=12293,h=12292,k=12300,f=12336,I=2,g=3,m=19,w=41;let y,p,R,S=null,b=null;const v=e.defineStore("starCloud",{state:()=>({env:null,clientId:null,clientSecret:null,envVersion:"",lockInfo:null,serverTimestamp:0,timeDifference:0,searchDeviceList:[],userInfo:null,accountInfo:null,messageCount:1,isReportLog:!1}),actions:{initStarCloud(t){const{clientId:o,clientSecret:c,env:i}=t,n=e.index.getAccountInfoSync();this.envVersion=n.miniProgram.envVersion,this.isReportLog=t.isReportLog,this.env=i||"XHJ",this.clientId=o,this.clientSecret=c,s.onBLECharacteristicValueChange(this.listenCharacteristicValue)},async register(){const{code:e,data:t,message:c}=await o.starCloudCreateUser({clientId:this.clientId,clientSecret:this.clientSecret});return new s.Result(e,t,c)},logout(e){var t;const{uid:o}=e;(null==(t=this.accountInfo)?void 0:t.uid)===o&&(this.userInfo=null,this.accountInfo=null,this.lockInfo=null,c.removeStorage("starCloudToken"))},async selectLock(e){const{accountInfo:t,lockId:i}=e,n=await this.login(t);if(n.code!==s.Result.Success.code)return n;const{code:a,data:r,message:l}=await o.getLockDetailRequest({lockId:i});if(a===s.Result.Success.code){this.lockInfo=r;let e=c.getStorage("starLockList");if(e||(e={}),e[t.uid]){const o=e[t.uid].findIndex((e=>e.lockId===i));-1===o?e[t.uid].push(this.lockInfo):(this.lockInfo.token=e[t.uid][o].token,e[t.uid][o]=this.lockInfo),c.setStorage("starLockList",e)}else e[t.uid]=[this.lockInfo],c.setStorage("starLockList",e)}else{const e=c.getStorage("starLockList");if(e[t.uid]){const o=e[t.uid].findIndex((e=>e.lockId===i));if(-1!==o)return this.lockInfo=e[t.uid][o],new s.Result(s.Result.Success.code,this.lockInfo)}}return new s.Result(a,r,l)},async openDoor(t){const{accountInfo:o,type:c}=t;n.log.info(new s.Result(s.Result.Success.code,{lockName:this.lockInfo.bluetooth.bluetoothDeviceName,lockId:this.lockInfo.lockId,uid:o.uid,time:(new Date).getTime()},"开始开门"));const a=await this.login(o);if(a.code!==s.Result.Success.code)return a;if(n.log.info(new s.Result(a.code,{lockName:this.lockInfo.bluetooth.bluetoothDeviceName,lockId:this.lockInfo.lockId,uid:o.uid,time:(new Date).getTime()},`登录星云账号: ${a.message}`)),!t.connected){const e=await s.searchAndConnectDevice(this.lockInfo.bluetooth.bluetoothDeviceName);if(n.log.info(new s.Result(e.code,{lockName:this.lockInfo.bluetooth.bluetoothDeviceName,lockId:this.lockInfo.lockId,uid:o.uid,time:(new Date).getTime()},`连接设备: ${e.message}`)),e.code!==s.Result.Success.code)return e;this.updateLockInfo(e.data)}const r=await this.checkLockUser();if(r.code!==s.Result.Success.code)return r;n.log.info(new s.Result(r.code,{lockName:this.lockInfo.bluetooth.bluetoothDeviceName,lockId:this.lockInfo.lockId,uid:o.uid,time:(new Date).getTime()},`确认是否为锁用户: ${r.message}`));let l,u="";if(this.lockInfo.lockSetting.appUnlockOnline){const e=await this.getNetToken();if(e.code!==s.Result.Success.code)return e;u=e.data.token}n.log.info(new s.Result(r.code,{lockName:this.lockInfo.bluetooth.bluetoothDeviceName,lockId:this.lockInfo.lockId,uid:o.uid,time:(new Date).getTime()},`判断是否需要联网token: ${this.lockInfo.lockSetting.appUnlockOnline}`)),l="close"===c?this.lockInfo.lockSetting.appUnlockOnline?33:32:this.lockInfo.lockSetting.appUnlockOnline?1:0;const h=this.lockInfo.bluetooth.bluetoothDeviceName,k=this.accountInfo.uid.toString(),f=Math.ceil((new Date).getTime()/1e3)+this.timeDifference,I=this.createPackageHeader(3,104),g=new Uint8Array(104);g[0]=d/256,g[1]=d%256;for(let e=0;e<h.length;e++)g[e+2]=h.charCodeAt(e);for(let e=0;e<k.length;e++)g[e+42]=k.charCodeAt(e);g[62]=l,g.set(i.timestampToArray(f),63),console.log("开门时token",this.lockInfo.token),g.set(this.lockInfo.token||i.timestampToArray(f),67),g[71]=16;const m=i.md5Encrypt(h+k,this.lockInfo.token||i.timestampToArray(f),this.lockInfo.bluetooth.signKey);g.set(
|