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