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 account_osAccount from '@ohos.account.osAccount'; 17import common from '@ohos.app.ability.common'; 18import { BusinessError } from '@ohos.base'; 19import Constants from '../constant'; 20import { AccountTips, AccountTipsConfig } from '../AccountTipsConfig'; 21import { GetAlertMessage } from '../GetAlertMessage'; 22import { DlpAlertDialog } from '../components/dlp_alert_dialog'; 23import { HiLog } from '../HiLog'; 24 25const TAG = 'staff'; 26 27@Extend(Text) 28function tipsTextStyle() { 29 .fontSize($r('sys.float.ohos_id_text_size_body2')) 30 .fontColor($r('sys.color.ohos_id_color_text_primary')) 31 .textOverflow({ 32 overflow: TextOverflow.Ellipsis 33 }) 34 .maxLines(Constants.PP_TEXT_MAX_LINE) 35} 36 37@Component 38struct staffItem { 39 @Prop authAccount: string = ''; 40 @Prop isActive: boolean = false; 41 @State color: Resource[] = [ 42 $r('sys.color.multi_color_01'), 43 $r('sys.color.multi_color_02'), 44 $r('sys.color.multi_color_03'), 45 $r('sys.color.multi_color_04'), 46 $r('sys.color.multi_color_05'), 47 $r('sys.color.multi_color_06'), 48 $r('sys.color.multi_color_07'), 49 $r('sys.color.multi_color_08'), 50 $r('sys.color.multi_color_09'), 51 $r('sys.color.multi_color_10'), 52 $r('sys.color.multi_color_11'), 53 ]; 54 @Prop changeIndex: number = 0; 55 @State domainTips: string = ''; 56 @State accountNameTips: string = ''; 57 @State handlePopupTips: boolean = false; 58 59 @Prop textContent: string = ''; 60 @State accountTipsArray: AccountTips[] = []; 61 @State isHover: boolean = false; 62 dlpAlertDialog?: CustomDialogController; 63 64 @Builder popupBuilderTips() { 65 Column() { 66 ForEach(this.accountTipsArray, (item: AccountTips) => { 67 if (item.isShow) { 68 Row() { 69 if (AccountTipsConfig.isSysLanguage()) { 70 Text(item.description?.replace('%s', item.value)).tipsTextStyle() 71 } else { 72 Text(item.descriptionEn?.replace('%s', item.value)).tipsTextStyle() 73 } 74 }.height(Constants.TIPS_HEIGHT_ITEM) 75 } 76 }, (item: AccountTips) => item.key) 77 } 78 .alignItems(HorizontalAlign.Start) 79 .padding({ 80 left: Constants.ROW_FONT_SIZE, 81 right: Constants.ROW_FONT_SIZE, 82 top: Constants.DA_MARGIN_TOP, 83 bottom: Constants.DA_MARGIN_TOP 84 }) 85 } 86 87 private rollColor() { 88 while (this.changeIndex > this.color.length - 1) { 89 this.changeIndex = this.changeIndex - this.color.length 90 } 91 return this.color[this.changeIndex] 92 } 93 94 private showErrorDialog(title: Resource, message: Resource) { 95 this.dlpAlertDialog = new CustomDialogController({ 96 builder: DlpAlertDialog({ 97 title: title, 98 message: message, 99 action: () => { 100 } 101 }), 102 autoCancel: false, 103 customStyle: true, 104 }); 105 this.dlpAlertDialog.open(); 106 } 107 108 private splitContent() { 109 if (this.textContent) { 110 if (this.textContent.length > Constants.TIPS_NAME_LENGTH) { 111 return this.textContent.split('').join(Constants.TIPS_SPACE_ZERO) 112 } else if (this.textContent.length === Constants.TIPS_NAME_LENGTH) { 113 return this.textContent.split('').join(Constants.TIPS_SPACE_EM) 114 } else { 115 return Constants.TIPS_SPACE_EN + this.textContent; 116 } 117 } 118 return this.textContent; 119 } 120 121 private getFontWeight() { 122 try { 123 return (getContext(this) as common.UIAbilityContext).resourceManager.getNumber( 124 $r('sys.float.font_weight_semibold')); 125 } catch (error) { 126 HiLog.error(TAG, `getFontWeight callback getNumber failed: ${JSON.stringify(error)}.`); 127 return FontWeight.Regular; 128 } 129 } 130 131 async aboutToAppear() {} 132 133 build() { 134 Row() { 135 Stack() { 136 Circle({ width: Constants.HEADER_ROW_FONT_SIZE, height: Constants.HEADER_ROW_FONT_SIZE }) 137 .fill(this.rollColor()) 138 .opacity(Constants.FOOTER_OPACITY_MID); 139 Text(this.authAccount?.[0]?.toLocaleUpperCase()) 140 .fontSize(Constants.STAFF_FONT_SIZE) 141 .fontWeight(this.getFontWeight()) 142 .width(Constants.HEADER_ROW_FONT_SIZE) 143 .height(Constants.HEADER_ROW_FONT_SIZE) 144 .borderRadius(Constants.ENCRYPTION_STAFF_BORDER_RADIUS) 145 .fontColor($r('sys.color.ohos_fa_text_contrary')) 146 .textAlign(TextAlign.Center) 147 .opacity(Constants.FOOTER_OPACITY_MID) 148 .padding({ bottom: Constants.STAFF_FONT_PADDING }); 149 } 150 .margin({ 151 right: Constants.ADD_STAFF_ITEM_MARGIN_RIGHT, 152 }); 153 Text(this.splitContent()) 154 .textOverflow({ 155 overflow: TextOverflow.Ellipsis 156 }) 157 .maxLines(Constants.PP_TEXT_MAX_LINE) 158 .fontSize($r('sys.float.ohos_id_text_size_button3')) 159 .height(Constants.HEADER_ROW_FONT_SIZE); 160 } 161 .onClick(() => { 162 try { 163 AccountTipsConfig.getAccountInfo(this.authAccount) 164 .then(async (result: account_osAccount.DomainAccountInfo) => { 165 this.accountTipsArray = await AccountTipsConfig.getConfigTips(); 166 this.handlePopupTips = !this.handlePopupTips; 167 this.accountTipsArray?.forEach((item: AccountTips) => { 168 let value: string = result[item.key]; 169 item.value = value; 170 }) 171 }) 172 .catch((err: BusinessError) => { 173 HiLog.error(TAG, `getAccountInfo error: ${JSON.stringify(err)}`); 174 let errInfo = GetAlertMessage.getAlertMessage(err); 175 this.showErrorDialog(errInfo.title, errInfo.msg); 176 }) 177 } catch (err) { 178 HiLog.error(TAG, `getAccountInfo exception: ${JSON.stringify(err)}`); 179 } 180 }) 181 .hitTestBehavior(HitTestMode.Block) 182 .bindPopup(this.handlePopupTips, { 183 builder: this.popupBuilderTips, 184 placement: Placement.BottomLeft, 185 enableArrow: true, 186 showInSubWindow: false, 187 onStateChange: (e) => { 188 if (!e.isVisible) { 189 this.handlePopupTips = false; 190 } 191 } 192 }) 193 .height(Constants.ENCRYPTION_ADD_STAFF_HEIGHT) 194 .alignItems(VerticalAlign.Center) 195 .backgroundColor(this.isActive && this.handlePopupTips ? '#1A0A59F7' : $r('sys.color.ohos_id_color_button_normal')) 196 .borderRadius(Constants.ENCRYPTION_SUCCESS_CHANGE_TOP) 197 .padding( 198 { 199 top: Constants.ENCRYPTION_STAFF_PAD, 200 bottom: Constants.ENCRYPTION_STAFF_PAD, 201 left: Constants.ENCRYPTION_STAFF_ITEM_MARGIN, 202 right: Constants.ENCRYPTION_STAFF_ITEM_MARGIN 203 } 204 ) 205 .margin({ 206 right: Constants.ENCRYPTION_ADD_STAFF_MARGIN_RIGHT, 207 top: Constants.ENCRYPTION_ADD_STAFF_MARGIN_BOTTOM 208 }) 209 } 210} 211 212export { staffItem }; 213