iotAR小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

1 lines
3.3 KiB

import{enc,pad,AES}from"crypto-js";const{encrypt,decrypt}=AES,encUtf8=enc.Utf8,padPkcs7=pad.Pkcs7,AESKey=4662573785223888,AESIv=0x9092593b7ae34,createrandkey=t=>{var a,o="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",n=o.length;let c="";for(let e=0;e<t;e+=1)a=Math.random()*n,a=Math.floor(a),c+=o.charAt(a);return c},aesEncrypt=(e,t=AESKey,a=AESIv)=>{t=encUtf8.parse(t),a=encUtf8.parse(a);let o=encrypt(e,t,{iv:a,padding:padPkcs7});return o.toString()},decjson=(e,t=AESKey,a=AESIv)=>{t=encUtf8.parse(t),a=encUtf8.parse(a);let o=decrypt(e,t,{iv:a,padding:padPkcs7});return o.toString(encUtf8)},encparam=function(e,t,a){e=aesEncrypt(JSON.stringify(e),t,a),t=createrandkey(16);return{encparam:aesEncrypt(e,t,a),enckey:t}};let beacons=null;const fakeImei=createrandkey(16),host="https://parking.baidu.com/dev";let hasUuidLinks=!1,touchBeaconUpdate=!1,beaconSetTimeout=null;const getProjectBasicInfo=o=>new Promise((t,a)=>{var e=encparam({project_id:o});wx.request({url:host+"/aispace/api/location/getprojectinfo",method:"GET",data:e,success:e=>{0===e.data.errno?e.data.data&&0<e.data.data.length?t(JSON.parse(decjson(e.data.data))):t(e.data.data):a("ak 不合法")}})}),addAddtionCodeLength=(t,a=4)=>{let o="";if(t.length<a){let e=a-t.length;for(;0<e;)o+="0",e--}return o+t},processRequestQuery=(e,t)=>{const{latitude:a,longitude:o}=e;e=[a,o,"wgs84"].join("|");let s="",n={ver:"1.0",trace:!1,prod:"test_loc_api",src:"baidu_loc_api",key:"5LONsKCadGA8T5r7gRPxoue37pFEXm8A",body:[{accesstype:3,imei:fakeImei,blelp:e,ctime:Math.ceil((new Date).getTime()/1e3).toString()}]};return t.forEach((e,t)=>{var a=e.major.toString(16).toUpperCase(),o=e.minor.toString(16).toUpperCase();const n=[e.uuid.toUpperCase(),addAddtionCodeLength(a),addAddtionCodeLength(o),e.accuracy].join("_"),c=Math.abs(e.rssi),r=Math.ceil((new Date).getTime()/1e3);s+=0<t?"|":"",s+=[n,c,r].join(";")}),n.body[0].bleios=s,n},startBeaconDiscovery=(e,t)=>{const a=(new Date).getTime();wx.startBeaconDiscovery({uuids:e,success:e=>{console.log("监听beacons成功,耗时:",(new Date).getTime()-a)},fail:e=>{t(e)}})},locationRequest=(e,o,n)=>{e=processRequestQuery(e,beacons);if(e.body[0].bleios||e.body[0].bleand){const t=encparam(e),c=(new Date).getTime();t.startEncryptTime=c,console.log(">>> 发起encrypt请求"),wx.request({url:host+"/aispace/api/location/encrypt",method:"GET",data:t,success:e=>{var{data:t,message:a}=e.data;console.log(">>> encrypt请求成功,耗时:"+((new Date).getTime()-c)),t&&0<t.length?o(JSON.parse(decjson(e.data.data))):n(a)},fail:e=>{console.log(">>> encrypt请求失败,耗时:"+((new Date).getTime()-c),e),n(e)},complete:function(){}})}},realGetLocation=(e,t,a,o)=>{locationRequest(e,a,o)},getLocation=e=>{const{projectInfo:t,successCb:a,intervalTime:o,failCb:n}=e;t&&0!==Object.keys(t).length?(startBeaconDiscovery(t.uuid,n),wx.onBeaconUpdate(e=>{hasUuidLinks=!0,clearTimeout(beaconSetTimeout),!beacons&&touchBeaconUpdate&&n({errno:1,errstr:"未扫描到附近蓝牙信息"}),touchBeaconUpdate=!0,beacons=e.beacons,beacons&&realGetLocation(t,o,a,n)}),beaconSetTimeout=setTimeout(()=>{hasUuidLinks||n({errno:1,errstr:"未扫描到附近蓝牙信息"})},5e3)):console.log(">>> projectInfo不能为空")},stopBeaconScan=()=>{wx.stopBeaconDiscovery({success:function(e){console.log(">>> 注销监听beacons成功")}})};export{getLocation,getProjectBasicInfo,stopBeaconScan};