这个有助于增加性能,把前端的放后端执行
后端:
public stringBatchQueryPage()
{
Stream reqStream =Request.InputStream;
byte[] buffer = new byte[(int)reqStream.Length];
reqStream.Read(buffer, 0, (int) reqStream.Length);
string str =System.Text.Encoding.ASCII.GetString(buffer);
JArray obj = (JArray) JsonConvert.DeserializeObject(str);
Dictionary<int, IDictionary<string, string>> request= new Dictionary<int, IDictionary<string, string>>();
foreach (JObject items in obj)
{
var index =obj.IndexOf(items);
request.Add(index, new Dictionary<string, string>());
foreach (var item in items)
{
request[index].Add(item.Key,item.Value.ToString());
}
}
string[] result = new string[request.Count];
for (int index = 0;index < request.Count; index++)
{
result[index]=ObjectContainer.Instance.GetObject<IJsonSerializer>()
.ToJson(ObjectContainer.Instance.GetObject<IQuickQueryWebService>().Query(request[index]));
}
var res="";
for (int index = 0;index < result.Count(); index++)
{
if (index == 0)
res = "{";
res =res+ "\""+ request[index]["queryId"] + "\":[";
res = res + result[index] + "]";
if (index ==result.Count() - 1)
res = res + "}";
else
{
res = res + ",";
}
}
return res;
}
前端:
functionBatchSerchSoufunAgent(pageIndex) {
var parameters = [
{
queryId: "SearchWeChatOrgUsers",
quickQueryResource: "WeChatOrgQuickQuery",
dataSource: "dataSource",
pageable:true,
pageIndex: pageIndex,
pageSize: 10,
order: "Levels-desc"
},
{
queryId: "SearchWeChatOrgUsers",
quickQueryResource: "WeChatOrgQuickQuery",
dataSource: "dataSource",
pageable: true,
pageIndex: pageIndex,
pageSize: 10,
order: "Levels-desc"
}
];
$.ajax({
type: "post",
url: "@Url.Content("~/BatchQueryPage")",
dataType: "json",
global: false,
data: JSON.stringify(parameters),
async: false,
beforeSend: function(xml) {},
success: function (data) {
if (data.SearchWeChatOrgUsers[0].Code< 0) {
alert(data.SearchWeChatOrgUsers[0].Message);
return;
}
if (data.SearchWeChatOrgUsers[0].Code== 1) {
alert("未找到相关的数据!");
return;
}
data.list = data.SearchWeChatOrgUsers[0].Data;
var html =template("SearchSouFunAgentTemplate", data);
$("#SearchSouFunAgentDiv").html(html);
$("#SearchSouFunAgentPageDiv").twbsPagination({
totalPages: data.SearchWeChatOrgUsers[0].PageInfo.PageCount,
visiblePages: 10,
startPage: pageIndex,
first: '首页',
prev: '上一页',
next: '下一页',
last: '尾页',
onPageClick: function(event, page) {
SerchSoufunAgent(page);
}
});
},
error: function(event, XMLHttpRequest, ajaxOptions, thrownError) {
alert(XMLHttpRequest);
},
complete: function(xml, ts) { }
});
}
其中头部是用byte数组并且获得Request.InputStream的原始流在用ASCII编码拿到后转换成string,接着用newtonsoft转换成JArray集合并且填充Dictionary复合数据,然后按照key来得到这两个parameter,接着根据获得的ajax数据集合的数量批量发quickquery,最后把这两个json集合成res输出。
前端要注意的是code和message以及list的位置都在index0的索引下面。