| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791 |
- /**
- *upload
- *
- */
- $(function() {
- (function($) {
- "use strict";
- $.fn.uploadFile = function(options) {
- var bool_validation = true; //是否要初始化validation
- if (options.validation == "null") {
- bool_validation = false;
- /* 改,去掉 "",去掉重复的,去掉大写的。Lin
- * 相应的 <ul.ss type='"jpg","png"' 也改为 "jpg,png" 了
- options.validation =
- '"JPG","JPEG","PNG","TIF","TIFF","GIF","MP3","MP4","HTML","HTM","SWF","PPT","PPTX","DOC","DOCX","PDF","XLS","XLSX","ZIP","RAR","BMP","flv","swf","mkv","avi","rm","rmvb","mpeg","mpg","ogg","ogv","mov","wmv","mp4","webm","mp3","wav","mid","png","jpg","jpeg","tif","tiff","gif","bmp","flv","swf","mkv","avi","rm","rmvb","mpeg","mpg","ogg","ogv","mov","wmv","mp4","webm","mp3","wav","mid","rar","zip","tar","gz","7z","bz2","cab","iso","doc","docx","xls","xlsx","ppt","pptx","pdf","txt","md","xml","png","jpg","jpeg","gif","bmp","flv","swf","mkv","avi","rm","rmvb","mpeg","mpg","ogg","ogv","mov","wmv","mp4","webm","mp3","wav","mid","rar","zip","tar","gz","7z","bz2","cab","iso","doc","docx","xls","xlsx","ppt","pptx","pdf","txt","md","xml"'; //用来验证文件类型
- */ options.validation = "jpg,jpeg,png,tif,tiff,gif,bmp,"+
- "mp3,wav,ogg,mid,"+
- "mp4,mov,wmv,webm,ogv,flv,swf,mkv,avi,rm,rmvb,mpeg,mpg,"+
- "html,htm,doc,docx,xls,xlsx,ppt,pptx,pdf,txt,md,xml,"+
- "zip,rar,tar,gz,7z,bz2,cab,iso"; //用来验证文件类型
- }
- //console.log(options.validation);
- var uploadParam = {
- app: wd.app?wd.app.name:getAccountInfo().xmmc,
- /* 再改,规范命名。Lin
- * 去掉 ?wdApplication=,不支持多个应用 -- 服务名可以写 ss.xxx
- * &wdService= 改为 ssServ
- action: "/service?wdApplication=wd&wdService=ulByA&type="+ options.type, // =uploadForAjax",。"表单的action"。Lin
- */ action: "/service?ssServ=ulByHttp&type="+ options.type, // =uploadForAjax",。"表单的action"。Lin
- validation: null,
- size: null, //文件大小 单位KB
- name: null,
- callback: null,
- imageCropping: true,
- beforeClick:null //点击触发前事件 如果返回false不触发click,wd.edit.initPhoto
- },
- settings = $.extend({}, uploadParam, options),
- formId = "uploadform";
- $(document.body).on("change", "[id='" + settings.name + "Edit_File']", onChange);
- //console.log(settings.validation);
- return this.each(function() {
- var name = settings.name;
- //var $this = $('#'+name);//Edit
- // $("#" + name + "Edit").on("click", onClick);
- $(this).on("click", onClick);
- /* 再改回 wj,支持 "sltwj"。Lin
- * 增加 wjInputVal
- if ($('input[name="' + name + 'BaseName"]').val() == "") { // + 'wj"]').。Lin
- */
- var wjInputVal = $('input[name="'+ name +'wj"]').val();
- if (wjInputVal == "") {
- $("[id='qx" + name+"']").hide();
- $("[id='check" + name+"']").hide();
- } else {
- /* 再改回 wj,支持 "sltwj"。Lin
- * 增加 wjInputVal
- $('[id=\'' + name + 'BaseName\']').html($('input[name="' + name + 'BaseName"]').val().substr($('input[name="' + name + 'BaseName"]') // + 'Name\']').,+ 'wj"]').,+ 'wj"]')。Lin
- .val().lastIndexOf('/') + 1));
- */ $('[id=\''+ name +'BaseName\']').html(wjInputVal.substr(wjInputVal.lastIndexOf('/') + 1)); // + 'Name\']').。Lin
- }
- //var id = $this.attr("id");
- /* 再改回 wj,支持 "sltwj"。Lin
- * 增加 wjInputVal
- var path = $('input[name="' + name + 'BaseName"]').val(); // + 'wj"]').。Lin
- */ var path = wjInputVal;
- formId = name + "uploadform";
- /* 改,改为 id="xxxEdit"。Lin
- var multiple = $('input[name="' + name + 'Edit"]').attr("multiple");
- */ var multiple = $('[id="'+ name +'Edit"]').attr("multiple");
- //产生新的隐藏form用来上传文件
- var form = "<div hidden='hidden' id='" + name + "uploadformDiv' name='uploadformDiv' ><form id='" + name +
- "uploadform' action=" + settings.action + " method='post' enctype='multipart/form-data' >";
- if (path) {
- form += "<input type='hidden' name='path' id='" + name + "path' value='" + path + "'/>";
- }
- form += "<input type='file' name='fileEdit' id='" + name + "Edit_File' fileid='" + name + "' ";
- if (multiple) {
- form += "multiple";
- }
- form += "/>";
- form += "<input type='hidden' name='application' id='" + settings.app + "'/>";
- form += "</form></div>";
- $("body").append(form);
- });
- function onClick() {
- if(settings.beforeClick){
- if(false==settings.beforeClick.call(this)){
- return ;
- }
- }
- //var id = $(this).attr("id");
- var name = settings.name;
- var path = $("input[name='" + name + "wj']").val();
- $("[id='" + name + "path']").val(path);
- $("[id='" + name + "Edit_File']").click();
- }
- //console.log(this.files[0].name);
- function onChange(e, finalsubmit) {
- var THIS = this;
- var filesLength = this.files.length; //用来判断是否选择文件
- var fileSize = Math.round(this.files[0].size / 1024 * 100) / 100;
- if (filesLength > 0) {
- if (settings.validation != undefined && settings.validation != 'null' && settings.validation != null &&
- settings.validation.length > 0) {
- var fileExt = this.files[0].name.replace(/.+\./, "").toLowerCase();
- //console.log(fileExt);
- var flag = false;
- var filetype = settings.validation.split(",");
- //var filetype = settings.validation
- $.each(filetype, function() {
- /* 改,.validation 去掉 ""、去掉重复的、去掉大写的。Lin
- * 相应的 <ul.ss type='"jpg","png"' 也改为 "jpg,png" 了
- if (this.toLowerCase() == ("\"" + fileExt + "\"")) {
- */ if (this == fileExt) {
- flag = true;
- return false;
- }
- });
- if (!flag) {
- if (bool_validation) {
- alert("请上传" + settings.validation + "格式文件!");
- } else {
- alert("请不要上传" + fileExt + "格式文件!");
- }
- return;
- }
- }
- if (fileSize > settings.size && settings.size > 0) {
- alert("上传文件大于" + settings.size + "KB");
- return;
- }
- settings.onchange && settings.onchange.call(this, settings);
- var progressHtml = '<div class="progress"><div class="bar"></div ><div class="percent">0%</div ></div>', //进度条HTML
- fileid = $(this).attr("fileid"), //获取button的id
- $fileName = $("[id='"+ fileid +"BaseName']"), // + "Name']"),。Lin
- defaultOption = { //默认的参数,可进行自定义
- type: 'post',
- dataType: 'json',
- beforeSubmit: function() {
- //console.log('beforeSubmit');
- $('[id=\'' + fileid+'\']').attr('disabled', 'disabled');
- $('[id=\'' + fileid+'\']').attr('class', 'disabled-button');
- },
- beforeSend: function() {
- //console.log('beforeSend');
- $('[id=\'' + fileid + 'BaseName\']').empty(); // + 'Name\']').。Lin
- $('[id=\'' + fileid + 'BaseName\']').append(progressHtml); // + 'Name\']').。Lin
- var percentVal = '0%';
- $('.bar').width(percentVal)
- $('.percent').html(percentVal);
- },
- uploadProgress: function(event, positon, total, percentComplete) { //进度条
- // console.log('uploadProgress');
- var percentVal = percentComplete + '%';
- $('.bar').width(percentVal)
- $('.percent').html(percentVal);
- },
- success: function(result) { //ajax上传成功
- var fileNames = "";
- var paths = "";
- var fileList = result.fileList;
- // console.log('result.size=' + result.fileList);
- for (var i = 0; i < fileList.length; i++) {
- fileNames += fileList[i].fileName;
- paths += fileList[i].path;
- if (i < fileList.length - 1) {
- fileNames += ";";
- paths += ";";
- }
- }
- //console.log('fileNames:'+fileNames);
- //console.log('paths:'+paths);
- // console.log('success');
- var percentVal = '100%';
- $('.bar').width(percentVal)
- $('.percent').html(percentVal);
- $('[id=\'' + fileid+'\']').removeAttr('disabled');
- $('[id=\'' + fileid+'\']').attr('class', 'list-button');
- /* 改,<span id="xxxBaseName"。Lin
- $('[id=\'' + fileid + 'Name'+'\']').html(fileNames); //原取值方式为result.fileName 后改为result.fileList[0].fileName(各个环境底层不一样,需兼容)
- */ $('[id=\''+ fileid +'BaseName\']').html(fileNames); //原取值方式为result.fileName 后改为result.fileList[0].fileName(各个环境底层不一样,需兼容)
- /* 改,paths > fileNames。Lin
- * 再改回 wj,并用回 paths,支持 "sltwj"
- $('input[name="' + fileid + 'wj"]').val(paths); //原取值方式为result.path 后改为result.fileList[0].path
- */ $('input[name="'+ fileid +'wj"]').val(paths);
- /// 再增加,<input name="xxxBaseName" -- paths 使用 UUID 了。Lin
- $('input[name="'+ fileid +'BaseName"]').val(fileNames);
- ///
- /* 改,cmsAdd.ss.jsp 里加 <input name='wpath'>。Lin
- $('[id=\'' + fileid + "path"+'\']').val(paths); //原取值方式为result.path 后改为result.fileList[0].path
- */
- /* 再去掉,共用 wj。Lin
- $('input[name="'+ fileid +'FileName"]').val(paths); //原取值方式为result.path 后改为result.fileList[0].path
- */
- $("[id='qx" + fileid+'\']').show();
- $("[id='check" + fileid+'\']').show();
- if (settings.callback != null && settings.callback != 'null' && settings.callback != undefined)
- settings.callback(result);
- },
- error: function(result) {
- //console.log('error');
- alert('上传失败');
- $('[id=\''+ fileid +'BaseName'+'\']').html(''); // + 'Name'+'\']').。Lin
- $('[id=\'' + fileid + 'Edit_File'+'\']').removeAttr('disabled');
- $('[id=\'' + fileid + 'Edit_File'+'\']').attr('class', 'list-button');
- //console.log(result);
- }
- },
- ajaxParam = $.extend({}, defaultOption, settings);
- //console.log(ajaxParam);
- if (ajaxParam.imageCropping && /\.(jpg|jpeg|png|)$/i.test(this.files[0].name)) {
- initCropper(THIS, ajaxParam, (function() {
- return function(result) {
- ajaxParam.success(result)
- }
- })());
- // bySPACE 20190612
- // 清空文件,防止多次上传同一张图片时无法被change捕获
- $(this).val("");
- } else {
- $('[id=\'' + formId+'\'').ajaxSubmit(ajaxParam); //调用from.js的ajax提交表单
- }
- }
- }
- };
- })(jQuery);
- });
- /**
- * 裁剪 :)
- * bySPACE 20190604
- */
- var wdCropper = {};
- function initWdCropper() {
- wdCropper = {
- topWin: undefined,
- layer: { // 弹层
- show: function(param) { // 弹出
- /* 再改,启用引入 .css、.js 语句 -- yx_zjz.js(迎新拍证件照) 时,会导致 resize( 里的 .Tdrag() 抛出异常 -- 没有引入 Tdrag.min.js。Lin
- * wdCropper.topWin.$("#space-cropper-panel").Tdrag();
- * wdCropper.topWin.$("#cropper").Tdrag();
- * 不能启用 wdCropper.layer.close(),会导致 上传 > 裁剪图片 时没显示裁剪窗口
- * 另,下面的 close( 里,也启用清除 .css、.js 语句
- //wdCropper.layer.close();
- //wdCropper.topWin.$("html").append('<link rel="stylesheet" type="text/css" href="/wd/js/Cropper/cropper-master/dist/cropper.css" />');
- //wdCropper.topWin.$("html").append('<script src="/wd/js/Cropper/cropper-master/dist/cropper.js"></script>');
- //wdCropper.topWin.$("html").append('<script src="/wd/js/Cropper/drag/Tdrag.min.js"></script>');
- */
- wdCropper.topWin.$("html").append('<link rel="stylesheet" type="text/css" href="/ss/Cropper/cropper-master/dist/cropper.css" />');
- wdCropper.topWin.$("html").append('<script src="/ss/Cropper/cropper-master/dist/cropper.js"></script>');
- wdCropper.topWin.$("html").append('<script src="/ss/Cropper/drag/Tdrag.min.js"></script>');
- var panelHtml =
- '<div id="space-cropper-panel" style="z-index:20190606;padding-top:5px;padding-bottom:5px;" class="space-cropper-panel floatToolbar-div">' +
- '<div id="zoom_btn" style="margin-left:5px;margin-bottom:5px;" class="space-cropper-panel-btn toolbar-button icon-zoomIn" onclick="wd.topWindow.wdCropper.cropper.zoom();" onmousedown="wd.topWindow.wdCropper.cropper.ZN_auto(`z`);" onmouseup="wd.topWindow.wdCropper.cropper.endFlag();" onmouseout="wd.topWindow.wdCropper.cropper.endFlag();" ></div>' +
- '<div class="space-cropper-panel-btn toolbar-button icon-zoomOut" style="margin-left:5px;margin-bottom:5px;" onclick="wd.topWindow.wdCropper.cropper.narrow();" onmousedown="wd.topWindow.wdCropper.cropper.ZN_auto(`n`);" onmouseup="wd.topWindow.wdCropper.cropper.endFlag();" onmouseout="wd.topWindow.wdCropper.cropper.endFlag();" ></div>' +
- '<span id="bl" class="bl hLine" style="display:inline-block;width:calc(100% - 2px);margin-left:1px;text-align:center;line-height:20px;margin-bottom:5px;"></span>' +
- '<div class="space-cropper-panel-btn toolbar-button icon-leftRotation" style="margin-left:5px;margin-bottom:5px;" onclick="wd.topWindow.wdCropper.cropper.rotate_left();" onmousedown="wd.topWindow.wdCropper.cropper.RL_auto(`l`);" onmouseup="wd.topWindow.wdCropper.cropper.endFlag();" onmouseout="wd.topWindow.wdCropper.cropper.endFlag();" ></div>' +
- '<div class="space-cropper-panel-btn toolbar-button icon-rightRotation" style="margin-left:5px;margin-bottom:5px;" onclick="wd.topWindow.wdCropper.cropper.rotate_right();" onmousedown="wd.topWindow.wdCropper.cropper.RL_auto(`r`);" onmouseup="wd.topWindow.wdCropper.cropper.endFlag();" onmouseout="wd.topWindow.wdCropper.cropper.endFlag();" ></div>' +
- '<span id="jd" class="jd hLine" style="display:inline-block;width:calc(100% - 2px);margin-left:1px;text-align:right;line-height:20px;margin-bottom:5px;" ></span>' +
- '<div class="space-cropper-panel-btn toolbar-button invertIcon-save" style="margin-left:8px;margin-bottom:5px;" onclick="wd.topWindow.wdCropper.cropper.getCroppedCanvas();"></button>' +
- '</div>';
- wdCropper.topWin.$("body").append('<div id="shade" class="space-shade"></div>');
- wdCropper.topWin.$("body").append(
- '<div id="cropper" style="display:none;z-index: 999;" class="space-layer lightDialog-div"><div id="showSize" class="area-msg" style="position: absolute; z-index: 999999999999999999999; top: 40px; right: 4px;height:19px;line-height:19px;"></div><div class="space-layer-head lightDialog-title"><span onclick="wd.topWindow.wdCropper.layer.full();" style="position:absolute;right:37px;top:10px;" class="icon-fullscreen"></span><span style="display:none;position:absolute;right:37px;top:10px;" onclick="wd.topWindow.wdCropper.layer.full();" class="icon-restore"></span><span class="icon-close" onclick="wd.topWindow.wdCropper.layer.close();" ></span></div><div id="space-layer-body" style="padding:0px;box-sizing:border-box;margin-left:10px;" class="space-layer-body"><img id="cropperImage" src="" />' +
- panelHtml + '</div></div>');
- wdCropper.topWin.$("body").append(
- '<div id="imgInfo" class="area-msg" style="display:none; position: absolute;z-index: 999999999999999999999; display: none;width:max-content;text-align:center;"><span style="display:inline-block;height:16px;line-height:16px;">W:<span id="width"></span></span><br><span style="display:inline-block;height:16px;line-height:16px;">H:<span id="height"></span></span></div>'
- );
- var cw = wdCropper.topWin.document.body.clientWidth,
- ch = wdCropper.topWin.document.body.clientHeight;
- if (!param) { // 默认全屏
- param = {
- width: cw - 20,
- height: ch - 20
- }
- } else { // 超出屏幕大小的话就默认全屏
- if (param.width > cw || param.height > ch) {
- param = {
- width: cw - 50,
- height: ch - 50
- }
- }
- }
- wdCropper.layer.resize({
- width: param.width + 20,
- height: param.height + 40
- })
- wdCropper.topWin.$("#cropper").fadeIn();
- },
- resize: function(size) { // 重置大小,最小不能小于200*200
- if (size) {
- if(size.width < 200) {
- size.width = 220;
- }
- if(size.height < 200) {
- size.height = 200;
- }
- }
- var c = wdCropper.topWin.$("#cropper");
- $(c).width(size.width + "px");
- $(c).height(size.height + "px");
- $(c).css({
- "top": ((wdCropper.topWin.document.body.clientHeight - size.height) / 2) + "px",
- "left": ((wdCropper.topWin.document.body.clientWidth - size.width) / 2) + "px"
- });
- var b = wdCropper.topWin.$("#space-layer-body");
- $(b).height(($(c).height() - 40) + "px");
- $(b).width(($(c).width() - 20) + "px");
- var p = wdCropper.topWin.$("#space-cropper-panel");
- var t = (($(c).height() / 2) - 210);
- t = t < 40 ? 50 : t;
- $(p).css({
- "top": t + "px",
- });
- wdCropper.topWin.$("#space-cropper-panel").Tdrag();
- wdCropper.topWin.$("#cropper").Tdrag();
- },
- fullIndex: 1,
- full: function(type) { // 全屏Tg
- if (wdCropper.layer.fullIndex == 1) {
- wdCropper.layer.fullScreen();
- wdCropper.layer.fullIndex = 2;
- wdCropper.topWin.$(".icon-fullscreen").hide();
- wdCropper.topWin.$(".icon-restore").show();
- } else {
- wdCropper.layer.exitScreen();
- wdCropper.layer.fullIndex = 1;
- // wdCropper.topWin.$(".icon-fullscreen").removeClass("icon-zoomOut");
- wdCropper.topWin.$(".icon-fullscreen").show();
- wdCropper.topWin.$(".icon-restore").hide();
- }
- },
- fullScreen: function() { // 全屏
- var el = wdCropper.topWin.document.documentElement;
- var rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullscreen;
- if (typeof rfs != "undefined" && rfs) {
- rfs.call(el);
- };
- return;
- },
- exitScreen: function() { // 退出全屏
- var d = wdCropper.topWin.document;
- if (d.exitFullscreen) {
- d.exitFullscreen();
- } else if (d.mozCancelFullScreen) {
- d.mozCancelFullScreen();
- } else if (d.webkitCancelFullScreen) {
- d.webkitCancelFullScreen();
- } else if (d.msExitFullscreen) {
- d.msExitFullscreen();
- }
- if (typeof cfs != "undefined" && cfs) {
- cfs.call(el);
- }
- },
- close: function() { // 关闭
- wdCropper.topWin.$("#shade").remove();
- wdCropper.topWin.$("#cropper").remove();
- wdCropper.topWin.$("#imgInfo").remove();
- /* 再改,启用清除 .css、.js 语句 -- 上面的 show(,启用了引入 .css、.js 语句。Lin
- //wdCropper.topWin.$("html link[href='/wd/js/Cropper/cropper-master/dist/cropper.css']").remove();
- //wdCropper.topWin.$("html script[src='/wd/js/Cropper/cropper-master/dist/cropper.js']").remove();
- //wdCropper.topWin.$("html script[src='/wd/js/Cropper/drag/Tdrag.min.js']").remove();
- */
- wdCropper.topWin.$("html link[href='/ss/Cropper/cropper-master/dist/cropper.css']").remove();
- wdCropper.topWin.$("html script[src='/ss/Cropper/cropper-master/dist/cropper.js']").remove();
- wdCropper.topWin.$("html script[src='/ss/Cropper/drag/Tdrag.min.js']").remove();
- wd.topWindow.$("#cropiframe").hide();
- }
- },
- cropper: {
- fileName: null,
- $image: undefined, // cropper实例,由init时赋值
- changeflag: false, // 是否被拖动标记,如被拖动为真,否则为假
- autoSizeFlag: 0, // 累计加载完毕次数
- imageURL: undefined, // BASE64 图像
- startInterval: undefined, // 连续操作的定时器对象
- startIntervalType: undefined, // 连续操作的类型
- rotatingNumber: 0, // 旋转度数
- img: undefined,
- init: function() {
- // 通过指定的宽和高计算裁剪框比例,出现小数向上取整
- var _widthRatio = 0,
- _heightRatio = 0,
- ratioFlag = false;
- if (wdCropper.settings.width && wdCropper.settings.height) {
- ratioFlag = true;
- _widthRatio = (wdCropper.settings.width / 100).toFixed(2);
- _heightRatio = (wdCropper.settings.height / 100).toFixed(2);
- }
- wdCropper.cropper.$image = wdCropper.topWin.$("#cropperImage");
- wdCropper.cropper.$image.cropper({
- aspectRatio: ratioFlag ? _widthRatio / _heightRatio : undefined,
- viewMode: 0, // 视点模式:【0:无限制;1:不能超出裁剪区域;】
- dragMode: 'move', // 拖曳模式:'crop'*创建一个新的麦田盒,'move'*移动画布,'none'*无所事事
- modal: true, // 是否显示黑色遮罩
- guides: true, // 显示麦田盒上方的虚线。
- center: true, // 显示位于“作物”框上方的中心指示器。
- highlight: true, // 在“作物”框上方显示白色模式(突出显示“作物”框)。
- background: true, // 是否显示透明背景
- movable: false, // 是否可以移动图像
- rotatable: true, // 可旋转
- scalable: true, // 可伸缩
- zoomable: true, // 可缩放
- responsive: true, // 调整窗口大小时,重新渲染裁剪器
- zoomOnWheel: false, // 通过移动鼠标使图像能够缩放
- wheelZoomRatio: 0.1, // 定义缩放比率时,通过鼠标旋转放大图像。(滚动时放大或缩小的比例)
- cropBoxMovable: true, // 通过拖动启用移动“裁剪”框。
- CropBoxResizable: false, // 可以通过拖动调整裁剪框的大小
- crop: function(event) { // 移动开始时
- try {
- if (!wdCropper.cropper.changeflag) {
- wdCropper.topWin.$("#imgInfo").show();
- }
- var offset = wdCropper.topWin.$(".cat").offset();
- if (offset != undefined) {
- wdCropper.topWin.$("#imgInfo").css({
- "left": (offset.left + 30) + "px",
- "top": (offset.top - 9) + "px"
- });
- }
- var d = wdCropper.cropper.$image.data('cropper').getData();
- wdCropper.topWin.$("#width").html(Math.round(d.width));
- wdCropper.topWin.$("#height").html(Math.round(d.height));
- if (!wdCropper.settings.width) {
- wdCropper.topWin.$("#showSize").html("" + Math.round(d.width) + " X " + Math.round(d.height) + "");
- }
- // 计算比例
- wdCropper.cropper.getRatio();
- } catch (e) {
- console.log(e);
- }
- return false;
- },
- cropend: function() { // 移动停止时
- wdCropper.topWin.$("#imgInfo").hide();
- wdCropper.cropper.changeflag = false;
- },
- ready: function() { // 渲染完毕时
- wd.topWindow.wdCropper = wdCropper;
- wdCropper.cropper.initDragStyle();
- wdCropper.topWin.$("#imgInfo").hide();
- // 图片小于200*100就会被Cropper放大,接下来要做的就是把图片还原到原大小
- var cd = wdCropper.cropper.$image.data('cropper').getCanvasData(); // 当前画布中的图片大小
- var img = wdCropper.cropper.$image.data('cropper').getImageData(); // 图片实际大小
- wdCropper.cropper.img = img;
- if (img.width < 200 || img.height < 200) {
- wdCropper.cropper.$image.data('cropper').setCanvasData({
- width: img.naturalWidth,
- height: img.naturalHeight,
- left: (200 - img.naturalWidth) / 2,
- top: (200 - img.naturalHeight) / 2,
- });
- }
- if (img) {
- wdCropper.cropper.getRatio();
- }
- // setting中没有指定裁剪宽和高,就设置裁剪框和图片一样大
- if (!wdCropper.settings.width) {
- var cd2 = wdCropper.cropper.$image.data('cropper').getCanvasData(); // 当前画布中的图片大小
- wdCropper.cropper.$image.data('cropper').setCropBoxData({
- "width": cd2.width,
- "height": cd2.height
- })
- }
- // 解决部分屏幕初始化后裁剪框无法居中的问题
- var w = wdCropper.cropper.$image.data('cropper').getContainerData().width;
- wd.topWindow.wdCropper.cropper.$image.data('cropper').setCropBoxData({
- left: (w - wd.topWindow.wdCropper.cropper.$image.data('cropper').getCropBoxData().width) / 2
- });
- // setTimeout(function() {
- // }, 500);
- }
- });
- },
- keepTwoDecimal: function(num) { // 四舍五入,保留一位小数
- return Math.round(num * 10) / 10;
- },
- replace: function replace(url) { // 重新渲染裁剪框
- wdCropper.cropper.imageURL = url;
- wdCropper.cropper.$image.data('cropper').replace(url, false);
- },
- zoom: function() { // 放大
- wdCropper.cropper.changeflag = true;
- var cd = wdCropper.cropper.$image.data('cropper').getCanvasData(); // 当前画布中的图片大小
- var img = wdCropper.cropper.$image.data('cropper').getImageData(); // 图片实际大小
- if (cd.width >= img.naturalWidth || cd.height >= img.naturalHeight) {
- wdCropper.topWin.$("#zoom_btn").removeClass("icon-zoomIn").addClass("disableIcon-zoomIn");
- return false;
- }
- wdCropper.cropper.$image.cropper("zoom", 0.01);
- },
- narrow: function() { // 缩小
- wdCropper.cropper.changeflag = true;
- wdCropper.cropper.$image.cropper("zoom", -0.01);
- wdCropper.topWin.$("#zoom_btn").removeClass("disableIcon-zoomIn").addClass("icon-zoomIn");
- },
- ZN_auto: function(type) { // 连续放大或缩小
- window.clearInterval(wdCropper.cropper.startInterval);
- type = wdCropper.cropper.startIntervalType = type;
- wdCropper.cropper.startInterval = setInterval(function() {
- if (type.trim() == 'z') {
- wdCropper.cropper.zoom();
- } else {
- wdCropper.cropper.narrow();
- }
- }, 100);
- },
- rotate_left: function() { // 逆时针
- wdCropper.cropper.rotatingNumber--;
- wdCropper.cropper.changeflag = true;
- wdCropper.cropper.$image.cropper("rotate", -1);
- wdCropper.topWin.$("#jd").html(wdCropper.cropper.rotatingNumber + "°");
- },
- rotate_right: function() { // 顺时针
- wdCropper.cropper.rotatingNumber++;
- wdCropper.cropper.changeflag = true;
- wdCropper.cropper.$image.cropper("rotate", 1);
- wdCropper.topWin.$("#jd").html(wdCropper.cropper.rotatingNumber + "°");
- },
- endFlag: function() { // 结束循环
- window.clearInterval(wdCropper.cropper.startInterval);
- },
- RL_auto: function(type) { // 连续旋转
- window.clearInterval(wdCropper.cropper.startInterval);
- type = wdCropper.cropper.startIntervalType = type;
- wdCropper.cropper.startInterval = setInterval(function() {
- if (type.trim() == 'l') {
- wdCropper.cropper.rotate_left();
- } else {
- wdCropper.cropper.rotate_right();
- }
- }, 100);
- },
- getRatio: function() { // 获取图片缩放比例
- try {
- var id = wdCropper.cropper.$image.data('cropper').getImageData();
- //console.log("id=" + id)
- if (id && id.width) {
- var bl = wdCropper.cropper.keepTwoDecimal(id.width / wdCropper.cropper.img.width);
- wdCropper.topWin.$("#bl").html(bl == 1 ? "1:1" : bl);
- }
- } catch (e) {
- console.log(e);
- }
- },
- initDragStyle: function() { // 初始化裁剪框
- wdCropper.topWin.$("span.line-e").remove();
- wdCropper.topWin.$("span.line-n").remove();
- wdCropper.topWin.$("span.line-w").remove();
- wdCropper.topWin.$("span.line-s").remove();
- wdCropper.topWin.$("span.point-e").remove();
- wdCropper.topWin.$("span.point-n").remove();
- wdCropper.topWin.$("span.point-w").remove();
- wdCropper.topWin.$("span.point-s").remove();
- wdCropper.topWin.$("span.point-ne").remove();
- wdCropper.topWin.$("span.point-nw").remove();
- wdCropper.topWin.$("span.point-sw").remove();
- wdCropper.topWin.$("span.point-se").addClass("cat plugin-drag");
- wdCropper.topWin.$("span.point-se").mousemove(function() {
- wdCropper.cropper.changeflag = false;
- });
- wdCropper.topWin.$(".cropper-modal").addClass("dialog-mask");
- wdCropper.topWin.$(".cropper-view-box").addClass("area-div");
- if (!wdCropper.settings.width) {
- var d = wdCropper.cropper.$image.data('cropper').getData();
- wdCropper.topWin.$("#showSize").html("" + Math.round(d.width) + " X " + Math.round(d.height) + "");
- } else {
- wdCropper.topWin.$("#showSize").html("" + wdCropper.settings.width + " X " + wdCropper.settings.height + "");
- }
- wdCropper.topWin.$("#jd").html("0°");
- },
- getCroppedCanvas: function() { // 获取裁剪图片
- var myForm = undefined; // 最终上传的表单
- // 新增需求:裁剪框和图片大小一致的情况下,直接上传原图,不再进行裁剪操作
- var canvasData = wdCropper.cropper.$image.data('cropper').getCanvasData(); // 画布大小
- var cropBoxData = wdCropper.cropper.$image.data('cropper').getCropBoxData(); // 裁剪框大小
- if (canvasData.width == cropBoxData.width && canvasData.height == cropBoxData.height) {
- //console.log("原图上传");
- var myForm = new FormData();
- myForm.append("application", "");
- myForm.append("fileEdit", wdCropper.fileObj);
- } else {
- var hce = wdCropper.cropper.$image.data('cropper').getCroppedCanvas({
- width: wdCropper.settings.width,
- height: wdCropper.settings.height,
- fillColor: '#FFF'
- });
- var src = hce.toDataURL("image/jpeg");
- var dataURLtoBlob = function(dataurl) {
- var arr = dataurl.split(','),
- mime = arr[0].match(/:(.*?);/)[1],
- bstr = atob(arr[1]),
- n = bstr.length,
- u8arr = new Uint8Array(n);
- while (n--) {
- u8arr[n] = bstr.charCodeAt(n);
- }
- return new Blob([u8arr], {
- type: mime
- });
- }
- var newFile = new File([dataURLtoBlob(src)], wdCropper.cropper.fileName);
- wdCropper.fileEle.files[0] = newFile;
- var myForm = new FormData();
- myForm.append("application", "");
- myForm.append("fileEdit", newFile);
- }
- //console.log(wdCropper.settings.action);
- $.ajax({
- url: wdCropper.settings.action,
- type: "POST",
- data: myForm,
- processData: false, // 告诉jQuery不要去处理发送的数据
- contentType: false, // 告诉jQuery不要去设置Content-Type请求头
- error: function() {
- wdCropper.layer.close();
- setTimeout(function() {
- this.wdCropper = undefined;
- wd.topWindow.wdCropper = undefined;
- }, 1000);
- },
- success: function(result) {
- result = eval("(" + result + ")");
- if (wdCropper.cropConfirm) {
- wdCropper.cropConfirm.call(wdCropper.thisWindow, result)
- }
- wdCropper.layer.close();
- setTimeout(function() {
- this.wdCropper = undefined;
- wd.topWindow.wdCropper = undefined;
- }, 1000);
- }
- });
- }
- }
- }
- }
- //裁剪
- function initCropper(fileEle, settings, cropConfirm) {
- initWdCropper();
- wdCropper.fileEle = fileEle;
- var files = fileEle.files;
- if (files && files.length) {
- if (!!!/^image\/\w+$/.test(files[0].type)) {
- window.alert('Please choose an image file.');
- return
- }
- } else {
- return;
- }
- wdCropper.thisWindow = window;
- wdCropper.settings = settings;
- wdCropper.cropConfirm = cropConfirm;
- var file = files[0];
- wdCropper.cropper.fileName = file.name;
- wdCropper.fileObj = file;
- //生成裁剪窗口
- if(wd.topWindow){
- wd.topWindow.$("#cropiframe").remove();
- }else{
- wd.topWindow = top;
- }
- var cropiframe = $(
- "<iframe style=\"width:100%;height:100%;position:absolute;z-index:1000;display:none;\" id=\"cropiframe\" src=\"/ss/Cropper/dialog.jsp\"></iframe>"
- )
- cropiframe.on("load", function() {
- wdCropper.topWin = this.contentWindow;
- var uploadedImageURL = URL.createObjectURL(file);
- var img = new Image();
- img.onload = function() {
- wdCropper.layer.show({
- width: img.width,
- height: img.height
- });
- wdCropper.cropper.init();
- wdCropper.cropper.replace(uploadedImageURL);
- }
- img.src = uploadedImageURL;
- }).appendTo(wd.topWindow.$("body")).show();
- }
- function uploadFileClean(id) {
- $('#' + id + 'Name').html('');
- $('input[name="' + id + 'wj"]').val('');
- $('#' + id + 'Edit_File').val('');
- $('#' + id + 'Edit_File').files = null;
- $('#' + id + 'path').val('');
- $("#qx" + id).hide();
- $("#check" + id).hide();
- };
|