Showing
9 changed files
with
219 additions
and
202 deletions
| @@ -7,7 +7,7 @@ export class Message { | @@ -7,7 +7,7 @@ export class Message { | ||
| 7 | callbackId: string = ""; //callbackId | 7 | callbackId: string = ""; //callbackId |
| 8 | responseId: string = ""; //responseId | 8 | responseId: string = ""; //responseId |
| 9 | responseData: string = ""; //responseData | 9 | responseData: string = ""; //responseData |
| 10 | - data?: Action; //data of message | 10 | + data?: object; //data of message |
| 11 | handlerName: string = ""; //name of handler | 11 | handlerName: string = ""; //name of handler |
| 12 | 12 | ||
| 13 | /** | 13 | /** |
| 1 | import { Action } from '../bean/Action' | 1 | import { Action } from '../bean/Action' |
| 2 | +import { Message } from '../bean/Message' | ||
| 2 | 3 | ||
| 3 | /** | 4 | /** |
| 4 | * 注册回调接口 | 5 | * 注册回调接口 |
| 5 | */ | 6 | */ |
| 6 | export type Callback = (data: string) => void | 7 | export type Callback = (data: string) => void |
| 7 | 8 | ||
| 8 | -export interface BridgeHandler { | ||
| 9 | - handle: (data: Action, f: Callback) => void | 9 | +export interface BridgeHandler { |
| 10 | + handle: (data: Message, f: Callback) => void | ||
| 10 | } | 11 | } |
| 11 | 12 | ||
| 12 | export class DefaultBridgeHandler implements BridgeHandler { | 13 | export class DefaultBridgeHandler implements BridgeHandler { |
| 13 | - handle(data: Action, f: Callback) { | 14 | + handle(data: Message, f: Callback) { |
| 14 | //1,2.3 | 15 | //1,2.3 |
| 15 | f("DefaultHandler response data") | 16 | f("DefaultHandler response data") |
| 16 | } | 17 | } |
| @@ -5,6 +5,7 @@ import { BridgeUtil } from '../utils/BridgeUtil'; | @@ -5,6 +5,7 @@ import { BridgeUtil } from '../utils/BridgeUtil'; | ||
| 5 | import { Message } from '../bean/Message'; | 5 | import { Message } from '../bean/Message'; |
| 6 | import { CallBackMessage } from '../bean/CallBackMessage'; | 6 | import { CallBackMessage } from '../bean/CallBackMessage'; |
| 7 | import { StringUtils } from '../utils/StringUtils'; | 7 | import { StringUtils } from '../utils/StringUtils'; |
| 8 | +import { hilog } from '@kit.PerformanceAnalysisKit'; | ||
| 8 | 9 | ||
| 9 | const TAG = 'BridgeWebViewControl'; | 10 | const TAG = 'BridgeWebViewControl'; |
| 10 | 11 | ||
| @@ -47,6 +48,7 @@ export class BridgeWebViewControl extends webview.WebviewController { | @@ -47,6 +48,7 @@ export class BridgeWebViewControl extends webview.WebviewController { | ||
| 47 | * 刷新消息 | 48 | * 刷新消息 |
| 48 | */ | 49 | */ |
| 49 | flushMessageQueue() { | 50 | flushMessageQueue() { |
| 51 | + hilog.error(0xFF00, TAG, 'flushMessageQueue'); | ||
| 50 | this.loadUrlCustom(BridgeUtil.JS_FETCH_QUEUE_FROM_JAVA, (data: string) => { | 52 | this.loadUrlCustom(BridgeUtil.JS_FETCH_QUEUE_FROM_JAVA, (data: string) => { |
| 51 | let list: Array<Message> = JSON.parse(data) | 53 | let list: Array<Message> = JSON.parse(data) |
| 52 | if (list == null || list.length == 0) { | 54 | if (list == null || list.length == 0) { |
| @@ -66,11 +68,13 @@ export class BridgeWebViewControl extends webview.WebviewController { | @@ -66,11 +68,13 @@ export class BridgeWebViewControl extends webview.WebviewController { | ||
| 66 | } else { | 68 | } else { |
| 67 | let responseFunction: Callback; | 69 | let responseFunction: Callback; |
| 68 | let callbackId: string = value.callbackId | 70 | let callbackId: string = value.callbackId |
| 71 | + let handlerName: string = value.handlerName | ||
| 69 | if (StringUtils.isNotEmpty(callbackId)) { | 72 | if (StringUtils.isNotEmpty(callbackId)) { |
| 70 | responseFunction = (data: string) => { | 73 | responseFunction = (data: string) => { |
| 71 | let msg: CallBackMessage = new CallBackMessage() | 74 | let msg: CallBackMessage = new CallBackMessage() |
| 72 | msg.responseId = callbackId | 75 | msg.responseId = callbackId |
| 73 | msg.responseData = data | 76 | msg.responseData = data |
| 77 | + msg.handlerName = handlerName | ||
| 74 | this.queueMessage(msg) | 78 | this.queueMessage(msg) |
| 75 | } | 79 | } |
| 76 | } else { | 80 | } else { |
| @@ -86,7 +90,7 @@ export class BridgeWebViewControl extends webview.WebviewController { | @@ -86,7 +90,7 @@ export class BridgeWebViewControl extends webview.WebviewController { | ||
| 86 | handle = new DefaultBridgeHandler() | 90 | handle = new DefaultBridgeHandler() |
| 87 | } | 91 | } |
| 88 | if (handle != undefined && value.data != undefined) { | 92 | if (handle != undefined && value.data != undefined) { |
| 89 | - handle.handle(value.data, responseFunction) | 93 | + handle.handle(value, responseFunction) |
| 90 | } | 94 | } |
| 91 | } | 95 | } |
| 92 | }) | 96 | }) |
| @@ -107,12 +111,15 @@ export class BridgeWebViewControl extends webview.WebviewController { | @@ -107,12 +111,15 @@ export class BridgeWebViewControl extends webview.WebviewController { | ||
| 107 | */ | 111 | */ |
| 108 | private dispatchMessage(msg: CallBackMessage) { | 112 | private dispatchMessage(msg: CallBackMessage) { |
| 109 | let messageJson: string = msg.toJson() | 113 | let messageJson: string = msg.toJson() |
| 114 | + hilog.error(0xFF00, TAG, 'dispatchMessage '+ messageJson); | ||
| 110 | // messageJson = messageJson.replace("%7B", encodeURIComponent("%7B")); | 115 | // messageJson = messageJson.replace("%7B", encodeURIComponent("%7B")); |
| 111 | // messageJson = messageJson.replace("%7D", encodeURIComponent("%7D")); | 116 | // messageJson = messageJson.replace("%7D", encodeURIComponent("%7D")); |
| 112 | // messageJson = messageJson.replace("%22", encodeURIComponent("%22")); | 117 | // messageJson = messageJson.replace("%22", encodeURIComponent("%22")); |
| 113 | 118 | ||
| 114 | let javascriptCommand: string = StringUtils.formatStringForJS(BridgeUtil.JS_HANDLE_MESSAGE_FROM_JAVA, messageJson); | 119 | let javascriptCommand: string = StringUtils.formatStringForJS(BridgeUtil.JS_HANDLE_MESSAGE_FROM_JAVA, messageJson); |
| 115 | - this.runJavaScript(javascriptCommand) | 120 | + this.runJavaScript(javascriptCommand).then((res)=>{ |
| 121 | + hilog.error(0xFF00, TAG, 'dispatchMessage res: '+ res); | ||
| 122 | + }) | ||
| 116 | } | 123 | } |
| 117 | 124 | ||
| 118 | /** | 125 | /** |
| 1 | +/** | ||
| 2 | + * h5调用native,事件id枚举, | ||
| 3 | + * h5主动调用app方法 | ||
| 4 | + */ | ||
| 5 | +export class H5CallNativeType { | ||
| 6 | + static JsCallTypeList: string[] = [] | ||
| 7 | + static jsCall_currentPageOperate = 'jsCall_currentPageOperate' | ||
| 8 | + static jsCall_getAppPublicInfo = 'jsCall_getAppPublicInfo' | ||
| 9 | + static jsCall_getArticleDetailBussinessData = 'jsCall_getArticleDetailBussinessData' | ||
| 10 | + static jsCall_callAppService = 'jsCall_callAppService' | ||
| 11 | + // TODO 业务自行新增类型、自行在JsBridgeBiz#performJSCallNative里添加接收分支处理。 | ||
| 12 | + | ||
| 13 | + static { | ||
| 14 | + H5CallNativeType.JsCallTypeList.push(H5CallNativeType.jsCall_currentPageOperate) | ||
| 15 | + H5CallNativeType.JsCallTypeList.push(H5CallNativeType.jsCall_getAppPublicInfo) | ||
| 16 | + H5CallNativeType.JsCallTypeList.push(H5CallNativeType.jsCall_getArticleDetailBussinessData) | ||
| 17 | + H5CallNativeType.JsCallTypeList.push(H5CallNativeType.jsCall_callAppService) | ||
| 18 | + } | ||
| 19 | +} | ||
| 20 | + | ||
| 21 | + |
| 1 | -import { Action } from 'wdBean'; | ||
| 2 | -import { Callback } from 'wdJsBridge'; | 1 | +import { Callback, BridgeWebViewControl } from 'wdJsBridge'; |
| 2 | +import { Message } from 'wdJsBridge/src/main/ets/bean/Message'; | ||
| 3 | import { Logger, StringUtils, } from 'wdKit'; | 3 | import { Logger, StringUtils, } from 'wdKit'; |
| 4 | -// import { AccountManagerUtils, ILoginService, Logger, ServiceManager, StringUtils, UserBean, UserInfo } from 'wdKit'; | ||
| 5 | -import { WDRouterPage, WDRouterRule } from 'wdRouter'; | 4 | +import { H5CallNativeType } from './H5CallNativeType'; |
| 6 | 5 | ||
| 7 | -// import { wdMenc } from 'wdMenc'; | 6 | +const TAG = 'JsBridgeBiz' |
| 8 | 7 | ||
| 9 | /** | 8 | /** |
| 10 | * h5调用native代码 | 9 | * h5调用native代码 |
| 11 | * @param data | 10 | * @param data |
| 12 | * @param call | 11 | * @param call |
| 13 | */ | 12 | */ |
| 14 | -export function performJSCallNative(data: Action, call: Callback) { | ||
| 15 | - | ||
| 16 | - // if (data.name == "GET_PROMOTION_SIGN") { | ||
| 17 | - // wdMenc.getActivitySign(data.params?.intfId, (sign) => { | ||
| 18 | - // if (sign) { | ||
| 19 | - // call(JSON.stringify(sign)); | ||
| 20 | - // } else { | ||
| 21 | - // call("") | ||
| 22 | - // } | ||
| 23 | - // }) | ||
| 24 | - // return | ||
| 25 | - // } | ||
| 26 | - | ||
| 27 | - //TODO 数据校验 | ||
| 28 | - switch (data.type) { | ||
| 29 | - //获取用户信息 | ||
| 30 | - case "GET_USER_INFO": | ||
| 31 | - //测试环境 | ||
| 32 | - // call(JSON.stringify(getTestUserBean())) | ||
| 33 | - // let isLogin = AccountManagerUtils.isLoginSync() | ||
| 34 | - // Logger.info("WebComponent", `GET_USER_INFO#:::refresh==` + isLogin); | ||
| 35 | - // | ||
| 36 | - // if (isLogin) { | ||
| 37 | - // const loginService = ServiceManager.getService(ILoginService.name) as ILoginService.Service | ||
| 38 | - // let userBean: UserBean = loginService.getUserBean() | ||
| 39 | - // // userBean.userInfo = JSON.stringify(loginService.getUserInfo()) | ||
| 40 | - // call(JSON.stringify(userBean)) | ||
| 41 | - // } else { | ||
| 42 | - // call("fail") | ||
| 43 | - // } | ||
| 44 | - | 13 | +export function performJSCallNative(data: Message, call: Callback) { |
| 14 | + Logger.debug(TAG, 'performJSCallNative handlerName: ' + data.handlerName + ', data: ' + data.data) | ||
| 15 | + switch (data.handlerName) { | ||
| 16 | + case H5CallNativeType.jsCall_currentPageOperate: | ||
| 17 | + break; | ||
| 18 | + case H5CallNativeType.jsCall_getAppPublicInfo: | ||
| 19 | + // h5获取app配置信息 | ||
| 20 | + call(getAppPublicInfo()) | ||
| 21 | + break; | ||
| 22 | + case H5CallNativeType.jsCall_getArticleDetailBussinessData: | ||
| 23 | + break; | ||
| 24 | + case H5CallNativeType.jsCall_callAppService: | ||
| 45 | break; | 25 | break; |
| 46 | - case "JUMP_DETAIL_PAGE": | ||
| 47 | - WDRouterRule.jumpWithAction(data) | 26 | + case 'changeNativeMessage': |
| 27 | + call("this is change Web Message") | ||
| 48 | break; | 28 | break; |
| 49 | - case "JUMP_INNER_NEW_PAGE": | ||
| 50 | - WDRouterRule.jumpWithAction(data) | ||
| 51 | - break | ||
| 52 | - case "GET_DEVICE_DATA": | ||
| 53 | - break | ||
| 54 | - //用户登陆 | ||
| 55 | - case "USER_LOGIN": | ||
| 56 | - WDRouterRule.jumpWithAction(data) | ||
| 57 | - //支付完成回调 | ||
| 58 | - case "REGISTER_H5_WEBVIEW_CREATE_ORDER_RESULT": | ||
| 59 | - break | ||
| 60 | - //打开H5页面 | ||
| 61 | - case "JUMP_H5_BY_WEB_VIEW": | ||
| 62 | - WDRouterRule.jumpWithAction(data) | ||
| 63 | - break | ||
| 64 | - case "USER_LOGOUT": | ||
| 65 | - // const loginService = ServiceManager.getService(ILoginService.name) as ILoginService.Service | ||
| 66 | - // loginService.logout() | ||
| 67 | - break | ||
| 68 | default: | 29 | default: |
| 69 | call("this is def value") | 30 | call("this is def value") |
| 70 | } | 31 | } |
| 71 | 32 | ||
| 72 | } | 33 | } |
| 73 | 34 | ||
| 74 | -// /** | ||
| 75 | -// * | ||
| 76 | -// * @returns 模拟测试环境的user信息 | ||
| 77 | -// */ | ||
| 78 | -// function getTestUserBean(): UserBean { | ||
| 79 | -// const loginService = ServiceManager.getService(ILoginService.name) as ILoginService.Service | ||
| 80 | -// //正是环境 | ||
| 81 | -// let userBean: UserBean = loginService.getUserBean() | ||
| 82 | -// userBean.userId = "930855275" | ||
| 83 | -// userBean.userToken = "nlps6FADECE38F5AAD2116F2" | ||
| 84 | -// // userBean.clientId = "f1bf515c-4006-4606-a752-2b4cdd9343d0" | ||
| 85 | -// // userBean.carrierCode = "CM" | ||
| 86 | -// userBean.mobile = "NTTrcDO4ADN1cTM5MTM" | ||
| 87 | -// userBean.uname = '139****0887' | ||
| 88 | -// userBean.picture = "http://36.155.98.104:23380/publish/voms2/uic_service/picture/userImage/543/626/5181.jpg" | ||
| 89 | -// // userBean.ssotoken = "STnid0000011700463753943SKPUlfNxEDJsrauGjCwUldiDfrd0mUuM" | ||
| 90 | -// // userBean.clientProvinceCode = "210" | ||
| 91 | -// // userBean.clientCityId = "0210" | ||
| 92 | -// // userBean.sign = "617DAC548595B7C9EBB13043735F1BE0" | ||
| 93 | -// // userBean.blurMobile = "177****9217" | ||
| 94 | -// let userinfo: UserInfo = ({ | ||
| 95 | -// userId: "930855275", | ||
| 96 | -// userNum: "NzDrsyN4gDM0UzNxkzMxYDO", | ||
| 97 | -// mobile: "NTTrcDO4ADN1cTM5MTM", | ||
| 98 | -// areaId: "210", | ||
| 99 | -// cityId: "0210", | ||
| 100 | -// carrierCode: "CM", | ||
| 101 | -// passId: "602201990200225921", | ||
| 102 | -// userToken: "nlps6FADECE38F5AAD2116F2", | ||
| 103 | -// expiredOn: "1705647754000", | ||
| 104 | -// blurMobile: "139****0887", | ||
| 105 | -// encrypted: true | ||
| 106 | -// }) | ||
| 107 | -// // userBean.userInfo = JSON.stringify(userinfo); | ||
| 108 | -// return userBean | ||
| 109 | -// } | ||
| 110 | -// | ||
| 111 | -// /** | ||
| 112 | -// * | ||
| 113 | -// * @returns 用户信息 | ||
| 114 | -// */ | ||
| 115 | -// function getUserBean(): UserBean { | ||
| 116 | -// const loginService = ServiceManager.getService(ILoginService.name) as ILoginService.Service | ||
| 117 | -// //正是环境 | ||
| 118 | -// let userBean: UserBean = loginService.getUserBean() | ||
| 119 | -// userBean.userId = "1437725487" | ||
| 120 | -// userBean.userToken = "nlps08468E117C554CA08A43" | ||
| 121 | -// // userBean.clientId = "27fb3129-5a54-45bc-8af1-7dc8f1155501" | ||
| 122 | -// // userBean.carrierCode = "CT" | ||
| 123 | -// userBean.mobile = "OTTrcTMykTO4ATM3cTM" | ||
| 124 | -// userBean.uname = '小可爱啊' | ||
| 125 | -// userBean.picture = "http://img.cmvideo.cn:8080/publish/voms2/uic_service/picture/userImage/1437/725/487/20211224174128beax.png" | ||
| 126 | -// // userBean.ssotoken = "STnid0000011700461738301N5rjsHdbvyzMpyzwvHrFRJsj7oNT1Juf" | ||
| 127 | -// // userBean.clientProvinceCode = "100" | ||
| 128 | -// // userBean.clientCityId = "0100" | ||
| 129 | -// // userBean.sign = "4ABFB8442EE914B57CCD9F1DE587D96D" | ||
| 130 | -// // userBean.blurMobile = "177****9217" | ||
| 131 | -// let userinfo: UserInfo = ({ | ||
| 132 | -// userId: "1437725487", | ||
| 133 | -// userNum: "MzDrsyNxITO5gDMxczNxYDO", | ||
| 134 | -// mobile: "OTTrcTMykTO4ATM3cTM", | ||
| 135 | -// areaId: "100", | ||
| 136 | -// cityId: "0100", | ||
| 137 | -// carrierCode: "CT", | ||
| 138 | -// passId: "467464726359024540", | ||
| 139 | -// userToken: "nlps08468E117C554CA08A43", | ||
| 140 | -// expiredOn: "1705645738000", | ||
| 141 | -// blurMobile: "177****9217", | ||
| 142 | -// encrypted: true | ||
| 143 | -// }) | ||
| 144 | -// // userBean.userInfo = JSON.stringify(userinfo); | ||
| 145 | -// return userBean | ||
| 146 | -// } | 35 | +class AppInfo { |
| 36 | + plat: string = '' | ||
| 37 | + system: string = '' | ||
| 38 | + // TODO 完善 | ||
| 39 | +} | ||
| 40 | + | ||
| 41 | +/** | ||
| 42 | + * 获取App公共信息 | ||
| 43 | + */ | ||
| 44 | +function getAppPublicInfo(): string { | ||
| 45 | + let info = new AppInfo() | ||
| 46 | + info.plat = 'Phone' | ||
| 47 | + // 直接用Android,后续适配再新增鸿蒙 | ||
| 48 | + info.system = 'Android' | ||
| 49 | + let result = JSON.stringify(info) | ||
| 50 | + return result; | ||
| 51 | +} | ||
| 147 | 52 | ||
| 148 | 53 |
| @@ -5,6 +5,7 @@ import { Logger } from 'wdKit'; | @@ -5,6 +5,7 @@ import { Logger } from 'wdKit'; | ||
| 5 | import { BridgeHandler, BridgeUtil, BridgeWebViewControl, Callback } from 'wdJsBridge'; | 5 | import { BridgeHandler, BridgeUtil, BridgeWebViewControl, Callback } from 'wdJsBridge'; |
| 6 | import { performJSCallNative } from './JsBridgeBiz'; | 6 | import { performJSCallNative } from './JsBridgeBiz'; |
| 7 | import { setDefaultNativeWebSettings } from './WebComponentUtil'; | 7 | import { setDefaultNativeWebSettings } from './WebComponentUtil'; |
| 8 | +import { Message } from 'wdJsBridge/src/main/ets/bean/Message'; | ||
| 8 | 9 | ||
| 9 | const TAG = 'WdWebComponent'; | 10 | const TAG = 'WdWebComponent'; |
| 10 | 11 | ||
| @@ -30,7 +31,7 @@ export struct WdWebComponent { | @@ -30,7 +31,7 @@ export struct WdWebComponent { | ||
| 30 | /** | 31 | /** |
| 31 | * 默认【CallNative】逻辑处理 | 32 | * 默认【CallNative】逻辑处理 |
| 32 | */ | 33 | */ |
| 33 | - private defaultPerformJSCallNative: (data: Action, f: Callback) => void = (data: Action, f: Callback) => { | 34 | + private defaultPerformJSCallNative: (data: Message, f: Callback) => void = (data: Message, f: Callback) => { |
| 34 | performJSCallNative(data, f) | 35 | performJSCallNative(data, f) |
| 35 | } | 36 | } |
| 36 | /** | 37 | /** |
| @@ -41,7 +42,7 @@ export struct WdWebComponent { | @@ -41,7 +42,7 @@ export struct WdWebComponent { | ||
| 41 | 42 | ||
| 42 | defaultRegisterHandler(): void { | 43 | defaultRegisterHandler(): void { |
| 43 | this.webviewControl.registerHandler("CallNative", { | 44 | this.webviewControl.registerHandler("CallNative", { |
| 44 | - handle: (data: Action, f: Callback) => { | 45 | + handle: (data: Message, f: Callback) => { |
| 45 | this.defaultPerformJSCallNative(data, f) | 46 | this.defaultPerformJSCallNative(data, f) |
| 46 | } | 47 | } |
| 47 | }); | 48 | }); |
| 1 | import router from '@ohos.router'; | 1 | import router from '@ohos.router'; |
| 2 | -import { BridgeUtil, BridgeWebViewControl } from 'wdJsBridge'; | ||
| 3 | -import { ResourceManager } from 'wdJsBridge/src/main/ets/utils/ResourceManager'; | 2 | +import { BridgeHandler, BridgeUtil, BridgeWebViewControl, Callback } from 'wdJsBridge'; |
| 3 | +import { Logger } from 'wdKit/Index'; | ||
| 4 | +import { setDefaultNativeWebSettings } from './WebComponentUtil'; | ||
| 5 | +import { Action } from 'wdBean'; | ||
| 6 | +import { performJSCallNative } from './JsBridgeBiz'; | ||
| 7 | +import { H5CallNativeType } from './H5CallNativeType'; | ||
| 8 | +import { Message } from 'wdJsBridge/src/main/ets/bean/Message'; | ||
| 4 | 9 | ||
| 5 | const TAG = 'WdWebLocalComponent'; | 10 | const TAG = 'WdWebLocalComponent'; |
| 6 | 11 | ||
| @@ -31,7 +36,25 @@ export struct WdWebLocalComponent { | @@ -31,7 +36,25 @@ export struct WdWebLocalComponent { | ||
| 31 | .javaScriptAccess(true) | 36 | .javaScriptAccess(true) |
| 32 | .onPageBegin((event) => { | 37 | .onPageBegin((event) => { |
| 33 | 38 | ||
| 34 | - }) | 39 | + // setDefaultNativeWebSettings(this.webviewControl, this.webResource).then(()=>{ |
| 40 | + // this.handleInfo && this.handleInfo.length > 1 ? this.handleInfo.forEach(value => { | ||
| 41 | + // this.webviewControl.registerHandler(value[0], value[1]) | ||
| 42 | + // }) : this.defaultRegisterHandler() | ||
| 43 | + // setTimeout(()=>{ | ||
| 44 | + // BridgeUtil.webViewLoadLocalJs(getContext(this), this.webviewControl) | ||
| 45 | + // },500) | ||
| 46 | + // }) | ||
| 47 | + // this.onPageBegin(event?.url) | ||
| 48 | + // BridgeUtil.webViewLoadLocalJs(getContext(this), this.webviewControl); | ||
| 49 | + this.onPageBegin(event?.url); | ||
| 50 | + this.registerHandlers(); | ||
| 51 | + setTimeout(() => { | ||
| 52 | + BridgeUtil.webViewLoadLocalJs(getContext(this), this.webviewControl) | ||
| 53 | + }, 200) | ||
| 54 | + }) | ||
| 55 | + .onPageEnd((event) => { | ||
| 56 | + this.onPageEnd(event?.url) | ||
| 57 | + }) | ||
| 35 | .onLoadIntercept((event) => { | 58 | .onLoadIntercept((event) => { |
| 36 | let url: string = event.data.getRequestUrl().toString() | 59 | let url: string = event.data.getRequestUrl().toString() |
| 37 | url = url.replace("%(?![0-9a-fA-F]{2})", "%25") | 60 | url = url.replace("%(?![0-9a-fA-F]{2})", "%25") |
| @@ -42,12 +65,48 @@ export struct WdWebLocalComponent { | @@ -42,12 +65,48 @@ export struct WdWebLocalComponent { | ||
| 42 | return true | 65 | return true |
| 43 | } | 66 | } |
| 44 | if (url.startsWith(BridgeUtil.YY_OVERRIDE_SCHEMA)) { | 67 | if (url.startsWith(BridgeUtil.YY_OVERRIDE_SCHEMA)) { |
| 68 | + Logger.debug(TAG, 'flushMessageQueue'); | ||
| 45 | this.webviewControl.flushMessageQueue() | 69 | this.webviewControl.flushMessageQueue() |
| 46 | return true | 70 | return true |
| 47 | } | 71 | } |
| 48 | - return false | 72 | + return this.onLoadIntercept(event.data.getRequestUrl().toString()); |
| 49 | }) | 73 | }) |
| 50 | } | 74 | } |
| 51 | } | 75 | } |
| 76 | + | ||
| 77 | + private registerHandlers(): void { | ||
| 78 | + // 注册h5调用js相关 | ||
| 79 | + for (let i = 0; i < H5CallNativeType.JsCallTypeList.length; i++) { | ||
| 80 | + let handleName = H5CallNativeType.JsCallTypeList[i]; | ||
| 81 | + let handle = (data: Message, f: Callback) => { | ||
| 82 | + this.defaultPerformJSCallNative(data, f) | ||
| 83 | + } ; | ||
| 84 | + this.webviewControl.registerHandler(handleName, { handle: handle }); | ||
| 85 | + } | ||
| 86 | + // // TODO test | ||
| 87 | + // this.webviewControl.registerHandler('changeNativeMessage', { | ||
| 88 | + // handle: (data: Message, f: Callback) => { | ||
| 89 | + // this.defaultPerformJSCallNative(data, f) | ||
| 90 | + // } | ||
| 91 | + // }); | ||
| 92 | + | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + /** | ||
| 96 | + * 默认【CallNative】逻辑处理 | ||
| 97 | + */ | ||
| 98 | + private defaultPerformJSCallNative: (data: Message, f: Callback) => void = (data: Message, f: Callback) => { | ||
| 99 | + performJSCallNative(data, f) | ||
| 100 | + } | ||
| 101 | + onPageBegin: (url?: string) => void = () => { | ||
| 102 | + Logger.debug(TAG, 'onPageBegin'); | ||
| 103 | + } | ||
| 104 | + onPageEnd: (url?: string) => void = () => { | ||
| 105 | + Logger.debug(TAG, 'onPageEnd'); | ||
| 106 | + } | ||
| 107 | + onLoadIntercept: (url?: string) => boolean = () => { | ||
| 108 | + Logger.debug(TAG, 'onPageBegin return false'); | ||
| 109 | + return false | ||
| 110 | + } | ||
| 52 | } | 111 | } |
| 53 | 112 |
| @@ -8,80 +8,91 @@ import { | @@ -8,80 +8,91 @@ import { | ||
| 8 | } from 'wdBean'; | 8 | } from 'wdBean'; |
| 9 | import { Logger } from 'wdKit'; | 9 | import { Logger } from 'wdKit'; |
| 10 | import { WdWebComponent, WdWebLocalComponent } from 'wdWebComponent'; | 10 | import { WdWebComponent, WdWebLocalComponent } from 'wdWebComponent'; |
| 11 | +import { NativeCallH5Type } from 'wdWebComponent/src/main/ets/pages/NativeCallH5Type'; | ||
| 12 | +import { BridgeWebViewControl } from '../../../../../../commons/wdWebComponent/oh_modules/wdJsBridge/Index'; | ||
| 11 | 13 | ||
| 12 | @Component | 14 | @Component |
| 13 | export struct ImageAndTextWebComponent { | 15 | export struct ImageAndTextWebComponent { |
| 14 | action: Action = {} as Action | 16 | action: Action = {} as Action |
| 15 | @State reload: number = 0; | 17 | @State reload: number = 0; |
| 16 | @Prop @Watch('onDetailDataUpdated') contentDetailData: ContentDetailDTO = {} as ContentDetailDTO | 18 | @Prop @Watch('onDetailDataUpdated') contentDetailData: ContentDetailDTO = {} as ContentDetailDTO |
| 19 | + webviewControl: BridgeWebViewControl = new BridgeWebViewControl() | ||
| 17 | 20 | ||
| 18 | onDetailDataUpdated() { | 21 | onDetailDataUpdated() { |
| 19 | - // if (this.action) { | ||
| 20 | - // let contentId: string = '' | ||
| 21 | - // let contentType: string = '' | ||
| 22 | - // let topicId: string = '' | ||
| 23 | - // let channelId: string = '' | ||
| 24 | - // let compId: string = '' | ||
| 25 | - // let sourcePage: string = '5' | ||
| 26 | - // if (this.action.params) { | ||
| 27 | - // if (this.action.params.contentID) { | ||
| 28 | - // contentId = this.action.params?.contentID | ||
| 29 | - // } | ||
| 30 | - // if (this.action.params.extra) { | ||
| 31 | - // if (this.action.params.extra.contentType) { | ||
| 32 | - // contentType = this.action.params.extra.contentType | ||
| 33 | - // } | ||
| 34 | - // if (this.action.params.extra.topicId) { | ||
| 35 | - // topicId = this.action.params.extra.topicId | ||
| 36 | - // } | ||
| 37 | - // if (this.action.params.extra.channelId) { | ||
| 38 | - // channelId = this.action.params.extra.channelId | ||
| 39 | - // } | ||
| 40 | - // if (this.action.params.extra.compId) { | ||
| 41 | - // compId = this.action.params.extra.compId | ||
| 42 | - // } | ||
| 43 | - // if (this.action.params.extra.sourcePage) { | ||
| 44 | - // sourcePage = this.action.params.extra.sourcePage | ||
| 45 | - // } | ||
| 46 | - // } | ||
| 47 | - // | ||
| 48 | - // } | ||
| 49 | - // | ||
| 50 | - // let h5ReceiveAppData: H5ReceiveDetailBean = { dataSource: '2' } as H5ReceiveDetailBean | ||
| 51 | - // let h5ReceiveDataExtraBean: H5ReceiveDataExtraBean = { creatorId: '', isLogin: '0' } as H5ReceiveDataExtraBean | ||
| 52 | - // let h5ReceiveDataJsonBean: H5ReceiveDataJsonBean = { | ||
| 53 | - // contentId: contentId, | ||
| 54 | - // contentType: contentType | ||
| 55 | - // } as H5ReceiveDataJsonBean | ||
| 56 | - // h5ReceiveDataJsonBean.topicId = topicId | ||
| 57 | - // h5ReceiveDataJsonBean.channelId = channelId | ||
| 58 | - // h5ReceiveDataJsonBean.compId = compId | ||
| 59 | - // h5ReceiveDataJsonBean.sourcePage = sourcePage | ||
| 60 | - // h5ReceiveDataJsonBean.netError = '0' | ||
| 61 | - // let response: ResponseBean = {} as ResponseBean | ||
| 62 | - // response.data = this.detailData | ||
| 63 | - // response.code = 0 | ||
| 64 | - // response.success = true | ||
| 65 | - // h5ReceiveDataJsonBean.responseMap = response | ||
| 66 | - // h5ReceiveAppData.dataJson = h5ReceiveDataJsonBean | ||
| 67 | - // h5ReceiveAppData.dataExt = h5ReceiveDataExtraBean | ||
| 68 | - // this.webviewControl.callHandle("jsCall_receiveAppData", JSON.stringify(h5ReceiveAppData), (data: string) => { | ||
| 69 | - // Logger.info("from js data = " + data); | ||
| 70 | - // }) | 22 | + if (this.action) { |
| 23 | + let contentId: string = '' | ||
| 24 | + let contentType: string = '' | ||
| 25 | + let topicId: string = '' | ||
| 26 | + let channelId: string = '' | ||
| 27 | + let compId: string = '' | ||
| 28 | + let sourcePage: string = '5' | ||
| 29 | + if (this.action.params) { | ||
| 30 | + if (this.action.params.contentID) { | ||
| 31 | + contentId = this.action.params?.contentID | ||
| 32 | + } | ||
| 33 | + if (this.action.params.extra) { | ||
| 34 | + if (this.action.params.extra.contentType) { | ||
| 35 | + contentType = this.action.params.extra.contentType | ||
| 36 | + } | ||
| 37 | + if (this.action.params.extra.topicId) { | ||
| 38 | + topicId = this.action.params.extra.topicId | ||
| 39 | + } | ||
| 40 | + if (this.action.params.extra.channelId) { | ||
| 41 | + channelId = this.action.params.extra.channelId | ||
| 42 | + } | ||
| 43 | + if (this.action.params.extra.compId) { | ||
| 44 | + compId = this.action.params.extra.compId | ||
| 45 | + } | ||
| 46 | + if (this.action.params.extra.sourcePage) { | ||
| 47 | + sourcePage = this.action.params.extra.sourcePage | ||
| 48 | + } | ||
| 49 | + } | ||
| 71 | 50 | ||
| 72 | - // } | 51 | + } |
| 52 | + | ||
| 53 | + let h5ReceiveAppData: H5ReceiveDetailBean = { dataSource: '2' } as H5ReceiveDetailBean | ||
| 54 | + // TODO 对接user信息、登录情况 | ||
| 55 | + let h5ReceiveDataExtraBean: H5ReceiveDataExtraBean = { creatorId: '', isLogin: '0' } as H5ReceiveDataExtraBean | ||
| 56 | + let h5ReceiveDataJsonBean: H5ReceiveDataJsonBean = { | ||
| 57 | + contentId: contentId, | ||
| 58 | + contentType: contentType | ||
| 59 | + } as H5ReceiveDataJsonBean | ||
| 60 | + h5ReceiveDataJsonBean.topicId = topicId | ||
| 61 | + h5ReceiveDataJsonBean.channelId = channelId | ||
| 62 | + h5ReceiveDataJsonBean.compId = compId | ||
| 63 | + h5ReceiveDataJsonBean.sourcePage = sourcePage | ||
| 64 | + h5ReceiveDataJsonBean.netError = '0' | ||
| 65 | + let response: ResponseBean = {} as ResponseBean | ||
| 66 | + response.data = this.contentDetailData | ||
| 67 | + response.code = 200 | ||
| 68 | + response.success = true | ||
| 69 | + h5ReceiveDataJsonBean.responseMap = response | ||
| 70 | + h5ReceiveAppData.dataJson = h5ReceiveDataJsonBean | ||
| 71 | + h5ReceiveAppData.dataExt = h5ReceiveDataExtraBean | ||
| 72 | + // TODO 暂延时1s,考虑业务流程再优化 | ||
| 73 | + setTimeout(() => { | ||
| 74 | + this.sendContentData2H5(h5ReceiveAppData); | ||
| 75 | + }, 1000) | ||
| 76 | + | ||
| 77 | + } | ||
| 73 | 78 | ||
| 74 | } | 79 | } |
| 75 | 80 | ||
| 76 | build() { | 81 | build() { |
| 77 | Column() { | 82 | Column() { |
| 78 | - if (this.contentDetailData && this.contentDetailData.shareInfo && this.contentDetailData.shareInfo.shareUrl){ | ||
| 79 | - WdWebComponent({ | ||
| 80 | - webUrl: this.contentDetailData?.shareInfo?.shareUrl, | ||
| 81 | - // webUrl: $rawfile('apph5/index.html'), | ||
| 82 | - backVisibility: false, | ||
| 83 | - }) | ||
| 84 | - } | 83 | + WdWebLocalComponent({ |
| 84 | + webviewControl: this.webviewControl, | ||
| 85 | + webResource: $rawfile('apph5/index.html'), | ||
| 86 | + backVisibility: false, | ||
| 87 | + }) | ||
| 85 | } | 88 | } |
| 86 | } | 89 | } |
| 90 | + | ||
| 91 | + private sendContentData2H5(h5ReceiveAppData: H5ReceiveDetailBean) { | ||
| 92 | + Logger.debug('ImageAndTextWebComponent', 'jsCall_receiveAppData'); | ||
| 93 | + this.webviewControl.callHandle(NativeCallH5Type.jsCall_receiveAppData, | ||
| 94 | + JSON.stringify(h5ReceiveAppData), (data: string) => { | ||
| 95 | + // Logger.debug('ImageAndTextWebComponent', "from js data = " + data); | ||
| 96 | + }) | ||
| 97 | + } | ||
| 87 | } | 98 | } |
-
Please register or login to post a comment