zhao
2021-07-19 8347f2fbddbd25369359dcb2da1233ac48a19fdc
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
 
 
 
var newle = 0;//最新文件个数
var isff = 0;//非法文件数
 
$.fn.bindUpload = function () {
    var myData = {
        url: arguments[0].url || "",
        type: arguments[0].type || "",
        num: arguments[0].num || "",
        callbackPath: arguments[0].callbackPath || "#callbackPath",
        size: arguments[0].size || 5
    }
    var _this = this;
    var input = this.find('input.add');
    var sike = 0;
    var itemList = null;
    var status = [];
    var reg = /[\u4e00-\u9fa5]/g;
    if (myData.num == 1) {
        input.removeAttr("multiple");
    } else {
        input.attr("multiple");
    }
    //打开上传进度窗口
    this.openWindow = function (files) {
        if (files.length > myData.num) {
            parent.ShowMsg("单次导入最多不超过" + myData.num + "张,本次预加载" + myData.num + "张!");
        }
        var allSize = 0;
        for (var x = 0; x < files.length; x++) {
            if (myData.num && x < myData.num || !myData.num) {
                allSize += files[x].size;
                newle++;
                status.push({ name: files[x].name, state: 0 });
            } else {
                status.push({ name: files[x].name, state: 1 });
            }
        }
        var box = '<div id="zwb_upload_status">' +
            '<div class="box">' +
            '        <div class="title">' +
           // '            共' + files.length + '个文件&nbsp;' + ((allSize / 1048576).toFixed(3) == 0 ? allSize / 1048576 : (allSize / 1048576).toFixed(3)) + 'mb' +
           '图 片 预 览' +
           '        </div>' +
            '        <div class="list">' +
            '<ul class="listBox"></ul>' +
            '</div>' +
            '        <div class="foot">' +
            '            <span class="ok">全部上传</span>' +
            '            <span class="off">关闭预览</span>' +
            '        </div>' +
            '</div>' +
            '    </div>';
        _this.append(box);
        //关闭进度窗口函数
        _this.find(".off").bind("click", function () {
            newle = 0;//最新文件个数
            isff = 0;//非法文件数
 
            input.val("");
            _this.find("#zwb_upload_status").fadeOut(100, function () {
                $(this).remove();
            });
            status = [];
            files = null;
        });
        _this.getImgSrc(files);
    }
    var aa = 0;
    //显示缩略图函数
    this.getImgSrc = function (files) {
        aa = 0;
        var reader = new FileReader();
        var size = files[sike].size;
        var name = files[sike].name;
        var index1 = name.lastIndexOf(".");
        var type = name.substring(index1 + 1, name.length);//后缀名
        var info = "";
 
        if (myData.type != "" && myData.type.indexOf(type) == -1) {
            info = "文件格式非法";
            status[sike].state = 1;//禁止上传
            isff++;
        }
        //if (reg.test(name)) {
        //    info = "文件名不能含有中文";
        //    status[sike].state = 1;//禁止上传
        //}
        var filename = name.substring(0, name.lastIndexOf("."));//不带后缀的文件名
        var ajaxUrl = "/PictureMgr/Picture/ExistPicName";
        var ajaxData = { picName: filename, imgGuid: "" };
        var msg = AjaxManagers(JSON.stringify(ajaxData), ajaxUrl);
        var resultstr = eval("(" + msg + ")");
        if (resultstr.Success == "true" || resultstr.Success == true) {
            info = "图片名与后台重复";
            status[sike].state = 1;//禁止上传
            isff++;
            newle--;
        }
 
        if (myData.size != "" && (size / 1048576) > myData.size) {
            info = "文件过大";
            isff++;
            status[sike].state = 1;//禁止上传
            newle--;
        }
        if (type == "png" || type == "jpg" || type == "bmp" || type == "jpeg") {
            reader.readAsDataURL(files[sike]);
            reader.onload = function () {
                var imgPath = reader.result;
                if (sike < myData.num || myData.num == "") {
                    _this.find("#zwb_upload_status ul.listBox").append('<li>' +
                        '<img  src="' + imgPath + '"/>' +
                        '<a>' +
                        '<p class="name">' + name + '</p>' +
                        '<p class="size">大小:&nbsp;' + ((size / 1048576).toFixed(3) == 0 ? size / 1048576 : (size / 1048576).toFixed(3)) + 'MB</p>' +
                        '<p>状态:&nbsp;<b class="state">未上传</b></p>' +
                        '</a>' +
                        '<span class="success"></span>' +
                        '<span class="delete"></span>' +
                        '<div class="err"' + (info ? 'style="width:100%;"' : '') + '>' + info +
                        '</div>' +
                        '<div class="progressBox">' +
                        '<div class="progress"></div>' +
                        '</div>' +
                        '</li>');
                }
 
                sike++;
                if (sike == files.length) {
                    sike = 0;
                    _this.find(_this.find('#zwb_upload_status').fadeIn(100));
                    itemList = _this.find("#zwb_upload_status ul.listBox li");
                    // newle = itemList.length;//设置文件原始数量
                    _this.find(".ok").bind("click", function () {
                        //if (isff > 0) {
                        //    parent.ShowMsg("请删除不合格文件后继续!");
                        //    return false;
                        //}
                        if (newle <= 0) {
                            parent.ShowMsg("无可上传图片,请关闭后重新选择图片上传!");
                            return false;
                        }
                        var lengthI = files.length;
 
                        _this.find(".ok")[0].innerHTML = "正在上传,请勿关闭…";
                        _this.find(".ok").off("click");
                        _this.find(".ok").css("color", "red");
 
                        for (var i = 0; i < files.length; i++) {
                            if (status[i].state == 0) {
                                var imgHeight = _this.find("#zwb_upload_status ul.listBox").eq(0).find("img")[i].naturalHeight;
                                var imgWidth = _this.find("#zwb_upload_status ul.listBox").eq(0).find("img")[i].naturalWidth;
                                _this.uploadimg(lengthI, i, files[i], imgHeight, imgWidth);
                            }
                        }
 
                    });
                    _this.find(".delete").bind("click", function () {
                        var index = $(this).parent().index();
                        if (status[index].state == 1) {
                            isff--;
                        }
                        if (status[index].state == 0) {
                            newle--;//点击删除时文件数量递减
                        }
                        status[index].state = 1;
                        $(this).parent().hide(100);
                    });
                } else {
                    return _this.getImgSrc(files);
                }
            };
        }
        else {
            if (type == "zip" || type == "rar" || type == "jar" || type == "7z" || type == "cab" || type == "iso") {
                imgPath = 'img/zip.png';
            } else if (type == "text") {
                imgPath = 'img/text.png';
            } else if (type == "doc") {
                imgPath = 'img/word.png';
            } else if (type == "html" || type == "htm" || type == "url") {
                imgPath = 'img/html.png';
            } else if (type == "xls") {
                imgPath = 'img/excal.png';
            } else if (type == "exe") {
                imgPath = 'img/exe.png';
            } else if (type == "css" || type == "less" || type == "sass") {
                imgPath = 'img/css.jpg';
            } else if (type == "js") {
                imgPath = 'img/js.jpg';
            } else if (type == "php") {
                imgPath = 'img/php.png';
            } else if (type == "sql") {
                imgPath = 'img/sql.png';
            } else if (type == "ttf" || type == "otf" || type == "ttc" || type == "woff" || type == "woff2") {
                imgPath = 'img/font.png';
            } else {
                imgPath = 'img/unknown.png';
            }
            if (sike < myData.num || myData.num == "") {
                _this.find("#zwb_upload_status ul.listBox").append('<li>' +
                    '<img  src="' + imgPath + '"/>' +
                    '<a>' +
                    '<p class="name">' + name + '</p>' +
                    '<p class="size">大小:&nbsp;' + ((size / 1048576).toFixed(3) == 0 ? size / 1048576 : (size / 1048576).toFixed(3)) + 'b</p>' +
                    '<p>状态:&nbsp;<b class="state">未上传</b></p>' +
                    '</a>' +
                    '<span class="success"></span>' +
                    '<span class="delete"></span>' +
                    '<div class="err"' + (info ? 'style="width:100%;"' : '') + '>' + info +
                    '</div>' +
                    '<div class="progressBox">' +
                    '<div class="progress"></div>' +
                    '</div>' +
                    '</li>');
            }
 
            sike++;
            if (sike == files.length) {
                sike = 0;
                _this.find(_this.find('#zwb_upload_status').fadeIn(100));
                itemList = _this.find("#zwb_upload_status ul.listBox li");
                newle = itemList.length;//设置文件原始数量
                //_this.find(".ok").bind("click", function () {
                //    var lengthI = files.length;
                //    for (var i = 0; i < files.length; i++) {
                //        if (status[i].state == 0) {
                //            _this.uploadimg(lengthI, i, files[i]);
                //        }
                //    }
                //});
                //_this.find(".delete").bind("click", function () {
                //    var index = $(this).parent().index();
                //    status[index].state = 1;
                //    $(this).parent().hide(100);
                //});
                _this.find(".ok").bind("click", function () {
                    var lengthI = files.length;
 
                    //if (isff > 0) {
                    //    parent.ShowMsg("请删除不合格文件后继续!");
                    //    return false;
                    //}
                    _this.find(".ok")[0].innerHTML = "正在上传,请勿关闭…";
                    _this.find(".ok").off("click");
                    _this.find(".ok").css("color", "red");
 
                    for (var i = 0; i < files.length; i++) {
                        if (status[i].state == 0) {
                            var imgHeight = _this.find("#zwb_upload_status ul.listBox").eq(0).find("img")[i].naturalHeight;
                            var imgWidth = _this.find("#zwb_upload_status ul.listBox").eq(0).find("img")[i].naturalWidth;
                            _this.uploadimg(lengthI, i, files[i], imgHeight, imgWidth);
                        }
                    }
                });
                _this.find(".delete").bind("click", function () {
                    var index = $(this).parent().index();
                    if (status[index].state == 1) {
                        isff--;
                    }
                    status[index].state = 1;
                    newle--;//点击删除时文件数量递减
                    $(this).parent().hide(100);
                });
            } else {
                return _this.getImgSrc(files);
            }
        }
    }
 
    this.uploadimg = function (max, i, file, imgHeight, imgWidth) {
        //sizelj = sizelj + file.size;
        //console.log(aa);
        //console.log("****");
        //console.log(newle);
        //console.log("sizelj:" + sizelj);
        //console.log("allSize:" + allSize);
        var reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function (e) {
            var ext = file.name.substring(file.name.lastIndexOf("."), file.name.length);
            $.ajax({
                type: "POST",
                url: FileServerData.WebServiceURL + "/api/File/POSTUploadImg", //FileServerData.WebServiceURL获取文件服务器地址 在webconfig中有设置
                data: {
                    "base64": this.result,
                    "type": ext,
                    "watermark": watermarkguid, //获取水印图guid
                    "watermarkShow": disType, //获取显示方式
                    "isMaxWatermark": bigNot, //获取大图是否需要水印
                    "isMinWatermark": smallNot, //获取小图是否需要水印
                    "maxWidth": bigSize, //获取大图尺寸
                    "minWidth": smallSize //获取小图尺寸
                }, //所有上传文件统一为此对象。result.value 不再截取。type 为文件类型,带.
                beforeSend: function () {
                    itemList.eq(i).find(".progressBox").show();
                    itemList.eq(i).find(".progress").width("20%");
                    itemList.eq(i).find(".state").html("正在导入");
                    //   xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
                    //console.log(1);
                },
                //xhr: function () {
 
                ////    //console.log("onprogress:" + onprogress); 
                //    //    //console.log(xhr.upload); 
                //    console.log(2);
                //    var xhr = $.ajaxSettings.xhr();
                //    console.log(3);
                ////    var xhr = new XMLHttpRequest();
                ////    xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
                //////    xhr.setRequestHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE,PUT");
                //////       if (xhr.upload) { //检查upload属性是否存在
                //////    //        //绑定progress事件的回调函数
                //           //xhr.upload.addEventListener('onprogress', function (e) {
                //           //     var percent = (e.loaded / e.total * 100).toFixed(2);
                //           //     itemList.eq(i).find(".progress").width(percent + "%");
                //           //        }, false);
                ////        xhr.upload.onprogress = progressFunction;
                ////       }
                //      return xhr; //xhr对象返回给jQuery使用
                //},
                success: function (data) {
                    status[i].state = 1;
                    var obj = eval("(" + data + ")");
                    var imgGuid = obj.Data;
                    var filename = file.name.substring(0, file.name.lastIndexOf("."));
                    if (imgGuid != "" || imgGuid != null || imgGuid != undefined) {
                        var res = dosave(filename, imgGuid, imgHeight, imgWidth);
                        if (res) {
                            itemList.eq(i).find(".state").html("导入成功");
                            itemList.eq(i).find(".progress").width("100%");
                            itemList.eq(i).find(".success").addClass("success2");
                            itemList.eq(i).find(".delete").remove();
                        } else {
                            itemList.eq(i).find(".state").html("导入失败");
                        }
                    } else {
                        itemList.eq(i).find(".state").html("上传失败");
                    }
                    aa++;
                    if (aa == newle) {
                        _this.find(".ok")[0].innerHTML = "上传结束";
                        _this.find(".ok").css("color", "white");
                    }
 
 
                    //isover = false;
                    //console.log(isover);
                    //if (i == max - 1) {
                    //    _this.find(".ok")[0].innerHTML = "上传结束";
                    //    _this.find(".ok").css("color", "white");
                    //}
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    parent.ShowMsg('XMLHttpRequest.status: ' + XMLHttpRequest.status);
                }
            });
        }
    }
 
 
    function dosave(filename, imgGuid, imgHeight, imgWidth) {
        var picName = filename;
        var picRemark = "";
        var Guid = "";
        var orinalSize = imgWidth + "|" + imgHeight;
        var index1 = filename.lastIndexOf("_");//正常图片需要去掉流水
        var ItemCode = filename.substring(0, index1);//后缀名
        var fromPage = GetQueryString("fromPage");
        var ajaxUrl = "/PictureMgr/Picture/ImportPicture";
        if (fromPage == "HotPicture") {
            ajaxUrl = "/PictureMgr/HotPicture/ImportHotPicture";
            ItemCode = filename;//热点图不存在物料编码+"_01"这种格式,属于:去掉后缀后全匹配 DBS提
        }
        var ajaxData = { imgGuid: imgGuid, picName: picName, picRemark: picRemark, orinalSize: orinalSize, bigSize: bigSize, smallSize: smallSize, ItemCode: ItemCode, CN_S_CLASSGUID: CN_S_Class_Guid, CN_S_FK_CLASSGUID: CN_S_Fk_Class_Guid };
 
        var msg = AjaxManagers(JSON.stringify(ajaxData), ajaxUrl);
        if (msg != "") {
            var resultstr = msg;
            if (resultstr.Success == "true" || resultstr.Success == true) {
                haveup = true;
                return true;
            } else {
                return false;
            }
        }
    }
 
 
    input.bind("change", function () {
        if ($(this).val() != "") {
            _this.openWindow(this.files);
        }
    });
};
 
 
$(function () {
    LoadWaterMarkData();//获取水印信息
    LoadClassGuid();//获取ITEM对应的类GUID
});
 
