web.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <template>
  2. <view class="web-box">
  3. <navbar ref="navbar" :config="config" backColor="#1A1A1A"></navbar>
  4. <!-- :fullscreen="false" -->
  5. <web-view id="web-view" :src="path" @message="onMessage" @error="onError()" />
  6. </view>
  7. </template>
  8. <script>
  9. let app = getApp();
  10. export default {
  11. data() {
  12. return {
  13. showWeb: false,
  14. path: '',
  15. title: '',
  16. navKey: 1,
  17. // path: 'https://www.baidu.com/',
  18. webHeight: 0,
  19. webviewStyles: {
  20. height: '180px',
  21. backgroundColor: 'red'
  22. },
  23. config: {
  24. back: true,
  25. web: true,
  26. closePath: '',
  27. title: '',
  28. color: 'black',
  29. backgroundColor: [1, '#fff'],
  30. statusBarFontColor: 'black'
  31. },
  32. }
  33. },
  34. onReady() {
  35. // #ifdef APP
  36. const currentWebview = this.$scope.$getAppWebview()
  37. // this.pageTitle = uni.getAppBaseInfo().appName
  38. setTimeout(() =>{
  39. const {
  40. statusBarHeight,
  41. screenHeight,
  42. navHeight
  43. } = app.globalData;
  44. const top = statusBarHeight + navHeight
  45. const wv = currentWebview.children()[0]
  46. wv.setStyle({
  47. top: top,
  48. bottom: 0
  49. })
  50. wv.addEventListener('titleUpdate', e => {
  51. if (e.title) {
  52. this.$set(this.config, 'title', e.title || this.title || '');
  53. // this.navKey = Math.random()
  54. this.$refs.navbar.init()
  55. }
  56. })
  57. }, 10); //如果是页面初始化调用时,需要延时一下
  58. // #endif
  59. },
  60. onLoad(opt) {
  61. this.path = opt.path
  62. this.title = opt.title
  63. },
  64. onShow() {
  65. },
  66. methods: {
  67. onError() {
  68. // console.log('页面加载失败')
  69. uni.redirectTo({
  70. url: '/pages/web-view/web-error'
  71. });
  72. },
  73. onMessage(e) {
  74. if (e.detail.data) {
  75. let {
  76. action
  77. } = e.detail.data[0];
  78. switch (action) {
  79. case "auth":
  80. this.getCheck(e.detail.data)
  81. break;
  82. }
  83. }
  84. },
  85. getCheck(data) {
  86. this.$yghttp.post('/auth/before/check', data[0]).then(res => {
  87. // console.log(res, 33333333)
  88. if (res.code == 200) {
  89. if (res.data.needConfirmAuth) {
  90. uni.setStorageSync('authInfo', data[0]);
  91. uni.setStorageSync('checkInfo', res.data);
  92. uni.navigateTo({
  93. url: '/pages/login/service-auth'
  94. })
  95. } else {
  96. uni.redirectTo({
  97. url: `/pages/web-view/Apps?path=${encodeURIComponent(res.data.redirect)}`
  98. });
  99. }
  100. }
  101. })
  102. }
  103. }
  104. }
  105. </script>
  106. <style lang="scss" scoped>
  107. </style>