webView.vue 2.9 KB

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