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 16 17import osAccount from '@ohos.account.osAccount'; 18import { BusinessError } from '@ohos.base'; 19import account_osAccount from '@ohos.account.osAccount'; 20import staffItem from './staff'; 21import Constants from '../constant'; 22import { isPC } from '../utils'; 23import GlobalContext from '../GlobalContext'; 24 25 26interface Staff { 27 authAccount: string; 28} 29 30const TAG = "[DLPManager_AddStaff]"; 31@Extend(Text) function inputMessageText() { 32 .fontSize($r('sys.float.ohos_fa_text_size_body')) 33 .lineHeight(Constants.PP_TEXT_LINE_HEIGHT2) 34 .fontColor($r('sys.color.ohos_id_color_handup')) 35 .fontWeight(FontWeight.Medium) 36 .margin({ top: Constants.ENCRYPTION_ADD_STAFF_BORDER_MARGIN_TOP }) 37 .textAlign(TextAlign.Start) 38} 39@Component 40struct staffInput { 41 @State inputId: string = '' 42 @State isAccountCheckSuccess: boolean = true; 43 @State staffArrayLength: boolean = false; 44 @State isInputInvalid: boolean = false; 45 @State isNetworkInvalid: boolean = false; 46 @State activeStaffIndex: number = -1; 47 @State domainTips: string = ''; 48 @State accountNameTips: string = ''; 49 @State handlePopupTips: boolean = false; 50 @Link staffArray: Staff[]; 51 52 @Prop isDisable: boolean = false; 53 @Builder MenuBuilder(index: number) { 54 Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) { 55 Text($r('app.string.delete_account')) 56 .fontSize($r('sys.float.ohos_id_text_size_sub_title1')) 57 .textAlign(TextAlign.Center) 58 .width(Constants.ENCRYPTION_ADD_STAFF_MENU_WIDTH) 59 .height(Constants.ENCRYPTION_ADD_STAFF_MENU_HEIGHT) 60 .onClick(() => { 61 this.removeItem(index) 62 }) 63 }.width(Constants.ENCRYPTION_ADD_STAFF_MENU_WIDTH) 64 } 65 66 @Builder popupBuilderTips() { 67 Column() { 68 Row() { 69 Text($r('app.string.encrypt_employee_id')) 70 .fontFamily('HarmonyHeiTi') 71 .fontSize($r('sys.float.ohos_id_text_size_body2')) 72 .fontColor($r('sys.color.ohos_id_color_primary_dark')) 73 Text(' : ') 74 .fontFamily('HarmonyHeiTi') 75 .fontSize($r('sys.float.ohos_id_text_size_body2')) 76 .fontColor($r('sys.color.ohos_id_color_primary_dark')) 77 Text(this.accountNameTips) 78 .fontFamily('HarmonyHeiTi') 79 .fontSize($r('sys.float.ohos_id_text_size_body2')) 80 .fontColor($r('sys.color.ohos_id_color_primary_dark')) 81 } 82 .width(Constants.HEADER_COLUMN_MESSAGE_TIPS) 83 if (GlobalContext.load('domainAccount')) { 84 Row() { 85 Text($r('app.string.encrypt_domain')) 86 .fontFamily('HarmonyHeiTi') 87 .fontSize($r('sys.float.ohos_id_text_size_body2')) 88 .fontColor($r('sys.color.ohos_id_color_primary_dark')) 89 Text(' : ') 90 .fontFamily('HarmonyHeiTi') 91 .fontSize($r('sys.float.ohos_id_text_size_body2')) 92 .fontColor($r('sys.color.ohos_id_color_primary_dark')) 93 Text(this.domainTips) 94 .fontFamily('HarmonyHeiTi') 95 .fontSize($r('sys.float.ohos_id_text_size_body2')) 96 .fontColor($r('sys.color.ohos_id_color_primary_dark')) 97 } 98 .width(Constants.HEADER_COLUMN_MESSAGE_TIPS) 99 } 100 } 101 .padding({ 102 left: Constants.ROW_FONT_SIZE, 103 right: Constants.ROW_FONT_SIZE, 104 top: Constants.DA_MARGIN_TOP, 105 bottom: Constants.DA_MARGIN_TOP 106 }) 107 } 108 109 removeItem(i: number) { 110 this.staffArray.splice(i, 1) 111 this.activeStaffIndex = -1 112 this.staffArrayLength = false; 113 } 114 115 build() { 116 Column() { 117 Flex({ 118 direction: FlexDirection.Row, 119 wrap: FlexWrap.Wrap, 120 }) { 121 ForEach( 122 this.staffArray, 123 (item: Staff, index) => { 124 Stack() { 125 staffItem({ 126 authAccount: item.authAccount, 127 isActive: this.activeStaffIndex === index, 128 changeIndex: Number(index), 129 }) 130 Text(item.authAccount) 131 .opacity(Constants.PP_ROW_RADIUS) 132 .focusable(true) 133 .focusOnTouch(true) 134 .borderRadius(Constants.ENCRYPTION_SUCCESS_CHANGE_TOP) 135 .onKeyEvent((event?: KeyEvent) => { 136 if (event?.keyText === 'KEYCODE_DEL' && 137 event.keyCode === 2055 && this.activeStaffIndex === index) { 138 this.removeItem(index) 139 } 140 if (event?.keyText === 'KEYCODE_FORWARD_DEL' && event.keyCode === 2071 && this.activeStaffIndex === index) { 141 this.removeItem(index) 142 } 143 }) 144 .onClick(() => { 145 this.activeStaffIndex = index!; 146 let domainAccountInfo: account_osAccount.GetDomainAccountInfoOptions = { 147 domain: 'china', 148 accountName: item.authAccount.toLocaleLowerCase() 149 } 150 if (GlobalContext.load('domainAccount')) { 151 try { 152 account_osAccount.DomainAccountManager.getAccountInfo(domainAccountInfo) 153 .then((result: account_osAccount.DomainAccountInfo) => { 154 this.domainTips = result.domain; 155 this.accountNameTips = result.accountName; 156 }).catch((err: BusinessError) => { 157 console.log(TAG, 'call getAccountInfo failed, error: ' + JSON.stringify(err)); 158 }); 159 } catch (err) { 160 console.log(TAG, 'getAccountInfo exception = ' + JSON.stringify(err)); 161 } 162 } else { 163 this.accountNameTips = item.authAccount; 164 } 165 }) 166 .padding( 167 { 168 top: Constants.FOOTER_ROW_PAD_LEFT, 169 bottom: Constants.FOOTER_ROW_PAD_RIGHT, 170 } 171 ) 172 .margin({ 173 right: Constants.ENCRYPTION_ADD_STAFF_MARGIN_RIGHT, 174 bottom: Constants.ENCRYPTION_ADD_STAFF_MARGIN_BOTTOM 175 }) 176 .bindPopup(this.activeStaffIndex === index, { 177 builder: this.popupBuilderTips, 178 placement: Placement.Bottom, 179 popupColor: ($r('sys.color.ohos_id_color_tooltip_background_dark')), 180 enableArrow: true, 181 showInSubWindow: false, 182 onStateChange: (e) => { 183 if (!e.isVisible) { 184 this.activeStaffIndex = -1; 185 } 186 } 187 }) 188 .bindContextMenu(this.MenuBuilder(index!), isPC() ? ResponseType.RightClick : ResponseType.LongPress) 189 .width(Constants.ENCRYPTION_ADD_STAFF_WIDTH) 190 .height(Constants.ENCRYPTION_ADD_STAFF_HEIGHT) 191 } 192 }, 193 (item: Staff) => item.authAccount 194 ) 195 TextInput({ 196 text: this.inputId, 197 placeholder: !this.staffArray.length ? 198 ((GlobalContext.load('domainAccount') as boolean) 199 ? $r('app.string.enter_a_complete_work_ID') 200 : $r('app.string.enter_a_complete_account')) : undefined, 201 }) 202 .flexGrow(Constants.ENCRYPTION_ADD_STAFF_FLEX_GROW) 203 .backgroundColor($r('app.color.da_button_color')) 204 .borderRadius(Constants.PP_ROW_RADIUS) 205 .fontSize($r('sys.float.ohos_id_text_size_body1')) 206 .fontColor(this.isInputInvalid ? $r('sys.color.ohos_id_color_handup') : $r('sys.color.ohos_id_color_text_primary')) 207 .padding({ 208 top: Constants.PP_BUTTON_PAD, 209 bottom: Constants.PP_BUTTON_PAD, 210 left: Constants.PP_TEXT_PAD_RIGHT, 211 right: Constants.PP_BUTTON_PAD 212 }) 213 .width( 214 this.staffArray.length ? Constants.ENCRYPTION_ADD_STAFF_WIDTH : Constants.FOOTER_ROW_WIDTH 215 ) 216 .height(Constants.ENCRYPTION_ADD_STAFF_HEIGHT) 217 .onChange((value) => { 218 this.inputId = value 219 if (this.isInputInvalid || this.isNetworkInvalid) { 220 this.isInputInvalid = false; 221 this.isNetworkInvalid = false; 222 } 223 }) 224 .onSubmit(() => { 225 if (this.staffArray.length >= Constants.ENCRYPTION_ADD_STAFF_LENGTH_MAX) { 226 this.inputId = ''; 227 this.staffArrayLength = true; 228 return; 229 } 230 if (!this.isAccountCheckSuccess) return; 231 if (!this.inputId) { 232 return 233 } 234 try { 235 let domainAccountInfo: osAccount.DomainAccountInfo = { 236 domain: 'china', 237 accountName: this.inputId.toLocaleLowerCase() 238 } 239 if (GlobalContext.load('domainAccount')) { 240 this.isAccountCheckSuccess = false; 241 account_osAccount.DomainAccountManager.hasAccount(domainAccountInfo, (err, isExist) => { 242 this.isAccountCheckSuccess = true; 243 if (isExist) { 244 let o1: Staff = { 245 authAccount: this.inputId.toLocaleLowerCase(), 246 } 247 this.staffArray.push(o1) 248 this.inputId = ''; 249 } else { 250 if ([Constants.ERR_JS_INVALID_PARAMETER, Constants.ERR_JS_ACCOUNT_NOT_FOUND].includes(err.code)){ 251 this.isInputInvalid = true; 252 return; 253 } else { 254 this.isNetworkInvalid = true; 255 return; 256 } 257 } 258 }) 259 } else { 260 this.isAccountCheckSuccess = true; 261 let o2: Staff = { 262 authAccount: this.inputId, 263 } 264 this.staffArray.push(o2) 265 this.inputId = '' 266 } 267 } 268 catch (e) { 269 this.isAccountCheckSuccess = true; 270 console.log(e.code); 271 } 272 }) 273 .onFocus(() => { 274 this.activeStaffIndex = -1; 275 }) 276 } 277 .padding({ 278 top: Constants.ENCRYPTION_ADD_STAFF_PADDING_TOP, 279 }) 280 .border({ 281 width: { bottom: Constants.ENCRYPTION_ADD_STAFF_BORDER }, 282 color: (this.isInputInvalid || this.staffArrayLength || this.isNetworkInvalid ) 283 ? $r('sys.color.ohos_id_color_handup') : $r('sys.color.ohos_id_color_text_secondary'), 284 }) 285 286 Flex({ direction: FlexDirection.Row }) { 287 if (this.isInputInvalid) { 288 Text((GlobalContext 289 .load('domainAccount') as boolean) ? $r('app.string.incorrect_work_ID') : $r('app.string.incorrect_account')) 290 .inputMessageText() 291 } 292 if (this.isNetworkInvalid) { 293 Text((GlobalContext 294 .load('domainAccount') as boolean) ? $r('app.string.network_invalid') : $r('app.string.incorrect_account')) 295 .inputMessageText() 296 } 297 Blank() 298 if (this.staffArray.length >= Constants.ENCRYPTION_ADD_STAFF_LENGTH_MAX * Constants.ENCRYPTION_ADD_STAFF_LENGTH) { 299 Text(`${this.staffArray.length}/${Constants.ENCRYPTION_ADD_STAFF_LENGTH_MAX}`) 300 .fontSize($r('sys.float.ohos_fa_text_size_body')) 301 .lineHeight(Constants.PP_TEXT_LINE_HEIGHT2) 302 .fontColor(this.staffArrayLength ? $r('sys.color.ohos_id_color_handup') : $r('sys.color.ohos_id_color_text_secondary')) 303 .fontWeight(FontWeight.Medium) 304 .margin({ top: Constants.ENCRYPTION_ADD_STAFF_BORDER_MARGIN_TOP }) 305 .textAlign(TextAlign.End) 306 } 307 } 308 } 309 .opacity(this.isDisable ? Constants.DU_LINE_WIDTH : Constants.FOOTER_OPACITY_ONE) 310 .enabled(this.isDisable ? false : true) 311 } 312} 313 314export default staffInput; 315