背景:

前几篇学习了元服务,后面几期就让我们开发简单的元服务吧,里面丰富的内容大家自己加,本期案例 仅供参考

先上本期效果图 ,里面图片自行替换

HarmonyOS NEXT 实战之元服务:静态案例效果---本地特色景色_Text

效果图1完整代码案例如下:

import { authentication } from '@kit.AccountKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

@Entry
@Component
struct Index {
  build() {
    Column({ space: 16 }) {
      Text($r('app.string.EntryAbility_label')).fontSize(24).fontWeight(FontWeight.Bold).margin({ top: 10, left: 16 })
      Column({ space: 10 }) {

        Row() {
          this.fuYong($r('app.media.img'), '西格拉姆', '艺术博物馆')
          Column({ space: 6 }) {
            Column({ space: 7 }) {
              Image($r('app.media.img_1')).width(150).height(100).borderRadius(10)
              Text('海港大桥             ').fontSize(20).fontWeight(FontWeight.Bold)
              Text('感受悉尼的繁华与活力').fontSize(15).fontColor('#B3B3B4')
            }.width(200)
            Column({ space: 7 }) {
              Image($r('app.media.img_2')).width(150).height(100).borderRadius(10)
              Text('崇礼雪场             ').fontSize(20).fontWeight(FontWeight.Bold)
              Text('冬奥会滑雪项目举办地').fontSize(15).fontColor('#B3B3B4')
            }.width(200)
          }
        }.width('100%')
        .height(190).justifyContent(FlexAlign.SpaceBetween)

      }
      .height('50%')
      .width('90%')
      .padding(16)
      .borderWidth(1)
      .borderRadius(12)
      .borderColor('#D09538')
    }

    .height('100%')
    .width('100%')
    .backgroundColor('#F2F3F5')
    .margin({ top: 60 })
  }

  @Builder
  fuYong(img: ResourceStr, text: string, content: string) {
    Column({ space: 7 }) {
      Image(img).width(120).height(280).borderRadius(10)
      Text(text).fontSize(20).fontWeight(FontWeight.Bold)
      Text(content).fontSize(15).fontColor('#B3B3B4')
    }
  }

  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 parameters
    let loginRequest = new authentication.HuaweiIDProvider().createLoginWithHuaweiIDRequest();
    // Whether to forcibly launch the HUAWEI ID login page when the user is not logged in with the HUAWEI ID
    loginRequest.forceLogin = false;
    // Execute login request
    let 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

      }
    });
  }
}

最近文章>>>>>>>>>>>

HarmonyOS NEXT实战:元服务与应用 APP 发布应用市场的详细步骤与流程

若本文对您稍有帮助,诚望您不吝点赞,多谢。

有兴趣的同学可以点击查看源码