map-sense.js 5.7 KB

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