webView.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <view class="web-view-container">
  3. <web-view
  4. :src="webUrl"
  5. @load="onWebLoad"
  6. @error="onWebError"
  7. @message="onWebMessage"
  8. ></web-view>
  9. </view>
  10. </template>
  11. <script>
  12. const modal = uni.requireNativePlugin('modal');
  13. const SpeechTTS = uni.requireNativePlugin('MT-TTS-Speech');
  14. export default {
  15. data() {
  16. return {
  17. webUrl: '',
  18. };
  19. },
  20. onLoad() {
  21. // 防御性检查:确保 ip/port 存在
  22. // #ifdef APP-PLUS
  23. var ip = plus.storage.getItem("ip");
  24. var port = plus.storage.getItem("port");
  25. if (!ip || !port) {
  26. uni.showModal({
  27. title: '提示',
  28. content: '服务器地址未配置,请先配置',
  29. showCancel: false,
  30. success: () => {
  31. uni.redirectTo({ url: '/pages/sample/settings' });
  32. }
  33. });
  34. return;
  35. }
  36. this.webUrl = 'http://' + ip + ':' + port + '/login_pda';
  37. // #endif
  38. this.speak_init();
  39. },
  40. onShow() {
  41. // 从 settings 页面返回时,重新加载 webUrl
  42. // #ifdef APP-PLUS
  43. var ip = plus.storage.getItem("ip");
  44. var port = plus.storage.getItem("port");
  45. if (ip && port) {
  46. var newUrl = 'http://' + ip + ':' + port + '/login_pda';
  47. if (this.webUrl !== newUrl) {
  48. this.webUrl = newUrl;
  49. }
  50. }
  51. // #endif
  52. },
  53. onUnload() {
  54. SpeechTTS.destroy();
  55. },
  56. methods: {
  57. speak_init() {
  58. SpeechTTS.init((callback) => {
  59. SpeechTTS.setEngine("com.google.android.tts");
  60. SpeechTTS.setPitch(50);
  61. SpeechTTS.setSpeed(65);
  62. });
  63. SpeechTTS.onDone((res) => {
  64. console.log("语音播报结束:", res);
  65. });
  66. },
  67. onWebLoad(e) {
  68. console.log('网页加载完成:', e.detail.url);
  69. },
  70. onWebError(e) {
  71. console.error('网页加载失败:', e);
  72. uni.showToast({
  73. title: '网页加载失败,请检查地址',
  74. icon: 'none',
  75. duration: 2000
  76. });
  77. },
  78. onWebMessage(e) {
  79. console.log('收到网页消息1:', e.detail);
  80. if (e.detail && e.detail.data) {
  81. const msgData = e.detail.data[0]
  82. SpeechTTS.speak({
  83. text: msgData.text,
  84. });
  85. modal.toast({
  86. message: msgData.text,
  87. duration: 6,
  88. });
  89. }
  90. }
  91. }
  92. };
  93. </script>
  94. <style scoped>
  95. .web-view-container {
  96. width: 100vw;
  97. height: 100vh;
  98. margin: 0 !important;
  99. padding: 0 !important;
  100. position: relative;
  101. }
  102. uni-web-view, web-view {
  103. width: 100%;
  104. height: 100%;
  105. margin: 0 !important;
  106. padding: 0 !important;
  107. }
  108. .fab-btn {
  109. position: fixed;
  110. right: 20px;
  111. bottom: 40px;
  112. width: 50px;
  113. height: 50px;
  114. border-radius: 50%;
  115. background: rgba(0, 0, 0, 0.5);
  116. color: #fff;
  117. font-size: 24px;
  118. text-align: center;
  119. line-height: 50px;
  120. z-index: 999;
  121. }
  122. </style>