map-sense.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 (!arr) {
  61. return
  62. }
  63. if (aveArr.length == 10) {
  64. aveArr[0] = data.beacons
  65. }
  66. else{
  67. aveArr.push(data.beacons)
  68. }
  69. aveArr.forEach(item => {
  70. item = item.map(sub=>{
  71. sub.accuracy
  72. })
  73. });
  74. console.log(aveArr,'=========');
  75. // let aaa = BeaconUtils.arrayAverage(arr)
  76. })
  77. }).catch(()=>{
  78. wx.showToast({
  79. title: '连接失败',
  80. icon: 'error',
  81. duration: 500
  82. })
  83. })
  84. })
  85. },
  86. startBeaconDiscovery() {
  87. wx.startBeaconDiscovery({
  88. uuids: ['FDA50693-A4E2-4FB1-AFCF-C6EB07647825'],
  89. success: function (res) {
  90. },
  91. fail: function (e) {
  92. wx.showToast({
  93. title: '连接失败',
  94. icon: 'error',
  95. duration: 500
  96. })
  97. }
  98. })
  99. // 超时停止扫描
  100. // setTimeout(function () {
  101. // wx.stopBeaconDiscovery({
  102. // success: function () {
  103. // }
  104. // });
  105. // }, 1 * 1000);
  106. },
  107. stopBeaconDiscovery() {
  108. var that = this;
  109. console.log('这是取消连接')
  110. wx.showToast({
  111. title: '取消连接成功',
  112. icon: 'error',
  113. duration: 1000
  114. })
  115. wx.stopBeaconDiscovery({})
  116. // 取消连接 停止播放音乐 目标对象置为{} 设置为第一次进入
  117. innerAudioContext.stop();
  118. that.setData({
  119. status: '0',
  120. targetObj: {}
  121. })
  122. },
  123. // 扫一扫
  124. toScanCode() {
  125. var that = this;
  126. that.setData({
  127. isScanPlay: true
  128. })
  129. wx.scanCode({
  130. success(res) {
  131. console.log('success', res)
  132. console.log('result', res['result'])
  133. console.log('innerAudioContext', innerAudioContext)
  134. that.setData({ targetObj: {} })
  135. innerAudioContext.autoplay = true;
  136. innerAudioContext.src = res['result']
  137. innerAudioContext.loop = true;
  138. innerAudioContext.play();
  139. },
  140. fail: function (res) {
  141. console.log('fail innerAudioContext', innerAudioContext)
  142. that.setData({
  143. isScanPlay: false
  144. })
  145. innerAudioContext.play();
  146. return;
  147. },
  148. complete(){
  149. // console.log()
  150. that.setData({
  151. isScanPlay: false
  152. })
  153. }
  154. })
  155. },
  156. getAudios() {
  157. http.get('/api/web/getAudioIndex')
  158. .then(res => {
  159. let { data } = res,target = {};
  160. data.forEach(item => {
  161. switch (item.type) {
  162. case 1:
  163. target['10001'] = `${this.data.api_base_url}/data/${item.audio}`;
  164. break;
  165. case 2:
  166. target['10002'] = `${this.data.api_base_url}/data/${item.audio}`;
  167. break;
  168. case 3:
  169. target['10003'] = `${this.data.api_base_url}/data/${item.audio}`;
  170. break;
  171. default:
  172. break
  173. }
  174. })
  175. this.setData({
  176. audio_address: target
  177. })
  178. })
  179. }
  180. },
  181. lifetimes:{
  182. attached: function () {
  183. innerAudioContext.stop();
  184. //获取语音
  185. this.getAudios();
  186. var that = this;
  187. // wx.onAccelerometerChange(function (e) {
  188. // console.log('手机咚咚咚给')
  189. // if (Math.abs(e.x) > 1.1 && Math.abs(e.y) > 1.1) {
  190. // //         wx.showToast({ title: "摇一摇" })
  191. // } else if (Math.abs(e.x) > 0.07 && Math.abs(e.y) > 0.02 && that.data.status === '2') {
  192. // // 扫一扫播放的话 移动无效
  193. // if (that.data.isScanPlay) return;
  194. // that.startBeaconDiscovery()
  195. // } else {
  196. // //         wx.showToast({ title: "静止" })
  197. // }
  198. // }),
  199. innerAudioContext.onEnded(() => {
  200. console.log('播放结束了')
  201. if (this.data.isScanPlay) {
  202. innerAudioContext.stop()
  203. this.setData({ isScanPlay: false })
  204. this.targetObj = {}
  205. console.log('innerAudioContext', innerAudioContext)
  206. if (this.data.status == 2) {
  207. console.log(2222222222222222)
  208. this.startBeaconDiscovery()
  209. }
  210. }
  211. })
  212. },
  213. detached: function () {
  214. innerAudioContext.stop();
  215. innerAudioContext.destroy();
  216. wx.stopBeaconDiscovery({})
  217. this.setData({status:"0"})
  218. }
  219. }
  220. })