resourceContentManage.js 20.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
var $ = require('jquery'),
    common = require('../common/common');
var Button = require('./partials/Button1');
var resourceObj = require('./partials/resourceObj');
var Validate = require('./partials/Validate1');
var addObj = require('./partials/addObj');

/*获取数据*/
var ViewModel = {};
//资源id
var param = location.href.substring(location.href.lastIndexOf("/") + 1);
common.util.__ajax({
    url: "/resources/resContentIndex",
    data: {id: param},
    async: false
}, function (res) {
    ViewModel = res.data;
}, true);

/*配置模块*/
var edit = new common.edit2(".modal-body", {
    bucket: "yhb-img01"
});
//输入领券码验证
$(document).on("change", "#couponID", function () {
    var couponID = $(this).val();
    common.util.__ajax({
        url: "/coupon/batchCheckCoupons",
        async:false,
        data: {
            params: couponID
        }
    }, function () {
    });
});

var Bll = {
    Brands: [],
    Brands1: {},
    Brdata: [],
    contentDatas: [],
    module: null,
    __render: function (selecter, templater, data) {
        $(selecter).html(common.util.__template2($("#" + templater).html(), data));
    },
    toast: function (index, module) {
        var btn = Button.filter(function (item) {
            return item.template_name == module.contentData.template_name;
        });
        var d = new common.dialog({
            title: (!!~index ? "修改" : "添加") + btn[0].button_name,
            //content: common.util.__template2($("#" + module.contentData.dialog).html(), module),
            content: common.util.__template2($("#" + btn[0].dialog).html(), module),
            width: '70%',
            button: [{
                value: "保存",
                callback: function () {
                    //if (Validate[module.contentData.template_name]) {
                    //    Validate[module.contentData.template_name].forEach(function (item) {
                    //        edit.on("validate", function(){item.fn(module.contentData)})
                    //    })
                    //}
                    if (edit.validate()) {
                        //TODO
                        if (resourceObj[module.contentData.template_name]) {
                            resourceObj[module.contentData.template_name](module.contentData.data);
                        }
                        !!~index ? Bll.contentDatas[index] = module : Bll.contentDatas.push(module);
                        Bll.__render("#add-content", "template_content", {modules: Bll.contentDatas});
                        d.close();
                    }
                    return false;
                },
                css: "btn-primary"
            }]
        });
        Bll.__editRender();
    },
    renderDialog: function (templater) {
        Bll.__render(".modal-body", templater, Bll.module);
        Bll.__editRender();
    },
    __editRender: function () {
        $('.draggable').each(function() {
            if($(this).children().length) {
                $(this).sortable().bind('sortupdate', function() {
                    var fn = new Function("Bll","return Bll.module.contentData." + $(this).data("array"));
                    var arr = fn(Bll);
                    var arr2 = [];//拖拽后顺序
                    var itemsUpdate = $(this).children();
                    if(itemsUpdate.length == arr.length) {
                        for (var i = 0; i < itemsUpdate.length; i++) {
                            arr2.push($(itemsUpdate[i]).attr("drag-index"));
                        }
                        for(var i = 0; i < arr.length; i++) {
                            arr2[i] = arr[arr2[i]];
                        }
                        arr = arr2;
                    }
                    //console.log(arr);
                })
            }
        });
        edit.init();
        edit.on("file_onComplete", function (obj) {
            var names = obj.field;
            Bll.module.contentData.data = common.util.__buildobj(names, '.', Bll.module.contentData.data, function (o, name) {
                o[name] = obj.data;
            });
            console.log( Bll.module.contentData.data );
        });
    },
    //获取品牌
    getBrands: function () {
        var Brand = {};
        $.get("/ajax/yohosearch", function (res) {
            res.data.forEach(function (item, index) {
                var brandAlif = (item.brand_alif || "").toUpperCase();
                if (/^[0-9]$/.test(item.brand_alif)) {
                    brandAlif = "0-9";
                }
                if (/^\W$/.test(item.brand_alif)) {
                    brandAlif = "#";
                }
                Brand[brandAlif] = Brand[brandAlif] || [];
                Brand[brandAlif].push(item);
                Bll.Brands1[item.id] = item;
            });
            for (var i in Brand) {
                Brand[i].sort(function (a, b) {
                    if (a.brand_name && b.brand_name) {
                        var aName = a.brand_name.toLowerCase(),
                            bName = b.brand_name.toLowerCase();
                        if (aName < bName) return -1;
                        if (aName > bName) return 1;
                    }
                    return 0;
                });
                Bll.Brands.push({
                    name: i,
                    items: Brand[i]
                });
            }
        });
    },
    renderBrandPic: function (Brdata) {
        var Brands2 = [];
        Brdata.forEach(function (item, index) {
            if (!item.brandIco) {
                var a = Bll.Brands1[item];
                a.brandIco = common.util.__joinImg("brandLogo", a.brand_ico);
                Brands2.push(a);
            } else {
                item.brandIco = common.util.__template(item.brandIco, {width: 110, height: 150});
                Brands2.push(item);
            }

        });
        Bll.module = Bll.module || {};
        Bll.module.contentData = Bll.module.contentData || {};
        Bll.module.contentData.data = Bll.module.contentData.data || {};
        Bll.module.contentData.data.list = Bll.module.contentData.data.list || [];
        for (var i = 0; i < Brands2.length; i++) {
            var pic = {};
            if (Bll.module.contentData.template_name == "kidsBrands") {
                pic = {
                    "src": Brands2[i].brandIco,
                    "id": Brands2[i].id,
                    "title": Brands2[i].brand_name
                };
            } else {
                pic = {
                    "src": Brands2[i].brandIco,
                    "id": Brands2[i].id,
                    "name": Brands2[i].brand_name
                };
            }
            Bll.module.contentData.data.list.push(pic);
        }
        Bll.renderDialog("brands-template");
    }
};


