webViews.vue 7.0 KB

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