HarmonyOS NEXT 实战之元服务:静态案例效果---咖啡制作实况窗
背景:
前几篇学习了元服务,后面几期就让我们开发简单的元服务吧,里面丰富的内容大家自己加,本期案例 仅供参考
先上本期效果图 ,里面图片自行替换
效果图1完整代码案例如下:
import { authentication } from '@kit.AccountKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
@Entry
@ComponentV2
struct Index {
build() {
Column() {
Column() {
Row() {
Row() {
Image($r('app.media.kefei')).width(50).height(50)
Column({ space: 4 }) {
Text('制作完成').fontColor(Color.White).fontSize(18)
Text('冰美式 | 中关村店铺').fontColor(Color.White)
}.alignItems(HorizontalAlign.Start).margin({ left: 6 })
}
Button('取餐码')
}.width('100%').justifyContent(FlexAlign.SpaceBetween)
Row() {
Column({ space: 4 }) {
Text('取餐号:').fontSize(16).fontColor(Color.White)
Text('1236').fontSize(24).fontColor(Color.White).fontWeight(FontWeight.Bold)
}.alignItems(HorizontalAlign.Start).margin({ left: 6 })
Image($r('app.media.img')).width(70).height(70).borderRadius(8)
}.width('100%').justifyContent(FlexAlign.SpaceBetween).margin({ top: 28 })
}
.height(180)
.margin({ top: 60, left: 8 })
.padding(16)
.width('96%')
.backgroundColor('#816A66')
.borderRadius(12)
Row() {
Text('订单号:').fontSize(16).fontColor(Color.White)
Text('12364946145641').fontSize(19).fontColor(Color.Black).fontWeight(FontWeight.Bold)
}
.width('100%')
.height(54)
.padding(5)
.borderRadius(10)
.justifyContent(FlexAlign.SpaceBetween)
.margin({ top: 28, left: 8 })
.backgroundColor('#837D79')
Row() {
Text('制作完成时间:').fontSize(16).fontColor(Color.White)
Text(this.getDate()).fontSize(19).fontColor(Color.Black).fontWeight(FontWeight.Bold)
}
.width('100%')
.height(54)
.padding(5)
.borderRadius(10)
.justifyContent(FlexAlign.SpaceBetween)
.margin({ top: 1, left: 8 })
.backgroundColor('#9EC1D4')
}.width('96%').height('100%')
}
private getDate(): string {
const now = new Date();
const year = now.getFullYear(); // 获取年份
const month = now.getMonth() + 1; // 获取月份,注意月份是从0开始的,所以需要加1
const day = now.getDate(); // 获取日期
const hour = now.getHours(); // 获取小时
const minute = now.getMinutes(); // 获取分钟
return `${year}-${month}-${day} ${hour}:${minute}`
}
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
}
});
}
}