index.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <template>
  2. <view class="content">
  3. <view class="status_bar">
  4. <!-- 这里是状态栏 -->
  5. </view>
  6. <web-view :src="url" :fullscreen='false' @message="messageData"></web-view>
  7. </view>
  8. </template>
  9. <script>
  10. // const jpushModule = uni.requireNativePlugin('JG-JPush')
  11. var wv
  12. export default {
  13. data() {
  14. return {
  15. title: 'jxst',
  16. // url: 'http://139.9.102.170:10012/#/',
  17. url: 'http://139.9.102.170:10023/#/',
  18. src: "",
  19. canBack: false,
  20. }
  21. },
  22. onBackPress() {
  23. if (wv && this.canBack) {
  24. wv.back();
  25. return true;
  26. }
  27. return false;
  28. },
  29. onReady() {
  30. // #ifdef APP-PLUS
  31. var self = this;
  32. var currentWebview = this.$scope.$getAppWebview();
  33. //此对象相当于html5plus里的plus.webview.currentWebview()。在uni-app里vue页面直接使用plus.webview.currentWebview()无效,非v3编译模式使用this.$mp.page.$getAppWebview()
  34. setTimeout(function() {
  35. wv = currentWebview.children()[0];
  36. wv.addEventListener(
  37. "progressChanged",
  38. function(e) {
  39. wv.canBack(function(e) {
  40. self.canBack = e.canBack;
  41. });
  42. },
  43. false
  44. );
  45. }, 500); //如果是页面初始化调用时,需要延时一下
  46. // #endif
  47. },
  48. onLoad() {
  49. var height = 0; //定义动态的高度变量,如高度为定值,可以直接写
  50. uni.getSystemInfo({
  51. //成功获取的回调函数,返回值为系统信息
  52. success: (sysinfo) => {
  53. console.log(sysinfo)
  54. height = sysinfo.windowHeight; //自行修改,自己需要的高度 此处如底部有其他内容,可以直接---(-50)这种
  55. },
  56. complete: () => {}
  57. });
  58. let info = uni.getSystemInfoSync();
  59. let top = info.statusBarHeight;
  60. console.log(info, 12312312312);
  61. var currentWebview = this.$scope.$getAppWebview(); //获取当前web-view
  62. setTimeout(function() {
  63. var wv = currentWebview.children()[0];
  64. wv.setStyle({ //设置web-view距离顶部的距离以及自己的高度,单位为px
  65. top: top, //此处是距离顶部的高度,应该是你页面的头部
  66. // bottom:0,//防止输入框被软键盘遮挡
  67. height: height - top, //webview的高度
  68. scalable: false, //webview的页面是否可以缩放,双指放大缩小
  69. })
  70. // wx.setTitleNViewButtonStyle({
  71. // index:0,
  72. // styles:{
  73. // type:'back'
  74. // }
  75. // })
  76. }, 1000);
  77. // 监听极光连接状态
  78. uni.$on('connectStatusChange', (connectStatus) => {
  79. if (connectStatus) {
  80. var currentWebview = this.$scope.$getAppWebview();
  81. var wv = currentWebview.children()[0];
  82. // 取得应用程序对应的 RegistrationID。 只有当应用程序成功注册到 JPush 的服务器时才返回对应的值,否则返回空字符串
  83. jpushModule.getRegistrationID(result => {
  84. console.log(result, "注册ID.....")
  85. wv.evalJS(`getVueMessage(${JSON.stringify(result.registerID)})`);
  86. })
  87. }
  88. })
  89. },
  90. onUnload() {
  91. // 移除监听事件
  92. uni.$off('connectStatusChange')
  93. },
  94. methods: {
  95. messageData(e) {
  96. const data = e.detail.data
  97. var currentWebview = this.$scope.$getAppWebview(); //获取当前web-view
  98. var wv = currentWebview.children()[0];
  99. if (data[0].type == 'file') {
  100. let url = data[0].url
  101. uni.downloadFile({
  102. url: url, // 文件的下载链接
  103. success: function(res) {
  104. // 下载成功
  105. if (res.statusCode === 200) {
  106. // 打开文件
  107. uni.openDocument({
  108. filePath: res.tempFilePath,
  109. success: function(res) {},
  110. fail: function(error) {}
  111. });
  112. }
  113. },
  114. fail: function(error) {}
  115. });
  116. } else if (data[0].type == 'scanCode') {
  117. // 允许从相机和相册扫码
  118. uni.scanCode({
  119. success: (res) => {
  120. wv.evalJS(`getVueMessage(${JSON.stringify(res.result)})`);
  121. }
  122. });
  123. }
  124. }
  125. }
  126. }
  127. </script>
  128. <style>
  129. .content {}
  130. .status_bar {
  131. height: var(--status-bar-height);
  132. width: 100%;
  133. }
  134. </style>sss