背景:
前几篇学习了元服务,后面几期就让我们开发简单的元服务吧,里面丰富的内容大家自己加,本期案例 仅供参考
先上本期效果图 ,里面图片自行替换
效果图1完整代码案例如下:
import { authentication } from '@kit.AccountKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
build() {
Column() {
Text($r('app.string.EntryAbility_label')).fontWeight(FontWeight.Bold).fontSize(29).fontColor("#222222").margin({ top: 20, bottom: 20 })
Row() {
Stack() {
Progress({
value: 6800,
total: 10000,
type: ProgressType.Ring
})
.width(84)
.color("#1B80FA")
.style({ strokeWidth: 10 })
.backgroundColor('#EDF3FD')
Text('68%').fontSize(14).fontColor("#222222").fontWeight(FontWeight.Bold)
}.margin({ left: 16 })
Column({ space: 6 }) {
Text('请放心使用').fontColor('#383838').fontSize(25)
Text('本周自动清理 1.13 GB').fontColor('#9E9E9E').fontSize(15)
Text('已使用 52.91 GB/256 GB >').fontColor('#9E9E9E').fontSize(15)
}.alignItems(HorizontalAlign.Start).margin({ left: 8 })
}.width('100%').height(150).backgroundColor(Color.White)
.borderRadius(10)
Text('推荐压缩').fontSize(24).fontColor("#222222").margin({ top: 20, bottom: 20 })
Row() {
Text().backgroundColor('#EB6F20').width(40).height(40).borderRadius(20)
Column({ space: 6 }) {
Text('113 组文件可压缩').fontColor('#383838').fontSize(20)
Text('491MB').fontColor('#A5A5A5').fontSize(15)
}.alignItems(HorizontalAlign.Start).margin({ left: 8 }).layoutWeight(1)
Button("无损压缩").height(40).fontSize(16).margin({ left: 4 })
}
.width('100%')
.height(100)
.backgroundColor(Color.White)
.borderRadius(10)
.padding(16)
Row() {
Text().backgroundColor('#F8A52B').width(40).height(40).borderRadius(20)
Column({ space: 6 }) {
Text('垃圾文件').fontColor('#383838').fontSize(20)
Text('2.36 GB').fontColor('#A5A5A5').fontSize(15)
}.alignItems(HorizontalAlign.Start).margin({ left: 8 }).layoutWeight(1)
Button("立即清理").height(40).fontSize(16).margin({ left: 4 }).backgroundColor('#F8A52B')
}
.width('100%')
.height(80)
.backgroundColor(Color.White)
.borderRadius(10)
.padding(16)
.margin({ top: 16 })
}
.height('100%')
.width('100%')
.padding(16)
.backgroundColor('#F2F3F5')
.margin({ top: 60 })
.alignItems(HorizontalAlign.Start)
}
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
}
});
}
}
效果图2完整代码案例如下:
import { authentication } from '@kit.AccountKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
@Entry
@ComponentV2
struct Index {
@Local c1: number = 535;
@Local c2: number = 348;
build() {
Column({ space: 8 }) {
Text($r('app.string.EntryAbility_label'))
.fontWeight(FontWeight.Bold)
.fontSize(29)
.fontColor("#222222")
.margin({ top: 20, bottom: 20 })
Row() {
Checkbox()
.select(true).onChange(value => {
console.log('xxxxxxxxxxxx' + value)
})
Column({ space: 6 }) {
Text('内存垃圾').fontColor('#383838').fontSize(20)
Text('已选2项').fontColor('#6A6A6A').fontSize(15)
}.alignItems(HorizontalAlign.Start).margin({ left: 8 }).layoutWeight(1)
Text('2 项 >').fontColor('#6A6A6A').fontSize(20)
}
.width('100%')
.height(100)
.backgroundColor(Color.White)
.borderRadius(10)
.padding(16)
// 缓存垃圾
// 已选 535 KB
// 系统垃圾
//
// 2 项
// 535 KB2
// 348 KB
Row() {
Checkbox()
.select(true).onChange(value => {
this.c1 = value ? 535 : 0
})
Column({ space: 6 }) {
Text('缓存垃圾').fontColor('#383838').fontSize(20)
Text('已选 535 KB').fontColor('#6A6A6A').fontSize(15)
}.alignItems(HorizontalAlign.Start).margin({ left: 8 }).layoutWeight(1)
Text('535 KB').fontColor('#6A6A6A').fontSize(20)
}
.width('100%')
.height(100)
.backgroundColor(Color.White)
.borderRadius(10)
.padding(16)
Row() {
Checkbox()
.select(true).onChange(value => {
this.c2 = value ? 348 : 0
})
Column({ space: 6 }) {
Text('系统垃圾').fontColor('#383838').fontSize(20)
Text('已选 348 KB').fontColor('#6A6A6A').fontSize(15)
}.alignItems(HorizontalAlign.Start).margin({ left: 8 }).layoutWeight(1)
Text('348 KB >').fontColor('#6A6A6A').fontSize(20)
}
.width('100%')
.height(100)
.backgroundColor(Color.White)
.borderRadius(10)
.padding(16)
Button(`删除(已选 ${this.c1 + this.c2} KB)`)
.width('70%')
.margin({ top: 40, left: 50 })
.backgroundColor('#E7E8EA')
.fontColor('#E65A5B')
}
.height('100%')
.width('100%')
.alignItems(HorizontalAlign.Start)
.padding(16)
.backgroundColor('#F2F3F5')
.margin({ top: 50 })
}
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 发布应用市场的详细步骤与流程
若本文对您稍有帮助,诚望您不吝点赞,多谢。