index.js 7.01 KB
/**
 * Created by JiangMin on 2016/4/19.
 */
var $ = require('jquery'),
    common = require('../common/common');
/**
 * 列表显示数据
 * @type {common.grid}
 */
var g = new common.grid({
    el: '#content-list',
    hash: false,
    //查询参数
    parms: function () {
        return {
            name: common.util.__input('name-filter')
        };
    },
    //列表显示
    columns: [
        {
            display: "创建时间",
            name: "createTime"
        },
        {
            display: "发券名称",
            name: "name"
        },
        {
            display: "优惠券ID",
            name: "couponsIds",
            render: function (item) {
                var couponsIds = [];
                for (var i = 0; i < item.couponsIds.length; i++) {
                    couponsIds.push(item.couponsIds[i].id);
                }
                return couponsIds.join(",");
            }
        },
        {
            display: "token值",
            name: "couponSendToken",
            render: function (item) {
                return '<div class="col-sm-12"><input class="col-sm-10" style="border: 0px" id="couponSendToken' + item.__index + '" value="' + item.couponSendToken + '" readonly/>' +
                    '<input class="col-sm-2 btn couponSendToken" value="复制" data-index="' + item.__index + '"/></div>'
            }
        },
        {
            display: "操作",
            name: "",
            render: function (item) {
                var arr = [];
                arr.push('<a class="btn btn-info update" data-index="' + item.__index + '">编辑</a>');
                arr.push('<a class="btn btn-danger delete" data-index="' + item.__index + '">删除</a>');
                return arr.join('');
            }
        }
    ]
});
g.init("/couponSendManager/queryList1");
/*验证*/
var edit = new common.edit2(".modal-body");
var Bll = {
    contentDatas: [],
    module: null,
    __render: function (selecter, templater, data) {
        $(selecter).html(common.util.__template2($("#" + templater).html(), data));
    },
    //弹框
    toast: function (module, hint) {
        Bll.module = module;
        var d = new common.dialog({
            title: hint + "发券",
            content: common.util.__template2($("#template").html(), Bll.module),
            width: '70%',
            button: [
                {
                value: "保存",
                callback: function () {
                    var couponIds = Bll.module.couponsIds || [];
                    var flag = true;
                    var ids = [];
                    for (var i = 0; i < couponIds.length; i++) {
                        ids.push(couponIds[i].id);
                    }
                    if (ids.join(",").indexOf("-1") > -1) {
                        flag = false;
                        common.util.__tip("请正确选择优惠券ID", "warning");
                    } else {
                        Bll.module.couponsIds = ids.join(",");
                    }
                    if (edit.validate() && flag) {
                        common.util.__ajax({
                            url: "/couponSendManager/addOrUpdate",
                            data: Bll.module
                        }, function (res) {
                            if (res.code == '200') {
                                g.reload();
                                d.close();
                            }
                            else {
                                common.util.__tip(res.message, "warning");
                            }
                        });
                    }
                    return false;
                },
                css: "btn-primary"
            },
                {
                    "value":"取消",
                    "css":"btn-info"
                }
            ]
        });
        Bll.__editRender();
    },
    renderDialog: function (templater) {
        Bll.__render(".modal-body", templater, Bll.module);
        Bll.__editRender();
    },
    __editRender: function () {
        edit.init();
        new common.dropDown({
            el: '.couponsIds',
            ajax: 'couponID'
        });
    }
};

/**
 * 监听输入框变化
 */
$(document).on("change", ".observe", function () {
    var $this = $(this);
    var name = $this.data("field");
    Bll.module = common.util.__buildobj(name, '.', Bll.module, function (obj, name1) {
        if (name.indexOf('id') > -1) {
            obj[name1] = $this.val();
            obj["text"] = $this.find("option[value='" + $this.val() + "']").text();
        }
        obj[name1] = $this.val();
    });
});

/*添加一行*/
$(document).on("click", ".addBtn", function () {
    Bll.module.couponsIds.push({
        "id": "",
        "text": ""
    });
    Bll.renderDialog("template");
});

/**
 * 删除行
 */
$(document).on("click", ".delBtn", function () {
    var index = $(this).data("index");
    if(Bll.module.couponsIds.length<=1){
        common.util.__tip("至少有一张优惠券","warning")
    }
    else {
        Bll.module.couponsIds.splice(index, 1);
        Bll.renderDialog("template");
    }
});

/**
 * 添加--点击事件
 */
$('#add-content').on('click', function () {
    var item = {
        "id": "",
        "name": "",
        "couponsIds": [
            {
                "id": "",
                "text": ""
            }
        ],
        "status": 1
    };
    Bll.toast(item, "添加");
});

/**
 * 编辑--点击事件
 */
$(document).on('click', '.update', function () {
    var item = g.rows[$(this).data("index")];
    var item1= $.extend(true,{},item);
    Bll.toast(item1, "修改");
});

/**
 * 删除--点击事件
 */
$(document).on('click', '.delete', function () {
    var item = g.rows[$(this).data("index")];
    common.dialog.confirm("警告",
        "确认删除?",
        function () {
            common.util.__ajax({
                url: '/couponSendManager/addOrUpdate',
                data: {
                    id: item.id,
                    status: 0
                }
            }, function () {
                g.reload();
            });
        });
});

/**
 * 查询按钮--点击事件
 */
$(document).on('click', '#filter-btn', function () {
    g.reload(1);
});

/**
 * 复制token点击事件
 */
$(document).on('click', '.couponSendToken', function () {
    var index = $(this).data("index");
    var id = "couponSendToken" + index;
    var Url2 = document.getElementById(id);
    Url2.select(); // 选择对象
    document.execCommand("Copy"); // 执行浏览器复制命令
    common.util.__tip("Token链接已复制好,可贴粘", "success")
});

/**
 * 查看token使用方法
 */
$(document).on('click', '#token', function () {
    var a = new common.dialog({
        title: "<h4>token使用方法</h4>",
        width: '50%',
        content: '<p>活动开发人员使用Token时,按照对应格式写在页面中,示例代码:</p>&lt;a class="yoho-conpon" ' +
        'href="javascript:;" data-token="29-6ea9ab1baa0efb9e19094440c317e21b"&gt;领券按钮&lt;/a&gt;<h5>注:必须引入 JS 插件</h5>'
    });
});