中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
实名认证Button的实现
微信扫码分享
// Real_name_authentication.ets import { FunctionalButton, functionalButtonComponentManager } from '@kit.ScenarioFusionKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import http from '@ohos.net.http'; import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; interface Result { state?: number; realNameLevel?: number; verifyResult?: number; verifyToken?: string; } @Entry @Component struct SecondPage { obtainRealNameDataInfo(authCode: string, openID: string, realName: string, idNo: string, callback: AsyncCallback<Result>) { // 每一个httpRequest对应一个HTTP请求任务,不可复用 let httpRequest = http.createHttp(); // 用于订阅HTTP响应头,此接口会比request请求先返回。可以根据业务需要订阅此消息 httpRequest.on('headersReceive', (header) => { hilog.info(0x0000, "testTag", "header: %{public}s", header as string); }); httpRequest.request( // 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定 "EXAMPLE_URL", { method: http.RequestMethod.POST, // 开发者根据自身业务需要添加header字段 header: { 'Content-Type': 'application/json' }, // 当使用POST请求时此字段用于传递内容 extraData: { "authCode": authCode, "openID": openID, "realname": realName, "idNo": idNo, }, expectDataType: http.HttpDataType.STRING, usingCache: true, priority: 1, connectTimeout: 60000, readTimeout: 60000, usingProtocol: http.HttpProtocol.HTTP1_1, }, (err, data) => { if (!err) { let res: Result = JSON.parse(data.result as string); callback(undefined, res); } else { let error: BusinessError = { code: err.code, message: err.message, name: '' } callback(error, undefined); // 取消订阅HTTP响应头事件 httpRequest.off('headersReceive'); // 当该请求使用完毕时,调用destroy方法主动销毁 httpRequest.destroy(); } }) } build() { Row() { Column() { FunctionalButton({ params: { // OpenType.REAL_NAME_AUTHENTICATION表示Button为实名认证类型 openType: functionalButtonComponentManager.OpenType.REAL_NAME_AUTHENTICATION, label: '实名认证', // 调整Button样式 styleOption: { styleConfig: new functionalButtonComponentManager.ButtonConfig() .fontSize(20) .fontColor(Color.Black) } }, // OpenType为“REAL_NAME_AUTHENTICATION”时,回调必须选择“onRealNameAuthentication” controller: new functionalButtonComponentManager.FunctionalButtonController() .onRealNameAuthentication((err, data) => { if (err) { // 错误日志处理 hilog.error(0x0000, "testTag", "error: %{public}d %{public}s", err.code, err.message); return; } // 成功日志处理 hilog.info(0x0000, "testTag", "succeeded in authenticating"); // 获取Authorization Code let authCode: string = data.authCode as string; let openID: string = data.openID as string; this.obtainRealNameDataInfo(authCode, openID, 'name', 'idNo', (err, data) => { if (err) { hilog.error(0x0000, "testTag", "error: %{public}d %{public}s", err.code, err.message); return; } let state = data.state; let realNameLevel = data.realNameLevel; let verifyResult = data.verifyResult; let verifyToken = data.verifyToken; hilog.info(0x0000, "testTag", "succeeded in verifying"); }) }) }) } .width('100%') } .height('100%') } }