1/** 2 * Copyright (c) 2022 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 HiLog from "../utils/HiLog"; 17import common from "../data/commonData"; 18import telephoneUtil from "../utils/TelephoneUtil"; 19import LooseObject from "../data/LooseObject"; 20import ContactsModel from "../model/ContactsModel"; 21 22let mContactsModel = new ContactsModel(); 23const TAG = "ContractService"; 24 25export default { 26 27 /** 28 * Querying the contact list 29 * 30 * @param actionData 31 * @callback callback 32 */ 33 queryContact(actionData, callback) { 34 // Obtain rawContractIds and query contacts. 35 mContactsModel.queryContact(actionData, rawContractIds => { 36 let result: LooseObject = {}; 37 actionData.contractIds = rawContractIds; 38 // Based on rawContractIds 39 mContactsModel.queryContactDataByIds(actionData, contracts => { 40 result.code = common.int.SUCCESS; 41 this.convertContracts(contracts, messageList => { 42 result.response = messageList; 43 callback(result); 44 }); 45 }); 46 }); 47 }, 48 49 /** 50 * Counting the number of rows of contacts 51 * 52 * @param actionData 53 * @callback callback 54 */ 55 countContact(actionData, callback) { 56 // Obtain rawContractIds and query contacts. 57 mContactsModel.countContact(actionData, count => { 58 let result: LooseObject = {}; 59 result.code = common.int.SUCCESS; 60 result.response = count; 61 callback(result); 62 }); 63 }, 64 65 /** 66 * Convert contact parameters 67 * 68 * @param contracts 69 * @callback callback 70 */ 71 async convertContracts(contracts, callback) { 72 let messageList = []; 73 for (let contract of contracts) { 74 let item: LooseObject = {}; 75 item.contactName = contract.displayName; 76 item.headImage = "icon/user_avatar_full_fill.svg"; 77 item.telephone = contract.detailInfo; 78 item.telephoneFormat = contract.detailInfo; 79 item.select = false; 80 messageList.push(item); 81 } 82 callback(messageList); 83 }, 84 85 /** 86 * Process contact parameters 87 * 88 * @param contactObjects 89 */ 90 dealContractParams(contactObjects) { 91 if (contactObjects && contactObjects != common.string.EMPTY_STR) { 92 let params = []; 93 try { 94 params = JSON.parse(contactObjects); 95 } catch (Error) { 96 HiLog.i(TAG, `JSON.parse error ${Error}`); 97 return {}; 98 } 99 let contactsNumber: string = common.string.EMPTY_STR; 100 let contactsName: string = common.string.EMPTY_STR; 101 let contactsNumberFormat: string = common.string.EMPTY_STR; 102 let length: number = params.length; 103 for (let item of params) { 104 contactsNumber = contactsNumber + item.telephone + common.string.COMMA; 105 contactsNumberFormat = contactsNumberFormat + item.telephone + common.string.COMMA; 106 if (item.contactsName) { 107 contactsName += (item.contactsName + common.string.COMMA); 108 } else if (length > 1) { 109 contactsName += (item.telephone + common.string.COMMA); 110 } 111 } 112 let telephnoe = contactsNumber.substring(0, contactsNumber.length - 1); 113 contactsNumber = telephoneUtil.dealTelephoneSort(telephnoe); 114 let contractParams = { 115 strContactsNumber: contactsNumber, 116 strContactsNumberFormat: contactsNumber, 117 strContactsName: contactsName.substring(0, contactsName.length - 1), 118 contactsNum: length, 119 } 120 return contractParams; 121 } 122 }, 123 124 /** 125 * Query a contact by mobile number. 126 * 127 * @param actionData 128 * @callback callback 129 */ 130 queryContactDataByTelephone(actionData, callback) { 131 mContactsModel.queryContactDataByTelephone(actionData, contracts => { 132 HiLog.i(TAG, "queryContactDataByTelephone, success"); 133 callback(contracts); 134 }); 135 }, 136 137 /** 138 * Fuzzy search for contacts 139 * 140 * @param actionData 141 * @callback callback 142 */ 143 searchContracts(actionData, callback) { 144 let result: LooseObject = {}; 145 mContactsModel.searchContracts(actionData, res => { 146 result.code = res.code 147 if (res.code == common.int.SUCCESS) { 148 result.response = this.dealSearchContracts(res.abilityResult); 149 callback(result); 150 } 151 }); 152 }, 153 154 dealSearchContracts(contracts) { 155 let searchContracts = [] 156 for (let item of contracts) { 157 let searchContract: LooseObject = {}; 158 searchContract.headImage = "icon/user_avatar_full_fill.svg"; 159 searchContract.contactName = item.displayName; 160 searchContract.telephone = item.detailInfo; 161 searchContract.telephoneFormat = item.detailInfo; 162 searchContracts.push(searchContract); 163 } 164 return searchContracts; 165 }, 166 167 /** 168 * Check whether a business card exists. 169 * 170 * @param actionData 171 * @callback callback 172 */ 173 judgeIsExitProfile(actionData, callback) { 174 HiLog.i(TAG, "judgeIsExitProfile, start"); 175 mContactsModel.queryProfile(actionData, res => { 176 let result = false; 177 if (res > 0) { 178 result = true; 179 } 180 callback(result); 181 }); 182 } 183}