import { LightningElement, track,api } from 'lwc';
|
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
|
import Init from '@salesforce/apex/LexSearchContactPIPLController.Init';
|
import searchContacts from '@salesforce/apex/LexSearchContactPIPLController.searchContacts';
|
import { AWSService } from 'c/piUtils';
|
|
const cols = [
|
//update by Link 2023-10-16
|
{label:'姓名', fieldName:'nameUrl', type:'url',typeAttributes:{label: { fieldName: 'Name' },target:'_blank'},hideDefaultActions: true,wrapText: true} ,
|
{label:'客户名称', fieldName:'AccountName',hideDefaultActions: true,wrapText: true, } ,
|
{label:'邮箱', fieldName:'Email',hideDefaultActions: true,wrapText: true, },
|
{label:'电话', fieldName:'Phone',hideDefaultActions: true,wrapText: true,},
|
{label:'手机号', fieldName:'MobilePhone',hideDefaultActions: true,wrapText: true,}
|
|
]
|
export default class LexSearchAWSContactByNamePIPL extends LightningElement {
|
@track searchKey;
|
@track contactsInfo;
|
@track contactAWSIds;
|
@track staticResources;
|
@track Contacts;
|
@track showSpinner;
|
cols = cols;
|
AWSService;
|
|
|
connectedCallback() {
|
this.AWSService = new AWSService();
|
console.log('进入 search');
|
Init({
|
accountId:'',
|
searchKey:''
|
}).then(result=>{
|
console.log('r==>'+JSON.stringify(result));
|
if (result.status == 'success') {
|
this.contactsInfo = result.contactsInfo;
|
this.contactAWSIds = result.contactAWSIds;
|
this.staticResources = JSON.parse(result.staticResource);
|
}
|
}).catch((error) => {
|
console.log('error = ' + JSON.stringify(error));
|
});
|
}
|
|
//dataChange
|
handelSearchKey(event){
|
this.searchKey = event.target.value;
|
console.log('this.searchKey'+this.searchKey);
|
}
|
SearchContact(){
|
console.log('进入搜索');
|
this.showSpinner = true;
|
if(this.searchKey.length<2){
|
this.showToast('请输入至少两个关键字','','error');
|
this.Contacts = [];
|
this.showSpinner = false;
|
return;
|
}
|
//1. Prepare the payload for contact search
|
let requestSearchPayload = this.preparePayloadForSearchContact();
|
console.log('request payload body:'+requestSearchPayload);
|
//2. Invoke AWS Service
|
fetch(this.staticResources.searchUrl,{
|
method: 'POST',
|
body: requestSearchPayload,
|
headers: {
|
'Content-Type': 'application/json',
|
'pi-token': this.staticResources.token
|
}
|
}).then((data) => {
|
console.log('data==>'+JSON.stringify(data));
|
return data.json();
|
|
}).then((result) => {
|
console.log('JSON Result from aws:'+JSON.stringify(result));
|
if(result.object&&result.object.length>0){
|
console.log('result'+result);
|
this.initContactTable(result);
|
}else{
|
alert('没有查到该联系人');
|
this.showSpinner = false;
|
}
|
})
|
}
|
preparePayloadForSearchContact(){
|
let searchPayload = new Object();
|
searchPayload.dataIds = [];
|
searchPayload.contactName = this.searchKey;
|
return JSON.stringify(searchPayload);
|
}
|
|
initContactTable(data){
|
let contactInfoList = [];
|
let awsDataIds = [];
|
for(let i=0;i<data.object.length;i++){
|
if(data.object[i].dataId){
|
let contactInfo = new Object();
|
contactInfo.Name = data.object[i].lastName;
|
contactInfo.Email = data.object[i].email;
|
contactInfo.MobilePhone = data.object[i].mobilePhone;
|
contactInfo.Phone = data.object[i].phone;
|
contactInfo.AWSDataId = data.object[i].dataId;
|
awsDataIds.push(contactInfo.AWSDataId);
|
contactInfo.sfRecordId = '';
|
contactInfoList.push(contactInfo);
|
}
|
}
|
let AWSIdToSFIdMapValue = {};
|
console.log('Contact Info from AWS:'+JSON.stringify(contactInfoList));
|
searchContacts({
|
awsContactIds:JSON.stringify(awsDataIds),
|
searchContactName:'',
|
accountId:'',
|
|
}).then(result=>{
|
console.log('r==>'+JSON.stringify(result));
|
if(result.status == 'success'){
|
this.contactsInfo = JSON.parse(result.message.replace(/("\;)/g,"\""));
|
console.log('Contact Info from SF:'+JSON.stringify(this.contactsInfo));
|
if(Object.keys(this.contactsInfo).length>0){
|
for(let i=0;i<contactInfoList.length;i++){
|
let contactFromSF = this.contactsInfo[contactInfoList[i]['AWSDataId']];
|
if(contactFromSF){
|
contactInfoList[i].sfRecordId = contactFromSF['Id'];
|
contactInfoList[i].AccountName = contactFromSF['Account']['Name'];
|
//add by Link 2023-10-16
|
contactInfoList[i]['nameUrl'] = '/lightning/r/Contact/'+contactFromSF['Id']+'/view';
|
}
|
}
|
}
|
let newConList = [];
|
for(var i = 0; i < contactInfoList.length; i++){
|
if(contactInfoList[i].sfRecordId != ''){
|
newConList.push(contactInfoList[i]);
|
}
|
}
|
contactInfoList = newConList;
|
console.log('contactInfoList = '+JSON.stringify(contactInfoList));
|
this.Contacts = contactInfoList;
|
this.showSpinner = false;
|
}else{
|
alert('没查询到该联系人');
|
console.log('No result');
|
this.Contacts = [];
|
this.showSpinner = false;
|
// refreshTable(cols,[]);
|
}
|
}).catch((error) => {
|
console.log('error = ' + JSON.stringify(error));
|
this.showSpinner = false;
|
});
|
}
|
showToast(title,Message,variant) {
|
const event = new ShowToastEvent({
|
title: title,
|
message: Message,
|
variant: variant,
|
mode: 'dismissable'
|
});
|
this.dispatchEvent(event);
|
}
|
|
|
|
}
|