map-sense.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. let AveLength = 4
  15. Component({
  16. /**
  17. * 组件的属性列表
  18. */
  19. properties: {
  20. },
  21. /**
  22. * 组件的初始数据
  23. */
  24. data: {
  25. status: "0",
  26. cdn_url: CDN_URL,
  27. connect_status: CONNECT_STATUS,
  28. status_text: STATUS_TEXT,
  29. status_pic: STATUS_PIC,
  30. targetObj: {},
  31. audio_address: {},
  32. api_base_url: API_BASE_URL,
  33. // 是否是扫码播放
  34. isScanPlay: false,
  35. cdn_url:CDN_URL
  36. },
  37. /**
  38. * 组件的方法列表
  39. */
  40. methods: {
  41. openBluetooth(cb) {
  42. openBluetoothAdapter().then(res=>{
  43. cb(res)
  44. },()=>{
  45. wx.showToast({
  46. title: '请打开蓝牙',
  47. icon: 'error',
  48. duration: 2000
  49. })
  50. })
  51. },
  52. toHandle() {
  53. switch (this.data.status) {
  54. case '0':
  55. this.openLink();
  56. break;
  57. case '1':
  58. this.stopBeaconDiscovery();
  59. break;
  60. case '2':
  61. this.stopBeaconDiscovery();
  62. break;
  63. case '3':
  64. this.startBeaconDiscovery();
  65. break;
  66. default:
  67. break
  68. }
  69. },
  70. openLink(){
  71. let aveArr = []
  72. wx.showLoading({
  73. mask:true,
  74. title: '正在连接,请稍等',
  75. })
  76. this.openBluetooth(()=>{
  77. startBeaconDiscovery({uuids: ['FDA50693-A4E2-4FB1-AFCF-C6EB07647825']}).then(()=>{
  78. wx.hideLoading()
  79. wx.showToast({
  80. title: '连接成功',
  81. icon: 'success'
  82. })
  83. this.setData({
  84. status:'2'
  85. })
  86. wx.onBeaconUpdate(data=>{
  87. if (aveArr.length == AveLength) {
  88. aveArr[0] = data.beacons
  89. }
  90. else{
  91. aveArr.push(data.beacons)
  92. }
  93. let all = []
  94. aveArr.forEach(item => {
  95. all = all.concat(item)
  96. });
  97. let classfiy = BeaconUtils.classification(all,'major')
  98. let aveAccuracy = {}
  99. let min = {
  100. id: '10001',
  101. val: 10000000
  102. } //取最小值
  103. Object.keys(classfiy).forEach(key=>{
  104. //每个major的AveLength个元素数组
  105. let arr = classfiy[key].filter(item=>{
  106. return item.accuracy > 0
  107. })
  108. if (arr.length<=0) {
  109. return
  110. }
  111. //每个major的平均值
  112. let ave = BeaconUtils.arrayAverage(arr.map(sub=>sub.accuracy))
  113. classfiy[key].forEach(item=>{
  114. item.accuracy = ave
  115. })
  116. aveAccuracy[key] = ave
  117. min = ave < min.val ? {id:key,val:ave} : min;
  118. })
  119. console.log(min);
  120. this.setData({
  121. classfiy,
  122. targetObj: {
  123. major:min.id
  124. }
  125. })
  126. })
  127. }).catch(()=>{
  128. wx.hideLoading()
  129. wx.showToast({
  130. title: '连接失败',
  131. icon: 'error'
  132. })
  133. })
  134. })
  135. },
  136. stopBeaconDiscovery() {
  137. wx.showLoading({
  138. mask:true,
  139. title: '正在取消连接,请稍等',
  140. })
  141. wx.stopBeaconDiscovery({
  142. success:()=>{
  143. wx.hideLoading()
  144. wx.showToast({
  145. title: '取消连接成功',
  146. icon: 'success'
  147. })
  148. innerAudioContext.stop();
  149. this.setData({
  150. status: '0',
  151. targetObj: {}
  152. })
  153. },
  154. fail:()=>{
  155. },
  156. complete:()=>{
  157. }
  158. })
  159. },
  160. // 扫一扫
  161. toScanCode() {
  162. var that = this;
  163. that.setData({
  164. isScanPlay: true
  165. })
  166. wx.scanCode({
  167. success(res) {
  168. console.log('success', res)
  169. console.log('result', res['result'])
  170. console.log('innerAudioContext', innerAudioContext)
  171. that.setData({ targetObj: {} })
  172. innerAudioContext.autoplay = true;
  173. innerAudioContext.src = res['result']
  174. innerAudioContext.loop = true;
  175. innerAudioContext.play();
  176. },
  177. fail: function (res) {
  178. console.log('fail innerAudioContext', innerAudioContext)
  179. that.setData({
  180. isScanPlay: false
  181. })
  182. innerAudioContext.play();
  183. return;
  184. },
  185. complete(){
  186. // console.log()
  187. that.setData({
  188. isScanPlay: false
  189. })
  190. }
  191. })
  192. },
  193. getAudios() {
  194. http.get('/api/web/getAudioIndex')
  195. .then(res => {
  196. let { data } = res,target = {};
  197. data.forEach(item => {
  198. target['1000'+item.type] = `${this.data.api_base_url}/data/${item.audio}`
  199. })
  200. this.setData({
  201. audio_address: target
  202. })
  203. })
  204. }
  205. },
  206. lifetimes:{
  207. attached: function () {
  208. innerAudioContext.stop();
  209. //获取语音
  210. this.getAudios();
  211. var that = this;
  212. // wx.onAccelerometerChange(function (e) {
  213. // console.log('手机咚咚咚给')
  214. // if (Math.abs(e.x) > 1.1 && Math.abs(e.y) > 1.1) {
  215. // //         wx.showToast({ title: "摇一摇" })
  216. // } else if (Math.abs(e.x) > 0.07 && Math.abs(e.y) > 0.02 && that.data.status === '2') {
  217. // // 扫一扫播放的话 移动无效
  218. // if (that.data.isScanPlay) return;
  219. // that.startBeaconDiscovery()
  220. // } else {
  221. // //         wx.showToast({ title: "静止" })
  222. // }
  223. // }),
  224. innerAudioContext.onEnded(() => {
  225. console.log('播放结束了')
  226. if (this.data.isScanPlay) {
  227. innerAudioContext.stop()
  228. this.setData({ isScanPlay: false,targetObj: {}})
  229. console.log('innerAudioContext', innerAudioContext)
  230. if (this.data.status == 2) {
  231. this.startBeaconDiscovery()
  232. }
  233. }
  234. })
  235. },
  236. detached: function () {
  237. innerAudioContext.stop();
  238. innerAudioContext.destroy();
  239. wx.stopBeaconDiscovery({})
  240. this.setData({status:"0"})
  241. }
  242. }
  243. })