1/* 2 * Copyright (c) 2023 Huawei Device 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 { CustomContentDialog, ButtonOptions } from '@ohos.arkui.advanced.Dialog'; 17import GlobalContext from '../common/GlobalContext'; 18import { GetAlertMessage } from '../common/GetAlertMessage'; 19import common from '@ohos.app.ability.common'; 20import Want from '@ohos.app.ability.Want'; 21import { BusinessError } from '@ohos.base'; 22import Constants from '../common/constant'; 23import ability from '@ohos.ability.ability'; 24import account_osAccount from '@ohos.account.osAccount'; 25import dlpPermission from '@ohos.dlpPermission'; 26import { sendDlpManagerAccountLogin, checkNetworkStatus } from '../common/utils'; 27import { HiLog } from '../common/HiLog'; 28 29const TAG = 'Alert'; 30let abilityResult: ability.AbilityResult = { 31 'resultCode': 0, 32 'want': {} 33}; 34 35let storage = LocalStorage.getShared(); 36@Entry(storage) 37@Component 38struct Index { 39 @State alertWant: Want | undefined = GlobalContext.load('alertWant'); 40 @State title: ResourceStr = ''; 41 @State message: ResourceStr = ''; 42 @State cancel: ResourceStr = ''; 43 @State actionButton: ResourceStr = ''; 44 @State buttonOptions: ButtonOptions[] = []; 45 46 dialogControllerButton: CustomDialogController | null = new CustomDialogController({ 47 builder: CustomContentDialog({ 48 primaryTitle: this.title, 49 contentBuilder: () => { 50 this.buildContent(); 51 }, 52 buttons: this.buttonOptions 53 }), 54 showInSubWindow: true, 55 isModal: true, 56 }); 57 58 async authWithPop(): Promise<void> { 59 HiLog.info(TAG, `authwithpop start`); 60 try { 61 await checkNetworkStatus(); 62 } catch { 63 let errInfo = GetAlertMessage.getAlertMessage( 64 { code: Constants.ERR_JS_APP_NETWORK_INVALID } as BusinessError); 65 this.title = ''; 66 this.message = errInfo.msg; 67 this.cancel = errInfo.cancel; 68 this.buttonOptions = [{ 69 value: this.cancel ? this.cancel : $r('app.string.da_button'), 70 background: $r('sys.color.ohos_id_color_button_normal'), action: () => { 71 abilityResult.resultCode = 0; 72 (getContext(this) as common.UIAbilityContext).terminateSelfWithResult(abilityResult); 73 }}]; 74 this.dialogControllerButton?.open(); 75 HiLog.info(TAG, `network failed`); 76 return; 77 }; 78 try { 79 account_osAccount.DomainAccountManager.authWithPopup({ 80 onResult: async (resultCode: number, authResult: account_osAccount.AuthResult) => { 81 sendDlpManagerAccountLogin(resultCode); 82 HiLog.info(TAG, `auth resultCode: ${resultCode}`); 83 HiLog.info(TAG, `auth authResult: ${JSON.stringify(authResult)}`); 84 } 85 }) 86 abilityResult.resultCode = 0; 87 (getContext(this) as common.UIAbilityContext).terminateSelfWithResult(abilityResult); 88 } catch (err) { 89 HiLog.info(TAG, `auth exception: ${JSON.stringify(err)}`); 90 } 91 } 92 93 async aboutToAppear() { 94 HiLog.info(TAG, `alert aboutToAppear start`); 95 try { 96 let messageCode = (this.alertWant?.parameters?.error as BusinessError).code; 97 let errorMsg = this.alertWant?.parameters?.error as BusinessError; 98 let accountType = this.alertWant?.parameters?.accountType as number; 99 let errInfo: Record<string, Resource> = {}; 100 if ([ 101 Constants.ERR_JS_APP_PARAM_ERROR, 102 Constants.ERR_JS_APP_OPEN_REJECTED, 103 Constants.ERR_JS_APP_ENCRYPTION_REJECTED, 104 Constants.ERR_JS_APP_ENCRYPTING, 105 Constants.ERR_JS_FILE_EXPIRATION, 106 Constants.ERR_JS_DLP_FILE_READ_ONLY, 107 Constants.ERR_JS_SYSTEM_NEED_TO_BE_UPGRADED, 108 ].includes(messageCode)) { 109 errInfo = GetAlertMessage.getAlertTitleMessage(errorMsg); 110 } else if ([Constants.ERR_JS_APP_SYSTEM_IS_AUTHENTICATED].includes(messageCode)) { 111 errInfo = GetAlertMessage.getAlertButtonMessage(errorMsg); 112 } else if ([Constants.ERR_JS_USER_NO_PERMISSION].includes(messageCode)) { 113 errInfo = accountType === dlpPermission.AccountType.CLOUD_ACCOUNT ? 114 GetAlertMessage.getAlertMessage(errorMsg) : GetAlertMessage.getAlertTitleMessage(errorMsg); 115 } else { 116 errInfo = GetAlertMessage.getAlertMessage(errorMsg); 117 } 118 this.title = errInfo.title; 119 this.message = errInfo.msg; 120 this.cancel = errInfo.cancel; 121 this.actionButton = errInfo.ok; 122 this.buttonOptions = [{ 123 value: this.cancel ? this.cancel : $r('app.string.da_button'), 124 background: $r('sys.color.ohos_id_color_button_normal'), action: () => { 125 abilityResult.resultCode = 0; 126 (getContext(this) as common.UIAbilityContext).terminateSelfWithResult(abilityResult); 127 }}]; 128 if (errInfo.ok) { 129 this.buttonOptions.push({ value: this.actionButton, background: $r('sys.color.ohos_id_color_text_primary_activated'), 130 fontColor: $r('sys.color.font_on_primary'), action: () => { 131 this.authWithPop(); 132 }}); 133 } 134 this.dialogControllerButton?.open(); 135 } catch (err) { 136 HiLog.error(TAG, `showErrorDialog failed: ${JSON.stringify(err)}`); 137 } 138 } 139 140 aboutToDisappear() { 141 this.dialogControllerButton = null; 142 } 143 144 build() { 145 } 146 147 @Builder 148 buildContent(): void { 149 Column() { 150 Text() { 151 Span(this.message) 152 } 153 } 154 .width(Constants.HEADER_TEXT_WIDTH) 155 .align(this.title ? Alignment.Start : Alignment.Center) 156 .margin({ 157 bottom: Constants.START_ABILITY_CUSTOM_CONTENT_MARGIN_BOTTOM 158 }) 159 } 160} 161