webview.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <template>
  2. <view class="wv-page">
  3. <view class="art-nav wv-nav" :style="{ paddingTop: statusBar + 'px' }">
  4. <view class="art-nav-row">
  5. <view class="wv-back" @tap="closePage">
  6. <text class="wv-back-icon">←</text>
  7. </view>
  8. <text class="art-nav-title">{{ navTitle }}</text>
  9. <view class="wv-home" @tap="goHome">
  10. <image class="wv-home-icon" src="/static/icon/icon-home.png" mode="aspectFit" />
  11. </view>
  12. </view>
  13. </view>
  14. <web-view v-if="pageUrl" class="wv-frame" :style="{ height: webViewHeight + 'px' }" :webview-styles="webviewStyles"
  15. :src="pageUrl" />
  16. <view v-if="!pageUrl" class="wv-empty" :style="{ height: webViewHeight + 'px' }">
  17. <text class="wv-empty-text">{{ copy.invalidLink }}</text>
  18. </view>
  19. </view>
  20. </template>
  21. <script>
  22. export default {
  23. data() {
  24. return {
  25. statusBar: 0,
  26. navTop: 0,
  27. webViewHeight: 600,
  28. navTitle: '',
  29. pageUrl: '',
  30. webviewStyles: {
  31. progress: {
  32. color: '#4a8cff'
  33. }
  34. }
  35. }
  36. },
  37. computed: {
  38. copy() {
  39. const t = this.$t.bind(this)
  40. return {
  41. invalidLink: t('common.invalidLink'),
  42. defaultTitle: t('common.webViewTitle')
  43. }
  44. }
  45. },
  46. onLoad(option) {
  47. const sys = uni.getSystemInfoSync()
  48. this.statusBar = sys.statusBarHeight || 0
  49. const navBarInner = uni.upx2px(88)
  50. this.navTop = this.statusBar + navBarInner
  51. this.webViewHeight = (sys.windowHeight || 667) - this.navTop
  52. let title = option.title || ''
  53. try {
  54. title = decodeURIComponent(title)
  55. } catch (_) { }
  56. this.navTitle = title || this.copy.defaultTitle
  57. this.webviewStyles = {
  58. progress: {
  59. color: '#4a8cff'
  60. },
  61. hideCallback: null,
  62. top: `${this.navTop}px`,
  63. height: `${this.webViewHeight}px`
  64. }
  65. const rawUrl = option.url ? decodeURIComponent(option.url) : ''
  66. this.pageUrl = this.normalizeUrl(rawUrl)
  67. },
  68. onReady() {
  69. // #ifdef APP-PLUS
  70. if (this.pageUrl) {
  71. this.adjustAppWebviewLayout()
  72. this.injectHideScript()
  73. }
  74. // #endif
  75. },
  76. methods: {
  77. // 注入隐藏广告的脚本
  78. injectHideScript() {
  79. const currentWebview = this.$scope.$getAppWebview()
  80. const webview = currentWebview.children()[0]
  81. if (!webview) return
  82. webview.removeEventListener('loaded', this.hideCallback)
  83. this.hideCallback = function () {
  84. const currentUrl = webview.getURL()
  85. if (currentUrl) {
  86. const jsCode = `
  87. (function() {
  88. var banner = document.querySelector('.float-banner-con');
  89. if (banner) banner.style.display = 'none';
  90. var overlay = document.querySelector('.mask, .overlay, .popup-mask');
  91. if (overlay) overlay.style.display = 'none';
  92. })();
  93. `
  94. webview.evalJS(jsCode)
  95. }
  96. }
  97. webview.addEventListener('loaded', this.hideCallback)
  98. },
  99. normalizeUrl(raw) {
  100. const url = String(raw || '').trim()
  101. if (!url) return ''
  102. if (/^https?:\/\//i.test(url)) return url
  103. if (url.startsWith('//')) return `https:${url}`
  104. return ''
  105. },
  106. adjustAppWebviewLayout(retry = 0) {
  107. // #ifdef APP-PLUS
  108. const pages = getCurrentPages()
  109. const page = pages[pages.length - 1]
  110. if (!page || !page.$getAppWebview) return
  111. const currentWebview = page.$getAppWebview()
  112. const apply = () => {
  113. const children = currentWebview.children()
  114. if (!children || !children.length) {
  115. if (retry < 10) {
  116. setTimeout(() => this.adjustAppWebviewLayout(retry + 1), 200)
  117. }
  118. return
  119. }
  120. const wv = children[0]
  121. wv.setStyle({
  122. top: `${this.navTop}px`,
  123. height: `${this.webViewHeight}px`,
  124. bottom: 'auto'
  125. })
  126. }
  127. setTimeout(apply, retry === 0 ? 300 : 0)
  128. // #endif
  129. },
  130. closePage() {
  131. const pages = getCurrentPages()
  132. if (pages.length > 1) {
  133. uni.navigateBack({ delta: 1 })
  134. return
  135. }
  136. this.goHome()
  137. },
  138. goHome() {
  139. uni.switchTab({
  140. url: '/pages/index/index',
  141. fail: () => uni.reLaunch({ url: '/pages/index/index' })
  142. })
  143. }
  144. }
  145. }
  146. </script>
  147. <style scoped>
  148. @import '../article/article-shared.css';
  149. .wv-page {
  150. min-height: 100vh;
  151. background-color: #ffffff;
  152. }
  153. .wv-nav {
  154. position: relative;
  155. z-index: 999;
  156. }
  157. .wv-back {
  158. min-width: 88rpx;
  159. height: 88rpx;
  160. margin-left: -12rpx;
  161. padding: 0 8rpx;
  162. display: flex;
  163. align-items: center;
  164. justify-content: center;
  165. box-sizing: border-box;
  166. }
  167. .wv-back-icon {
  168. font-size: 52rpx;
  169. font-weight: 600;
  170. color: #1a1d26;
  171. line-height: 1;
  172. }
  173. .wv-home {
  174. min-width: 88rpx;
  175. height: 88rpx;
  176. margin-right: -12rpx;
  177. padding: 0 8rpx;
  178. display: flex;
  179. align-items: center;
  180. justify-content: center;
  181. box-sizing: border-box;
  182. }
  183. .wv-home-icon {
  184. width: 48rpx;
  185. height: 48rpx;
  186. }
  187. .wv-frame {
  188. width: 100%;
  189. }
  190. .wv-empty {
  191. display: flex;
  192. align-items: center;
  193. justify-content: center;
  194. }
  195. .wv-empty-text {
  196. font-size: 28rpx;
  197. color: #8a8d9e;
  198. }
  199. </style>