imageUpload.js
9.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
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();
}
})
}
}
});