Edit.js 2.4 KB
var $ = require('jquery'), common = require('../../../common/common');

var e = new common.edit('#editor-group', {bucket: "yhb-img01"});

e.on("render", function() {

    $("#templateKey").select2({ minimumResultsForSearch: Infinity });

});

// 验证图片
e.on("validate", function() {
    var img1 = $("#wechat1_img").attr("value");
    if (img1.length == 0) {
        return "请设置图片1";
    }
    var img2 = $("#wechat2_img").attr("value");
    if (img2.length == 0) {
        return "请设置图片2";
    }
});

e.init();

// submit
$('#add-btn').click(function(option) {
    e.submit($('#add-form').attr('action'), function(option) {

        var data = buildToObjects(option.data);

        addSuffix(data);

        option.data.contentData = JSON.stringify(data);

        console.log(data);

        option.beforeSend = function() {
            $('#add-btn').addClass('disabled');
        };

        option.success = function(res) {
            if (res.code == "200") {
                e.$tip('提交成功', function() {
                    location.href = "/resources/officialAccounts/index";
                }, 'growl-success');
            } else {
                $('#add-btn').removeClass('disabled');
                e.$tip(res.message);
            }

            return false;
        }
        option.error = function(res) {
            e.$tip("提交失败");
        };
    });
});

/**
 * [
        {
             "wechat_name":"YOHOFamiliy",
             "wechat_id":"yoho_familiy",
             "src":""
        },
        {
             "wechat_name":"YOHOFamiliy",
             "wechat_id":"yoho_familiy",
             "src":""
        }
    ] */
function buildToObjects(data) {
    var wechats = [];
    wechats.push(Wechat(data.wechat_id1, data.wechat_name1, data.wechat1_img));
    wechats.push(Wechat(data.wechat_id2, data.wechat_name2, data.wechat2_img));
    return wechats;
}

function Wechat(id, name, src) {
    return {
        "wechat_id": id,
        "wechat_name": name,
        "src": src
    };
}

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;
};