proPhoto.js 5.15 KB
/**
 * Created by JiangMin on 2016/3/22.
 * 产品图片管理
 */
var $ = require('jquery');
var common = require('../common/common');
//日期插件
$('.hasDatepicker').fdatepicker({
    format: 'yyyy-mm-dd'
});

/**
 * 列表显示数据
 * @type {common.grid}
 */
var g = new common.grid({
    el: '#content-list',
    hash: false,
    parms: function () {
        return {
            //查询参数
            productSkn: common.util.__input('content-filter1'),
            productSkc: common.util.__input('content-filter2'),
            productSku: common.util.__input('content-filter3'),
            startTime:((new Date($('#starttime').val())).getTime())/1000,
            endTime: ((new Date($('#endtime').val())).getTime())/1000
        };
    },
    //列表显示
    columns: [
        {display: "SKN", name: "productSkn"},
        {
            display: "最后上传时间", name: "lastAddTime",
            render: function (item) {
                var t = new Date(item.lastAddTime * 1000);
                var formatted = common.util.__dateFormat(t, "yyyy-MM-dd hh/mm/ss");
                return "<p>" + formatted + "</p>";
            }
        },
        {
            display: "图片", name: "pictureBoList", render: function (item) {
            var a = item.pictureBoList || [];
            if (a.length > 0) {
                var b;
                for (var i = 0; i < a.length; i++) {
                    b = b + '<img src="' + item.pictureBoList[i].fileName + '" width="100" height="60"/>'
                }
                return b.substr(9);
            }
            else {
                return ""
            }
        }
        },
        {
            display: "操作", name: "", render: function (item) {
            var arr = [];
            arr.push('<a class="btn btn-info update" data-index="' + item.__index + '">编辑</a>');
            return arr.join('');
        }
        }
    ]
});
g.init("/shotManage/proPhoto/index2");


var Bll = {
    pictureBoList: [],
    selectedBoId: [],
    //重新渲染图片列表
    rendBoList: function (pictureBoList) {
        $(".image-list").html('');
        $("#addPic").append(common.util.__template2($("#template2").html(),
            {
                pictureBoList: pictureBoList
            }
        ));
    },
    toast: function (url, item, datacall) {
        Bll.pictureBoList = item.pictureBoList || [];
        Bll.selectedBoId = [];
        var a = new common.dialog({
            title: "图片",
            width: '80%',
            content: common.util.__template2($("#template1").html(), item),
            button: [
                {
                    value: "提交", callback: function () {
                    var data = datacall && datacall(Bll.pictureBoList, Bll.selectedBoId);
                    common.util.__ajax({
                        url: url,
                        data: data
                    }, function (res) {
                        if (res.code == '200') {
                            g.reload();
                            a.close();
                        }
                    });
                    return false;
                }, css: "btn-primary"
                },
                {
                   "value":"取消",
                    css: "btn-info"
                }
            ]
        });
        Bll.rendBoList(Bll.pictureBoList);
        common.edit.ajaxfileupload(".picfile", {
            params: {
                __type: "upload",
                bucket: "goodsimg"
            },
            valid_extensions: ['png', 'jpg', 'jpeg'],
            onComplete: function (response) {
                if (response.status && response.code == 200) {
                    //console.log("response", response);
                    var data = {
                        "fileName": response.data,
                        "originalName": "1035027.jpg"
                    };
                    Bll.pictureBoList.push(data);
                    Bll.rendBoList(Bll.pictureBoList);
                }
                else {
                    common.util.__tip(response.message, 'warning');
                }
            }
        });

    }
};
//上传图片--点击事件
$('#upload-btn').on('click', function () {
    var item = {
        __state: "add"
    };
    Bll.toast('/shotManage/proPhoto/add', item, function (pictureBoList, selectedBoId) {
        return {
            productSku: parseInt($("#Sku").val()),
            productPhotoAddStrList: JSON.stringify(pictureBoList)
        }
    });
});
//删除单张图片
$(document).on('click', '.remove1', function () {
    var index = $(this).data("index");
    Bll.selectedBoId.push(Bll.pictureBoList[index].id);
    Bll.pictureBoList.splice(index, 1);
    Bll.rendBoList(Bll.pictureBoList);
});

//编辑
$(document).on('click', '.update', function () {
    var item = g.rows[$(this).data("index")];
    item.__state = "update";
    Bll.toast('/shotManage/proPhoto/update', item, function (pictureBoList, selectedBoId) {
        return {
            productSkn: item.productSkn,
            ids: JSON.stringify(selectedBoId)
        }
    });
});

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