背景:
前几篇学习了元服务,后面几期就让我们开发简单的元服务吧,里面丰富的内容大家自己加,本期案例 仅供参考
先上本期效果图 ,里面图片自行替换
效果图1完整代码案例如下:
import { authentication } from '@kit.AccountKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';@Entry
@ComponentV2
struct Index {readonly PAGE_FULL: string = "100%";readonly PRE_96: string = "96%";readonly SIZE_40: number = 40;readonly SIZE_30: number = 30;readonly SIZE_20: number = 20;readonly SIZE_16: number = 16;readonly SIZE_8: number = 8;readonly SIZE_4: number = 4;@Local origin: string = '山园小梅・其一'@Local author: string = '林逋'@Local content: string = '疏影横斜水清浅,暗香浮动月黄昏。'@Local category: string = '古诗文 - 自然 - 梅花'@Local status: number = 0// {// "content": "疏影横斜水清浅,暗香浮动月黄昏。",// "author": "林逋",// "origin": "山园小梅・其一",// "category": "古诗文 - 自然 - 梅花"// },// {// "content": "秋风生渭水,落叶满长安。",// "author": "贾岛",// "origin": "忆江上吴处士",// "category": "古诗文 - 四季 - 秋天"// },// {// "content": "等闲识得东风面,万紫千红总是春。",// "author": "朱熹",// "origin": "春日",// "category": "古诗文 - 四季 - 春天"// }build() {Column() {Row({ space: this.SIZE_8 }) {Image($r('app.media.miaoyujiyu')).width(this.SIZE_20).height(this.SIZE_20)Text($r('app.string.EntryAbility_label')).fontSize(14).fontColor(Color.White)}.width(this.PAGE_FULL).borderRadius({ topLeft: 12, topRight: 12 }).height(this.SIZE_40).linearGradient({angle: 45,colors: [['#51A3AF', 0.1], ['#468C9E', 1.0]]}).padding({left: this.SIZE_16,right: this.SIZE_16})Column({ space: this.SIZE_8 }) {Text(this.origin).fontSize(18).fontWeight(FontWeight.Bolder).fontColor('#3F3F61')Text(this.author).fontSize(14).fontWeight(FontWeight.Medium).fontColor('#9D9D9D')Text(this.content).fontSize(16).fontColor('#454561')Text(' ' + this.category).fontSize(16).fontColor('#999999').margin({ left: 20 })Row({ space: 8 }) {Text('刷新')Image($r('app.media.uaxing')).width(this.SIZE_20).height(this.SIZE_20).fillColor(Color.White)}.borderWidth(1).margin({ top: 30 }).borderRadius(12).borderColor('#51A3AF').width('30%').justifyContent(FlexAlign.Center).padding(5).onClick(() => {// {// "content": "疏影横斜水清浅,暗香浮动月黄昏。",// "author": "林逋",// "origin": "山园小梅・其一",// "category": "古诗文 - 自然 - 梅花"// },// {// "content": "秋风生渭水,落叶满长安。",// "author": "贾岛",// "origin": "忆江上吴处士",// "category": "古诗文 - 四季 - 秋天"// },// {// "content": "等闲识得东风面,万紫千红总是春。",// "author": "朱熹",// "origin": "春日",// "category": "古诗文 - 四季 - 春天"// }// {// "content": "明月松间照,清泉石上流。",// "author": "王维",// "origin": "山居秋暝",// "category": "古诗文 - 自然 - 山水"// }console.log(`xxxthis.status : ---` + this.status)this.status = this.status + 1if (this.status == 0) {this.content = '疏影横斜水清浅,暗香浮动月黄昏。'this.author = '林逋'this.origin = '山园小梅・其一'this.category = '古诗文 - 自然 - 梅花'} else if (this.status == 1) {this.content = '秋风生渭水,落叶满长安。'this.author = '贾岛'this.origin = '忆江上吴处士'this.category = '古诗文 - 四季 - 秋天'} else if (this.status == 2) {this.content = '等闲识得东风面,万紫千红总是春。'this.author = '朱熹'this.origin = '春日'this.category = '古诗文 - 四季 - 春天'} else if (this.status == 3) {this.content = '明月松间照,清泉石上流。'this.author = '王维'this.origin = '山居秋暝'this.category = '古诗文 - 自然 - 山水'this.status = -1}})}.width(this.PAGE_FULL).margin({ top: 10 }).padding({ top: this.SIZE_4, bottom: this.SIZE_8 })}.width('86%').height('40%').borderWidth(1).borderRadius(12).margin({ left: 23, top: 70, right: 13 }).borderColor('#51A3AF')// .padding(16)}aboutToAppear() {hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');this.loginWithHuaweiID();}/*** Sample code for using HUAWEI ID to log in to atomic service.* According to the Atomic Service Review Guide, when a atomic service has an account system,* the option to log in with a HUAWEI ID must be provided.* The following presets the atomic service to use the HUAWEI ID silent login function.* To enable the atomic service to log in successfully using the HUAWEI ID, please refer* to the HarmonyOS HUAWEI ID Access Guide to configure the client ID and fingerprint certificate.*/private loginWithHuaweiID() {// Create a login request and set parameterslet loginRequest = new authentication.HuaweiIDProvider().createLoginWithHuaweiIDRequest();// Whether to forcibly launch the HUAWEI ID login page when the user is not logged in with the HUAWEI IDloginRequest.forceLogin = false;// Execute login requestlet controller = new authentication.AuthenticationController();controller.executeRequest(loginRequest).then((data) => {let loginWithHuaweiIDResponse = data as authentication.LoginWithHuaweiIDResponse;let authCode = loginWithHuaweiIDResponse.data?.authorizationCode;// Send authCode to the backend in exchange for unionID, session}).catch((error: BusinessError) => {hilog.error(0x0000, 'testTag', 'error: %{public}s', JSON.stringify(error));if (error.code == authentication.AuthenticationErrorCode.ACCOUNT_NOT_LOGGED_IN) {// HUAWEI ID is not logged in, it is recommended to jump to the login guide page}});}
}