webViews.vue 6.2 KB

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