1/* 2* Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd. 3* Licensed under the Apache License, Version 2.0 (the "License"); 4* you may not use this file except in compliance with the License. 5* You may obtain a copy of the License at 6* 7* http://www.apache.org/licenses/LICENSE-2.0 8* 9* Unless required by applicable law or agreed to in writing, software 10* distributed under the License is distributed on an "AS IS" BASIS, 11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12* See the License for the specific language governing permissions and 13* limitations under the License. 14*/ 15 16import PayView from '../component/payView'; 17import TitleBar from '../component/TitleBar'; 18import { PayResult } from '@ohos/cloud'; 19import promptAction from '@ohos.promptAction'; 20import common from '@ohos.app.ability.common'; 21 22@Entry 23@Component 24struct Index { 25 @State isPaying: boolean = false; 26 @StorageLink('totalPrice') totalPrice: string = ''; 27 @StorageLink('orderId') orderId: string = ''; 28 @State isPaySuccess: boolean = false; 29 @State countdownNum: number = 3; 30 private timeOut: number = 0; 31 private context = getContext(this) as common.UIAbilityContext; 32 private dialogController: CustomDialogController = new CustomDialogController({ 33 builder: PayView({ payComplete: (result: PayResult) => { 34 switch (result) { 35 case PayResult.SUCCESS: 36 this.isPaySuccess = true; 37 this.dialogController.close(); 38 this.timeOut = setInterval(() => { 39 this.countdownNum--; 40 if (this.countdownNum === 0) { 41 this.timeOut = 0; 42 this.context.terminateSelf(); 43 } 44 }, 1000); 45 break; 46 case PayResult.FAIL: 47 promptAction.showToast({ message: $r('app.string.pay_fail') }); 48 break; 49 case PayResult.ERROR: 50 this.dialogController.close(); 51 promptAction.showToast({ message: $r('app.string.pay_exception') }); 52 break; 53 default: 54 this.dialogController.close(); 55 promptAction.showToast({ message: $r('app.string.pay_exception') }); 56 break; 57 } 58 } }), 59 alignment: DialogAlignment.Bottom, 60 customStyle: true, 61 autoCancel: false 62 }) 63 64 aboutToDisappear() { 65 if (this.timeOut) { 66 this.timeOut = 0; 67 } 68 } 69 70 build() { 71 Column() { 72 TitleBar({ hasBackPress: true, title: $r('app.string.pay') }) 73 Column() { 74 if (this.isPaySuccess) { 75 Column() { 76 Row() { 77 Image($r('app.media.ic_public_confirm')) 78 .height(48) 79 .width(48) 80 } 81 .height(72) 82 .width(72) 83 .borderRadius(36) 84 .justifyContent(FlexAlign.Center) 85 .backgroundColor($r('app.color.icon_background')) 86 87 Text($r('app.string.pay_success')) 88 .fontSize(20) 89 .margin({ top: 24 }) 90 .fontWeight(500) 91 .width('100%') 92 .textAlign(TextAlign.Center) 93 .fontColor($r('app.color.normal_font_color')) 94 95 Button() { 96 Text() { 97 Span($r('app.string.return_to_merchant')) 98 .fontColor(Color.White) 99 .fontSize(16) 100 .fontWeight(500) 101 Span(`(${this.countdownNum})`) 102 .fontColor(Color.White) 103 .fontSize(16) 104 .fontWeight(500) 105 } 106 .id('to_merchant') 107 .textAlign(TextAlign.Center) 108 .width('100%') 109 .height('100%') 110 } 111 .height(39) 112 .width(180) 113 .margin({ top: 101 }) 114 .onClick(() => { 115 this.timeOut = 0; 116 this.context.terminateSelf(); 117 }) 118 } 119 .height('100%') 120 .justifyContent(FlexAlign.Center) 121 } else { 122 Text($r('app.string.merchant_tracking_number')) 123 .textAlign(TextAlign.Center) 124 .width('100%') 125 .fontColor(Color.Black) 126 .opacity(0.9) 127 .fontSize(16) 128 .fontWeight(500) 129 .margin({ top: 48 }) 130 Text(`${this.orderId}`) 131 .fontColor(Color.Black) 132 .opacity(0.9) 133 .fontSize(14) 134 .margin({ top: 8 }) 135 Text($r('app.string.please_pay')) 136 .fontColor(Color.Black) 137 .fontWeight(500) 138 .opacity(0.9) 139 .fontSize(20) 140 .margin({ top: 32 }) 141 Text(`${this.totalPrice}`) 142 .fontColor(Color.Black) 143 .opacity(0.9) 144 .fontSize(38) 145 .fontWeight(500) 146 .margin({ top: 52 }) 147 Button($r('app.string.pay_now')) 148 .width(180) 149 .height(39) 150 .margin({ top: 135 }) 151 .onClick(() => { 152 this.dialogController.open() 153 }) 154 } 155 } 156 .backgroundColor(Color.White) 157 .margin({ left: 12, right: 12, bottom: 24 }) 158 .border({ radius: 16 }) 159 .layoutWeight(1) 160 } 161 .height('100%') 162 .backgroundColor($r('app.color.normal_background')) 163 } 164}