1/* 2 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import router from '@ohos.router'; 17import image from '@ohos.multimedia.image'; 18import Logger from '../../../util/Logger'; 19import testNapi from 'libentry.so'; 20import { TitleBar } from '../../../common/TitleBar' 21import hilog from '@ohos.hilog'; 22 23const TAG: string = 'napi_roi'; 24 25@Entry 26@Component 27struct napistatus { 28 private btnFontColor: Resource = $r('app.color.white'); 29 private pixelMapFormat: image.PixelMapFormat = 3; 30 @State isSetInstance: Boolean = false; 31 @State imagePixelMap: PixelMap | undefined = undefined; 32 @State textcont: string = '指示 Node-API 调用成功或失败的完整状态代码。目前支持以下状态码。' 33 + '如果 API 返回失败状态时需要额外的信息,可以通过调用 napi_get_last_error_info 获取。' 34 @State testcont: string = ' // 获取导出的 N-API 实例对象 \n' 35 + " const addon = require('./build/Release/module_name'); \n" 36 + ' // 使用实例对象进行操作 \n' 37 + ' const result = addon.add(5, 1); \n' 38 + ' // 成功案例 \n' 39 + ' console.log(`The result of the addition is: ${result}`); \n' 40 + ' // 失败案例,设置 > 10 报错 \n' 41 + ' try { \n' 42 + ' const result = addon.add(5, 10); \n' 43 + ' } catch (error) { \n' 44 + " console.error('An error occurred in the addon:', error.message); \n" 45 + ' } \n'; 46 47 controller: TextAreaController = new TextAreaController() 48 49 build() { 50 Column() { 51 // 标题 52 TitleBar({ title: $r('app.string.napi_status') }) 53 54 Column() { 55 Column() { 56 TextArea({ 57 text: this.textcont, 58 placeholder: '', 59 }) 60 .placeholderFont({ size: 16, weight: 400 }) 61 .width('90%') 62 .margin(10) 63 .fontSize(16) 64 .fontColor('#182431') 65 .backgroundColor('#FFFFFF') 66 .enabled(false) 67 68 TextArea({ 69 text: this.testcont, 70 placeholder: '', 71 }) 72 .placeholderFont({ size: 16, weight: 400 }) 73 .width('90%') 74 .margin(10) 75 .fontSize(16) 76 .fontColor('#ff400336') 77 .backgroundColor('#ff985307') 78 .enabled(false) 79 } 80 .width('100%') 81 .alignItems(HorizontalAlign.Center) 82 .justifyContent(FlexAlign.Start) 83 84 Row() { 85 86 Button($r('app.string.napi_status'), { type: ButtonType.Capsule }) 87 .backgroundColor(Color.Blue) 88 .width('80%') 89 .height(48) 90 .fontSize(16) 91 .fontWeight(500) 92 .fontColor(this.btnFontColor) 93 .margin({ left: 24 }) 94 .id('napstatus') 95 .onClick(() => { 96 let result = testNapi.testNapiStatus(5, 1); 97 console.log(`The result of the testNapiStatus is: ${result}`); 98 this.testcont = this.testcont.replace('${result}', 'log(## '+result+' ##)'); 99 try { 100 let result1 = testNapi.testNapiStatus(5, 10); 101 } catch (error) { 102 console.log(`Catch eror of testNapiStatus is: ${error.message}`); 103 this.testcont = this.testcont.replace('error.message', 'log(## '+error.message+' ##)'); 104 } 105 }) 106 } 107 .width('100%') 108 .height(48) 109 .alignItems(VerticalAlign.Center) 110 .justifyContent(FlexAlign.SpaceBetween) 111 } 112 .width('100%') 113 } 114 .height('100%') 115 .width('100%') 116 .backgroundColor($r('app.color.background_shallow_grey')) 117 } 118} 119