From cefd4212da5ecab9d95c64a29ba6628808751527 Mon Sep 17 00:00:00 2001 From: gou-jingjing Date: Wed, 22 Nov 2023 16:07:14 +0800 Subject: [PATCH] add sample code Signed-off-by: gou-jingjing --- examples/Index.ets | 179 +++++++++++++++++++++++++++++ examples/app/napiGenerator.hap | Bin 104308 -> 107359 bytes examples/cfg.json | 26 +++++ examples/napitest.cpp | 158 +++++++++++++++++++++++++ examples/serviceCode/say_hello.cpp | 108 +++++++++++++++++ examples/serviceCode/say_hello.h | 32 ++++++ 6 files changed, 503 insertions(+) create mode 100644 examples/Index.ets create mode 100644 examples/cfg.json create mode 100644 examples/napitest.cpp create mode 100644 examples/serviceCode/say_hello.cpp create mode 100644 examples/serviceCode/say_hello.h diff --git a/examples/Index.ets b/examples/Index.ets new file mode 100644 index 00000000..60d63e4a --- /dev/null +++ b/examples/Index.ets @@ -0,0 +1,179 @@ +import napitest from '@ohos.napitest'; + +let ns = new napitest.NodeISayHello(); +class NodeISayHelloListenerImpl { + listener = null; + constructor(listener) { + this.listener = listener; + } + onSayHelloStart(info) { + AppStorage.SetOrCreate("textInfoStart", JSON.stringify(info)) + console.log('napiTestDemo ----onSayHelloStart', JSON.stringify(info)); + } + onSayHelloEnd(info) { + AppStorage.SetOrCreate("textInfoEnd", JSON.stringify(info)) + console.log('napiTestDemo ----onSayHelloEnd.', JSON.stringify(info)); + } +} +// @ts-ignore +let listener = new NodeISayHelloListenerImpl() + +// register注册的回调 +function onCallbackfunnm(wid) { + AppStorage.SetOrCreate("callBackNum", JSON.stringify(wid)) + console.info("napiTestDemo ----onCallbackfunnm wid = " + wid) + return "ocCallbackfuncnm"; +} + +@Entry +@Component +struct Index { + @State textErrMsg: string = '' + @State textResult: string = '' + @State textResponse: string = '' + @State returnVal: string = '' + @StorageLink("textInfoStart")textInfoStart: string = "" + @StorageLink("textInfoEnd")textInfoEnd: string = "" + @StorageLink("callBackNum")callBackNum: string = "" + + build() { + Row() { + Column() { + Button() { + Text('注册object回调后SayHello调用回调') + .fontSize(20) + .fontWeight(FontWeight.Bold) + } + .type(ButtonType.Capsule) + .margin({ + top: 10 + }) + .backgroundColor('#0D9FFB') + .width('90%') + .height('5%') + .onClick(() => { + AppStorage.SetOrCreate("textInfoStart", "") + AppStorage.SetOrCreate("textInfoEnd", "") + // 注册回调 + ns.addSayHelloListener(listener); + // 调用回调 + ns.sayHello("js1", "native1", napitest.SayType.kInitiative); + }) + + Button() { + Text('注销object回调后SayHello调用回调') + .fontSize(20) + .fontWeight(FontWeight.Bold) + } + .type(ButtonType.Capsule) + .margin({ + top: 10 + }) + .backgroundColor('#0D9FFB') + .width('90%') + .height('5%') + .onClick(() => { + AppStorage.SetOrCreate("textInfoStart", "") + AppStorage.SetOrCreate("textInfoEnd", "") + // 注销回调 removeSayHelloListener + ns.removeSayHelloListener(listener); + // 调用回调 + ns.sayHello("js2", "native2", napitest.SayType.kInitiative); + }) + + // promise回调 + Button() { + Text('Promise 回调') + .fontSize(20) + .fontWeight(FontWeight.Bold) + } + .type(ButtonType.Capsule) + .margin({ + top: 10 + }) + .backgroundColor('#0D9FFB') + .width('90%') + .height('5%') + .onClick(async () => { + await ns.sayHelloWithResponse("response from", "response to", napitest.SayType.kResponse).then(ret => { + this.textErrMsg = ret.errMsg + this.textResult = JSON.stringify(ret.result) + this.textResponse = ret.response + console.info("napiTestDemo ----sayHelloWithResponse ret.errMsg = " + ret.errMsg) + console.info("napiTestDemo ----sayHelloWithResponse ret.result = " + ret.result) + console.info("napiTestDemo ----sayHelloWithResponse ret.response = " + ret.response) + }); + }) + + Button() { + Text('register回调后sayHi调用回调') + .fontSize(20) + .fontWeight(FontWeight.Bold) + } + .type(ButtonType.Capsule) + .margin({ + top: 10 + }) + .backgroundColor('#0D9FFB') + .width('90%') + .height('5%') + .onClick( () => { + AppStorage.SetOrCreate("callBackNum", "") + // 注册回调 + ns.registerCallbackfunc(onCallbackfunnm); + // 调用回调 + ns.sayHi("js3", "native3", napitest.SayType.kResponse); + }) + + Button() { + Text('unRegister回调后sayHi调用回调') + .fontSize(20) + .fontWeight(FontWeight.Bold) + } + .type(ButtonType.Capsule) + .margin({ + top: 10 + }) + .backgroundColor('#0D9FFB') + .width('90%') + .height('5%') + .onClick( () => { + AppStorage.SetOrCreate("callBackNum", "") + // 注销回调 + ns.unRegisterCallbackfunc(onCallbackfunnm); + // 调用回调 + ns.sayHi("js4", "native4", napitest.SayType.kResponse); + }) + + // 调用普通函数 + Button() { + Text('调用funcTest方法') + .fontSize(20) + .fontWeight(FontWeight.Bold) + } + .type(ButtonType.Capsule) + .margin({ + top: 10 + }) + .backgroundColor('#0D9FFB') + .width('90%') + .height('5%') + .onClick( () => { + this.returnVal = napitest.funcTest(false); + console.info("napiTestDemo ----funcTest returnVal = " + this.returnVal) + }) + + Text('promise回调: errMsg = ' + this.textErrMsg + ', result = ' + this.textResult + ', response = ' + this.textResponse) + .margin({ + top: 10 + }) + Text('sayHelloStart回调: info = ' + this.textInfoStart).margin({ top: 10 }) + Text('sayHelloEnd回调: info = ' + this.textInfoEnd).margin({ top: 10 }) + Text('register注册的回调: wid = ' + this.callBackNum).margin({ top: 10 }) + Text('普通方法funcTest返回值: returnVal = ' + this.returnVal).margin({ top: 10 }) + } + .width('100%') + } + .height('100%') + } +} diff --git a/examples/app/napiGenerator.hap b/examples/app/napiGenerator.hap index b3e87cc9bb4aea80ec7e017e9a96fe9e3bff6aa5..78b1c5dc8ed7977df5d21cb92814e9feca45b67c 100644 GIT binary patch delta 10988 zcmb_i33!uLx;`h#pZrPNB!#xnJ!RyIxrTP0W1VI0}a5J2I3}H@Dc?N3BW<%b>MBF8Tb%53w#A!2mS%P22Zgj86a=T zW#BsSZ@>~vh#d$4!hv*P6fhQ;0?Y&ofeN4sSRL#pWD{Oq1)6~Ofd2&k0Q?OI>OhDC zhzC-DY+yX_I8X>I0jhx=z{|if;4JVh@DtD+NytaQ?|{p|P2d(_j6$G5lpBwDpeK+9 z3<5?1xxfTq3NQ^Q04jiGKsB%t*akEM-v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bxe0<09Xk&+ojGHiQ>e%7QqbE-oPYzzoqX~W7 z-PS(Ttl#;R_`UCr`b*yiM~X`7_TJp>sk9Ft?f&b?3-vZ^JD9qGW~tu$Q&LmX(%KA6 zX>GA3Eqq|%|H7!ve19Xe`|ou(E!lqZ%YHXb?A!GCH>E?ZQy>5N;<9CDe*W#h|GfYF zkk?Y|77Ysj$S|7}bN7_4N&?w`_S9MLjR(%$E^M-P)Aw}GvJQEd`}E07I=b~;PsXXm z>56-J+rHNvt&SYo+j{-`4}FrE;&-m>w6O7lwlDtl?E_P{KX*8NZ6!^ySO2FjU#Cji zDdx`kM+_g&Rz4fsM4JqK=QF4=AHKp$8sWQANgFD4J-(1C+Ey9S&6iU})0L$U`aYXvP9nO1tDD~#};>QT*CYS diff --git a/examples/cfg.json b/examples/cfg.json new file mode 100644 index 00000000..c839c3ee --- /dev/null +++ b/examples/cfg.json @@ -0,0 +1,26 @@ +[ + { + "includeName": "../serviceCode/say_hello.h", + "cppName": "../serviceCode/say_hello.cpp", + "interfaceName": "NodeISayHello::sayHello", + "serviceCode": "napitest::NodeISayHello *p = new napitest::NodeISayHello();\n p->sayHello(from, to, sayType);\n delete p;" + }, + { + "includeName": "../serviceCode/say_hello.h", + "cppName": "../serviceCode/say_hello.cpp", + "interfaceName": "NodeISayHello::sayHi", + "serviceCode": "napitest::NodeISayHello *p = new napitest::NodeISayHello();\n p->sayHi(from, to, sayType);\n delete p;" + }, + { + "includeName": "../serviceCode/say_hello.h", + "cppName": "../serviceCode/say_hello.cpp", + "interfaceName": "funcTest", + "serviceCode": "out = napitest::funcTest(v);" + }, + { + "includeName": "../serviceCode/say_hello.h", + "cppName": "../serviceCode/say_hello.cpp", + "interfaceName": "NodeISayHello::sayHelloWithResponse", + "serviceCode": "napitest::NodeISayHello *p = new napitest::NodeISayHello();\n p->sayHelloWithResponse(from, to, sayType);\n delete p;" + } +] \ No newline at end of file diff --git a/examples/napitest.cpp b/examples/napitest.cpp new file mode 100644 index 00000000..520aa488 --- /dev/null +++ b/examples/napitest.cpp @@ -0,0 +1,158 @@ +/* +* Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#include "napitest.h" +#include "napitest_middle.h" +#include "hilog/log.h" +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0XD002E00, "NAPITESTNAPILayer"}; +#define NAPITEST_LOGI(fmt, ...) OHOS::HiviewDFX::HiLog::Info(LABEL, \ + "%{public}s:%{public}d " fmt, __func__, __LINE__, ##__VA_ARGS__) + +namespace napitest { +namespace napitest_interface { +NodeISayHelloListener NodeISayHello::listener_ = {}; +bool NodeISayHello::addSayHelloListener(NodeISayHelloListener& listener) +{ + NodeISayHello::listener_ = listener; + return true; +} + +bool NodeISayHello::removeSayHelloListener(NodeISayHelloListener& listener) +{ + return true; +} + +bool NodeISayHello::registerCallbackfunc() +{ + return true; +} + +// 供业务调用的回调接口 +void NodeISayHello::CallbackfuncCallback(NUMBER_TYPE_2& wid) +{ + std::string eventName = "Callbackfunc"; + NodeISayHello_middle *ptr = new NodeISayHello_middle(); + ptr->CallbackfuncCallbackMiddle(eventName, wid); + delete ptr; +} + +bool NodeISayHello::unRegisterCallbackfunc() +{ + return true; +} + +bool NodeISayHello::sayHello(std::string& from, std::string& to, NUMBER_TYPE_9& sayType) +{ + NAPITEST_LOGI("NAPITEST_LOGI sayHello from = %s\r\n", from.c_str()); + NAPITEST_LOGI("NAPITEST_LOGI sayHello to = %s\r\n", to.c_str()); + NAPITEST_LOGI("NAPITEST_LOGI sayHello sayType = %d\r\n", sayType); + SayInfo info1; + info1.from = "js1"; + uint32_t a = 992; + info1.fromId.emplace(a); + uint32_t b = 1014; + info1.toId.emplace(b); + info1.to = "native1"; + info1.content = "hello1"; + info1.saidTime = "123456789"; + info1.isEnd = false; + SayInfo info2; + info2.from = "native"; + uint32_t c = 101; + info2.fromId.emplace(c); + uint32_t d = 99; + info2.toId.emplace(d); + info2.to = "js"; + info2.content = "hello"; + info2.saidTime = "987654321"; + info2.isEnd = true; + // 业务代码调用 onSayHelloStart callback + listener_.NodeISayHelloListener_onSayHelloStartCallback(info1); + // 业务代码调用 onSayHelloEnd callback + listener_.NodeISayHelloListener_onSayHelloEndCallback(info2); + return true; +} + +bool NodeISayHello::sayHi(std::string& from, std::string& to, NUMBER_TYPE_10& sayType) +{ + NAPITEST_LOGI("NAPITEST_LOGI sayHi from = %s\r\n", from.c_str()); + NAPITEST_LOGI("NAPITEST_LOGI sayHi to = %s\r\n", to.c_str()); + NAPITEST_LOGI("NAPITEST_LOGI sayHi sayType = %d\r\n", sayType); + NodeISayHello *ptr = new NodeISayHello(); + uint32_t callbackNum = 50; + ptr->CallbackfuncCallback(callbackNum); + delete ptr; + return true; +} + +bool NodeISayHello::sayHelloWithResponse(std::string& from, std::string& to, NUMBER_TYPE_11& sayType, + uint32_t& outErrCode, AUTO_INTERFACE_5& out) +{ + NAPITEST_LOGI("NAPITEST_LOGI sayHelloWithResponse from = %s\r\n", from.c_str()); + NAPITEST_LOGI("NAPITEST_LOGI sayHelloWithResponse to = %s\r\n", to.c_str()); + NAPITEST_LOGI("NAPITEST_LOGI sayHelloWithResponse sayType = %d\r\n", sayType); + out.errMsg = ""; + out.response = "rec hello."; + out.result = 0; + return true; +} + +AUTO_INTERFACE_5 NodeISayHello::auto_interface_5OutRes = {}; +void NodeISayHello::auto_interface_5SetCbValue(NUMBER_TYPE_6 result, std::string errMsg, std::string response) +{ + NodeISayHello::auto_interface_5OutRes.result = result; + NodeISayHello::auto_interface_5OutRes.errMsg = errMsg; + NodeISayHello::auto_interface_5OutRes.response = response; + return; +} + +bool NodeISayHelloListener::onSayHelloStart() +{ + return true; +} + +// 供业务调用的回调接口 +void NodeISayHelloListener::NodeISayHelloListener_onSayHelloStartCallback(SayInfo& info) +{ + std::string eventName = "NodeISayHelloListener_onSayHelloStart"; + NodeISayHelloListener_middle *ptr = new NodeISayHelloListener_middle(); + ptr->NodeISayHelloListener_onSayHelloStartCallbackMiddle(eventName, info); + delete ptr; +} + +bool NodeISayHelloListener::onSayHelloEnd() +{ + return true; +} + +// 供业务调用的回调接口 +void NodeISayHelloListener::NodeISayHelloListener_onSayHelloEndCallback(SayInfo& info) +{ + std::string eventName = "NodeISayHelloListener_onSayHelloEnd"; + NodeISayHelloListener_middle *ptr = new NodeISayHelloListener_middle(); + ptr->NodeISayHelloListener_onSayHelloEndCallbackMiddle(eventName, info); + delete ptr; +} + +bool funcTest(bool& v, std::string& out) +{ + if (v) { + out = "ret is true"; + } else { + out = "ret is false"; + } + return true; +} +} +} diff --git a/examples/serviceCode/say_hello.cpp b/examples/serviceCode/say_hello.cpp new file mode 100644 index 00000000..def2b80c --- /dev/null +++ b/examples/serviceCode/say_hello.cpp @@ -0,0 +1,108 @@ +/* +* Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#include "say_hello.h" +#include "../generatorCode/napitest.h" +#include "hilog/log.h" +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0XD002E00, "NAPITESTNAPILayer"}; +#define NAPITEST_LOGI(fmt, ...) OHOS::HiviewDFX::HiLog::Info(LABEL, \ + "%{public}s:%{public}d " fmt, __func__, __LINE__, ##__VA_ARGS__) + +namespace napitest { + // 1. 打印from, to, enum sayType的值 + // 2. 调用注册的NodeISayHelloListenerSayHelloStart(info: SayInfo)方法 + // 工具提供的业务接口(回调) void NodeISayHello::SayHelloListenerSayHelloStartCallback(SayInfo& info) + // 3. 调用注册的NodeISayHelloListenerSayHelloEnd(info: SayInfo)方法 + // 工具提供的业务接口(回调) void NodeISayHello::SayHelloListenerSayHelloEndCallback(SayInfo& info) +void NodeISayHello::sayHello(std::string& from, std::string& to, uint32_t& sayType) +{ + // 1.打印 + NAPITEST_LOGI("NAPITEST_LOGI sayHello from = %s\r\n", from.c_str()); + NAPITEST_LOGI("NAPITEST_LOGI sayHello to = %s\r\n", to.c_str()); + NAPITEST_LOGI("NAPITEST_LOGI sayHello sayType = %d\r\n", sayType); + + // 2.调用回调 + napitest::napitest_interface::SayInfo info1; + info1.from = "js1"; + uint32_t a = 992; + info1.fromId.emplace(a); + uint32_t b = 1014; + info1.toId.emplace(b); + info1.to = "native1"; + info1.content = "hello1"; + info1.saidTime = "123456789"; + info1.isEnd = false; + + napitest::napitest_interface::SayInfo info2; + info2.from = "native"; + uint32_t c = 101; + info2.fromId.emplace(c); + uint32_t d = 99; + info2.toId.emplace(d); + info2.to = "js"; + info2.content = "hello"; + info2.saidTime = "987654321"; + info2.isEnd = true; + // 业务代码调用 onSayHelloStart callback + NAPITEST_LOGI("NAPITEST_LOGI NodeISayHelloListener_onSayHelloStartCallback begin\r\n"); + napitest::napitest_interface::NodeISayHello::listener_.NodeISayHelloListener_onSayHelloStartCallback(info1); + NAPITEST_LOGI("NAPITEST_LOGI NodeISayHelloListener_onSayHelloStartCallback end\r\n"); + // 业务代码调用 onSayHelloEnd callback + NAPITEST_LOGI("NAPITEST_LOGI NodeISayHelloListener_onSayHelloEndCallback begin\r\n"); + napitest::napitest_interface::NodeISayHello::listener_.NodeISayHelloListener_onSayHelloEndCallback(info2); + NAPITEST_LOGI("NAPITEST_LOGI NodeISayHelloListener_onSayHelloEndCallback end\r\n"); + return; +} + +// 调用register注册的回调 +void NodeISayHello::sayHi(std::string& from, std::string& to, uint32_t& sayType) +{ + // 1.打印 + NAPITEST_LOGI("NAPITEST_LOGI sayHi from = %s\r\n", from.c_str()); + NAPITEST_LOGI("NAPITEST_LOGI sayHi to = %s\r\n", to.c_str()); + NAPITEST_LOGI("NAPITEST_LOGI sayHi sayType = %d\r\n", sayType); + // 2.调用回调 + napitest::napitest_interface::NodeISayHello *ptr = new napitest::napitest_interface::NodeISayHello(); + uint32_t callbackNum = 50; + ptr->CallbackfuncCallback(callbackNum); + delete ptr; + return; +} + +// 普通函数调用,返回str +std::string funcTest(bool& v) +{ + if (v) { + return "ret is true"; + } else { + return "ret is false"; + } +} + +// 1.打印值:from, to 以及枚举enum SayType的值 +// 2. 将回调值(0, "", "recv hello.")的值传回Js层 +void NodeISayHello::sayHelloWithResponse(std::string& from, std::string& to, uint32_t& sayType) +{ + // 1.打印 + NAPITEST_LOGI("NAPITEST_LOGI sayHelloWithResponse from = %s\r\n", from.c_str()); + NAPITEST_LOGI("NAPITEST_LOGI sayHelloWithResponse to = %s\r\n", to.c_str()); + NAPITEST_LOGI("NAPITEST_LOGI sayHelloWithResponse sayType = %d\r\n", sayType); + // 2.调用promise回调 (0, "", "recv hello.") + napitest::napitest_interface::NodeISayHello *p = new napitest::napitest_interface::NodeISayHello(); + // 调用工具接口将回调传回工具 + p->auto_interface_5SetCbValue(0, "", "recv hello."); + delete p; + return; +} +} diff --git a/examples/serviceCode/say_hello.h b/examples/serviceCode/say_hello.h new file mode 100644 index 00000000..6f2ef6a5 --- /dev/null +++ b/examples/serviceCode/say_hello.h @@ -0,0 +1,32 @@ +/* +* Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#ifndef IMPL_SAYHELLO_H +#define IMPL_SAYHELLO_H + +#include +#include + +namespace napitest { +class NodeISayHello; +class NodeISayHello { +public: + void sayHello(std::string& from, std::string& to, uint32_t& sayType); + void sayHi(std::string& from, std::string& to, uint32_t& sayType); + void sayHelloWithResponse(std::string& from, std::string& to, uint32_t& sayType); +}; +std::string funcTest(bool& v); +} +#endif // IMPL_SAYHELLO_H +