webViews.vue 6.7 KB

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