index.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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: 'sanfan',
  16. url: 'http://175.178.26.5:20011/#/',
  17. // url: 'http://139.9.102.170:20011/#/',
  18. src: "",
  19. wv: null,
  20. canBack: false,
  21. top: 0, //webview 头部高度
  22. height: 0, //webview 高度
  23. kbHeight: 0 //键盘高度
  24. }
  25. },
  26. onBackPress() {
  27. if (wv && this.canBack) {
  28. wv.back();
  29. return true;
  30. }
  31. return false;
  32. },
  33. onReady() {
  34. // #ifdef APP-PLUS
  35. var self = this;
  36. var currentWebview = this.$scope.$getAppWebview();
  37. //此对象相当于html5plus里的plus.webview.currentWebview()。在uni-app里vue页面直接使用plus.webview.currentWebview()无效,非v3编译模式使用this.$mp.page.$getAppWebview()
  38. setTimeout(function() {
  39. wv = currentWebview.children()[0];
  40. wv.addEventListener(
  41. "progressChanged",
  42. function(e) {
  43. wv.canBack(function(e) {
  44. self.canBack = e.canBack;
  45. });
  46. },
  47. false
  48. );
  49. }, 500); //如果是页面初始化调用时,需要延时一下
  50. // #endif
  51. },
  52. onLoad() {
  53. uni.getSystemInfo({
  54. //成功获取的回调函数,返回值为系统信息
  55. success: (sysinfo) => {
  56. this.height = sysinfo.windowHeight; //自行修改,自己需要的高度 此处如底部有其他内容,可以直接---(-50)这种
  57. },
  58. complete: () => {}
  59. });
  60. let info = uni.getSystemInfoSync();
  61. this.top = info.statusBarHeight;
  62. var currentWebview = this.$scope.$getAppWebview();
  63. setTimeout(() => {
  64. this.wv = currentWebview.children()[0];
  65. this.wv.setStyle({ //设置web-view距离顶部的距离以及自己的高度,单位为px
  66. top: this.top, //此处是距离顶部的高度,应该是你页面的头部
  67. // bottom: 0, //防止输入框被软键盘遮挡
  68. height: this.height - this.top, //webview的高度
  69. scalable: false, //webview的页面是否可以缩放,双指放大缩小
  70. })
  71. // wx.setTitleNViewButtonStyle({
  72. // index:0,
  73. // styles:{
  74. // type:'back'
  75. // }
  76. // })
  77. }, 1000);
  78. // 监听键盘高度
  79. uni.onKeyboardHeightChange((obj) => {
  80. // 获取系统信息
  81. let _sysInfo = uni.getSystemInfoSync();
  82. let _heightDiff = _sysInfo.screenHeight - _sysInfo.windowHeight
  83. let _diff = obj.height - _heightDiff
  84. // 键盘高度
  85. this.kbHeight = (_diff > 0 ? _diff : 0) - 2;
  86. this.wv.setStyle({
  87. top: this.top,
  88. // webview的高度动态修改为减去键盘高度后的
  89. height: this.height - this.kbHeight,
  90. // bottom: 0,
  91. scalable: false,
  92. })
  93. })
  94. // 同时监听页面变化
  95. uni.onWindowResize(res => {
  96. if (res.size.windowHeight < this.height) {
  97. setTimeout(() => {
  98. this.wv.setStyle({
  99. top: this.top,
  100. // webview的高度动态修改为减去键盘高度后的
  101. height: this.height - this.kbHeight,
  102. // bottom: 0,
  103. scalable: false,
  104. })
  105. }, 50)
  106. } else {
  107. setTimeout(() => {
  108. this.wv.setStyle({
  109. top: this.top,
  110. // 这里可以根据自己情况微调
  111. height: this.height - this.top,
  112. bottom: 0
  113. })
  114. }, 50)
  115. }
  116. })
  117. // 监听极光连接状态
  118. uni.$on('connectStatusChange', (connectStatus) => {
  119. if (connectStatus) {
  120. // 取得应用程序对应的 RegistrationID。 只有当应用程序成功注册到 JPush 的服务器时才返回对应的值,否则返回空字符串
  121. jpushModule.getRegistrationID(result => {
  122. console.log(result, "注册ID.....")
  123. this.wv.evalJS(`getVueMessage(${JSON.stringify(result.registerID)})`);
  124. })
  125. }
  126. })
  127. },
  128. onUnload() {
  129. // 移除监听事件
  130. uni.$off('connectStatusChange')
  131. },
  132. methods: {
  133. messageData(e) {
  134. const data = e.detail.data
  135. // 文件
  136. if (data[0].type == 'file') {
  137. let url = data[0].url
  138. uni.downloadFile({
  139. url: url, // 文件的下载链接
  140. success: function(res) {
  141. // 下载成功
  142. if (res.statusCode === 200) {
  143. // 打开文件
  144. uni.openDocument({
  145. filePath: res.tempFilePath,
  146. success: function(res) {},
  147. fail: function(error) {}
  148. });
  149. }
  150. },
  151. fail: function(error) {}
  152. });
  153. //扫码
  154. } else if (data[0].type == 'scanCode') {
  155. // 允许从相机和相册扫码
  156. uni.scanCode({
  157. success: (res) => {
  158. this.wv.evalJS(
  159. `getVueMessage(${JSON.stringify(res.result)} , ${data[0].scanType})`);
  160. }
  161. });
  162. } else if (data[0].type == 'push') {
  163. uni.createPushMessage({
  164. title: "通知",
  165. content: data[0].content
  166. })
  167. } else if (data[0].type == 'message') {
  168. plus.runtime.setBadgeNumber(data[0].count)
  169. }
  170. }
  171. }
  172. }
  173. </script>
  174. <style>
  175. .content {}
  176. .status_bar {
  177. height: var(--status-bar-height);
  178. width: 100%;
  179. }
  180. </style>sss