1/* 2 * Copyright (c) 2024 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 */ 15import DomainAccountInfo from '../bean/data/DomainAccountInfo'; 16import DomainAccountResponse from '../bean/response/DomainAccountResponse'; 17import CredConnection from '../common/connection/CredConnection'; 18import AppStorageConstant from '../common/constant/AppStorageConstant'; 19import { HiLog } from '../common/HiLog'; 20import { getOsAccountInfo } from '../common/utils'; 21import DomainAccountConvertor from '../convertor/DomainAccountConvertor'; 22import account_osAccount from '@ohos.account.osAccount'; 23import CommonUtil from '../common/CommonUtil'; 24 25const TAG = 'AccountManager'; 26 27export default class AccountManager { 28 29 public static connectAbility(context: ESObject): void { 30 let connection: CredConnection | undefined = AppStorage.get<CredConnection>(AppStorageConstant.CRED_CONNECTION); 31 if (!connection) { 32 connection = new CredConnection(context); 33 } 34 if (!connection.getRemoteProxy()) { 35 connection.connectDomainAccountQueryAbility(); 36 } 37 AppStorage.setOrCreate(AppStorageConstant.CRED_CONNECTION, connection); 38 } 39 40 public static async getDomainAccountByAccountName(search: string): Promise<DomainAccountInfo> { 41 let domainAccountMap: Map<string, DomainAccountInfo> | undefined = 42 AppStorage.get(AppStorageConstant.DOMAIN_ACCOUNT_MAP); 43 let domainAccount: DomainAccountInfo | undefined = domainAccountMap?.get(search); 44 if (domainAccount) { 45 return domainAccount; 46 } 47 let domainResponse: DomainAccountResponse | undefined = 48 await AccountManager.getDomainAccountByAccountNames([search]); 49 if (domainResponse && domainResponse.getData() && domainResponse.getData().length > 0) { 50 return domainResponse.getData()[0]; 51 } 52 return new DomainAccountInfo(); 53 } 54 55 public static async getDomainAccountByAccountNames(searchArray: string[]): 56 Promise<DomainAccountResponse | undefined> { 57 let accountInfo = await AccountManager.getAccountInfo(); 58 if (!accountInfo) { 59 return undefined; 60 } 61 let searchReq = DomainAccountConvertor.convertBatchToDomainAccountReq( 62 searchArray, accountInfo.domainInfo?.accountName, accountInfo.domainInfo?.accountId); 63 let connection: CredConnection | undefined = AppStorage.get<CredConnection>(AppStorageConstant.CRED_CONNECTION); 64 let response: DomainAccountResponse | undefined = await connection?.getDomainAccountInfo(searchReq); 65 AccountManager.dealDomainAccountMapCache(response?.getData()); 66 return response; 67 } 68 69 public static async getDomainAccountWithRetry(searchArray: string[]): Promise<DomainAccountResponse | undefined> { 70 let result = await AccountManager.getDomainAccountByAccountNames(searchArray); 71 if (!result || CommonUtil.isEmptyArray(result.getData())) { 72 result = await AccountManager.getDomainAccountByAccountNames(searchArray); 73 } 74 return result; 75 } 76 77 public static async getAccountInfo(): Promise<account_osAccount.OsAccountInfo | null> { 78 try { 79 return await getOsAccountInfo(); 80 } catch (error) { 81 HiLog.error(TAG, `getAccountInfo error: ${error}`); 82 } 83 return null; 84 } 85 86 private static dealDomainAccountMapCache(dataArray: Array<DomainAccountInfo> | undefined) { 87 if (!dataArray) { 88 return; 89 } 90 let domainAccountMap: Map<string, DomainAccountInfo> | undefined = 91 AppStorage.get(AppStorageConstant.DOMAIN_ACCOUNT_MAP); 92 if (!domainAccountMap) { 93 domainAccountMap = new Map<string, DomainAccountInfo>(); 94 } 95 dataArray.forEach(data =>{ 96 domainAccountMap?.set(data.accountName, data); 97 }); 98 AppStorage.setOrCreate(AppStorageConstant.DOMAIN_ACCOUNT_MAP, domainAccountMap); 99 } 100}