autoWebview.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* eslint-disable */
  2. 'ui';
  3. // 一. 开发使用本地地址调试
  4. initUi('http://192.168.2.24:5666/#/')
  5. // 二.上线
  6. // initUi('http://xx.com/#/')
  7. /**
  8. * 初始化 UI
  9. * @param {string} htmlPath html文件的地址
  10. */
  11. function initUi (htmlPath) {
  12. ui.layout('<webview id="web" h="*" w="*" />')
  13. webViewExpand_init(ui.web)
  14. if (htmlPath.indexOf('http') === 0) {
  15. ui.web.loadUrl(htmlPath)
  16. } else {
  17. let path = 'file:' + files.path(htmlPath)
  18. ui.web.loadUrl(path)
  19. }
  20. }
  21. /**
  22. * 执行 js
  23. * @template T
  24. * @param {*} webViewWidget webview对象
  25. * @param {string} script 脚本
  26. * @param {(data:T)=>void} callback 回调函数
  27. */
  28. function callJavaScript (webViewWidget, script, callback) {
  29. try {
  30. console.assert(webViewWidget != null, 'webView控件为空')
  31. // runtimeLog(script.toString())
  32. webViewWidget.evaluateJavascript(
  33. 'javascript:' + script,
  34. new JavaAdapter(android.webkit.ValueCallback, {
  35. onReceiveValue: (val) => {
  36. if (callback) {
  37. callback(val)
  38. }
  39. },
  40. }),
  41. )
  42. } catch (e) {
  43. console.error('执行JavaScript失败')
  44. console.trace(e)
  45. }
  46. }
  47. /**
  48. * 执行 js
  49. * @template T
  50. * @param {string} code 欲执行的js代码
  51. * @param {(data:T)=>void} callback 回调函数
  52. */
  53. function callJs (code, callback) {
  54. callJavaScript(ui.web, code, callback)
  55. }
  56. function bridgeHandler_handle (cmd, args) {
  57. let ret
  58. // 执行 js代码
  59. if (args == '[code]') {
  60. ret = eval(cmd)
  61. } else {
  62. // 调用方法
  63. /** @type {Function} */
  64. let fun = this[cmd]
  65. if (!fun) {
  66. throw new Error('cmd= ' + cmd + ' 没有定义实现')
  67. }
  68. // 判断参数类型
  69. if (Array.isArray(args)) {
  70. ret = fun.apply(this, args)
  71. } else {
  72. ret = fun.call(this, args)
  73. }
  74. }
  75. return ret
  76. }
  77. function webViewExpand_init (webview) {
  78. webview.webViewClient = new JavaAdapter(android.webkit.WebViewClient, {
  79. /** 页面开始加载, 此时还没有加载 index.html 中的代码 */
  80. onPageStarted: (webView, url, favicon) => {
  81. },
  82. /** 页面加载完成, 在 window.onload 之后触发 */
  83. onPageFinished: (webView, curUrl) => {
  84. },
  85. onReceivedError: (webView, webResourceRequest, webResourceError) => {
  86. let url = webResourceRequest.getUrl()
  87. let errorCode = webResourceError.getErrorCode()
  88. let description = webResourceError.getDescription()
  89. console.trace(errorCode + ' ' + description + ' ' + url)
  90. },
  91. })
  92. webview.webChromeClient = new JavaAdapter(android.webkit.WebChromeClient, {
  93. /** 拦截 web console 消息 */
  94. onConsoleMessage: (consoleMessage) => {
  95. /** @type {string} */
  96. let msg = consoleMessage.message()
  97. let sourceId = consoleMessage.sourceId().split('/')
  98. let sourceIdStr = sourceId[sourceId.length - 1]
  99. let lineNumber = consoleMessage.lineNumber()
  100. let msgLevel = consoleMessage.messageLevel()
  101. if (msg.indexOf('jsbridge://') == 0) {
  102. let uris = msg.split('/')
  103. /** @type {{cmd:string, callId:number, args:any}} */
  104. let data = JSON.parse(java.net.URLDecoder.decode(uris[2], 'UTF-8'))
  105. let cmd = data.cmd
  106. let callId = data.callId
  107. let args = data.args
  108. runtimeLog('⭐ AJ 收到调用请求:', JSON.stringify(data).slice(0, 50))
  109. let result = null
  110. try {
  111. result = bridgeHandler_handle(cmd, args)
  112. } catch (e) {
  113. console.trace(e)
  114. result = {
  115. message: e.message,
  116. }
  117. }
  118. let callbackArgs = {
  119. callId: callId,
  120. args: result,
  121. }
  122. runtimeLog("11",JSON.stringify(callbackArgs))
  123. // 调用 web , 完成回调
  124. callJs(['auto.callback(', JSON.stringify(callbackArgs), ')'].join(''))
  125. } else {
  126. runtimeLog('📖 浏览器日志: %s [%s:%s] %s ', msgLevel, sourceIdStr, lineNumber, msg)
  127. }
  128. },
  129. })
  130. }
  131. /**
  132. * 框架 log
  133. * @description 注释方法中的代码就可以关闭所有的框架日志了.
  134. */
  135. function runtimeLog () {
  136. console.log.apply(this, arguments)
  137. }