Index.ets 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import web_webview from '@ohos.web.webview';
  2. import process from '@ohos.process'
  3. import router from '@ohos.router';
  4. import uploadInstance from "../utils/UploadUtil"
  5. const TAG = "openHarmonyBridge" //webview和vue沟通的名称
  6. // const webUrl = "http://192.168.1.4:11000/" //创客前端服务
  7. //const webUrl = "http://192.168.0.49:3005/" //本地跑的项目
  8. //const webUrl = "http://10.88.11.200:11000/" //险峰服务
  9. const webUrl = "http://192.168.2.100:11000/" //江山服务
  10. class BridgeModel {
  11. constructor() {
  12. }
  13. // this.controller.registerJavaScriptProxy 这个里面要对应
  14. download(params: string): void {
  15. // download(value: string, params: BridgeParams): string {
  16. // let downloadUrl: string = baseUrl + proxyPrefix + params.path
  17. // console.info(TAG, `download:`, downloadUrl);
  18. console.info(TAG, `params:`, params);
  19. // return true;
  20. }
  21. goToDevicePage(params: string): void {
  22. console.info(TAG, `goToDevicePage:`, params);
  23. uploadInstance.configUploadParam(params)
  24. router.pushUrl({ url: "pages/StationDevicesPage" })
  25. }
  26. selectFile(params: string): void {
  27. console.info(TAG, `selectFile:`, params);
  28. uploadInstance.configUploadParam(params)
  29. router.pushUrl({ url: "pages/SelectFilePage" })
  30. }
  31. startCamera(params: string): void {
  32. console.info(TAG, `startCamera:`, params);
  33. // 如果在web端第的登录页面传参数不对,这里要注释掉以免报错
  34. uploadInstance.configUploadParam(params)
  35. // router.pushUrl({ url: "pages/CameraPage" })
  36. router.pushUrl({
  37. url: "pages/CameraVersion2"
  38. })
  39. }
  40. exitApp(params: string): void {
  41. console.info(TAG, `exitApp:`, params);
  42. let pro = new process.ProcessManager();
  43. pro.exit(0);
  44. }
  45. }
  46. @Entry
  47. @Component
  48. struct Index {
  49. controller: web_webview.WebviewController = new web_webview.WebviewController();
  50. // 声明需要注册的对象
  51. @State bridgeObj: BridgeModel = new BridgeModel();
  52. hasRegistered: boolean = false
  53. registerJS = () => {
  54. // 这个必须要调用刷新才起作用, 而且只能执行一次
  55. if (!this.hasRegistered) {
  56. console.info(TAG, `registerJavaScriptProxy:`);
  57. this.controller.registerJavaScriptProxy(this.bridgeObj, TAG, ["download", "selectFile", "startCamera", "goToDevicePage", "exitApp"])
  58. this.controller.refresh()
  59. this.hasRegistered = true
  60. uploadInstance.controller = this.controller
  61. }
  62. }
  63. build() {
  64. Column() {
  65. // 这个webview调用相机不知道为什么只能执行一次,选择文件的回到还行,但是为了统一写法还是用registerJavaScriptProxy,但是有时候注册的方法也只能执行一次,不知道为什么。
  66. // Web({ src: $rawfile('doc_old.html'), controller: this.controller })//测试官方文档 这个是好使的
  67. // Button("跳转到相机页面").onClick(() => {
  68. // router.pushUrl({
  69. // url: "pages/CameraVersion2"
  70. // })
  71. // })
  72. Web({
  73. src: webUrl,
  74. controller: this.controller
  75. })
  76. .mixedMode(MixedMode.All)
  77. .onlineImageAccess(true)
  78. .javaScriptAccess(true)
  79. .overviewModeAccess(true)
  80. .databaseAccess(true)
  81. .domStorageAccess(true)
  82. .onShowFileSelector((event) => {
  83. console.log(TAG, "onShowFileSelector", JSON.stringify(event))
  84. // this.openGallery()
  85. // setTimeout(() => {
  86. // // 这个会触发addEventListener('change')的事件,但是每次的值都要不同。
  87. // event?.result.handleFileList([Date.now() + '.jpg']);
  88. // }, 1300)
  89. return true;
  90. })
  91. .onProgressChange((e) => {
  92. if (e && e.newProgress == 100) {
  93. this.registerJS()
  94. }
  95. })
  96. .onPermissionRequest((event) => {
  97. console.log(TAG, "onPermissionRequest")
  98. // event.request.grant([""])
  99. })
  100. .onConsole((e) => {
  101. console.log(TAG, "onConsole", JSON.stringify(e))
  102. return true
  103. })
  104. // 退出程序
  105. // Row() {
  106. // Image($r('app.media.shutdown'))
  107. // .width(px2vp(80))
  108. // .height(px2vp(80))
  109. // .onClick(() => {
  110. // let pro = new process.ProcessManager();
  111. // pro.exit(0);
  112. // })
  113. //
  114. // }.width(px2vp(1920))
  115. // .height(px2vp(80))
  116. // .justifyContent(FlexAlign.End)
  117. }
  118. .width("100%")
  119. .height("100%")
  120. }
  121. }