webViews.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <template>
  2. <web-view :src="src">
  3. </web-view>
  4. </template>
  5. <script>
  6. import { sendPos } from "@/api/core/api";
  7. import {
  8. server_url
  9. } from '@/utils/config.js'
  10. var wv;
  11. const bgAudioManager = uni.getBackgroundAudioManager();
  12. export default {
  13. data() {
  14. return {
  15. timer: '',
  16. src: '',
  17. query: {},
  18. webviewServer: server_url,
  19. uuid: '',
  20. bgAudioManager: '',
  21. lastPlayTime: 0
  22. };
  23. },
  24. onLoad(query) {
  25. this.query = query
  26. this.initData(query)
  27. },
  28. onReady() {
  29. // #ifdef APP-PLUS
  30. var self = this;
  31. var currentWebview = this.$scope
  32. .$getAppWebview(); //此对象相当于html5plus里的plus.webview.currentWebview()。在uni-app里vue页面直接使用plus.webview.currentWebview()无效,非v3编译模式使用this.$mp.page.$getAppWebview()
  33. setTimeout(function () {
  34. wv = currentWebview.children()[0];
  35. wv.addEventListener(
  36. "progressChanged",
  37. function (e) {
  38. wv.canBack(function (e) {
  39. self.canBack = e.canBack;
  40. });
  41. },
  42. false
  43. );
  44. }, 500); //如果是页面初始化调用时,需要延时一下
  45. // #endif
  46. },
  47. // 设备上点击返回按钮时的处理
  48. onBackPress(e) {
  49. if (wv && this.canBack) {
  50. wv.back();
  51. } else {
  52. // 没有可返回的页面了, 可以做些其他的处理, 比如回首页等等
  53. }
  54. return true;
  55. },
  56. /* onNavigationBarButtonTap(e) {
  57. // 返回
  58. if (e.index === 0) {
  59. // #ifdef H5
  60. uni.navigateBack();
  61. // #endif
  62. // #ifdef APP-PLUS
  63. if (this.canBack) {
  64. wv.back();
  65. } else {
  66. // 没有可返回的页面了, 可以做些其他的处理, 比如回首页等等
  67. }
  68. // #endif
  69. }
  70. // 首页
  71. if (e.index === 1) {
  72. // 显示tabbar
  73. //uni.showTabBar({
  74. // animation: false
  75. //});
  76. uni.switchTab({
  77. url: '/pages/index/index'
  78. });
  79. }
  80. }, */
  81. watch: {
  82. authtoken: {
  83. immediate: true,
  84. deep: true,
  85. handler(newVal) {
  86. // console.log(newVal,'newValnewValnewVal')
  87. // this.initData(this.query)
  88. }
  89. }
  90. },
  91. computed: {
  92. token() {
  93. return this.$store.state.user.token
  94. }
  95. },
  96. methods: {
  97. PlayVoice(url) {
  98. //测试背景音频播放
  99. try {
  100. // 检查是否在8秒防重复时间内
  101. const now = Date.now();
  102. if (this.lastPlayTime && (now - this.lastPlayTime) < 7500) {
  103. console.log('8秒内重复调用,跳过本次播放');
  104. return;
  105. }
  106. console.log('调用语音播放');
  107. bgAudioManager.title = '报警播报';
  108. bgAudioManager.singer = '暂无';
  109. bgAudioManager.coverImgUrl = 'https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/music-a.png';
  110. bgAudioManager.src = url;
  111. bgAudioManager.play()
  112. bgAudioManager.onPlay(() => {
  113. console.log('开始播放');
  114. });
  115. bgAudioManager.onError((res) => {
  116. console.log('播放错误', res);
  117. });
  118. // 更新上次播放时间
  119. // this.lastPlayTime = now;
  120. } catch (error) {
  121. //TODO handle the exception
  122. console.log('语音播报失败', error)
  123. }
  124. },
  125. getUuid() {
  126. var uuid = "";
  127. // #ifdef APP-PLUS
  128. if (plus.os.name == "Android") {
  129. try {
  130. var Build = plus.android.importClass("android.os.Build");
  131. var serial = Build.SERIAL;
  132. var mainActivity = plus.android.runtimeMainActivity();
  133. var Settings = plus.android.importClass("android.provider.Settings");
  134. var ANDROID_ID = Settings.Secure.getString(mainActivity.getContentResolver(), Settings.Secure
  135. .ANDROID_ID)
  136. uuid = `${ANDROID_ID}-${serial}`
  137. } catch (e) {
  138. console.log("获取设备唯一标识失败:", e);
  139. }
  140. }
  141. // #endif
  142. return uuid;
  143. },
  144. initData(query) {
  145. let urlParams = ''
  146. for (let key in query) {
  147. if (key != 'token') {
  148. urlParams += `&${key}=${query[key]}`
  149. }
  150. }
  151. urlParams += `&uuid=${this.getUuid()}`
  152. urlParams = urlParams.replace('&', '')
  153. this.src = `${this.webviewServer}/mobile/pages/attachedVessel/index/?${urlParams}`
  154. console.log('webview地址', this.src)
  155. this.uuid = this.getUuid()
  156. try {
  157. let this_ = this
  158. // 高精度定位
  159. this.timer = setInterval(() => {
  160. uni.getLocation({
  161. type: 'gcj02',
  162. // isHighAccuracy: true,
  163. success: function (res) {
  164. console.log('持续位置信息', res)
  165. // console.log('当前位置的经度:' + res.longitude);
  166. // console.log('当前位置的纬度:' + res.latitude);
  167. let datas = {
  168. deviceId: this_.uuid,
  169. gpsTime: this_.dayjs().format('YYYY-MM-DD HH:mm:ss'),
  170. lat02: res.latitude,
  171. lng02: res.longitude,
  172. accuracy: res.accuracy,
  173. speed: res.speed
  174. }
  175. console.log('上传定位信息', datas)
  176. sendPos(datas).then(res => {
  177. console.log('位置信息上传成功', res)
  178. if (res.code == 0) {
  179. this_.PlayVoice(`https://yhdd.dasongyuhe.com${res.data}`);
  180. }
  181. }).catch(err => {
  182. console.log('位置信息上传失败', err)
  183. })
  184. }
  185. });
  186. }, 8 * 1000)
  187. // 持续定位
  188. // uni.startLocationUpdate({
  189. // type: 'gcj02',
  190. // // success: res => console.log('开启小程序接收位置消息成功'),
  191. // fail: err => console.error('开启接收位置消息失败:', err),
  192. // complete: msg => console.log('调用开启接收位置消息 API 完成')
  193. // });
  194. // uni.onLocationChange((res) => {
  195. // console.log('持续位置信息', res)
  196. // let datas = {
  197. // deviceId: this_.uuid,
  198. // gpsTime: this_.dayjs().format('YYYY-MM-DD HH:mm:ss'),
  199. // lat02: res.latitude,
  200. // lng02: res.longitude,
  201. // accuracy: res.accuracy,
  202. // speed: res.speed
  203. // }
  204. // console.log('上传定位信息', datas)
  205. // sendPos(datas).then(res => {
  206. // console.log('位置信息上传成功', res)
  207. // if (res.code == 0) {
  208. // this_.PlayVoice(`https://yhdd.dasongyuhe.com${res.data}`);
  209. // }
  210. // }).catch(err => {
  211. // console.log('位置信息上传失败', err)
  212. // })
  213. // });
  214. // uni.startLocationUpdateBackground({
  215. // type:'gcj02',
  216. // // success: res => console.log('开启小程序接收位置消息成功'),
  217. // fail: err => console.error('开启接收位置消息失败:', err),
  218. // complete: msg => console.log('调用开启接收位置消息 API 完成')
  219. // });
  220. } catch (error) {
  221. console.log('获取定位失败', error)
  222. }
  223. },
  224. getLocation() {
  225. },
  226. scanCode() {
  227. }
  228. }
  229. }
  230. </script>
  231. <style lang="scss"></style>