|
@@ -0,0 +1,180 @@
|
|
|
+/**
|
|
|
+ * Create by sxd
|
|
|
+ * date 2018-04-13
|
|
|
+ * Description:
|
|
|
+ */
|
|
|
+var Photo = function(cfg) {
|
|
|
+
|
|
|
+ this.width = (cfg && cfg.width) || 150;
|
|
|
+ this.height = (cfg && cfg.height) || 120;
|
|
|
+ this.top = (cfg && cfg.height) || 10;
|
|
|
+ this.left = (cfg && cfg.height) || 20;
|
|
|
+ this.url = (cfg && cfg.url);
|
|
|
+ this.vdom = null;
|
|
|
+ this.inputDom = (cfg && cfg.dom) || null;
|
|
|
+
|
|
|
+ this.config = {
|
|
|
+ // server: null,
|
|
|
+ // fieldName: 'image',
|
|
|
+ // compress: true,
|
|
|
+ // width: 1600,
|
|
|
+ // height: 1600,
|
|
|
+ // quality: 80,
|
|
|
+ sizeLimit: 512 * 1024,// 512k
|
|
|
+ // upload: {
|
|
|
+ // url: null,
|
|
|
+ // headers: {},
|
|
|
+ // params: {},
|
|
|
+ // fieldName: {}
|
|
|
+ // },
|
|
|
+ compress: {
|
|
|
+ width: 1600,
|
|
|
+ height: 1600,
|
|
|
+ quality: 80
|
|
|
+ },
|
|
|
+ uploadHandler(responseText){
|
|
|
+ const json = JSON.parse(responseText);
|
|
|
+ return json.ok ? json.data : null
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+};
|
|
|
+
|
|
|
+Photo.prototype.renderToDom = function () {
|
|
|
+ this.vdom = $('<div class="outer-photo"></div>');
|
|
|
+ this.vdom({ width: this.width, height: this.height ,top: this.top, left: this.left })
|
|
|
+ this.renderPhotoDom();
|
|
|
+};
|
|
|
+
|
|
|
+Photo.prototype.renderPhotoDom = function () {
|
|
|
+ let image = $('<img class="question-photo" src='+ this.url +' />');
|
|
|
+ this.vdom.append(image);
|
|
|
+};
|
|
|
+
|
|
|
+Photo.prototype.process = function () {
|
|
|
+ const _this = this;
|
|
|
+ const config = this.config;
|
|
|
+ if (!config.upload && typeof config.server === 'string') {
|
|
|
+ config.upload = {url: config.server}
|
|
|
+ }
|
|
|
+ if (config.upload && !config.upload.url) {
|
|
|
+ config.upload = null
|
|
|
+ }
|
|
|
+ if (config.upload && typeof config.fieldName === 'string') {
|
|
|
+ config.upload.fieldName = config.fieldName
|
|
|
+ }
|
|
|
+
|
|
|
+ if (typeof config.compress === 'boolean') {
|
|
|
+ config.compress = {
|
|
|
+ width: config.width,
|
|
|
+ height: config.height,
|
|
|
+ quality: config.quality
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ const file = this.inputDom.files[0];
|
|
|
+
|
|
|
+ if (file.size > config.sizeLimit) {
|
|
|
+ alter('上传的图片不能大于' + config.sizeLimit);
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ this.inputDom.value = null;
|
|
|
+
|
|
|
+ if (config.compress) {
|
|
|
+ config.compress.fieldName = config.upload && config.upload.fieldName
|
|
|
+ ? config.upload.fieldName : 'image';
|
|
|
+ lrz(file, config.compress).then((rst) => {
|
|
|
+ if (config.upload) {
|
|
|
+ _this.uploadToServer(rst.file)
|
|
|
+ } else {
|
|
|
+ _this.insertBase64(rst.base64)
|
|
|
+ }
|
|
|
+ }).catch((err) => {
|
|
|
+ this.setUploadError(err.toString())
|
|
|
+ });
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!config.upload) {
|
|
|
+ const reader = new FileReader();
|
|
|
+ reader.onload = (e) => {
|
|
|
+ _this.insertBase64(e.target.result)
|
|
|
+ }
|
|
|
+ reader.readAsDataURL(file);
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 上传服务器
|
|
|
+ _this.uploadToServer(file)
|
|
|
+
|
|
|
+};
|
|
|
+
|
|
|
+Photo.prototype.uploadToServer = function(file) {
|
|
|
+ const config = this.config;
|
|
|
+
|
|
|
+ const formData = new FormData()
|
|
|
+ formData.append(config.upload.fieldName || 'image', file)
|
|
|
+
|
|
|
+ if (typeof config.upload.params === 'object') {
|
|
|
+ Object.keys(config.upload.params).forEach((key) => {
|
|
|
+ const value = config.upload.params[key]
|
|
|
+ if (Array.isArray(value)) {
|
|
|
+ value.forEach((v) => {
|
|
|
+ formData.append(key, v)
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ formData.append(key, value)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ const xhr = new XMLHttpRequest()
|
|
|
+
|
|
|
+ xhr.onprogress = (e) => {
|
|
|
+ this.upload.status = 'progress'
|
|
|
+ if (e.lengthComputable) {
|
|
|
+ this.upload.progressComputable = true
|
|
|
+ const percentComplete = e.loaded / e.total
|
|
|
+ this.upload.complete = (percentComplete * 100).toFixed(2)
|
|
|
+ } else {
|
|
|
+ this.upload.progressComputable = false
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ xhr.onload = () => {
|
|
|
+ if (xhr.status >= 300) {
|
|
|
+ this.setUploadError(`request error,code ${xhr.status}`)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ const url = config.uploadHandler(xhr.responseText)
|
|
|
+ if (url) {
|
|
|
+ this.$parent.execCommand(Command.INSERT_IMAGE, url)
|
|
|
+ }
|
|
|
+ } catch (err) {
|
|
|
+ this.setUploadError(err.toString())
|
|
|
+ } finally {
|
|
|
+ this.upload.status = 'ready'
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ xhr.onerror = () => {
|
|
|
+ // find network info in brower tools
|
|
|
+ this.setUploadError('request error')
|
|
|
+ }
|
|
|
+
|
|
|
+ xhr.onabort = () => {
|
|
|
+ this.upload.status = 'abort'
|
|
|
+ }
|
|
|
+
|
|
|
+ xhr.open('POST', config.upload.url)
|
|
|
+ if (typeof config.upload.headers === 'object') {
|
|
|
+ Object.keys(config.upload.headers).forEach((k) => {
|
|
|
+ xhr.setRequestHeader(k, config.upload.headers[k])
|
|
|
+ })
|
|
|
+ }
|
|
|
+ xhr.send(formData)
|
|
|
+}
|
|
|
+
|
|
|
+export default Photo;
|