Index.js 8.57 KB
'use strict';
var $ = require('jquery'),
common = require('../../../common/common');
////只能开启的入口
//var OPEN_ONLY_ENTRANCE = ['设置', '二维码', '站内信','订单-待收货','订单-待发货','订单-待评价','订单-退换货','首页','分类','逛','购物车','我的'];
//可以编辑、关闭的入口
var CAN_EDIT_CLOSE_ENTRANCE = ['地址管理','我的晒单','分享购','生日券','有货分期','学生认证','红包','服务与反馈','生日帽','专享折扣','购物返币','生日礼包','升级礼包','免邮服务','优享客服','7天退换货','极速发货','拼团'];
//只能关闭的入口
var CLOSE_ONLY_ENTRANCE = ['Yoho!Family','潮流口令'];
var ENUM = {
    StatusEnum: {
        0: '关闭',
        1: '开启'
    }
};
new common.dropDown({el: '#supplier-name', ajax: 'supplier'});
//下拉框
new common.dropDown({
    el: '#sel-category',
    ajax: 'selectAppEntranceIconCategory'

});
var g = new common.grid({
    el: '#basicTable',
    size: 10,
    parms: function () {
        return {
            displayName: common.util.__input('sel-displayName'),
            status: common.util.__input('sel-status'),
            categoryId: common.util.__input('sel-category')
        };
    },
    columns: [
        {
            display: '入口',
            name: "entranceName"
        },
        {
            display: '前端展示名称',
            name: "displayName"
        },
        {
            display: '分类',
            name: "categoryName"
        },
        {
            display: '状态',
            name:"status",
            render: function (item) {
                return "<p>" + ENUM.StatusEnum[item.status] + "</p>";
            }
        },
        {
            display: '操作',
            name: "",
            render: function (items) {
                var HtmArr = [];
                var entranceName = items.entranceName;
                if(!contains(CLOSE_ONLY_ENTRANCE,entranceName)){
                    HtmArr.push('<a data-index="' + items.__index + '" href="JavaScript:;" class="btn btn-primary btn-xs info-modify">编辑</a>');
                }
                if(contains(CAN_EDIT_CLOSE_ENTRANCE,entranceName)||contains(CLOSE_ONLY_ENTRANCE,entranceName)){
                    if(items.status == 0){
                        HtmArr.push('<a onclick="changeStatus(1,'+items.id+')" href="javascript:;" class="btn btn-info btn-xs">开启</a>');
                    }else{
                        HtmArr.push('<a onclick="changeStatus(0,'+items.id+')" href="javascript:;" class="btn btn-danger btn-xs">关闭</a>');
                    }
                }

                return HtmArr.join('');
            }
        }]
});
g.init('/appEntranceIcon/queryList');

//==================== 按钮点击事件 =====================//

$("#filter-btn").click(function () {
    g.reload(1);
});
/**
 * 监听输入框变化
 */
$(document).on("change", ".observe", function () {
    var $this = $(this);
    var name = $this.data("field");
    Bll.module = common.util.__buildobj(name, '.', Bll.module, function (obj, name1) {
        obj[name1] = $this.val();
    });
    Bll.module.shopName = $('#shopId').find("option[value='" + Bll.module.shopId + "']").text();
});

/*验证*/
var edit = new common.edit2(".modal-body");
var Bll = {
    module: null,
    //弹框
    toast: function (module, url) {
        Bll.module = module;
        var d = new common.dialog({
            title: "入口名称",
            content: common.util.__template2($("#template").html(), Bll.module),
            width: '40%',
            button: [
                {
                    value: "保存",
                    callback: function () {
                        if (edit.validate()) {
                            common.util.__ajax2({
                                url: url,
                                data: Bll.module
                            }, function (res) {
                                if (res.code == '200') {
                                    g.reload();
                                    d.close();
                                }else{
                                    common.util.__tip(res.message, "warning");
                                }
                            }, true);
                        }
                        return false;
                    },
                    css: "btn-primary"
                },
                {
                    "value": "取消",
                    "css": "btn-info"
                }
            ]
        });
        Bll.__editRender();
    },
    __editRender: function () {
        edit.init();
        //new common.dropDown({
        //    el: "#sel-displayName",
        //    ajax: 'selectAppEntranceIconCategory'
        //
        //});
    }
};

/**
 * 编辑--点击事件
 */
$(document).on('click', '.info-modify', function () {
    var item = g.rows[$(this).data("index")];
    common.util.__ajax(
        {
            url: '/appEntranceIcon/queryById',
            data: {
                id: item.id,
            }
        }, function (res) {

            //var item = res.data;
            appEntranceIconOP( '/appEntranceIcon/update', res.data);
        }, true);
});
//重发
$(document).on("click", "#uploadZip-btn", function () {
    common.dialog.confirm("确认上传","是否确认上传zip?",function() {
        common.util.__ajax2({
            url: '/appEntranceIcon/uploadZip',
            data: {}
        }, function (res) {
            if (res.code == '200') {
                common.util.__tip("保存成功","success");
                d.close();
            }else{
                common.util.__tip(res.message, "warning");
            }
        }, true);

    });
});
// 图片上传
function uploadImage() {
    common.edit.ajaxfileupload(".picfile", {
        params: {
            __type: "upload",
            bucket: "plustar"
        },
        valid_extensions: ['png', 'jpg', 'jpeg'],
        onComplete: function (response) {

            if (response.status && response.code == 200) {
                console.log("response", response);
                if (response.data) {
                    imgArr.push(response.data);
                    rendBoList(imgArr);
                }
            }
            else {
                common.util.__tip(response.message, 'warning');
            }
        }
    });
};
var imgArr = [];
function appEntranceIconOP( url, item) {

    var a = new common.edit("#templete-top", {
        "bucket": "plustar"
    });

    //a.on("validate", function () {
    //    if (imgArr.length == 0) {
    //        return "请上传banner图";
    //    }
    //});
    common.dialog.confirm('入口', common.util.__template2($("#template").html(), item), function () {
        return a.submit(url, function (option) {

            if (imgArr.length) {
                option.data.bannerImg = imgArr.toString();
            }
            imgArr = [];

            option.success = function (res) {
                if (res.code == "200") {
                    a.$tip("提交成功", function () {
                        g.reload();
                    }, 'growl-success');
                } else {
                    a.$tip(res.message);
                }
                return false;
            },
                option.error = function (res) {
                    a.$tip(res.message);
                }
        });
    });
    $(".modal-dialog").css("width","800px");
    /**
     * 删除图片
    */
    $(document).on("click", ".del-btn", function () {
        var key = $(this).data('key');

        var $parent = $(this).parent().parent();

        $parent.find("input[type=file]").each(function(){
            $(this).attr("value", "");
        });

        $parent.find(".upload-image-list .fileinput-button-icon").html('+');
    });

    a.init();

    uploadImage();
    rendBoList(imgArr);
}

function rendBoList(pictureBoList) {
    $(".image-list").html('');
    $("#addPic").append(common.util.__template2($("#imgeUpload").html(),
        {
            pictureBoList: pictureBoList
        }
    ));
}


function contains(arr, obj) {
    var i = arr.length;
    while (i--) {
        if (arr[i] === obj) {
            return true;
        }
    }
    return false;
}
window.changeStatus = changeStatus;
function changeStatus(status,id) {
    var param = {
        status: status,
        id:id
    };
    common.dialog.confirm("警告",
        "确认修改开启状态?",
        function () {
            common.util.__ajax({
                url: '/appEntranceIcon/update',
                data: param
            }, function(res) {
                g.reload();
            });
        });
}