其他
HarmonyOS鸿蒙应用开发,原生与H5通信框架
https://juejin.cn/user/782508007891048/posts
已适配鸿蒙NEXT版本; 原生同步方法内支持串行并发异步任务,同步等待异步结果,是根据鸿蒙特点设计的需求; 支持以类的方式集中统一管理API; 支持同步和异步调用; 支持进度回调/回传:一次调用,多次返回; 支持API是否存在检测; 支持Javascript关闭页面的监听与拦截, 支持命名空间API。
private cHandler: CompleteHandler = null
/**
* 同步
* @param p
* @returns
*/
@JavaScriptInterface(false)
testSync(p: string): string {
LogUtils.d("testSync: " + JSON.stringify(p))
return "hello native"
}
/**
* 异步
* @param p
* @param handler
*/
@JavaScriptInterface()
testAsync(p: string, handler: CompleteHandler) {
LogUtils.d("testAsync: " + JSON.stringify(p))
this.cHandler = handler
}
}
aboutToAppear() {
this.controller.addJavascriptObject(new JsBridge())
}
Web({ src: this.localPath, controller: this.controller.getWebViewController() })
.javaScriptAccess(true)
.javaScriptProxy(this.controller.getJavaScriptProxy())
.onAlert((event) => {
// AlertDialog.show({ message: event.message })
return false
})
)
let msg = dsBridge.call('testSync', JSON.stringify({data: 100}))
// 异步
dsBridge.call('testAsync', JSON.stringify({data: 200}), (msg) => {
updateMsg(msg)
})
// 或者cdn引入
<script src="https://cdn.jsdelivr.net/npm/m-dsbridge/dsBridge.js"></script>
dsBridge.register('showAlert', function (a, b, c) {
// return "原生调用JS showAlert函数"
alert("原生调用JS showAlert函数" + a + " " + b + " " + c)
return true
})
// 注册异步函数
dsBridge.registerAsyn('showAlertAsync', function (a, b, c, callback) {
let counter = 0
let id = setInterval(() => {
if (counter < 5) {
callback(counter, false)
alert("原生调用JS showAlertAsync函数" + a + " " + b + " " + c + " " + counter)
counter++
} else {
callback(counter, true)
alert("原生调用JS showAlertAsync函数" + a + " " + b + " " + c + " " + counter)
clearInterval(id)
}
}, 1000)
})
.onClick(() => {
this.controller.callJs("showAlert", [1, 2, '666'], (v) => {
this.msg = v + ""
})
})
Button("调用js函数-异步")
.onClick(() => {
this.controller.callJs("showAlertAsync", [1, 2, '666'], (v) => {
this.msg = v + ""
})
})
}
testAsync(p: string, handler: CompleteHandler) {
LogUtils.d("testAsync: " + JSON.stringify(p))
this.cHandler = handler
let counter = 0
setInterval(() => {
if (counter < 5) {
counter++
handler.setProgressData("异步返回的数据--" + counter)
} else {
this.cHandler.complete("异步返回的数据--结束")
this.cHandler.complete("异步返回的数据--结束2")
}
}, 1000)
updateMsg(msg)
})
this.controller.setClosePageListener(() => {
return true; // false 会拦截关闭页面
})
}
this.jsBridge.destroy()
}
let msg = dsBridge.call('namespace.testSync',{msg:'来自js命名空间的数据'})
updateMsg(msg)
}
const callNative7 = () => {
dsBridge.call('namespace.testAsync', 'test', (msg) => {
updateMsg(msg)
})
}
dsBridge.register('sync', {
test: function (a, b) {
return "namespace: " + (a + b)
}
})
dsBridge.registerAsyn("asny",{
test: function (a,b ,callback) {
callback("namespace: " + (a + b))
}
})
this.msg = value
})
this.controller.callJs("asny.test", [3, 2], (value: string) => {
this.msg = value
})
* 同步模版
* @param p
* @returns
*/
@JavaScriptInterface(false)
testSync(p: string): string {
LogUtils.d("testSync: " + JSON.stringify(p))
return "原生同步testSync方法返回的数据"
}
* 同步方法中执行异步并发任务
* @param args
* @returns
*/
@JavaScriptInterface(false)
testTaskWait(args: string): number {
let p = new Param(100, 200)
let p1 = new Param(100, 300)
taskWait(p)
p1.tag = "Param"
taskWait(p1)
LogUtils.d(`testTaskWait sum: ${p.sum} ${p1.sum}`)
return p.sum + p1.sum
}
export class Param extends BaseSendable{
private a : number = 0
private b : number = 0
public sum: number = 0
public enableLog: boolean = true
constructor(a: number, b: number) {
super();
this.a = a;
this.b = b;
}
// 异步任务执行
async run(): Promise<void> {
this.sum = await this.add()
}
async add(){
return this.a + this.b
}
}