Toggle navigation
Toggle navigation
This project
Loading...
Sign in
developOne
/
harmonyPool
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
zhangbo1_wd
2024-04-12 10:48:25 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
81cd75528373e1e5d864e625607cec601120cb51
81cd7552
1 parent
8f447bfd
jsbridge适配
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
219 additions
and
202 deletions
sight_harmony/commons/wdJsBridge/src/main/ets/bean/Message.ets
sight_harmony/commons/wdJsBridge/src/main/ets/core/BridgeHandler.ets
sight_harmony/commons/wdJsBridge/src/main/ets/core/BridgeWebViewControl.ets
sight_harmony/commons/wdWebComponent/src/main/ets/pages/H5CallNativeType.ets
sight_harmony/commons/wdWebComponent/src/main/ets/pages/JsBridgeBiz.ets
sight_harmony/commons/wdWebComponent/src/main/ets/pages/NativeCallH5Type.ets
sight_harmony/commons/wdWebComponent/src/main/ets/pages/WdWebComponent.ets
sight_harmony/commons/wdWebComponent/src/main/ets/pages/WdWebLocalComponent.ets
sight_harmony/features/wdComponent/src/main/ets/components/ImageAndTextWebComponent.ets
sight_harmony/commons/wdJsBridge/src/main/ets/bean/Message.ets
View file @
81cd755
...
...
@@ -7,7 +7,7 @@ export class Message {
callbackId: string = ""; //callbackId
responseId: string = ""; //responseId
responseData: string = ""; //responseData
data?:
Action
; //data of message
data?:
object
; //data of message
handlerName: string = ""; //name of handler
/**
...
...
sight_harmony/commons/wdJsBridge/src/main/ets/core/BridgeHandler.ets
View file @
81cd755
import { Action } from '../bean/Action'
import { Message } from '../bean/Message'
/**
* 注册回调接口
*/
export type Callback = (data: string) => void
export interface BridgeHandler {
handle: (data: Action, f: Callback) => void
export interface BridgeHandler {
handle: (data: Message, f: Callback) => void
}
export class DefaultBridgeHandler implements BridgeHandler {
handle(data:
Action
, f: Callback) {
handle(data:
Message
, f: Callback) {
//1,2.3
f("DefaultHandler response data")
}
...
...
sight_harmony/commons/wdJsBridge/src/main/ets/core/BridgeWebViewControl.ets
View file @
81cd755
...
...
@@ -5,6 +5,7 @@ import { BridgeUtil } from '../utils/BridgeUtil';
import { Message } from '../bean/Message';
import { CallBackMessage } from '../bean/CallBackMessage';
import { StringUtils } from '../utils/StringUtils';
import { hilog } from '@kit.PerformanceAnalysisKit';
const TAG = 'BridgeWebViewControl';
...
...
@@ -47,6 +48,7 @@ export class BridgeWebViewControl extends webview.WebviewController {
* 刷新消息
*/
flushMessageQueue() {
hilog.error(0xFF00, TAG, 'flushMessageQueue');
this.loadUrlCustom(BridgeUtil.JS_FETCH_QUEUE_FROM_JAVA, (data: string) => {
let list: Array<Message> = JSON.parse(data)
if (list == null || list.length == 0) {
...
...
@@ -66,11 +68,13 @@ export class BridgeWebViewControl extends webview.WebviewController {
} else {
let responseFunction: Callback;
let callbackId: string = value.callbackId
let handlerName: string = value.handlerName
if (StringUtils.isNotEmpty(callbackId)) {
responseFunction = (data: string) => {
let msg: CallBackMessage = new CallBackMessage()
msg.responseId = callbackId
msg.responseData = data
msg.handlerName = handlerName
this.queueMessage(msg)
}
} else {
...
...
@@ -86,7 +90,7 @@ export class BridgeWebViewControl extends webview.WebviewController {
handle = new DefaultBridgeHandler()
}
if (handle != undefined && value.data != undefined) {
handle.handle(value
.data
, responseFunction)
handle.handle(value, responseFunction)
}
}
})
...
...
@@ -107,12 +111,15 @@ export class BridgeWebViewControl extends webview.WebviewController {
*/
private dispatchMessage(msg: CallBackMessage) {
let messageJson: string = msg.toJson()
hilog.error(0xFF00, TAG, 'dispatchMessage '+ messageJson);
// messageJson = messageJson.replace("%7B", encodeURIComponent("%7B"));
// messageJson = messageJson.replace("%7D", encodeURIComponent("%7D"));
// messageJson = messageJson.replace("%22", encodeURIComponent("%22"));
let javascriptCommand: string = StringUtils.formatStringForJS(BridgeUtil.JS_HANDLE_MESSAGE_FROM_JAVA, messageJson);
this.runJavaScript(javascriptCommand)
this.runJavaScript(javascriptCommand).then((res)=>{
hilog.error(0xFF00, TAG, 'dispatchMessage res: '+ res);
})
}
/**
...
...
sight_harmony/commons/wdWebComponent/src/main/ets/pages/H5CallNativeType.ets
0 → 100644
View file @
81cd755
/**
* h5调用native,事件id枚举,
* h5主动调用app方法
*/
export class H5CallNativeType {
static JsCallTypeList: string[] = []
static jsCall_currentPageOperate = 'jsCall_currentPageOperate'
static jsCall_getAppPublicInfo = 'jsCall_getAppPublicInfo'
static jsCall_getArticleDetailBussinessData = 'jsCall_getArticleDetailBussinessData'
static jsCall_callAppService = 'jsCall_callAppService'
// TODO 业务自行新增类型、自行在JsBridgeBiz#performJSCallNative里添加接收分支处理。
static {
H5CallNativeType.JsCallTypeList.push(H5CallNativeType.jsCall_currentPageOperate)
H5CallNativeType.JsCallTypeList.push(H5CallNativeType.jsCall_getAppPublicInfo)
H5CallNativeType.JsCallTypeList.push(H5CallNativeType.jsCall_getArticleDetailBussinessData)
H5CallNativeType.JsCallTypeList.push(H5CallNativeType.jsCall_callAppService)
}
}
...
...
sight_harmony/commons/wdWebComponent/src/main/ets/pages/JsBridgeBiz.ets
View file @
81cd755
import { Action } from 'wdBean';
import { Callback } from 'wdJsBridge';
import { Callback, BridgeWebViewControl } from 'wdJsBridge';
import { Message } from 'wdJsBridge/src/main/ets/bean/Message';
import { Logger, StringUtils, } from 'wdKit';
// import { AccountManagerUtils, ILoginService, Logger, ServiceManager, StringUtils, UserBean, UserInfo } from 'wdKit';
import { WDRouterPage, WDRouterRule } from 'wdRouter';
import { H5CallNativeType } from './H5CallNativeType';
// import { wdMenc } from 'wdMenc';
const TAG = 'JsBridgeBiz'
/**
* h5调用native代码
* @param data
* @param call
*/
export function performJSCallNative(data: Action, call: Callback) {
// if (data.name == "GET_PROMOTION_SIGN") {
// wdMenc.getActivitySign(data.params?.intfId, (sign) => {
// if (sign) {
// call(JSON.stringify(sign));
// } else {
// call("")
// }
// })
// return
// }
//TODO 数据校验
switch (data.type) {
//获取用户信息
case "GET_USER_INFO":
//测试环境
// call(JSON.stringify(getTestUserBean()))
// let isLogin = AccountManagerUtils.isLoginSync()
// Logger.info("WebComponent", `GET_USER_INFO#:::refresh==` + isLogin);
//
// if (isLogin) {
// const loginService = ServiceManager.getService(ILoginService.name) as ILoginService.Service
// let userBean: UserBean = loginService.getUserBean()
// // userBean.userInfo = JSON.stringify(loginService.getUserInfo())
// call(JSON.stringify(userBean))
// } else {
// call("fail")
// }
export function performJSCallNative(data: Message, call: Callback) {
Logger.debug(TAG, 'performJSCallNative handlerName: ' + data.handlerName + ', data: ' + data.data)
switch (data.handlerName) {
case H5CallNativeType.jsCall_currentPageOperate:
break;
case H5CallNativeType.jsCall_getAppPublicInfo:
// h5获取app配置信息
call(getAppPublicInfo())
break;
case H5CallNativeType.jsCall_getArticleDetailBussinessData:
break;
case H5CallNativeType.jsCall_callAppService:
break;
case "JUMP_DETAIL_PAGE":
WDRouterRule.jumpWithAction(data)
case 'changeNativeMessage':
call("this is change Web Message")
break;
case "JUMP_INNER_NEW_PAGE":
WDRouterRule.jumpWithAction(data)
break
case "GET_DEVICE_DATA":
break
//用户登陆
case "USER_LOGIN":
WDRouterRule.jumpWithAction(data)
//支付完成回调
case "REGISTER_H5_WEBVIEW_CREATE_ORDER_RESULT":
break
//打开H5页面
case "JUMP_H5_BY_WEB_VIEW":
WDRouterRule.jumpWithAction(data)
break
case "USER_LOGOUT":
// const loginService = ServiceManager.getService(ILoginService.name) as ILoginService.Service
// loginService.logout()
break
default:
call("this is def value")
}
}
// /**
// *
// * @returns 模拟测试环境的user信息
// */
// function getTestUserBean(): UserBean {
// const loginService = ServiceManager.getService(ILoginService.name) as ILoginService.Service
// //正是环境
// let userBean: UserBean = loginService.getUserBean()
// userBean.userId = "930855275"
// userBean.userToken = "nlps6FADECE38F5AAD2116F2"
// // userBean.clientId = "f1bf515c-4006-4606-a752-2b4cdd9343d0"
// // userBean.carrierCode = "CM"
// userBean.mobile = "NTTrcDO4ADN1cTM5MTM"
// userBean.uname = '139****0887'
// userBean.picture = "http://36.155.98.104:23380/publish/voms2/uic_service/picture/userImage/543/626/5181.jpg"
// // userBean.ssotoken = "STnid0000011700463753943SKPUlfNxEDJsrauGjCwUldiDfrd0mUuM"
// // userBean.clientProvinceCode = "210"
// // userBean.clientCityId = "0210"
// // userBean.sign = "617DAC548595B7C9EBB13043735F1BE0"
// // userBean.blurMobile = "177****9217"
// let userinfo: UserInfo = ({
// userId: "930855275",
// userNum: "NzDrsyN4gDM0UzNxkzMxYDO",
// mobile: "NTTrcDO4ADN1cTM5MTM",
// areaId: "210",
// cityId: "0210",
// carrierCode: "CM",
// passId: "602201990200225921",
// userToken: "nlps6FADECE38F5AAD2116F2",
// expiredOn: "1705647754000",
// blurMobile: "139****0887",
// encrypted: true
// })
// // userBean.userInfo = JSON.stringify(userinfo);
// return userBean
// }
//
// /**
// *
// * @returns 用户信息
// */
// function getUserBean(): UserBean {
// const loginService = ServiceManager.getService(ILoginService.name) as ILoginService.Service
// //正是环境
// let userBean: UserBean = loginService.getUserBean()
// userBean.userId = "1437725487"
// userBean.userToken = "nlps08468E117C554CA08A43"
// // userBean.clientId = "27fb3129-5a54-45bc-8af1-7dc8f1155501"
// // userBean.carrierCode = "CT"
// userBean.mobile = "OTTrcTMykTO4ATM3cTM"
// userBean.uname = '小可爱啊'
// userBean.picture = "http://img.cmvideo.cn:8080/publish/voms2/uic_service/picture/userImage/1437/725/487/20211224174128beax.png"
// // userBean.ssotoken = "STnid0000011700461738301N5rjsHdbvyzMpyzwvHrFRJsj7oNT1Juf"
// // userBean.clientProvinceCode = "100"
// // userBean.clientCityId = "0100"
// // userBean.sign = "4ABFB8442EE914B57CCD9F1DE587D96D"
// // userBean.blurMobile = "177****9217"
// let userinfo: UserInfo = ({
// userId: "1437725487",
// userNum: "MzDrsyNxITO5gDMxczNxYDO",
// mobile: "OTTrcTMykTO4ATM3cTM",
// areaId: "100",
// cityId: "0100",
// carrierCode: "CT",
// passId: "467464726359024540",
// userToken: "nlps08468E117C554CA08A43",
// expiredOn: "1705645738000",
// blurMobile: "177****9217",
// encrypted: true
// })
// // userBean.userInfo = JSON.stringify(userinfo);
// return userBean
// }
class AppInfo {
plat: string = ''
system: string = ''
// TODO 完善
}
/**
* 获取App公共信息
*/
function getAppPublicInfo(): string {
let info = new AppInfo()
info.plat = 'Phone'
// 直接用Android,后续适配再新增鸿蒙
info.system = 'Android'
let result = JSON.stringify(info)
return result;
}
...
...
sight_harmony/commons/wdWebComponent/src/main/ets/pages/NativeCallH5Type.ets
0 → 100644
View file @
81cd755
/**
* native调用h5,事件id枚举
* app主动调用h5方法
*/
export const enum NativeCallH5Type {
jsCall_receiveAppData = 'jsCall_receiveAppData',
// TODO 业务自行新增类型,自行调用,例:
// TODO this.webviewControl.callHandle(NativeCallH5Type.jsCall_receiveAppData,xxxx)
}
...
...
sight_harmony/commons/wdWebComponent/src/main/ets/pages/WdWebComponent.ets
View file @
81cd755
...
...
@@ -5,6 +5,7 @@ import { Logger } from 'wdKit';
import { BridgeHandler, BridgeUtil, BridgeWebViewControl, Callback } from 'wdJsBridge';
import { performJSCallNative } from './JsBridgeBiz';
import { setDefaultNativeWebSettings } from './WebComponentUtil';
import { Message } from 'wdJsBridge/src/main/ets/bean/Message';
const TAG = 'WdWebComponent';
...
...
@@ -30,7 +31,7 @@ export struct WdWebComponent {
/**
* 默认【CallNative】逻辑处理
*/
private defaultPerformJSCallNative: (data:
Action, f: Callback) => void = (data: Action
, f: Callback) => {
private defaultPerformJSCallNative: (data:
Message, f: Callback) => void = (data: Message
, f: Callback) => {
performJSCallNative(data, f)
}
/**
...
...
@@ -41,7 +42,7 @@ export struct WdWebComponent {
defaultRegisterHandler(): void {
this.webviewControl.registerHandler("CallNative", {
handle: (data:
Action
, f: Callback) => {
handle: (data:
Message
, f: Callback) => {
this.defaultPerformJSCallNative(data, f)
}
});
...
...
sight_harmony/commons/wdWebComponent/src/main/ets/pages/WdWebLocalComponent.ets
View file @
81cd755
import router from '@ohos.router';
import { BridgeUtil, BridgeWebViewControl } from 'wdJsBridge';
import { ResourceManager } from 'wdJsBridge/src/main/ets/utils/ResourceManager';
import { BridgeHandler, BridgeUtil, BridgeWebViewControl, Callback } from 'wdJsBridge';
import { Logger } from 'wdKit/Index';
import { setDefaultNativeWebSettings } from './WebComponentUtil';
import { Action } from 'wdBean';
import { performJSCallNative } from './JsBridgeBiz';
import { H5CallNativeType } from './H5CallNativeType';
import { Message } from 'wdJsBridge/src/main/ets/bean/Message';
const TAG = 'WdWebLocalComponent';
...
...
@@ -31,7 +36,25 @@ export struct WdWebLocalComponent {
.javaScriptAccess(true)
.onPageBegin((event) => {
})
// setDefaultNativeWebSettings(this.webviewControl, this.webResource).then(()=>{
// this.handleInfo && this.handleInfo.length > 1 ? this.handleInfo.forEach(value => {
// this.webviewControl.registerHandler(value[0], value[1])
// }) : this.defaultRegisterHandler()
// setTimeout(()=>{
// BridgeUtil.webViewLoadLocalJs(getContext(this), this.webviewControl)
// },500)
// })
// this.onPageBegin(event?.url)
// BridgeUtil.webViewLoadLocalJs(getContext(this), this.webviewControl);
this.onPageBegin(event?.url);
this.registerHandlers();
setTimeout(() => {
BridgeUtil.webViewLoadLocalJs(getContext(this), this.webviewControl)
}, 200)
})
.onPageEnd((event) => {
this.onPageEnd(event?.url)
})
.onLoadIntercept((event) => {
let url: string = event.data.getRequestUrl().toString()
url = url.replace("%(?![0-9a-fA-F]{2})", "%25")
...
...
@@ -42,12 +65,48 @@ export struct WdWebLocalComponent {
return true
}
if (url.startsWith(BridgeUtil.YY_OVERRIDE_SCHEMA)) {
Logger.debug(TAG, 'flushMessageQueue');
this.webviewControl.flushMessageQueue()
return true
}
return
false
return
this.onLoadIntercept(event.data.getRequestUrl().toString());
})
}
}
private registerHandlers(): void {
// 注册h5调用js相关
for (let i = 0; i < H5CallNativeType.JsCallTypeList.length; i++) {
let handleName = H5CallNativeType.JsCallTypeList[i];
let handle = (data: Message, f: Callback) => {
this.defaultPerformJSCallNative(data, f)
} ;
this.webviewControl.registerHandler(handleName, { handle: handle });
}
// // TODO test
// this.webviewControl.registerHandler('changeNativeMessage', {
// handle: (data: Message, f: Callback) => {
// this.defaultPerformJSCallNative(data, f)
// }
// });
}
/**
* 默认【CallNative】逻辑处理
*/
private defaultPerformJSCallNative: (data: Message, f: Callback) => void = (data: Message, f: Callback) => {
performJSCallNative(data, f)
}
onPageBegin: (url?: string) => void = () => {
Logger.debug(TAG, 'onPageBegin');
}
onPageEnd: (url?: string) => void = () => {
Logger.debug(TAG, 'onPageEnd');
}
onLoadIntercept: (url?: string) => boolean = () => {
Logger.debug(TAG, 'onPageBegin return false');
return false
}
}
...
...
sight_harmony/features/wdComponent/src/main/ets/components/ImageAndTextWebComponent.ets
View file @
81cd755
...
...
@@ -8,80 +8,91 @@ import {
} from 'wdBean';
import { Logger } from 'wdKit';
import { WdWebComponent, WdWebLocalComponent } from 'wdWebComponent';
import { NativeCallH5Type } from 'wdWebComponent/src/main/ets/pages/NativeCallH5Type';
import { BridgeWebViewControl } from '../../../../../../commons/wdWebComponent/oh_modules/wdJsBridge/Index';
@Component
export struct ImageAndTextWebComponent {
action: Action = {} as Action
@State reload: number = 0;
@Prop @Watch('onDetailDataUpdated') contentDetailData: ContentDetailDTO = {} as ContentDetailDTO
webviewControl: BridgeWebViewControl = new BridgeWebViewControl()
onDetailDataUpdated() {
// if (this.action) {
// let contentId: string = ''
// let contentType: string = ''
// let topicId: string = ''
// let channelId: string = ''
// let compId: string = ''
// let sourcePage: string = '5'
// if (this.action.params) {
// if (this.action.params.contentID) {
// contentId = this.action.params?.contentID
// }
// if (this.action.params.extra) {
// if (this.action.params.extra.contentType) {
// contentType = this.action.params.extra.contentType
// }
// if (this.action.params.extra.topicId) {
// topicId = this.action.params.extra.topicId
// }
// if (this.action.params.extra.channelId) {
// channelId = this.action.params.extra.channelId
// }
// if (this.action.params.extra.compId) {
// compId = this.action.params.extra.compId
// }
// if (this.action.params.extra.sourcePage) {
// sourcePage = this.action.params.extra.sourcePage
// }
// }
//
// }
//
// let h5ReceiveAppData: H5ReceiveDetailBean = { dataSource: '2' } as H5ReceiveDetailBean
// let h5ReceiveDataExtraBean: H5ReceiveDataExtraBean = { creatorId: '', isLogin: '0' } as H5ReceiveDataExtraBean
// let h5ReceiveDataJsonBean: H5ReceiveDataJsonBean = {
// contentId: contentId,
// contentType: contentType
// } as H5ReceiveDataJsonBean
// h5ReceiveDataJsonBean.topicId = topicId
// h5ReceiveDataJsonBean.channelId = channelId
// h5ReceiveDataJsonBean.compId = compId
// h5ReceiveDataJsonBean.sourcePage = sourcePage
// h5ReceiveDataJsonBean.netError = '0'
// let response: ResponseBean = {} as ResponseBean
// response.data = this.detailData
// response.code = 0
// response.success = true
// h5ReceiveDataJsonBean.responseMap = response
// h5ReceiveAppData.dataJson = h5ReceiveDataJsonBean
// h5ReceiveAppData.dataExt = h5ReceiveDataExtraBean
// this.webviewControl.callHandle("jsCall_receiveAppData", JSON.stringify(h5ReceiveAppData), (data: string) => {
// Logger.info("from js data = " + data);
// })
if (this.action) {
let contentId: string = ''
let contentType: string = ''
let topicId: string = ''
let channelId: string = ''
let compId: string = ''
let sourcePage: string = '5'
if (this.action.params) {
if (this.action.params.contentID) {
contentId = this.action.params?.contentID
}
if (this.action.params.extra) {
if (this.action.params.extra.contentType) {
contentType = this.action.params.extra.contentType
}
if (this.action.params.extra.topicId) {
topicId = this.action.params.extra.topicId
}
if (this.action.params.extra.channelId) {
channelId = this.action.params.extra.channelId
}
if (this.action.params.extra.compId) {
compId = this.action.params.extra.compId
}
if (this.action.params.extra.sourcePage) {
sourcePage = this.action.params.extra.sourcePage
}
}
// }
}
let h5ReceiveAppData: H5ReceiveDetailBean = { dataSource: '2' } as H5ReceiveDetailBean
// TODO 对接user信息、登录情况
let h5ReceiveDataExtraBean: H5ReceiveDataExtraBean = { creatorId: '', isLogin: '0' } as H5ReceiveDataExtraBean
let h5ReceiveDataJsonBean: H5ReceiveDataJsonBean = {
contentId: contentId,
contentType: contentType
} as H5ReceiveDataJsonBean
h5ReceiveDataJsonBean.topicId = topicId
h5ReceiveDataJsonBean.channelId = channelId
h5ReceiveDataJsonBean.compId = compId
h5ReceiveDataJsonBean.sourcePage = sourcePage
h5ReceiveDataJsonBean.netError = '0'
let response: ResponseBean = {} as ResponseBean
response.data = this.contentDetailData
response.code = 200
response.success = true
h5ReceiveDataJsonBean.responseMap = response
h5ReceiveAppData.dataJson = h5ReceiveDataJsonBean
h5ReceiveAppData.dataExt = h5ReceiveDataExtraBean
// TODO 暂延时1s,考虑业务流程再优化
setTimeout(() => {
this.sendContentData2H5(h5ReceiveAppData);
}, 1000)
}
}
build() {
Column() {
if (this.contentDetailData && this.contentDetailData.shareInfo && this.contentDetailData.shareInfo.shareUrl){
WdWebComponent({
webUrl: this.contentDetailData?.shareInfo?.shareUrl,
// webUrl: $rawfile('apph5/index.html'),
backVisibility: false,
})
}
WdWebLocalComponent({
webviewControl: this.webviewControl,
webResource: $rawfile('apph5/index.html'),
backVisibility: false,
})
}
}
private sendContentData2H5(h5ReceiveAppData: H5ReceiveDetailBean) {
Logger.debug('ImageAndTextWebComponent', 'jsCall_receiveAppData');
this.webviewControl.callHandle(NativeCallH5Type.jsCall_receiveAppData,
JSON.stringify(h5ReceiveAppData), (data: string) => {
// Logger.debug('ImageAndTextWebComponent', "from js data = " + data);
})
}
}
\ No newline at end of file
...
...
Please
register
or
login
to post a comment