<apex:page controller="SearchContactController" id="page">
|
<apex:includeScript value="{!URLFOR($Resource.jquery183minjs)}" />
|
<apex:includeScript value="{! URLFOR($Resource.AWSService, 'AWSService.js') }" />
|
<apex:includeScript value="{!URLFOR($Resource.jquery183minjs)}" />
|
<apex:includeScript value="{!URLFOR($Resource.PleaseWaitDialog)}" />
|
<apex:stylesheet value="{!URLFOR($Resource.blockUIcss)}" />
|
|
<head>
|
</head>
|
<style>
|
/* 1. 定义表单样式 To Do Later*/
|
|
table {
|
border-collapse: collapse;
|
border-spacing: 0;
|
width: 98%;
|
margin-left: 5px;
|
}
|
</style>
|
|
<body id="body" class="lookupTab">
|
<script type="text/javascript">
|
var staticResources = JSON.parse('{!staticResource}');
|
var contactAWSIds = JSON.parse('{!contactAWSIds}');
|
var contactsInfo = JSON.parse('{!contactsInfo}');
|
var searchContactName = '';
|
function searchAWSContact(){
|
console.log('Search process!');
|
//1. reset table;
|
blockme();
|
resetTable();
|
//2. get contact name value
|
searchContactName = document.getElementById('page:form:lksrch').value;
|
//3. check searchContactName.length>=2
|
if(searchContactName.length>=2){
|
queryLeadFromAWS();
|
}else{
|
alert("请输入至少2个关键字");
|
unblockUI();
|
}
|
|
}
|
function resetTable(){
|
let queryResult = document.getElementById('QueryResult');
|
let table = document.getElementById('table');
|
if(table){
|
queryResult.removeChild(table);
|
}
|
}
|
function preparePayloadForSearchContact(){
|
let searchPayload = new Object();
|
searchPayload.dataIds = [];
|
searchPayload.contactName = searchContactName;
|
return JSON.stringify(searchPayload);
|
}
|
function queryLeadFromAWS() {
|
//1. Prepare the payload for contact search
|
let requestSearchPayload = preparePayloadForSearchContact();
|
console.log('request payload body:'+requestSearchPayload);
|
//2. Invoke AWS Service
|
fetch(staticResources.searchUrl, {
|
method: 'POST',
|
body: requestSearchPayload,
|
headers: {
|
'Content-Type': 'application/json',
|
'pi-token': staticResources.token
|
}
|
}).then((data) => {
|
return data.json();
|
}).then((result) => {
|
console.log('JSON Result from aws:'+JSON.stringify(result));
|
if(result.object&&result.object.length>0){
|
initContactTable(result);
|
}else{
|
unblockUI();
|
alert('没有查到该联系人')
|
}
|
})
|
}
|
|
function redirectToParentPage(obj) {
|
var winMain = window.opener;
|
if (null == winMain) {
|
winMain = window.parent.opener;
|
}
|
let value = obj.currentTarget.innerText;
|
let contactInfo = new Object();
|
contactInfo.Name = obj.currentTarget.innerText;
|
contactInfo.ContactId = obj.currentTarget.id;
|
window.open('\\'+obj.currentTarget.id,'_blank');
|
console.log('Contact Infor:'+JSON.stringify(contactInfo));
|
}
|
|
function refreshTable(cols,contactInfoList){
|
let myTableDiv = document.getElementById("QueryResult");
|
let table = document.createElement('TABLE');
|
table.border = '1';
|
table.id = 'table'
|
let tableBody = document.createElement('TBODY');
|
table.appendChild(tableBody);
|
let headerTR = document.createElement('TR');
|
let colsHeader = ['姓名','客户名称','邮箱', '电话','手机号'];
|
tableBody.appendChild(headerTR);
|
for (let i = 0; i < colsHeader.length; i++) {
|
let td = document.createElement('TH');
|
td.width = '75';
|
td.appendChild(document.createTextNode(colsHeader[i]));
|
headerTR.appendChild(td);
|
}
|
//3. Init the AWS data
|
for (let i = 0; i < contactInfoList.length; i++) {
|
let contactInfoTemp = contactInfoList[i]
|
if(contactsInfo[contactInfoTemp.AWSDataId] != null){
|
let tr = document.createElement('TR');
|
tableBody.appendChild(tr);
|
for (let j = 0; j < cols.length; j++) {
|
let td = document.createElement('TD');
|
td.width = '75';
|
if(j == 0){
|
td.id = contactsInfo[contactInfoTemp.AWSDataId].Id;
|
}
|
td.appendChild(document.createTextNode(contactInfoTemp[cols[j]]!=null?contactInfoTemp[cols[j]]:''));
|
if (cols[j] == 'Name') {
|
td.addEventListener("click", function (obj) {
|
redirectToParentPage(obj);
|
});
|
}
|
tr.appendChild(td);
|
}
|
}
|
}
|
myTableDiv.appendChild(table);
|
unblockUI();
|
}
|
function initContactTable(data) {
|
let cols = ['Name','AccountName','Email', 'Phone','MobilePhone'];
|
let contactInfoList = [];
|
let awsDataIds = [];
|
for(var 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));
|
//Invoke SF BackEnd
|
Visualforce.remoting.Manager.invokeAction(
|
'{!$RemoteAction.SearchContactController.searchContacts}',
|
JSON.stringify(awsDataIds),'','',
|
function (result, event) {
|
if(event.status){
|
if(result.status == 'success'){
|
contactsInfo = JSON.parse(result.message.replace(/("\;)/g,"\""));
|
console.log('Contact Info from SF:'+JSON.stringify(contactsInfo));
|
if(Object.keys(contactsInfo).length>0){
|
for(let i=0;i<contactInfoList.length;i++){
|
let contactFromSF = contactsInfo[contactInfoList[i]['AWSDataId']];
|
if(contactFromSF){
|
contactInfoList[i].sfRecordId = contactFromSF['Id'];
|
contactInfoList[i].AccountName = contactFromSF['Account']['Name'];
|
}
|
}
|
}
|
|
refreshTable(cols,contactInfoList);
|
}else{
|
alert('没查询到该联系人');
|
console.log('No result');
|
refreshTable(cols,[]);
|
}
|
}
|
},
|
{ escape: true }
|
);
|
}
|
</script>
|
<apex:form id="form">
|
<!-- Search Filter-->
|
<div class="lookup">
|
<div class="bPageTitle">
|
<div class="ptBody secondaryPalette">
|
<div class="content">
|
<img src="/img/s.gif" alt="" class="pageTitleIcon" title="" />
|
<h1>联系人搜索</h1>
|
</div>
|
</div>
|
</div>
|
<div class="pBody">
|
<label class="assistiveText" for="lksrch">Search</label>
|
<apex:inputText id="lksrch" html-placeholder="{!PIPL_Search_Contact_Label}" value="{!searchKeyWord}" />
|
<input value=" Go! " type="Button" onclick="searchAWSContact()" styleClass="btn" />
|
<div class="bDescription">请输入联系人的姓名进行搜索。
|
</div>
|
</div>
|
</div>
|
<div id="QueryResult">
|
</div>
|
</apex:form>
|
</body>
|
</apex:page>
|