qt qml 利用xmlhttprequest 調用有贊api

最近朋友在有贊商城上面開了一個店鋪,由於有實體店,通常賣商品後送貨上門,可是打票時候總是人工用world文檔人工複製黏貼訂單打印小票,
因此就找我幫忙作一個軟件專門打印小票的,就研究起來調用有贊第三方api來着,本篇主要介紹如何用qml中的xmlhttprequest來實現http請求協議,就以調用有贊商城api的查詢訂單接口爲例:kdt.trades.sold.get;json

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來顯示出來。api

效果截圖:微信

clipboard.png

相關文章
相關標籤/搜索