imageUpload.js 9.56 KB
define(function (require, exports, module) {
    $(function(){
        //绑定删除事件
        $(".img-remove").live('click', function () {
            $(this).parents('li').remove();
        });
        //认证图片上传
        $('#uploadAuthImg').live('change', function(){
            var authType = $('#authSelect option:selected').val();
            var authTypeName = $('#authSelect option:selected').text();
            var endTime = $("#maxTime").val();
            if(!checkAuth(authType, endTime)){
              return false;
            }
            if(!checkImage("uploadAuthImg", 1024 * 1024 * 5)){
              return false;
            }
            //图片上传
            $.ajaxFileUpload({
                url: "/common/uploadImgNoMark",
                secureuri: false,
                fileElementId: $(this),
                dataType: 'json',
                success: function (result) {
                    var errorMsg = null;
                    console.log(result);
                    if (result.success) {
                        $("#rzList").append('<li  class="cleard u-heightauto authLi" authId="' + authType + '" endTime="' + endTime + '"><div class="c-img"><img src="' + result.message + '"><span class="certification-info"><span class="stext" val="' + authType + '">' + authTypeName + '</span><i>' + endTime + '</i></span></div><span class="img-remove"></span></li>').show();
                        $('#error-place-auth').html("");
                        $($("#authSelect").next()[0]).find("em.pack_jselect_arrow").prev().html("请选择证书");
                        $("#maxTime").val("");
                    } else {
                        errorMsg = result.message;
                    }
                    if (errorMsg) {
                        $('#error-place-auth').css({"color": "red"}).html(errorMsg);
                        //return false;
                    }
                    //$(this).empty();
                },
                error: function (data) {
                    console.log(data);
                    $('#error-place-auth').css({"color": "red"}).html(data.message);
                }
            });
        });

        //商品图片上传
        $('#uploadImg').live('change', function () {
            var picSize = $("#user-list li").length;
            if (picSize >= 6) {
                alert("最多可添加6张商品图片!");
                return false;
            }
            if (!checkImage("uploadImg", 1024 * 1024 * 5)) {
                return false;
            }

            //图片上传
            $.ajaxFileUpload({
                url: "/common/uploadImg",
                secureuri: false,
                fileElementId: $(this),
                dataType: 'json',
                success: function (result) {
                    var errorMsg = null;
                    console.log(result);
                    if (result.success) {

                        var html = '';
                        if (picSize == 0) {
                            html = '<li class="selected first-img"><span class="img-box"><img src="';
                        } else {
                            html = '<li class="selected"><span class="img-box"><img src="';
                        }
                        html = html + result.message;

                        if (picSize == 0) {
                            html = html + '"><span class="img-remove"></span></span><span class="img-label">首图</span></li>';
                        } else {
                            html = html + '"><span class="img-remove"></span></span><span class="img-label setIndexImg">设为首图</span></li>';
                        }
                        $("#user-list").append(html);
                        $('#error-place').html("");
                        $("input[name='imgJson']").blur();
                    } else {
                        errorMsg = result.message;
                    }
                    if (errorMsg) {
                        $('#error-place').css({"color": "red"}).html(errorMsg);
                    }
                    //$(this).empty();
                },
                error: function (data,a,b,c) {
                    console.log(data);
                    $('#error-place').css({"color": "red"}).html(data.message);
                }
            });
        });

        //绑定设为首图事件
        $(".setIndexImg").live('click', function () {
            var currImg = $(this).prev('.img-box').html();
            var firstImg = $("#user-list li .img-box").eq(0).html();
            $(this).prev(".img-box").html(firstImg);
            $("#user-list li .img-box").eq(0).html(currImg);

        });

        //图片切换
        $(".s-ntab li").click(function () {
            $(".s-ntab li a").removeClass("current");
            $(this).find("a").addClass("current");
            $(".s-ntab-content").hide();
            $(".s-ntab-content").eq($(this).index()).show();
            if ($(this).text() == '我的图片') {
                $.ImageStore.loadStore(0);
            }
            return false;
        });

        //选中用户图片添加到商品列表
        $('.u_img').live("click", function () {
            var picSize = $("#user-list li").length;
            if (picSize >= 6) {
                alert("最多可添加6张商品图片!");
                return false;
            }
            var url = $(this).find("img").attr('src');

            var html = '';
            if (picSize == 0) {
                html = '<li class="selected first-img"><span class="img-box"><img src="';
            } else {
                html = '<li class="selected"><span class="img-box"><img src="';
            }
            html = html + url

            if (picSize == 0) {
                html = html + '"><span class="img-remove"></span></span><span class="img-label">首图</span></li>';
            } else {
                html = html + '"><span class="img-remove"></span></span><span class="img-label setIndexImg">设为首图</span></li>';
            }
            $("#user-list").append(html);
        });

        //验证图片
        var checkImage = function (elementId, maxsize)
        {
            var ua = window.navigator.userAgent.toLowerCase();
            var obj_file = document.getElementById(elementId);
            var AllImgExt = ".jpg|.jpeg|.gif|.bmp|.png|";
            if (obj_file.value == "") {
                alert("请先选择上传文件!");
                return false;
            }
            var extname = obj_file.value.substr(obj_file.value.lastIndexOf(".")).toLowerCase();
            if (AllImgExt.indexOf(extname + "|") < 0) {
                alert("只能上传jpg|jpeg|gif|bmp|png格式图片!");
                return false;
            }
            if (ua.indexOf("chrome") >= 0 || ua.indexOf("mozilla") >= 0) {

                var filesize = obj_file.files[0].size;
                if (filesize > maxsize) {
                    alert("图片大小不能超过5M!");
                    return false;
                }
            }
            return true;
        };
        //验证认证
        var checkAuth=function(authType, endTime){
            if ($("#rzList li").length >= 5) {
                alert("最多可添加5个证书!");
                return false;
            }
            if (parseInt(authType) < 0) {
                alert("请先选择要添加的证书,再上传认证图片!");
                return false;
            }

            var isExist = false;
            $(".stext").each(function () {
                if ($(this).attr("val") == authType) {
                    isExist = true;
                    return;
                }
            });

            if (isExist) {
                alert("该证书已经添加,请选择其他类型!");
                return false;
            }
            if (endTime == "") {
                alert("请先选择认证结束日期,再上传认证图片!");
                return false;
            }
            return true;
        }
    });

    jQuery.ImageStore = {
        loadStore: function () {
            var  id = $("#mainCid").val();
            var tempLi = '<li class="u_img" time="{imgTime}"><span class="imgs"><img src="{imgUrl}"></span></li>';
            $("#my-imgs").empty();

            $.ajax({
                type: "get",
                dataType: "json",
                url: "/image/getImage?cateId=" + id,
                success: function (data) {
                    if (data != null && data.length > 0) {
                        for (var i in data) {
                            var img = data[i];
                            var time = new Date(img.ctime).format("yyyy-MM-dd");
                            var imgDiv = riot.render(tempLi, {"imgUrl": img.imageUrl, "imgTime": time});
                            $("#my-imgs").append(imgDiv);
                        }
                    }
                },
                error: function () {
                    alert("加载图片库数据,网络错误,请稍后重试");
                }
            });
        },
        screen: function () {
            var startTime = $("#startTime").val();
            var startDate = new Date(startTime);
            var endTime = $("#endTime").val();
            var endDate = new Date(endTime);
            $(".u_img").each(function () {
                var time = $(this).attr("time");
                var imgDate = new Date(time);
                if (startDate > imgDate || imgDate > endDate) {
                    $(this).hide();
                } else {
                    $(this).show();
                }
            })
        }
    }
});