var watermarkguid = "";      //获取水印图guid
var disType = "";            //获取显示方式
var bigNot = "";             //获取大图是否需要水印
var smallNot = "";           //获取小图是否需要水印
var bigSize = "";            //获取大图尺寸
var smallSize = "";          //获取小图尺寸
function LoadWaterMarkData() {
    var ajaxUrl = "/PictureMgr/WaterMark/GetMarkImg";
    var ajaxData = { CN_S_KEY: "WatermarkSetting" };
    var msg = AjaxManagers(JSON.stringify(ajaxData), ajaxUrl);
    if (msg == "[]") {
        parent.ShowMsg("加载异常,请先上传水印图!");
        return false;
    } else {
        msg = msg.replace('[', '').replace(']', '');
        var data = eval('(' + msg + ')');
        var xm = data.CN_S_VALUE;//获取水印设置
        var imgGuid = xm.match(/ID='(\S*)'/)[1];//后台返回水印图路径
        // $("#ImgPic").attr('src', imgGuid);
 
        watermarkguid = imgGuid.match(/{(\S*)}/)[1];    //获取水印图guid
        disType = xm.match(/Type='(\S*)'/)[1];        //获取显示方式
        bigNot = xm.match(/BigNot='(\S*)'/)[1];       //获取大图是否需要水印
        smallNot = xm.match(/SmallNot='(\S*)'/)[1];   //获取小图是否需要水印
        bigSize = xm.match(/bigSize='(\S*)'/)[1];       //获取大图尺寸
        smallSize = xm.match(/smallSize='(\S*)'/)[1]; //获取小图尺寸
    }
}
 
var CN_S_Class_Guid = "";
var CN_S_Fk_Class_Guid = "";
//获取ITEM对应的类GUID
function LoadClassGuid() {
    var ajaxUrl = "/ItemArchives/ItemVerson/MagicItemQueto";
    var ajaxData = { ITEM: "ITEM" };
    var msg = AjaxManagers(JSON.stringify(ajaxData), ajaxUrl);
 
    for (var i = 0; i < msg.LinkClassList.length; i++) {
        if (msg.LinkClassList[i].CN_S_FK_CLASS_NAME == "PICTURE") {
            CN_S_Class_Guid = msg.LinkClassList[i].CN_S_CLASS_GUID;
            CN_S_Fk_Class_Guid = msg.LinkClassList[i].CN_S_FK_CLASS_GUID;
        }
    }
}
 
var haveup = false;
function doHaveUp() {
    return haveup;
}