/*第一步,基础模板*/
Bll.__render("#content-list", "content-template", ViewModel);
Bll.__render(".contents", "template_content_btns", {btns: Button});

/*第二部,把楼层数据转化成数组*/
ViewModel.contentData.forEach(function (item, index) {
    item.contentData = JSON.parse(item.contentData);
    var temp;
    if (item.contentData.template_name == "kidsBrands") {
        temp = item.contentData.data.params.more_url;
        item.contentData.data.params.more_url = {};
        item.contentData.data.params.more_url.action = JSON.parse(temp).action || "";
        item.contentData.data.params.more_url.url = JSON.parse(temp).url || "";
    }
    if (item.contentData.template_name == 'title') {
        temp = item.contentData.data.more_link;
        item.contentData.data.more_link = {};
        item.contentData.data.more_link.action = JSON.parse(temp).action || "";
        item.contentData.data.more_link.url = JSON.parse(temp).url || "";
    }
    item.contentData = JSON.stringify(item.contentData);
    item.contentData = item.contentData.replace(/(gif|png|jpg|jpeg)\?[^"]*/g, '$1');
    item.contentData = common.util.__ObjToArray(JSON.parse(item.contentData));
    Bll.contentDatas.push(item);
});

/*第三部解析楼层*/
Bll.__render("#add-content", "template_content", {modules: Bll.contentDatas});


/*第四部 操作按钮  添加 删除 修改*/
$(document).on("click", ".add_btn", function () {
    var item = Button[$(this).data("index")];
    Bll.module = {};
    Bll.module.contentData = $.extend(true, {}, item);
    Bll.toast(-1, Bll.module);
});

$(document).on("click", ".del", function () {//删除
    var index = $(this).data("index");
    common.dialog.confirm("警告",
        common.util.__template2("是否确认删除?", {}),
        function () {
            if (Bll.contentDatas[index].id) {
                common.util.__ajax({
                    url: "/resources/delResContent",
                    data: {id: Bll.contentDatas[index].id}
                });
            }
            Bll.contentDatas.splice(index, 1);
            Bll.__render("#add-content", "template_content", {modules: Bll.contentDatas});
        });

    //$("#index_" + index).remove();
});
$(document).on("click", ".edit", function () {
    var index = $(this).data("index");
    var item = Bll.contentDatas[index];
    Bll.module = $.extend(true, {}, item);
    Bll.toast(index, Bll.module);
});

/*第五步 绑定监听事件*/
$(document).on("change", ".observe", function () {
    var $this = $(this);
    var name = $this.data("field");
    Bll.module.contentData.data = common.util.__buildobj(name, '.', Bll.module.contentData.data, function (obj, name) {
        obj[name] = $this.val();
        if (name == "image_style") {
            delete obj["default"];
            delete obj["T1F2"];
            delete obj["L1R2"];
            delete obj["imageList"];
            obj[obj[name]] = true;
        }
    });
});
/*根据limit判断最多添加条数, 根据event判断添加的类型, data-event:template_name + "-template"*/
$(document).on("click", ".addBtn", function() {
    var length = $(this).data("limit");
    var arr = $(this).data("event").split(".");
    if (arr[1] == "data") {
        if (Bll.module.contentData.data.length >= length) {
            common.util.__tip("最多"+ length +"条!","warning");
            return;
        }
        Bll.module.contentData.data.push(addObj[arr.join("_")]);
    } else {
        if (Bll.module.contentData.data[arr[1]].length >= length) {
            common.util.__tip("最多"+ length +"条!","warning");
            return;
        }
        Bll.module.contentData.data[arr[1]].push(addObj[arr.join("_")]);
    }
    Bll.renderDialog(arr[0] + "-template");
});

/*删除行*/
$(document).on("click", ".delBtn", function () {
    var arr = $(this).data("event").split(".");
    var index = $(this).data("index");
    if(arr[1] == "data") {
        Bll.module.contentData.data.splice(index, 1);
    } else {
        Bll.module.contentData.data[arr[1]].splice(index, 1);
    }
    Bll.renderDialog(arr[0] + "-template");
});


$(document).on("change", ".chkbox", function() {

});

//获取品牌
Bll.getBrands();
//打开品牌选择模态
$(document).on("click", "#addBrands", function () {
    var e = new common.edit("#brandForm");
    new common.dialog({
        title: "选择品牌",
        width: "70%",
        content: common.util.__template2($("#template5").html(), {
            Brands: Bll.Brands,//所有品牌数据
            Brdata: []
        }),
        button: [
            {
                value: "确定",
                callback: function () {
                    Bll.Brdata = $("#brandCheckBox").val().split('|');
                    Bll.renderBrandPic(Bll.Brdata);
                },
                css: "btn-primary"
            },
            {
                value: "取消"
            }
        ]
    });
    e.init();
});
//品牌筛选
$(document).on('click', '.brand-index', function () {
    var brandIndex = $(this).text();
    $('.brand-wrap').find('[name="' + brandIndex + '"]').show().siblings().hide();
});

//*****************************************************************//
/*图片列表*/
$(document).on("click", '.is_show_name', function () {
    Bll.module.contentData.data.title.is_show_name = $(this).val();
    Bll.renderDialog("imageList-template");
});
//*****************************************************************//
/*推荐(标题 + 12张图)*/
$(document).on("change", '#recommendContentFive-is_show', function () {
    var fn = new Function("console.log(111)");
    fn();
    Bll.module.contentData.data.title.is_show = 1 - Bll.module.contentData.data.title.is_show;
    Bll.renderDialog("recommendContent-template");
});
//*****************************************************************//
/*焦点图*/
$(document).on("change", '#focus-select', function () {
    Bll.module.contentData.focus_type = $(this).val();
    Bll.renderDialog("focus-template");
});
//*****************************************************************//
/*编辑推荐*/
$(document).on("change", '#editorTalk-is_show', function () {
    Bll.module.contentData.data.title.is_show = 1 - Bll.module.contentData.data.title.is_show;
    Bll.renderDialog("editorTalk-template");
});
//推荐品牌 是否显示名称
$(document).on("click", '.is_show_name_brand', function () {
    Bll.module.contentData.data.is_show_name = $(this).val();
    Bll.renderDialog("brands-template");
});

$(document).on("click", "#sub_btn", function () {
    var data = {
        "content": {},
        "data_id": {},
        "rId": ""
    };
    for (var i = 0; i < Bll.contentDatas.length; i++) {
        var contentData = Bll.contentDatas[i].contentData;
        var action;
        var url;
        addSuffix(contentData);
        delete contentData.button_name;
        delete contentData.dialog;
        if (contentData.template_name == "kidsBrands") {
            action = contentData.data.params.more_url.action || "";
            url = contentData.data.params.more_url.url || "";
            contentData.data.params.more_url = "{\"action\":\"" + action + "\",\"url\":\"" + url + "\"}";
        }
        if (contentData.template_name == "title") {
            action = contentData.data.more_link.action || "";
            url = contentData.data.more_link.url || "";
            contentData.data.more_link = "{\"action\":\"" + action + "\",\"url\":\"" + url + "\"}";
        }
        data.content[i] = JSON.stringify(common.util.__ArrayToObj(contentData));
        data.content[i] = JSON.stringify(common.util.__ArrayToObj(Bll.contentDatas[i].contentData));
        if (Bll.contentDatas[i].id) {
            data.data_id[i] = "id_" + Bll.contentDatas[i].id;
        }
    }
    data.rId = param;
    data.content = JSON.stringify(data.content);
    data.data_id = JSON.stringify(data.data_id);
    common.util.__ajax({
        url: "/resources/addResContent",
        data: data
    }, function (res) {
        window.location.href = window.location.href;
    });
});

var addSuffix = function (contentData) {
    if (typeof  contentData == "object") {
        for (var i in contentData) {
            if (i == "src" && contentData[i].indexOf("?") == -1) {
                contentData[i] = contentData[i] + "?imageView2/{mode}/w/{width}/h/{height}";
            } else {
                addSuffix(contentData[i]);
            }
        }
    }
    return contentData;
}


/****************************************************************************************************/
/*商品,商品组*/

/*选择商品表格*/
var goodsgird = new common.grid({
    el: '#goodsgird',
    parms: function () {
        var beginPrice = 0, endPrice = 1000000;
        if (common.util.__input('beginPrice')) {
            beginPrice = common.util.__input('beginPrice');
        }
        if (common.util.__input('endPrice')) {
            endPrice = common.util.__input('endPrice')
        }
        return {
            query: common.util.__input('skns'),
            price: beginPrice + "," + endPrice
        };
    },
    columns: [
        {
            display: "选择",
            type: "checkbox"
        }, {
            display: "产品图片",
            render: function (item) {
                if (item.images_url) {
                    item.images_url = common.util.__joinImg("goodsimg", item.images_url);
                }
                else {
                    item.images_url = "";
                }
                return "<img width=120 height=60 src='" + item.images_url + "?imageView/2/w/100/h/100'/>";
            }
        }, {
            display: "产品名称",
            name: "product_name"
        }, {
            display: "品牌",
            name: "brand_name"
        }, {
            display: "现价",
            name: "sales_price"
        }, {
            display: "牌价",
            name: "market_price"
        }, {
            display: "预售",
            name: "stock_number"
        }, {
            display: "库存",
            name: "storage_num"
        }]
});
//搜索按钮
$(document).on("click", "#search", function () {
    goodsgird.reload();
});
//点击“选择标签”按钮(添加商品)
$(document).on("click", "#goodsSelectBtn", function () {
    new common.dialog({
        title: "选择商品",
        content: $("#template_dialog_goodsgird").html(),
        width: '80%',
        button: [{
            value: "确定",
            callback: function () {
                Bll.module.contentData.data = goodsgird.selected.map(function (item, index) {
                    return {
                        src: item.images_url,
                        id: item.product_skn
                    }
                });
                // Bll.module.data=goodsgird.selected;
                Bll.__render("#goodspic", "template_dialog_goodsimgs", {
                    datas: Bll.module.contentData.data
                });
            },
            css: "btn-primary"
        }]
    });
    goodsgird.init('/yohosearch/search');
});
//点击“添加组”按钮(添加商品组)
$(document).on("click", "#goodsaddBtn", function () {
    if (Bll.module.contentData.data[0].list.length) {
        Bll.module.contentData.data.push(Button[5].data[0]);
    }
    Bll.__render("#groupsgoods", "template_dialog_remgoodsgroup", Bll.module);
});
//点击“选择标签”按钮(添加商品组)
$(document).on("click", ".goodsSelectBtn", function () {
    var index = $(this).data("index");
    new common.dialog({
        title: "选择商品",
        content: $("#template_dialog_goodsgird").html(),
        width: '80%',
        button: [{
            value: "确定",
            callback: function () {
                goodsgird.selected.forEach(function (item, i) {
                    Bll.module.contentData.data[index].list[i] = {
                        src: item.images_url,
                        id: item.product_skn,
                        maxSortId: item.max_sort_id
                    };
                    if (!i) {
                        Bll.module.contentData.data[index].cover = {
                            cover: item.images_url,
                            maxSortId: item.max_sort_id
                        };
                    }
                });
                Bll.__render("#groupsgoods", "template_dialog_remgoodsgroup", Bll.module);
            },
            css: "btn-primary"
        }]
    });
    goodsgird.init('/yohosearch/search');
});
//删除图片按钮
$(document).on("click", ".removepic", function () {
    Bll.module.contentData.data.splice($(this).data("index"), 1);
    $("#goodspic").html(common.util.__template2($("#template_dialog_goodsimgs").html(), {
        datas: Bll.module.contentData.data
    }));
});

$(document).on("mouseover", "#add-content .dragItem", function () {
    var drag = new common.drag("#add-content", Bll.contentDatas, function (data) {
        Bll.contentDatas = data;
        Bll.__render("#add-content", "template_content", {modules: Bll.contentDatas});
    })
    drag.Initialize();
});
$(document).on("mouseleave", "#add-content .dragItem", function () {
    new common.drag("#add-content").destroy();
});

/*
 $(document).on("mouseover","#sortable .dragItem",function(){
 //console.log(Bll.module.contentData.data.list);
 var drag = new common.drag("#sortable", Bll.module.contentData.data.list, function(data){
 Bll.module.contentData.data.list = data;
 console.log(Bll.module.contentData);
 console.log(Bll.module);
 Bll.renderDialog("paramsGroup-template");
 //Bll.__render("#sortable","paramsGroup-template",{modules:Bll.module});
 })
 drag.Initialize();
 });
 $(document).on("mouseleave","#sortable .dragItem",function(){
 new common.drag("#sortable").destroy();
 });
 */