最近朋友在有赞商城上面开了一个店铺,因为有实体店,一般卖商品后送货上门,但是打票时候老是人工用world文档人工复制黏贴订单打印小票, 所以就找我帮忙做一个软件专门打印小票的,就研究起来调用有赞第三方api来着,本篇主要介绍如何用qml中的xmlhttprequest来实现http请求协议,就以调用有赞商城api的查询订单接口为例:kdt.trades.sold.get;
function searchTrades(startDate,endDate){
tradesModel.clear();//清空列表
var request=new XMLHttpRequest();
var data;
//监听onreadystatechange事件
request.onreadystatechange=function(){
if(request.readyState===request.DONE){
data=JSON.parse(request.responseText.toString());
console.log(request.responseText.toString())
for(var i=0;i<data.response.trades.length;i++){
var defference=true;
for(var index=0;index<tradesModel.count;index++){
if(tradesModel.get(index).tid===data.response.trades[i].tid){
defference=false;
}
}
if(defference){
var str="";
var orders=data.response.trades[i].orders;
for(var a=0;a<orders.length;a++){
str+=(" "+orders[a].title+" 数量:"+orders[a].num+" 单价:"+orders[a].price+"元"+"\n\n\n")
}
var sum=0.0;
for(var a=0;a<orders.length;a++){
sum+=Number(orders[a].total_fee);
}
var pay_type;
if(data.response.trades[i].pay_type==="WEIXIN"){
pay_type="微信支付";
}
else if(data.response.trades[i].pay_type==="ALIPAY "){
pay_type="支付宝支付";
}
else if(data.response.trades[i].pay_type==="BANKCARDPAY "){
pay_type="银行卡支付";
}
else if(data.response.trades[i].pay_type==="PEERPAY "){
pay_type="代付";
}
else if(data.response.trades[i].pay_type==="CODPAY "){
pay_type="货到付款";
}
else if(data.response.trades[i].pay_type==="BAIDUPAY "){
pay_type="百度钱包支付";
}
else if(data.response.trades[i].pay_type==="PRESENTTAKE "){
pay_type="直接领取赠品";
}
else if(data.response.trades[i].pay_type==="COUPONPAY"){
pay_type="优惠券/码全额抵扣";
}
else if(data.response.trades[i].pay_type==="BULKPURCHASE"){
pay_type="来自分销商的采购";
}
else{
pay_type="未知类型"
}
str+=" 应付:"+sum.toFixed(2)+"元\n\n"+
" 订单创建日期:"+data.response.trades[i].created+"\n\n"+
"付款类型:"+pay_type;
tradesModel.append({"tid":data.response.trades[i].tid,
"orders":data.response.trades[i].orders,
"ordersContent":str,
"trade":data.response.trades[i],
"selected":false});
str=""
}
}
}
}
//params
var startCreated=startDate;
var endCreadted=endDate;
var secret="your app secret";
var appid="your app id";
var currentDate=new Date();
var method="kdt.trades.sold.get";//调用第三方接口函数
var dateString=currentDate.getFullYear().toString()+"-"+
(currentDate.getMonth()+1<10?"0"+currentDate.getMonth()+1:currentDate.getMonth()+1)+"-"+
(currentDate.getDate()<10?"0"+currentDate.getDate():currentDate.getDate())+" "+
(currentDate.getHours()<10?"0"+currentDate.getHours():currentDate.getHours())+":"+
(currentDate.getMinutes()<10?"0"+currentDate.getMinutes():currentDate.getMinutes())+":"+
(currentDate.getSeconds()<10?"0"+currentDate.getSeconds():currentDate.getSeconds());
//md5验证码
var md5=Qt.md5(secret+"app_id"+appid+"end_created"+endCreadted+"formatjson"+"method"
+method+"sign_methodmd5"+"start_created"+startCreated+"timestamp"+dateString+"v1.0"+secret
);
var url ="https://open.koudaitong.com/api/entry?sign="+md5+"&"+"timestamp="+
dateString+"&v=1.0&app_id="+appid+"&method="+
method+"&sign_method=md5&format=json"+
"&start_created="+startCreated+
"&end_created="+endCreadted;
request.open("GET",url);
request.send();
}
函数调用后返回来的是一个Json文本,qml中非常友好的读取这些信息,并且进行处理,我直接用Listmodel来存放json中的订单列表,用Listviewl来显示出来。
http://www.heilqt.com/topic/5622160642ca1a2d046d6548
时间: 2024-10-12 10:49:25