map-sense.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // components/map-sense.js
  2. const innerAudioContext = wx.createInnerAudioContext();
  3. import http from '../../utils/http';
  4. import {promisify,BeaconUtils} from '../../utils/util';
  5. let openBluetoothAdapter = promisify(wx.openBluetoothAdapter)
  6. let startBeaconDiscovery = promisify(wx.startBeaconDiscovery)
  7. import { CDN_URL, CONNECT_STATUS, STATUS_TEXT, API_BASE_URL } from '../../config/index';
  8. const STATUS_PIC = {
  9. '0': 'default',
  10. '1': 'loading',
  11. '2': 'success',
  12. '3': 'fail'
  13. };
  14. const TXPOWER = 0
  15. // 距离经验值(调试所得)
  16. const N = 14
  17. let AveLength = 10
  18. Component({
  19. /**
  20. * 组件的属性列表
  21. */
  22. properties: {
  23. },
  24. /**
  25. * 组件的初始数据
  26. */
  27. data: {
  28. status: "0",
  29. cdn_url: CDN_URL,
  30. connect_status: CONNECT_STATUS,
  31. status_text: STATUS_TEXT,
  32. status_pic: STATUS_PIC,
  33. targetObj: {},
  34. audio_address: {},
  35. api_base_url: API_BASE_URL,
  36. // 是否是扫码播放
  37. isScanPlay: false,
  38. cdn_url:CDN_URL
  39. },
  40. /**
  41. * 组件的方法列表
  42. */
  43. methods: {
  44. openBluetooth(cb) {
  45. openBluetoothAdapter().then(res=>{
  46. cb(res)
  47. },()=>{
  48. wx.showToast({
  49. title: '请打开蓝牙',
  50. icon: 'error',
  51. duration: 2000
  52. })
  53. })
  54. },
  55. toHandle() {
  56. let aveArr = []
  57. this.openBluetooth(()=>{
  58. startBeaconDiscovery({uuids: ['FDA50693-A4E2-4FB1-AFCF-C6EB07647825']}).then((res)=>{
  59. wx.onBeaconUpdate(data=>{
  60. if (aveArr.length == AveLength) {
  61. aveArr[0] = data.beacons
  62. }
  63. else{
  64. aveArr.push(data.beacons)
  65. }
  66. let all = []
  67. aveArr.forEach(item => {
  68. all = all.concat(item)
  69. });
  70. let classfiy = BeaconUtils.classification(all,'major')
  71. Object.keys(classfiy).forEach(key=>{
  72. //每个major的AveLength个元素数组
  73. let arr = classfiy[key].map(item=>{
  74. return item.accuracy
  75. })
  76. //每个major的平均值
  77. let ave = BeaconUtils.arrayAverage(arr)
  78. classfiy[key].forEach(item=>{
  79. item.accuracy = ave
  80. })
  81. })
  82. this.setData({
  83. classfiy,
  84. status:'2'
  85. })
  86. })
  87. }).catch(()=>{
  88. wx.showToast({
  89. title: '连接失败',
  90. icon: 'error',
  91. duration: 500
  92. })
  93. })
  94. })
  95. },
  96. stopBeaconDiscovery() {
  97. var that = this;
  98. console.log('这是取消连接')
  99. wx.showToast({
  100. title: '取消连接成功',
  101. icon: 'error',
  102. duration: 1000
  103. })
  104. wx.stopBeaconDiscovery({})
  105. // 取消连接 停止播放音乐 目标对象置为{} 设置为第一次进入
  106. innerAudioContext.stop();
  107. that.setData({
  108. status: '0',
  109. targetObj: {}
  110. })
  111. },
  112. // 扫一扫
  113. toScanCode() {
  114. var that = this;
  115. that.setData({
  116. isScanPlay: true
  117. })
  118. wx.scanCode({
  119. success(res) {
  120. console.log('success', res)
  121. console.log('result', res['result'])
  122. console.log('innerAudioContext', innerAudioContext)
  123. that.setData({ targetObj: {} })
  124. innerAudioContext.autoplay = true;
  125. innerAudioContext.src = res['result']
  126. innerAudioContext.loop = true;
  127. innerAudioContext.play();
  128. },
  129. fail: function (res) {
  130. console.log('fail innerAudioContext', innerAudioContext)
  131. that.setData({
  132. isScanPlay: false
  133. })
  134. innerAudioContext.play();
  135. return;
  136. },
  137. complete(){
  138. // console.log()
  139. that.setData({
  140. isScanPlay: false
  141. })
  142. }
  143. })
  144. },
  145. getAudios() {
  146. http.get('/api/web/getAudioIndex')
  147. .then(res => {
  148. let { data } = res,target = {};
  149. data.forEach(item => {
  150. switch (item.type) {
  151. case 1:
  152. target['10001'] = `${this.data.api_base_url}/data/${item.audio}`;
  153. break;
  154. case 2:
  155. target['10002'] = `${this.data.api_base_url}/data/${item.audio}`;
  156. break;
  157. case 3:
  158. target['10003'] = `${this.data.api_base_url}/data/${item.audio}`;
  159. break;
  160. default:
  161. break
  162. }
  163. })
  164. this.setData({
  165. audio_address: target
  166. })
  167. })
  168. }
  169. },
  170. lifetimes:{
  171. attached: function () {
  172. innerAudioContext.stop();
  173. //获取语音
  174. this.getAudios();
  175. var that = this;
  176. // wx.onAccelerometerChange(function (e) {
  177. // console.log('手机咚咚咚给')
  178. // if (Math.abs(e.x) > 1.1 && Math.abs(e.y) > 1.1) {
  179. // //         wx.showToast({ title: "摇一摇" })
  180. // } else if (Math.abs(e.x) > 0.07 && Math.abs(e.y) > 0.02 && that.data.status === '2') {
  181. // // 扫一扫播放的话 移动无效
  182. // if (that.data.isScanPlay) return;
  183. // that.startBeaconDiscovery()
  184. // } else {
  185. // //         wx.showToast({ title: "静止" })
  186. // }
  187. // }),
  188. innerAudioContext.onEnded(() => {
  189. console.log('播放结束了')
  190. if (this.data.isScanPlay) {
  191. innerAudioContext.stop()
  192. this.setData({ isScanPlay: false })
  193. this.targetObj = {}
  194. console.log('innerAudioContext', innerAudioContext)
  195. if (this.data.status == 2) {
  196. console.log(2222222222222222)
  197. this.startBeaconDiscovery()
  198. }
  199. }
  200. })
  201. },
  202. detached: function () {
  203. innerAudioContext.stop();
  204. innerAudioContext.destroy();
  205. wx.stopBeaconDiscovery({})
  206. this.setData({status:"0"})
  207. }
  208. }
  209. })