背景:

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

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

HarmonyOS NEXT 实战之元服务:静态案例效果--- 我的一课表_git


HarmonyOS NEXT 实战之元服务:静态案例效果--- 我的一课表_git_02

效果图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 = '08:00-10:15'
  @Local category: string = '教学楼A'
  @Local teacher: string = '王老师'
  @Local status: number = 0

  build() {
    Column() {
      Row({ space: this.SIZE_8 }) {
        Row({ space: this.SIZE_8 }) {
          Image($r('app.media.startIcon'))
            .width(this.SIZE_20)
            .height(this.SIZE_20)

          Text($r('app.string.EntryAbility_label'))
            .fontSize(14)
            .fontColor(Color.Black)
        }

        Image($r('app.media.shuaxin'))
          .width(this.SIZE_20)
          .height(this.SIZE_20).onClick(() => {
          this.status = this.status + 1

          if (this.status == 0) {
            this.content = '08:00-10:15'
            this.author = '课程目录:语文'
            this.origin = '课程表一'
            this.category = '教学楼A'
            this.teacher='王老师'
          } else if (this.status == 1) {
            this.content = '08:20-10:00'
            this.author = '课程目录:数学'
            this.origin = '课程表二'
            this.category = '教学楼B'
            this.teacher='李老师'
          } else if (this.status == 2) {
            this.content = '09:00-10:15'
            this.author = '课程目录:英语'
            this.origin = '课程表三'
            this.category = '教学楼C'
            this.teacher='贾老师'
          } else if (this.status == 3) {
            this.content = '11:00-12:15'
            this.author = '课程目录:物理'
            this.origin = '课程表四'
            this.category = '教学楼D'
            this.teacher='樊老师'
          }else if (this.status == 4) {
            this.content = '14:00-15:15'
            this.author = '课程目录:地理'
            this.origin = '课程表五'
            this.category = '教学楼E'
            this.teacher='朱老师'
            this.status = -1

          }


        })

      }
      .justifyContent(FlexAlign.SpaceBetween)
      .width(this.PAGE_FULL)
      .borderRadius({ topLeft: 12, topRight: 12 })
      .height(this.SIZE_40)
      .linearGradient({
        angle: 45,
        colors: [['#F4FCFF', 0.1], ['#45B0E6', 1.0]]
      })
      .padding({
        left: this.SIZE_16,
        right: this.SIZE_16
      })

      Column({ space: 12}) {
        Column({ space:12 }) {
          Row() {
            Text(this.origin).fontColor(Color.Black).fontSize(18).fontWeight(FontWeight.Bold)
            Text().width(20).height(20).borderRadius(10).backgroundColor('#3877FE')
          }.width('100%').justifyContent(FlexAlign.SpaceBetween)
          Text(this.author).fontColor(Color.Black).fontSize(18)
          Text('上课时间:'+this.content).fontColor(Color.Black).fontSize(18)

          Row() {
            Image($r('app.media.weizhi')).width(30).height(30)
            Text(this.category)
          }.width('100%')
          Text('授课老师:'+this.teacher)
        }.width('86%').backgroundColor('#EBF1FF').borderRadius(10).padding(16).alignItems(HorizontalAlign.Start)

      }
      .width(this.PAGE_FULL)
      .margin({ top: 10 })
      .padding({ top: 10, bottom: 14 })
    }
    .width('86%')
    .height('40%')
    .borderWidth(1)
    .borderRadius(12)
    .margin({ left: 23, top: 70, right: 13 })
    .borderColor('#45B0E6')
  }

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


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