HarmonyOS NEXT 实战之元服务:静态案例效果--- 歌手推荐

server/2024/12/27 9:13:44/

背景:

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

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

在这里插入图片描述

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

import { authentication } from '@kit.AccountKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';@Entry
@ComponentV2
struct Index {@Local threeArrChild: CheckModel[] = GetThreeLevelWeiBoData@Local hotTypeChild: string = '0'@Local hotType: string = '2'@Local threeArr: CheckModel[] = GetThreeLevelWeiBoDatabuild() {Column({space:10}) {Text($r('app.string.EntryAbility_label')).fontSize(25)threeLevel(this)listData(this)}.alignItems(HorizontalAlign.Start).height('100%').width('100%').margin({ top: 50 }).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}});}
}@ObservedV2
export class CheckModel {name: ResourceStr = ''value: string = ''@Trace checked: boolean = falseconstructor(name: ResourceStr, value: string, checked: boolean = false) {this.name = namethis.value = valuethis.checked = checked}
}@Builder
function threeLevel(searchAllThis: Index) {List() {ForEach(searchAllThis.threeArr, (item: CheckModel, index: number) => {ListItem() {Text(item.name).height('100%').fontSize(14).fontColor(item.checked ? "#5090F1" : '#666666').margin({ right: 16 }).onClick(() => {if (item.checked) {return}for (let i = 0; i < searchAllThis.threeArr.length; i++) {searchAllThis.threeArr[i].checked = false}for (let i = 0; i < searchAllThis.threeArr.length; i++) {}item.checked = truesearchAllThis.hotTypeChild = item.name + ''//todo 模拟数据searchAllThis.threeArrChild = GetThreeLevelWeiBoData})}}, (item: CheckModel, index: number) => {//每个tab下都有相同数据: 如 “热榜” ,键值生成函数不能相同return searchAllThis.hotType + '__' + JSON.stringify(item) + '__' + index})}.width('100%').height(14).listDirection(Axis.Horizontal).scrollBar(BarState.Off)
}export const GetThreeLevelWeiBoData: CheckModel[] = [new CheckModel('热榜', '0', true),new CheckModel('国内榜', '2'),new CheckModel('实时上升榜', '3'),new CheckModel('国外榜', '1'),
]/** 列表数据 */
@Builder
function listData(searchAllThis: Index) {List({ space: 2 }) {ForEach(searchAllThis.threeArrChild, (item: string, index: number) => {ListItem() {Row({ space: 4 }) {Text(`${index + 1}`).width(16).height(16).fontSize(10).fontColor(Color.White).textAlign(TextAlign.Center).backgroundImage(index == 0 ? $r('app.media.ic_hot_no1') : (index == 1 ? $r('app.media.ic_hot_no2') :(index == 2 ? $r('app.media.ic_hot_no3') : $r('app.media.ic_hot_no_other')))).backgroundImageSize(ImageSize.Cover)Text(generateFiveCharacterRandomString() + '__' +searchAllThis.hotTypeChild /*`衡阳市女子看守所所长夫妇大人衡阳市女子看守所所长夫妇大人`*/).fontSize(14).fontColor('#222222').layoutWeight(1).maxLines(1).textOverflow({ overflow: TextOverflow.Ellipsis })Row({ space: 4 }) {Image($r('app.media.ic_hot')).width(16).height(16)Text(generateFiveDigitRandomNumber() + '').fontSize(12).fontColor('#E65441').fontWeight(FontWeight.Bold)}Text(generateRandomDate()).fontSize(11).fontColor('#222222')}.width('#100%').height(44)}})}.width('#100%').height('#100%').margin({ top: 10 }).layoutWeight(1).padding({ right: 12 })}function generateFiveDigitRandomNumber(): number {const min = 10000; // 五位数的最小值const max = 99999; // 五位数的最大值return Math.floor(Math.random() * (max - min + 1)) + min;
}function generateFiveCharacterRandomString(): string {const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';let result = '';for (let i = 0; i < 5; i++) {result += characters.charAt(Math.floor(Math.random() * characters.length));}return result;
}function generateRandomDate(): string {const minYear = 2023; // 最小年份const maxYear = 2024; // 最大年份const minMonth = 1; // 最小月份const maxMonth = 12; // 最大月份const minDay = 1; // 最小日期const maxDay = 31; // 最大日期// 生成随机年份const year = Math.floor(Math.random() * (maxYear - minYear + 1)) + minYear;// 生成随机月份const month = Math.floor(Math.random() * (maxMonth - minMonth + 1)) + minMonth;// 根据月份生成合理的日期let day = 0;if ([1, 3, 5, 7, 8, 10, 12].includes(month)) {day = Math.floor(Math.random() * (31 - minDay + 1)) + minDay;} else if ([4, 6, 9, 11].includes(month)) {day = Math.floor(Math.random() * (30 - minDay + 1)) + minDay;} else if (month === 2) {// 处理闰年if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) {day = Math.floor(Math.random() * (29 - minDay + 1)) + minDay;} else {day = Math.floor(Math.random() * (28 - minDay + 1)) + minDay;}}// 返回格式化的日期字符串return `${year}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`;
}

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

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

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

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

  • gitee:https://gitee.com/jiaojiaoone/explore-harmony-next/tree/case%2Fwanandroid/
  • github:https://github.com/JasonYinH/ExploreHarmonyNext.git

http://www.ppmy.cn/server/153265.html

相关文章

AppAgent 源码 (xml 解析)

1. 数据准备 adb shell uiautomator dump /sdcard/output.xml # 获取手机ui界面的xml文件 adb pull /sdcard/output.xml output.xml # 将手机上的xml文件拉取到电脑上具体的xml文件&#xff1a; <?xml version1.0 encodingUTF-8 standaloneyes ?> <hierarchy ro…

威联通NAS部署openwrt软路由保姆级教程附镜像文件

创作立场&#xff1a;原创不易&#xff0c;拒绝搬运~ hello 大家好&#xff0c;我是你们的老伙伴&#xff0c;稳重的大王~ 本期教程为大家分享&#xff0c;怎么在NAS里面部署软路由&#xff0c;下面是软路由的镜像文件&#xff0c;有两个版本&#xff0c;400M的是定制版~ Sh…

基于vue-popperjs的二次封装弹窗

前言&#xff1a; 基于vue-popperjs的二次封装代码 <template><!-- 1. :appendToBody"true"是否把位置加到body外层标签上饿了么UI和antD是true&#xff0c;iview和vuetifyjs是false2. trigger属性触发方式&#xff0c;常用hover悬浮触发、clickToOpen鼠标…

如何部署SparkHistoryServer

spark-defaults.conf的配置&#xff1a; # 镜像内配置路径&#xff1a; /opt/spark/conf/spark-defaults.confspark.history.fs.logDirectoryhdfs://xxx spark.history.ui.port18080 spark.history.retainedApplications20 在提交Spark任务时&#xff0c;需要指定下面两个参数…

MySQL45讲 第三十六讲 为什么临时表可以重名?——阅读总结

文章目录 MySQL45讲 第三十六讲 为什么临时表可以重名&#xff1f;——阅读总结一、引言二、临时表与内存表的区别&#xff08;一&#xff09;内存表&#xff08;二&#xff09;临时表 三、临时表的特性&#xff08;一&#xff09;可见性与生命周期&#xff08;二&#xff09;与…

Linux 日志监控与报警系统实操

1.日志监控基础 监控文件变化&#xff1a; tail -f&#xff1a;实时查看文件末尾的变化。 tail -n&#xff1a;指定查看最近的 N 行。 结合管道过滤关键内容&#xff1a; 配合grep 提取特定关键字。 例子&#xff1a; tail -f /var/log/syslog | grep "error"2.…

Hbase的特点、特性

Hbase是一种非关系型数据库&#xff0c;它的存储基于hdfs&#xff0c;是一种分布式的、稀疏的多维排序的映射。 &#xff08;1&#xff09;分布式。Hbase是分布式、可拓展的&#xff0c;以hdfs为数据存储 &#xff08;2&#xff09;稀疏。hbase中存储的表的每一行可以有不同数…

ChatGPT生成接口文档实践案例(一)

生成接口文档的方法有很多&#xff0c;如研发人员手工编写或通过Swagger、Postman、Apiary、ChatGPT自动生成。在此仅针对利用ChatGPT生成接口文档的方法进行介绍&#xff0c;其他两种方法不赘述。 以飞机订票系统的用户注册功能为例&#xff0c;其用户注册的界面如图5-9所示。…