Index.ets 4.3 KB

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