tremble 4 years ago
parent
commit
ff1c12d082

+ 96 - 60
macaoApplets/components/map-sense/map-sense.js

@@ -14,13 +14,7 @@ const STATUS_PIC = {
   '3': 'fail'
 };
 
-const TXPOWER = 0
-
-// 距离经验值(调试所得)
-const N = 14
-
-
-let AveLength = 10
+let AveLength = 4
 
 Component({
   /**
@@ -62,11 +56,41 @@ Component({
       })
     },
     toHandle() {
-      let aveArr = []
-
+      switch (this.data.status) {
+        case '0':
+          this.openLink();
+          break;
+        case '1':
+          this.stopBeaconDiscovery();
+          break;
+        case '2':
+          this.stopBeaconDiscovery();
+          break;
+        case '3':
+          this.startBeaconDiscovery();
+          break;
+        default:
+          break
+      }
       
+    },
+
+    openLink(){
+      let aveArr = []
+      wx.showLoading({
+        mask:true,
+        title: '正在连接,请稍等',
+      })
       this.openBluetooth(()=>{
-        startBeaconDiscovery({uuids: ['FDA50693-A4E2-4FB1-AFCF-C6EB07647825']}).then((res)=>{
+        startBeaconDiscovery({uuids: ['FDA50693-A4E2-4FB1-AFCF-C6EB07647825']}).then(()=>{
+          wx.hideLoading()
+          wx.showToast({
+            title: '连接成功',
+            icon: 'success'
+          })
+          this.setData({
+            status:'2'
+          })
           wx.onBeaconUpdate(data=>{
             if (aveArr.length == AveLength) {
               aveArr[0] = data.beacons
@@ -80,47 +104,73 @@ Component({
             });
 
             let classfiy = BeaconUtils.classification(all,'major')
+            let aveAccuracy = {}
+
+            let min = {
+              id: '10001',
+              val: 10000000
+            } //取最小值
             Object.keys(classfiy).forEach(key=>{
               //每个major的AveLength个元素数组
-              let arr = classfiy[key].map(item=>{
-                return item.accuracy
+              let arr = classfiy[key].filter(item=>{
+                return item.accuracy > 0
               })
+              if (arr.length<=0) {
+                return
+              }
+
               //每个major的平均值
-              let ave = BeaconUtils.arrayAverage(arr)
+              let ave = BeaconUtils.arrayAverage(arr.map(sub=>sub.accuracy))
               classfiy[key].forEach(item=>{
                  item.accuracy = ave
               })
+              aveAccuracy[key] = ave
+              min = ave < min.val ? {id:key,val:ave} : min;
             })
 
+            console.log(min);
+
             this.setData({
               classfiy,
-              status:'2'
+              targetObj: {
+                major:min.id
+              }
             })
           })
         }).catch(()=>{
+          wx.hideLoading()
           wx.showToast({
             title: '连接失败',
-            icon: 'error',
-            duration: 500
+            icon: 'error'
           })
         })
       })
     },
     
     stopBeaconDiscovery() {
-      var that = this;
-      console.log('这是取消连接')
-      wx.showToast({
-        title: '取消连接成功',
-        icon: 'error',
-        duration: 1000
+      wx.showLoading({
+        mask:true,
+        title: '正在取消连接,请稍等',
       })
-      wx.stopBeaconDiscovery({})
-      // 取消连接 停止播放音乐 目标对象置为{} 设置为第一次进入
-      innerAudioContext.stop();
-      that.setData({
-        status: '0',
-        targetObj: {}
+      wx.stopBeaconDiscovery({
+        success:()=>{
+          wx.hideLoading()
+          wx.showToast({
+            title: '取消连接成功',
+            icon: 'success'
+          })
+          innerAudioContext.stop();
+          this.setData({
+            status: '0',
+            targetObj: {}
+          })
+        },
+        fail:()=>{
+          
+        },
+        complete:()=>{
+          
+        }
       })
     },
     // 扫一扫
@@ -159,27 +209,15 @@ Component({
 
     getAudios() {
       http.get('/api/web/getAudioIndex')
-        .then(res => {
-          let { data } = res,target = {};
-          data.forEach(item => {
-            switch (item.type) {
-              case 1:
-                target['10001'] = `${this.data.api_base_url}/data/${item.audio}`;
-                break;
-              case 2:
-                target['10002'] = `${this.data.api_base_url}/data/${item.audio}`;
-                break;
-              case 3:
-                target['10003'] = `${this.data.api_base_url}/data/${item.audio}`;
-                break;
-              default:
-                break
-            }
-          })
-          this.setData({
-            audio_address: target
-          })
+      .then(res => {
+        let { data } = res,target = {};
+        data.forEach(item => {
+          target['1000'+item.type] = `${this.data.api_base_url}/data/${item.audio}`
         })
+        this.setData({
+          audio_address: target
+        })
+      })
     }
   },
   lifetimes:{
@@ -202,19 +240,17 @@ Component({
       //     //         wx.showToast({ title: "静止" })
       //   }
       // }),
-        innerAudioContext.onEnded(() => {
-          console.log('播放结束了')
-          if (this.data.isScanPlay) {
-            innerAudioContext.stop()
-            this.setData({ isScanPlay: false })
-            this.targetObj = {}
-            console.log('innerAudioContext', innerAudioContext)
-            if (this.data.status == 2) {
-              console.log(2222222222222222)
-              this.startBeaconDiscovery()
-            }
+      innerAudioContext.onEnded(() => {
+        console.log('播放结束了')
+        if (this.data.isScanPlay) {
+          innerAudioContext.stop()
+          this.setData({ isScanPlay: false,targetObj: {}})
+          console.log('innerAudioContext', innerAudioContext)
+          if (this.data.status == 2) {
+            this.startBeaconDiscovery()
           }
-        })
+        }
+      })
     },
     detached: function () {
       innerAudioContext.stop();

+ 6 - 6
macaoApplets/components/map-sense/map-sense.wxml

@@ -2,23 +2,23 @@
 <view class="content">
   <view class="top">
     <view class="top-bottom">
-      <image class="top-bottom-img" src="{{cdn_url}}/project/big-bg-{{targetObj.major === 10001? 'brown':'blue'}}.png">
+      <image class="top-bottom-img" src="{{cdn_url}}/project/big-bg-{{targetObj.major == 10001? 'brown':'blue'}}.png">
       </image>
-      <view class="{{targetObj.major === 10001 ?'light-sound':'small-sound'}}">
+      <view class="{{targetObj.major == 10001 ?'light-sound':'small-sound'}}">
         <image class="sound-img" src="{{cdn_url}}/project/sound.png"></image>
       </view>
     </view>
     <view class="right-bottom">
-      <image class="right-bottom-img" src="{{cdn_url}}/project/right-{{targetObj.major === 10002? 'brown':'blue'}}.png">
+      <image class="right-bottom-img" src="{{cdn_url}}/project/right-{{targetObj.major == 10002? 'brown':'blue'}}.png">
       </image>
-      <view class="{{targetObj.major === 10002 ?'light-sound':'small-sound'}}">
+      <view class="{{targetObj.major == 10002 ?'light-sound':'small-sound'}}">
         <image class="sound-img" src="{{cdn_url}}/project/sound.png"></image>
       </view>
     </view>
     <view class="center-bottom">
-      <image class="center-bottom-img" src="{{cdn_url}}/project/xt-{{targetObj.major === 10003? 'brown':'blue'}}.png">
+      <image class="center-bottom-img" src="{{cdn_url}}/project/xt-{{targetObj.major == 10003? 'brown':'blue'}}.png">
       </image>
-      <view class="{{targetObj.major === 10003 ?'light-sound':'small-sound'}}">
+      <view class="{{targetObj.major == 10003 ?'light-sound':'small-sound'}}">
         <image class="sound-img" src="{{cdn_url}}/project/sound.png"></image>
       </view>
       <view class="txt">序厅</view>

+ 5 - 1
macaoApplets/components/map-sense/map-sense.wxss

@@ -82,7 +82,7 @@
   border-radius: 50%;
   opacity: 1;
   position: absolute;
-  top: 35%;
+  top: 20%;
   right: 35%;
   text-align: center;
 }
@@ -90,6 +90,10 @@
 .content .top .light-sound .sound-img {
   width: 28rpx;
   height: 26rpx;
+  position: absolute;
+  top: 50%;
+  left: 50%;
+  transform: translate(-50%,-50%);
   display: inline-block;
 }