grass_article.js 4.37 KB
function handleGoodsBeforeSubmit(html) {
    var div=document.createElement("div");
    div.innerHTML = html;

    var node = div.querySelectorAll(".yhproduct");
    for (var i = 0 ;i<node.length;i++){
        var good = document.createElement("div");
        var anode = node[i];
        good.setAttribute("class","yhproduct");
        good.setAttribute("data-option", anode.getAttribute("data-option"));
        var product = '<product-group-item v-bind="getNoteProduct(\''+anode.getAttribute("data-option")+'\')"></product-group-item>';
        good.innerHTML = product;
        // if(anode.parentNode.parentNode){
        //     anode.parentNode.parentNode.insertBefore(good, anode.parentNode);
        //     anode.parentNode.parentNode.removeChild(anode.parentNode);
        // }else{
        // }
        anode.parentNode.insertBefore(good, anode);
        anode.parentNode.removeChild(anode);

    }

    return div.innerHTML;

}

function handleGoodsAfterEdit(html) {
    var productSkns = [];
    var yohoproducts = {};
    var ufoproducts = {};
    var div=document.createElement("div");
    div.innerHTML = html;

    var node = div.querySelectorAll(".yhproduct");
    for (var i = 0 ;i<node.length;i++){
        productSkns.push(node[i].getAttribute("data-option"));
    }
    if(productSkns.length==0){
        return html;
    }
    yohoproducts = initGoods(productSkns,1);
    ufoproducts = initGoods(productSkns,2);

    var productHtml = {};
    for (var k = 0 ;k<productSkns.length;k++){
        productHtml[productSkns[k]] = buildgood(productSkns[k],yohoproducts,ufoproducts);
    }
    for (var n = 0 ;n<node.length;n++){
        var skn = node[n].getAttribute("data-option");
        node[n].parentNode.insertBefore(createDocument(productHtml[skn]), node[n]);
        // node[n].parentNode.insertBefore(document.createElement("br"), node[n]);
        node[n].parentNode.removeChild(node[n]);
    }
   
    return div.innerHTML;
}

function initGoods(skns,type) {
    var products = {};
    var url=serverContextPath+"/grassArticle/queryGoods";
    // var url = contextPath+"/search/queryGoods";
    var query ='';
    var params ='';
    for (var i=0;i<skns.length;i++){
        if(query==''){
            query = skns[i];
        }else{
            query = query+","+skns[i];
        }
    }
    if(type==1){
        params = "?query="+query;
    }
    if(type==2){
        params = "?query="+query+"&type=UFO";
    }
    $.ajax({
        type : "post",
        url : url+params,
        async : false,
        success : function(data){

            if(data.code == 200){
                for (var i=0; i<data.data.list.length;i++){
                    var rowdata = data.data.list[i]
                    products[rowdata.product_skn] = rowdata;
                }
            }
        }
    });

    return products;

}

function buildgood(skn, yohoproducts,ufoprodcut) {
    debugger;
    var data = yohoproducts[skn];
    if(!data){
        data = ufoprodcut[skn];
    }
    if(data){
        return gooddiv(data);
    }
    return "";
}

function gooddiv(data) {

    // return '商品图:<img width=\"120\" height=\"60\" src="'+getImageUrl(data.default_images)+ '">' +
    //     '<h5 style=\"margin-top: 0px;margin-bottom: 0px\">商品名:'+ data.product_name +'</h5>\n'+
    //     '<h5 style=\"margin-top: 0px;margin-bottom: 0px\">价格:'+data.sales_price+'</h5>\n';

    return '<a  href="#" title="商品" class="yhproduct" data-option="'+data.product_skn+'"'+'>'+ data.product_name.substring(10)+'(skn:'+data.product_skn+')'+ '</a>'+'<br>';

}

function getImageUrl(image) {
    var result='';
    if (image) {
        if(image.indexOf('http://') == 0){
            result = image.split("?")[0];
        }else{
            var fileMode = image.substring(15, 17);
            if(fileMode=='01'){
                result= "http://"+"img11.static.yhbimg.com"+"/goodsimg"+image;
            }
            if(fileMode=='02'){
                result= "http://"+"img12.static.yhbimg.com"+"/goodsimg"+image;
            }
        }
    }
    else {
        result = ""
    }
    if (result) {
        return result + "?imageMogr2/thumbnail/320x320/extent/320x320/background/d2hpdGU=/position/center/quality/90";
    } else {
        return "";
    }
}

function createDocument(txt) {
    var template =txt;
    var doc = new DOMParser().parseFromString(template, 'text/html');
    var div = doc.querySelector('.yhproduct');
    return div;
}