Source: emapAvatarUpload.js

(function() {
  var Plugin, _eventBind;
  var defaultPhoto = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAASABIAAD/4QBARXhpZgAATU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAAqACAAQAAAABAAABE6ADAAQAAAABAAABSAAAAAD/7QA4UGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAAA4QklNBCUAAAAAABDUHYzZjwCyBOmACZjs+EJ+/8AAEQgBSAETAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5+jp6vLz9PX29/j5+v/bAEMABgYGBgYGCgYGCg4KCgoOEw4ODg4TGBMTExMTGB0YGBgYGBgdHR0dHR0dHSMjIyMjIygoKCgoLS0tLS0tLS0tLf/bAEMBBwcHDAsMFAsLFC8gGiAvLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vL//dAAQAEv/aAAwDAQACEQMRAD8A6miiivpz89CiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooA//Q6miiivpz89CiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooA//R6miiivpz89CiiigAooooAKKKKACiiigAooooAKKw9S8Q6dpuUdvMkH8Ccn8fSuIvfGGo3BK2wEC+3J/OuOvjqVPRvU9LC5TXr6xVl3Z6kzKg3OQB6nis6XWNLh/1lzGPoc/yrxqe8urlt08rOfck1Xrz55u/sxPZpcNL/l5P7kewt4n0Rf8AluD9Af8ACo/+Eq0X/nqf++TXkNFY/wBrVeyOlcO4fu/w/wAj2JfE+iN/y3A+oP8AhV2LV9Mn/wBXcRn6nH868QpaqOb1OsURPhyi/hk/wPflZXG5SCPUUteEwXt3bNut5XQ+xNdNZeMb+AhbtRMvr0b8666WbU3pNWPOr8O1o605J/geoUVj6brun6mAsL7ZP7jcH8PWtivShUjNc0XdHh1aU6cuWaswoooqzMKKKKACiiigAooooAKKKKACiiigD//S6miiivpz89CiiigAooooAKKKKACiignHJoAZJJHDG0srBVUZJPQCvNta8VzXJa308mOLoX/ib6egqDxLrzX8ps7VsW6Hkj+Mj+npXJV4GOzBtunTenc+vynJ4xSrV1r0Xb/gikknJ5JpKKK8g+kCiiigAooooAKKKKACiiigBysykMpwR0IrudD8VvGy2upnch4EncfX2rhKK2oYidKXNBnLisHTxEeSoj39WV1DKcg8gilrzTwvr7W0i6ddtmJjhGP8J9Poa9Lr6jDYiNaPMj4LHYKeGqckvk+4UUUV0HGFFFFABRRRQAUUUUAFFFFAH//T6miiivpz89CiiigAooooAKKKKACuS8WaqbO0FnCcSz9cdl7/AJ9K60nAya8U1m+OoajLcZyudqf7o6f415+ZYj2dOy3Z7OSYNVq3NLaOv+RlUUUV8yfdBRRRQAUUUUAFFFFABRRRQAUUUUAFFFFAC16z4X1U6hZeTMczQYU+47GvJa2tAvzp+pxSk4Rzsf6H/A812YHEe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/0Opooor6c/PQooooAKKKKAMTxBqw0fTXuhgyH5Ywe7H/AA614RPPNdTNPOxd3OST3Ndx4+v/ADb+KwU/LAu5v95v/rfzrga8fGVeafL0R9XlWHUKSm92FFFFcZ6gUUUUAFLSUUAb1lrc0AEdwPMQd+4rpINRs58GOQA+h4Nee0tc9TDRlqtD18LnVakuWXvLzPXYtU1CIDy53wOgySKkfWdTkGDOw+nH8q8iWaVPuOR9DTjcXDcNIx/E1j9Tfc7v7dovV0dfl/kejXN6gJkupvm7lmya5671+NQUtBuP949PyrlCSetJWkMJFavU58Rn1WS5aS5USyzSTuZJWLMe5qKiiupI8OUm3dhRRRQIKKKKACvUPBGuyTE6RdNuKrmInrgdV/wry+r2m3jaffwXi/8ALJwx9x3H4itqFRwkmcuMw6rU3B79D6KopqOsiB0OQwyD7GnV7x8UFFFFABRRRQAUUUUAf//R6miiivpz89CiiigAoJAGT2orG8Q3f2LRbqcHB2FV+rfKP51MpWTZdODnJRXU8Q1W7N/qVxdk5Ejkj6dB+lZ9FFfPN3d2fdRiopRQUUUUigooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigD3XwnefbNDgZjloh5bf8B4H6Yro68z+Ht3/AMfViT6SKP0P9K9Mr3cPPmppnxePpclaUf61CiiitzkCiiigAooooA//0upooor6c/PQooooAK4H4gXXl6fBaA8yybj9FH+JFd9Xj3jy687V0tweIIwPxbk/piubFytTZ6GV0+auvLU4iiiivEPrwooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooA6fwfdfZdegycLKDGfxHH6gV7jXzdbTtbXMVynWJ1cfgc19HxuskayIchgCD7GvVwEvdcT5vO6dpxn3HUUUV3niBRRRQAUUUUAf/9PqaKKK+nPz0KKKKACvn3Xbn7XrF1PnIMjAfReB+gr6CPSvnC7/AOPub/fb+defmD0SPcyOK5psrUUUV5Z9GFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABXvfhm5+1aFayE5ITYf+AfL/SvBK9u8F/8i/D/ALz/APoRruwD99ryPHzqK9kn5nVUUUV6x8wFFFFABRRRQB//2Q==';
  /**
   * @module emapAvatarUpload
   * @alias 头像裁剪上传
   * @description 头像裁剪<br>
   * 依赖项:<br>
   * @example
   * <script src="http://res.wisedu.com/bower_components/cropper/cropper.min.js"></script> 
   * $container.emapAvatarUpload({
   *     contextPath: '/emap',
   *     size: 5120
   * })
   */
  Plugin = (function() {
    function Plugin(element, options) {
      this.options = $.extend({}, $.fn.emapAvatarUpload.defaults, options);
      this.$element = $(element);
      _init(this.$element, this.options);
    }

    /**
     * @method destroy
     * @description 销毁
     */
    Plugin.prototype.destroy = function() {
      this.options = null;
      $(this.$element).data('emapAvatarUpload', false).empty();
    };
    Plugin.prototype.getValue = function() {};

    /**
     * @method disable
     * @description 禁用
     */
    Plugin.prototype.disable = function() {
      $('.bh-emapAvartar-btn', this.$element).hide()
    };

    /**
     * @method disable
     * @description 启用
     */
    Plugin.prototype.enable = function() {
      $('.bh-emapAvartar-btn', this.$element).show()
    };

    /**
     * @method save
     * @description 保存附件
     */
    Plugin.prototype.save = function() {
      if (this.options.fileId) {
        var result = deleteFileByToken(this.options).success && saveAttachment(this.options).success;
        delete this.options.fileId;
        return result;
      }
    };
    return Plugin;
  })();


  //生成dom
  function _init(element, options) {
    // contextPath 兼容
    options.contextPath = options.contextPath || WIS_EMAP_SERV.getContextPath();
    // 默认展示头像
    if (options.defaultAvatar === undefined) {
      options.defaultAvatar = defaultPhoto
    }
    if (options.token && options.token != null) {
      // 已有图片
      options.scope = options.token.substring(0, options.token.length - 1);
      // options.displayAvatar = options.contextPath + '/sys/emapcomponent/file/getFileByToken/' + options.token + '.do?date=' + new Date().getTime();
      options.displayAvatar = options.contextPath + '/sys/emapcomponent/file/getSingleImageByToken.do?fileToken=' + options.token + '&date=' + new Date().getTime();
      options.newToken = false;
    } else {
      // 新上传
      options.scope = new Date().getTime() + "" + parseInt(Math.random() * 100).toString();
      options.token = options.scope + 1;
      options.displayAvatar = options.defaultAvatar;
      options.newToken = true;
    }

    options.$wrap = $('<div class="bh-emapAvatar-wrap"><img class="bh-emapAvatar-avatar" src="' + options.displayAvatar + '"><a class="bh-emapAvartar-btn" href="javascript:void(0)">' + $.i18n('bh-au-adjustHeadImage') + '</a></div>')
      .css({
        'height': options.width / options.ratio,
        'width': options.width
      });

    if (options.disabled) $('.bh-emapAvartar-btn', options.$wrap).hide();
    element.append(options.$wrap);

    _eventBind(element, options);

  }

  _eventBind = function(element, options) {
    var $wrap = options.$wrap;
    // 点击修改头像
    $wrap.on('click', '.bh-emapAvartar-btn', function() {
      $.emapAvatarUploadWindow(element, options);
    });

    $('.bh-emapAvatar-avatar', element).one('error', function() {
      $(this).attr('src', options.defaultAvatar);
    });
  };


  $.fn.emapAvatarUpload = function(options, params) {
    var instance;
    instance = $(this).data('emapAvatarUpload');
    if (!instance) {
      return this.each(function() {
        if (options == 'destroy') {
          return this;
        }
        return $(this).data('emapAvatarUpload', new Plugin(this, options));
      });
    }
    if (options === true) return instance;
    if ($.type(options) === 'string') return instance[options](params);
    return this;
  };


  /**
   * @memberof module:emapAvatarUpload
   * @prop {String} [contextPath] - 根路径
   * @prop {Number} [ratio=1] - 图片宽高比
   * @prop {Int} [width=100] - 图片显示宽度
   * @prop {Int} [actualWidth] - 裁剪出图片的实际宽度
   * @prop {String} [token] - 文件token,不传则组件生成随机的新token, 传token则组件自动请求token下已有的文件信息并渲染到页面上
   * @prop {String} [storeId=image] - emap文件类型
   * @prop {Array} [type=['jpg', 'png', 'jpeg']] - 上传文件的格式要求
   * @prop {Int} [size=5120] - 上传文件的大小要求,单位为KB
   * @prop {Int} [disabled=false] - 是否禁用
   * @prop {Function} [onSubmit] - 头像裁剪成功的回调 参数为token
   * @prop {Object} [saveParams] - 保存请求的附带参数
   * @prop {Boolean} [autoSave=true] - 是否自动调用保存方法
   */
  $.fn.emapAvatarUpload.defaults = {
    storeId: 'image',
    contextPath: "",
    ratio: 1,
    width: 100,
    actualWidth: null, // 需要图片的实际尺寸
    type: ['jpg', 'png', 'jpeg'],
    size: 5120,
    disabled: false,
    autoSave: true,
    saveParams: {},
    replaceMode:true
  };

  $.emapAvatarUploadWindow = function(element, options) {
    // 兼容直接调用此方法导致默认参数没有传进来的问题
    options.autoSave = options.autoSave === undefined ? true : options.autoSave
    options.winContent = $('<div id="emapAvatarUploadWindow">' +
      '<ul style="display: none;">' +
      '<li>' + $.i18n('bh-au-uploadFromLocal') + '</li>' +
      '<li>' + $.i18n('bh-au-RecommondHeadImage') + '</li>' +
      '</ul>' +
      '<div>' +
      '<div class="bh-emapAvatar-local"></div>' +
      '<div class="bh-emapAcatar-error-msg bh-text-center" style="color: red;"></div>' +
      '</div>' +
      '<div>' +
      '</div>');

    var initUpload = function(input, options) {
      /***
       * emap 相关逻辑代码
       *
       */
      var fileReader = 'FileReader' in window;
      input.fileupload({
        url: options.uploadUrl,
        autoUpload: true,
        multiple: false,
        dataType: 'json',
        limitMultiFileUploads: 1,
        formData: {
          size: options.size,
          type: options.type,
          storeId: options.storeId
        },
        submit: function(e, data) {
          $('.bh-emapAcatar-error-msg', options.winContent).html('').hide();
          var file = data.files[0];
          $('.bh-emapAvatar-text-info', options.winContent).show();
          $('.bh-emapAvatar-text-error', options.winContent).hide();

          // 文件的大小 和类型校验
          if (options.type && options.type.length > 0) {
            if (!new RegExp(options.type.join('|').toUpperCase()).test(file.name.toUpperCase())) {
              $('.bh-emapAvatar-text-info', options.winContent).hide();
              $('.bh-emapAvatar-text-error', options.winContent).html($.i18n('bh-au-wrongType')).show();
              return false;
            }
          }

          if (fileReader && options.size) {
            if (file.size / 1024 > options.size) {
              $('.bh-emapAvatar-text-info', options.winContent).hide();
              $('.bh-emapAvatar-text-error', options.winContent).html($.i18n('bh-au-passTheMax')).show();
              return false;
            }
          }
          $('.bh-emapAvatar-loader', options.winContent).jqxLoader('open');

        },
        done: function(e, data) {
          var file = data.files[0];
          var checkResult = true;
          // options增加add回调,用来在提交前检查图像.  --add by jbxu from 学工 2018-11-02 
          if(options.add && (typeof options.add === 'function')){
            checkResult = options.add(data.result);
          }
          if (data.result.success && checkResult) {
            // 上传成功
            $('.bh-avatar-img', options.winContent).attr('src', data.result.tempFileUrl).cropper('destroy');
            initCrop($('.bh-avatar-img', options.winContent), options);
            $('.bh-avatar-img-block', options.winContent).show();
            // 上传成功后删除原有的 临时文件图片
            deleteTempFile(options);

            options.fileId = data.result.id;
          } else {
            // 上传失败
            $('.bh-emapAcatar-error-msg', options.winContent).html($.i18n('bh-au-imageUploadFail')).show();
          }
          $('.bh-emapAvatar-loader', options.winContent).jqxLoader('close');
        },
        fail: function(e, data) {
          var file = data.files[0];
          $('.bh-emapAvatar-loader', options.winContent).jqxLoader('open');
          $('.bh-emapAcatar-error-msg', options.winContent).html($.i18n('bh-au-imageUploadFail')).show();
        }
      });
    };

    options.uploadUrl = options.contextPath + '/sys/emapcomponent/file/uploadTempFile/' + options.scope + '/' + options.token + '.do';


    $('.bh-emapAvatar-local', options.winContent).html('<div class="bh-emapAvatar-editArea" bh-avatar-role="editArea">' +
      '<div class="bh-emapAvatar-upload-block">' +
      '<a href="javascript:void(0)" class="bh-btn bh-btn-default bh-emapAvatar-upload">' +
      '<i class="iconfont icon-add"></i>' + $.i18n('bh-au-chooseImages') +
      '<input type="file">' +
      '</a>' +
      '<p class="bh-text-caption bh-color-caption bh-text-center bh-emapAvatar-text-info"></p>' +
      '<p class="bh-text-caption bh-color-danger bh-text-center bh-emapAvatar-text-error"></p>' +
      '</div>' +
      '<div class="bh-avatar-img-block">' +
      '<img class="bh-avatar-img" src="' + options.displayAvatar + '" style="display: none;">' +
      '<p ><a href="javascript:void(0)" class="bh-emapAvatar-reUpload">' + $.i18n('bh-au-reupload') + '</a> ' +
      //  旋转功能暂未提供
      //'| <a href="javascript:void(0)"><i class="iconfont icon-refresh"></i>90°旋转</a>' +
      '</p>' +
      '</div>' +
      '<div class="bh-emapAvatar-loader"></div>' +
      '</div>' +
      '<div class="bh-emapAvatar-display">' +
      '<div class="bh-emapAvatar-preview-100 bh-emapAvatar-preview"><div class="bh-emapAvatar-preview-div"></div></div>' +
      '<p class="bh-mb-8"></p>' +
      '<div>' +
      '<div class="bh-emapAvatar-preview-40 bh-pull-left bh-emapAvatar-preview" style="margin-right: 8px;"><div class="bh-emapAvatar-preview-div"></div></div>' +
      '<div class="bh-emapAvatar-preview-40 bh-pull-left bh-emapAvatar-preview" style="border-radius: 50%;"><div class="bh-emapAvatar-preview-div"></div></div>' +
      '<p class="bh-clearfix bh-mb-8"></p>' +
      '<div class="bh-emapAvatar-preview-28 bh-pull-left bh-emapAvatar-preview" style="margin-right: 8px;"><div class="bh-emapAvatar-preview-div"></div></div>' +
      '<div class="bh-emapAvatar-preview-28 bh-pull-left bh-emapAvatar-preview" style="border-radius: 50%;"><div class="bh-emapAvatar-preview-div"></div></div>' +
      '<p class="bh-clearfix bh-mb-8"></p>' +
      '</div>' +
      '</div>' +
      '');

    $('.bh-avatar-img', options.winContent).one('error', function() {
        $(this).attr('src', options.defaultAvatar);
      })
      .on('load', function() {
        initCrop($(this), options);
      }); // 初始化裁剪插件

    BH_UTILS.bhWindow(options.winContent, $.i18n('bh-au-uploadHeadImage'), undefined, {
      width: '560px',
      close: function() {
        $('.bh-avatar-img', options.winContent).cropper('destroy');
      }
    }, function() {
      return submitEdit(options);
    });
    // 预览窗口 尺寸和文字渲染
    $('.bh-emapAvatar-preview', options.winContent).each(function() {
      var width = $(this).width();
      var height = parseInt($(this).width() / options.ratio);
      var p = $(this).next('p');
      $(this).css({
        height: height
      });
      if (p.length > 0) {
        p.html(width + ' * ' + height + 'px');
      }
    });

    // 上传说明渲染
    var typeStr = options.type.join('、').toUpperCase();
    var sizeStr = (options.size > 1024) ? (options.size / 1024).toFixed(1) + 'M' : options.size + 'K';
    $('.bh-emapAvatar-text-info', options.winContent).html($.i18n('bh-au-onlyHave') + typeStr + ',' + $.i18n('bh-be-sizeNotPass') + sizeStr);

    // 初始化tab  添加推荐头像功能时放开注释
    //$('#emapAvatarUploadWindow').jqxTabs({ width: '100%', height: 200, position: 'top'});

    //初始化loader
    $('.bh-emapAvatar-loader', options.winContent).jqxLoader({});

    // 初始化 上传控件
    initUpload($('input[type=file]', options.winContent), options)

    // 重新上传事件绑定
    $('.bh-emapAvatar-reUpload', options.winContent).on('click', function() {
      $('input[type=file]', options.winContent).click();
    });

    function initCrop(ele, options) {
      ele.cropper({
        aspectRatio: options.ratio,
        viewMode: 1,
        movable: false,
        zoomOnWheel: false,
        preview: '.bh-emapAvatar-preview'
      });
    }

    function submitEdit(options) {
      if (options.fileId) {
        var cutResult = cutTempFile(options);
        if (cutResult.success && deleteTempFile(options).success) {
          if (options.autoSave && deleteFileByToken(options).success && saveAttachment(options).success) {
            options.onSubmit && options.onSubmit(options.token);
            $('.bh-emapAvatar-avatar', element).attr('src', options.contextPath + '/sys/emapcomponent/file/getSingleImageByToken.do?fileToken=' + options.token + '&date=' + new Date().getTime());
            options.displayAvatar = options.contextPath + '/sys/emapcomponent/file/getSingleImageByToken.do?fileToken=' + options.token + '&date=' + new Date().getTime();
            return true;
          } else {
            // 调整了onSubmit触发的位置,避免保存成功后临时文件取不到的问题
            $('.bh-emapAvatar-avatar', element).attr('src', cutResult.tempFileUrl);
            $('.bh-emapAvatar-avatar', element).one('load', function() {
              options.displayAvatar = cutResult.tempFileUrl;
              options.onSubmit && options.onSubmit(options.token);
            })
          }
        } else {
          if (!cutResult.success) {
            $('.bh-emapAcatar-error-msg', options.winContent).html(cutResult.error ? cutResult.error : $.i18n('bh-au-avatarFail')).show();
            return false;
          }
        }
      } else {
        if (options.newToken) {
          options.onSubmit && options.onSubmit();
        } else {
          options.onSubmit && options.onSubmit(options.token);
        }
      }
    }

    // 裁剪图片
    function cutTempFile(options) {
      var cutData = $('.bh-avatar-img', options.winContent).cropper('getData', true);
      if (options.actualWidth) {
        cutData.actualWidth = options.actualWidth;
        cutData.actualHeight = parseInt(options.actualWidth / options.ratio);
      }
      return doRequest(
        options.contextPath + '/sys/emapcomponent/file/cutTempFile/' + options.scope + '/' + options.token + '/' + options.fileId + '.do',
        cutData
      );
    }

    // 删除原有的临时文件
    function deleteTempFile(options) {
      if (options.fileId) {
        return doRequest(
          options.contextPath + '/sys/emapcomponent/file/deleteTempFile/' + options.scope + '/' + options.token + '/' + options.fileId + '.do', {}
        );
      }
      return {
        success: true
      };
    }
  };

  function deleteFileByToken(options) {
    return doRequest(
      options.contextPath + '/sys/emapcomponent/file/deleteFileByToken/' + options.token + '.do', {}
    );
  }

  function saveAttachment(options) {
    var saveParams = $.extend({}, {
      attachmentParam: JSON.stringify({
        storeId: options.storeId
      })
    }, options.saveParams);
    return doRequest(options.contextPath + '/sys/emapcomponent/file/saveAttachment/' + options.scope + '/' + options.token + '.do', saveParams);
  }

  function doRequest(url, param) {
    var result;
    $.ajax({
      type: 'post',
      data: param,
      url: url,
      dataType: 'json',
      async: false
    }).done(function(res) {
      result = res;
    }).fail(function(res) {
      $('.bh-emapAcatar-error-msg').html($.i18n('bh-au-requestFail')).show();
      result = res;
    });
    return result;
  }

}).call(undefined);