HarmonyOS NEXT 实战之元服务:静态案例效果---本地生活服务

2024-12-30 13:43:33
30次阅读
0个评论

背景:

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

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

73.png

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

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

@Entry
@ComponentV2
struct Index {
  @Local isOn: boolean = true;

  build() {
    Column({ space: 13 }) {
      Text($r('app.string.EntryAbility_label'))
        .fontSize(24)
        .fontColor(Color.White)
        .fontWeight(FontWeight.Bold)
        .margin({ top: 10, left: 16 })

      Column() {

        Row() {
          this.fuYong($r('app.media.img'), '手机充值')
          this.fuYong($r('app.media.img_1'), '生活缴费')
          this.fuYong($r('app.media.img_2'), '附近加油')
          this.fuYong($r('app.media.img_3'), '挂号体检')
        }.width('95%').justifyContent(FlexAlign.SpaceAround)

        Row() {
          this.fuYong($r('app.media.img_4'), '同城服务')
          this.fuYong($r('app.media.img_5'), '买房租房')
        }.width('95%').justifyContent(FlexAlign.SpaceAround)
      }
      .width('95%')
      .padding(16)
      .borderRadius(12)
      .backgroundColor('#DCE1DD')

      Row() {
        Column({ space: 5 }) {
          Text('接收服务通知').fontColor(Color.Black).fontSize(24).fontWeight(FontWeight.Bold)
          Text('根据您用此服务的规律,提供智能化更新').fontColor('#B5B5B5').fontSize(12)
        }.alignItems(HorizontalAlign.Start)

        Toggle({ type: ToggleType.Switch, isOn: this.isOn }).onChange(value => {
          this.isOn = value
          if (this.isOn) {
            promptAction.showToast({ message: '已开启服务通知' })
          } else {
            promptAction.showToast({ message: '已关闭服务通知' })
          }
        })
      }
      .width('96%')
      .justifyContent(FlexAlign.SpaceBetween)
      .backgroundColor('#DCE1DD')
      .borderRadius(10)
      .padding(8)

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

  @Builder
  fuYong(img: ResourceStr, text: string) {
    Column({ space: 3 }) {
      Image(img).width(40).height(40)
      Text(text)
    }.backgroundColor('#DCE1DD').padding(5)
    .onClick(() => {
      promptAction.showToast({ message: '当前城市暂不支持此服务,请耐心等待后续开通...' })
    })
  }

  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 发布应用市场的详细步骤与流程

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

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

收藏00

登录 后评论。没有帐号? 注册 一个。