// components/map-sense.js let innerAudioContext = wx.createInnerAudioContext(); innerAudioContext.autoplay = true; innerAudioContext.loop = true; import http from "../../utils/http"; import { promisify, BeaconUtils } from "../../utils/util"; let openBluetoothAdapter = promisify(wx.openBluetoothAdapter); let startBeaconDiscovery = promisify(wx.startBeaconDiscovery); import { CDN_URL, CONNECT_STATUS, STATUS_TEXT, API_BASE_URL, } from "../../config/index"; const STATUS_PIC = { 0: "default", 1: "loading", 2: "success", 3: "fail", }; let AveLength = 4; Component({ /** * 组件的属性列表 */ properties: {}, /** * 组件的初始数据 */ data: { status: "0", cdn_url: CDN_URL, connect_status: CONNECT_STATUS, status_text: STATUS_TEXT, status_pic: STATUS_PIC, targetObj: {}, audio_address: {}, api_base_url: API_BASE_URL, // 是否是扫码播放 isScanPlay: false, cdn_url: CDN_URL, }, /** * 组件的方法列表 */ methods: { openBluetooth(cb) { openBluetoothAdapter().then( (res) => { cb(res); }, () => { wx.showToast({ title: "请打开蓝牙", icon: "error", duration: 2000, }); } ); }, toHandle() { 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; } }, playMusic(url) { innerAudioContext.src =encodeURI(url); innerAudioContext.play(); }, openLink() { let aveArr = []; wx.showLoading({ mask: true, title: "正在连接,请稍等", }); this.openBluetooth(() => { 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; } else { aveArr.push(data.beacons); } let all = []; aveArr.forEach((item) => { all = all.concat(item); }); 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].filter((item) => { return item.accuracy > 0; }); if (arr.length <= 0) { return; } //每个major的平均值 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; }); this.setData({ classfiy, targetObj: { major: min.id, }, }); this.data.audio_address[min.id] && this.playMusic(this.data.audio_address[min.id]); }); }) .catch(() => { wx.hideLoading(); wx.showToast({ title: "连接失败", icon: "error", }); }); }); }, stopBeaconDiscovery() { wx.showLoading({ mask: true, title: "正在取消连接,请稍等", }); wx.stopBeaconDiscovery({ success: () => { wx.hideLoading(); wx.showToast({ title: "取消连接成功", icon: "success", }); innerAudioContext.stop(); this.setData({ status: "0", targetObj: {}, }); }, fail: () => {}, complete: () => {}, }); }, // 扫一扫 toScanCode() { this.setData({ isScanPlay: true, }); wx.scanCode({ onlyFromCamera: true, success: (res) => { this.setData({ targetObj: {} }); setTimeout(() => { this.playMusic(res["result"]) }, 800); }, fail: () => { wx.showToast({ title: "二维码识别失败", icon: "error", }); }, complete: () => { this.setData({ isScanPlay: false, }); }, }); }, getAudios() { http.get("/api/web/getAudioIndex").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: { attached: function () { innerAudioContext.stop(); //获取语音 this.getAudios(); }, detached: function () { innerAudioContext.stop(); innerAudioContext.destroy(); wx.stopBeaconDiscovery({}); this.setData({ status: "0" }); }, }, });