web-view.uvue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <template>
  2. <view class="uni-flex-item">
  3. <web-view id="web-view" class="uni-flex-item" :style="{ 'pointer-events': pointerEvents }" :src="src"
  4. :webview-styles="webview_styles" :horizontalScrollBarAccess="horizontalScrollBarAccess" :verticalScrollBarAccess="verticalScrollBarAccess" :bounces="bounces" @message="message" @error="error" @loading="loading"
  5. @load="load" @download="download" @contentheightchange="contentheightchange" @touchstart="touchstart" @tap="tap">
  6. </web-view>
  7. <!-- #ifdef APP -->
  8. <view class="uni-padding-wrap uni-common-mt">
  9. <view class="uni-btn-v">
  10. <input class="uni-input" confirmType="go" placeholder="输入网址跳转" @confirm="confirm" maxlength="-1" />
  11. </view>
  12. <view class="uni-row uni-btn-v">
  13. <button class="uni-flex-item" type="primary" :disabled="!canGoBack" @click="back">后退</button>
  14. <button class="margin-left-5 uni-flex-item" type="primary" :disabled="!canGoForward"
  15. @click="forward">前进</button>
  16. </view>
  17. <view class="uni-row uni-btn-v">
  18. <button class="uni-flex-item" type="primary" @click="reload">重新加载</button>
  19. <button class="margin-left-5 uni-flex-item" type="primary" @click="stop">停止加载</button>
  20. </view>
  21. <view class="uni-row uni-btn-v">
  22. <button class="uni-flex-item" type="primary" @click="nativeToWeb">原生和Web通信</button>
  23. <!-- #ifdef APP-ANDROID || APP-IOS || APP-HARMONY -->
  24. <button class="margin-left-5 uni-flex-item" type="primary" @click="getContentHeight">获取内容高度</button>
  25. <!-- #endif -->
  26. </view>
  27. <view class="uni-row uni-btn-v">
  28. <button class="uni-flex-item" type="primary" @click="loadData">加载页面内容</button>
  29. </view>
  30. <view class="uni-btn-v">
  31. <navigator url="/pages/component/web-view/web-view-scroll">
  32. <button type="primary">scroll-view嵌套web-view</button>
  33. </navigator>
  34. </view>
  35. <!-- #ifdef APP-ANDROID || APP-HARMONY || APP-IOS -->
  36. <view class="uni-row uni-btn-v">
  37. <view class="uni-row uni-flex-item align-items-center">
  38. <text>显示横向滚动条</text>
  39. <switch :checked="true" @change="changeHorizontalScrollBarAccess"></switch>
  40. </view>
  41. <view class="uni-row uni-flex-item align-items-center">
  42. <text>显示竖向滚动条</text>
  43. <switch :checked="true" @change="changeVerticalScrollBarAccess"></switch>
  44. </view>
  45. </view>
  46. <view class="uni-row uni-btn-v">
  47. <view class="uni-row uni-flex-item align-items-center">
  48. <text>开启bounces</text>
  49. <switch :checked="true" @change="changeBounces"></switch>
  50. </view>
  51. </view>
  52. <!-- #endif -->
  53. <!-- #ifdef APP-IOS -->
  54. <view class="uni-row uni-btn-v" v-if="isProd() === false">
  55. <view class="uni-row uni-flex-item align-items-center">
  56. <text>前进、后退功能在Windows端需要打自定义基座,MAC端需要配置Xcode环境后进行真机运行或者打自定义基座</text>
  57. </view>
  58. </view>
  59. <!-- #endif -->
  60. </view>
  61. <!-- #endif -->
  62. <!-- #ifdef APP-ANDROID || APP-IOS -->
  63. <view class="safe-area-inset-bottom"></view>
  64. <!-- #endif -->
  65. </view>
  66. </template>
  67. <script>
  68. // #ifdef APP
  69. import { canWebViewGoBack, canWebViewGoForward, hasNativeView } from '@/uni_modules/uts-get-native-view';
  70. // #endif
  71. let webviewElement = null as UniWebViewElement | null
  72. export default {
  73. data() {
  74. return {
  75. src: 'https://www.dcloud.io',
  76. webview_styles: {
  77. progress: {
  78. color: '#FF3333'
  79. }
  80. },
  81. webviewContext: null as WebviewContext | null,
  82. loadError: false,
  83. horizontalScrollBarAccess: true,
  84. verticalScrollBarAccess: true,
  85. bounces: true,
  86. canGoBack: false,
  87. canGoForward: false,
  88. // 自动化测试
  89. autoTest: false,
  90. eventLoading: null as UTSJSONObject | null,
  91. eventLoad: null as UTSJSONObject | null,
  92. eventError: null as UTSJSONObject | null,
  93. eventContentHeightChange: null as UTSJSONObject | null,
  94. pointerEvents: 'auto',
  95. isTouchEnable: false
  96. }
  97. },
  98. onReady() {
  99. // #ifdef APP
  100. // TODO web 实现createWebviewContext
  101. // this.webviewContext = uni.createWebviewContext('web-view', this)
  102. // NOTE 绑定到 this 上会被代理导致无法调用方法
  103. webviewElement = uni.getElementById('web-view') as UniWebViewElement //推荐使用element,功能更丰富
  104. // console.log('url: ',this.webviewElement?.getAttribute("src"));
  105. // this.webviewElement?.setAttribute("src","https://ext.dcloud.net.cn/")
  106. // #endif
  107. },
  108. onUnload() {
  109. webviewElement = null;
  110. },
  111. methods: {
  112. getPackageName() : string {
  113. let packageName : string = ""
  114. // #ifdef APP-IOS
  115. const res = uni.getAppBaseInfo();
  116. packageName = res.bundleId
  117. // #endif
  118. return packageName
  119. },
  120. isProd() : boolean {
  121. if (this.getPackageName() == 'io.dcloud.hellouniappx') {
  122. return true
  123. }
  124. return false
  125. },
  126. back() {
  127. webviewElement?.back();
  128. },
  129. forward() {
  130. webviewElement?.forward();
  131. },
  132. reload() {
  133. webviewElement?.reload();
  134. },
  135. stop() {
  136. webviewElement?.stop();
  137. },
  138. nativeToWeb() {
  139. webviewElement?.evalJS("alert('hello uni-app x')");
  140. },
  141. // #ifdef APP-ANDROID || APP-IOS || APP-HARMONY
  142. getContentHeight() : number {
  143. const height = webviewElement?.getContentHeight() ?? 0;
  144. console.log('contentHeight', height);
  145. if (!this.autoTest) {
  146. uni.showModal({
  147. content: ' 当前内容高度: ' + height,
  148. showCancel: false
  149. });
  150. }
  151. return height;
  152. },
  153. loadData() {
  154. webviewElement?.loadData({
  155. data: '<p><a href="https://www.dcloud.io/hbuilderx.html">HBuilderX</a><br/><img src="/unidoc/zh/uni@2x.png"></img><h1>HBuilderX,轻巧、极速,极客编辑器</h1><p style="color:red;"><small>HBuilderX,轻巧、极速,极客编辑器 </small><big>HBuilderX,轻巧、极速,极客编辑器</big><strong>HBuilderX,轻巧、极速,极客编辑器</strong><i>HBuilderX,轻巧、极速,极客编辑器 </i><u>HBuilderX,轻巧、极速,极客编辑器</u><del>HBuilderX,轻巧、极速,极客编辑器</del></p><h2>uni-app x,终极跨平台方案</h2>、<p style="background-color: yellow;"><small>uni-app x,终极跨平台方案 </small><big>uni-app x,终极跨平台方案</big><strong>uni-appx,终极跨平台方案 </strong><i>uni-app x,终极跨平台方案 </i><u>uni-app x,终极跨平台方案 </u><del>uni-appx,终极跨平台方案</del></p><h3>uniCloud,js serverless云服务</h3><p style="text-decoration: line-through;"><small>uniCloud,js serverless云服务 </small><big>uniCloud,jsserverless云服务</big><strong>uniCloud,js serverless云服务 </strong><i>uniCloud,js serverless云服务 </i><u>uniCloud,jsserverless云服务</u><del>uniCloud,js serverless云服务</del></p><h4>uts,大一统语言</h4><p style="text-align: center;"><small>uts,大一统语言 </small><big>uts,大一统语言 </big><strong>uts,大一统语言</strong><i>uts,大一统语言</i><u>uts,大一统语言 </u><del>uts,大一统语言</del></p><h5>uniMPSdk,让你的App具备小程序能力</h5><h6>uni-admin,开源、现成的全端管理后台</h6><ul><li style="color: red; text-align: left;">uni-app x,终极跨平台方案</li><li style="color: green; text-align: center;">uni-app x,终极跨平台方案</li><li style="color: blue; text-align: right;">uni-app x,终极跨平台方案</li></ul><a href="https://uniapp.dcloud.net.cn">uni-app</a><br/><img src="/unidoc/zh/uni@2x.png"></img></p>',
  156. baseURL: 'https://qiniu-web-assets.dcloud.net.cn'
  157. });
  158. },
  159. // #endif
  160. message(event : UniWebViewMessageEvent) {
  161. console.log(JSON.stringify(event.detail));
  162. },
  163. error(event : UniWebViewErrorEvent) {
  164. this.loadError = true
  165. console.log(JSON.stringify(event.detail));
  166. if (this.autoTest) {
  167. this.eventError = {
  168. "tagName": event.target?.tagName,
  169. "type": event.type,
  170. "errCode": event.detail.errCode,
  171. "errMsg": event.detail.errMsg,
  172. "url": event.detail.url,
  173. "fullUrl": event.detail.fullUrl,
  174. "src": event.detail.src
  175. };
  176. }
  177. },
  178. loading(event : UniWebViewLoadingEvent) {
  179. console.log(JSON.stringify(event.detail));
  180. if (this.autoTest) {
  181. this.eventLoading = {
  182. "tagName": event.target?.tagName,
  183. "type": event.type,
  184. "src": event.detail.src
  185. };
  186. }
  187. },
  188. load(event : UniWebViewLoadEvent) {
  189. console.log(JSON.stringify(event.detail));
  190. // #ifdef APP
  191. this.canGoBack = canWebViewGoBack('web-view');
  192. this.canGoForward = canWebViewGoForward('web-view');
  193. // #endif
  194. if (this.autoTest) {
  195. this.eventLoad = {
  196. "tagName": event.target?.tagName,
  197. "type": event.type,
  198. "src": event.detail.src,
  199. "url": event.detail.url,
  200. };
  201. }
  202. },
  203. download(event : UniWebViewDownloadEvent) {
  204. console.log(JSON.stringify(event.detail));
  205. uni.showModal({
  206. content: "下载链接: " + event.detail.url + "\n文件大小: " + event.detail.contentLength / 1024 + "KB",
  207. showCancel: false
  208. });
  209. },
  210. contentheightchange(event : UniWebViewContentHeightChangeEvent) {
  211. console.log(JSON.stringify(event.detail));
  212. this.eventContentHeightChange = {
  213. "tagName": event.target?.tagName,
  214. "type": event.type,
  215. "isValidHeight": event.detail.height > 0
  216. };
  217. },
  218. confirm(event : UniInputConfirmEvent) {
  219. let url = event.detail.value;
  220. if (!url.startsWith('https://') && !url.startsWith('http://')) {
  221. url = 'https://' + url;
  222. }
  223. this.src = url;
  224. },
  225. changeHorizontalScrollBarAccess(event : UniSwitchChangeEvent) {
  226. this.horizontalScrollBarAccess = event.detail.value;
  227. },
  228. changeVerticalScrollBarAccess(event : UniSwitchChangeEvent) {
  229. this.verticalScrollBarAccess = event.detail.value;
  230. },
  231. changeBounces(event : UniSwitchChangeEvent) {
  232. this.bounces = event.detail.value;
  233. },
  234. // 自动化测试
  235. touchstart(event : UniTouchEvent) {
  236. if (this.autoTest) {
  237. this.isTouchEnable = event.touches[0].clientX > 0 && event.touches[0].clientY > 0;
  238. }
  239. },
  240. tap(event : UniPointerEvent) {
  241. if (this.autoTest) {
  242. this.isTouchEnable = event.clientX > 0 && event.clientY > 0;
  243. }
  244. },
  245. //自动化测试专用
  246. checkNativeWebView() : boolean {
  247. // #ifdef APP
  248. return hasNativeView('web-view')
  249. // #endif
  250. // #ifdef WEB
  251. return true
  252. // #endif
  253. }
  254. }
  255. }
  256. </script>
  257. <style>
  258. .margin-left-5 {
  259. margin-left: 5px;
  260. }
  261. .align-items-center {
  262. align-items: center;
  263. }
  264. .safe-area-inset-bottom {
  265. height: var(--uni-safe-area-inset-bottom);
  266. }
  267. </style>