Authored by htoooth

add uploadfile

@@ -8,7 +8,7 @@ class ResourceApi extends Api { @@ -8,7 +8,7 @@ class ResourceApi extends Api {
8 info(id) { 8 info(id) {
9 return this._post('/ufoPlatform/resource/getResourceInfo', { 9 return this._post('/ufoPlatform/resource/getResourceInfo', {
10 id 10 id
11 - }, true); 11 + });
12 } 12 }
13 13
14 editResource(data) { 14 editResource(data) {
@@ -72,7 +72,7 @@ @@ -72,7 +72,7 @@
72 72
73 <script> 73 <script>
74 74
75 -import fileUpload from './file-upload'; 75 +import fileUpload from './drag-file-upload';
76 76
77 export default { 77 export default {
78 props: { 78 props: {
  1 +<template>
  2 + <div v-if="show">
  3 + <image-purview
  4 + v-if="uploadList[0]"
  5 + :status="uploadList[0].status"
  6 + :url="uploadList[0].url"
  7 + :progress="uploadList[0].showProgress"
  8 + :percentage="uploadList[0].percentage"
  9 + :remove="true"
  10 + @remove-image="handleRemove"></image-purview>
  11 +
  12 + <div class="upload-box" v-show="!uploadList[0]">
  13 + <Upload ref="upload"
  14 + :show-upload-list="false"
  15 + :data="{bucket: bucket}"
  16 + :on-success="handleSuccess"
  17 + :on-error="handleError"
  18 + :default-file-list="defaultList"
  19 + :format="['jpg','jpeg','png']"
  20 + :max-size="2048"
  21 + :on-format-error="handleFormatError"
  22 + :on-exceeded-size="handleMaxSize"
  23 + action="/ufoPlatform/fileupload/upload?debug=XYZ">
  24 + <Icon type="ios-cloud-upload-outline" title="上传图片"></Icon>
  25 + </Upload>
  26 + </div>
  27 + </div>
  28 +</template>
  29 +<script>
  30 +
  31 +import imagePurview from './image-purview'
  32 +
  33 +export default {
  34 + name: 'drag-file-upload',
  35 + props: {
  36 + defaultFile: {
  37 + type: String
  38 + },
  39 + bucket: {
  40 + type: String,
  41 + default() {
  42 + return 'yhb-img01';
  43 + }
  44 + }
  45 + },
  46 + data() {
  47 + let _this = this;
  48 +
  49 + return {
  50 + imgUrl: '',
  51 + show: true,
  52 + visible: false,
  53 + uploadList: [],
  54 + defaultList: _this.defaultFile ? [{url: _this.defaultFile}] : []
  55 + };
  56 + },
  57 + methods: {
  58 + handleView(url) {
  59 + this.imgUrl = url;
  60 + this.visible = true;
  61 + },
  62 + handleRemove() {
  63 + let file = this.uploadList[0];
  64 + let files = this.$refs.upload.fileList;
  65 +
  66 + this.$refs.upload.fileList.splice(files.indexOf(file), 1);
  67 + this.uploadList = this.$refs.upload.fileList;
  68 + this.$emit('remove', this.id);
  69 + },
  70 + handleSuccess(response, file, files) {
  71 + if (response.data) {
  72 + file.url = response.data.imagesList[0];
  73 + }
  74 +
  75 + this.uploadList = files;
  76 + this.$emit('success', this.id, file);
  77 + },
  78 + handleError() {
  79 + this.$Notice.error('上传失败');
  80 + this.$emit('error', this.id);
  81 + },
  82 + handleFormatError(file) {
  83 + this.$Notice.warning({
  84 + title: '文件格式不正确',
  85 + desc: '文件 ' + file.name + ' 格式不正确,请上传 jpg 或 png 格式的图片。'
  86 + });
  87 + },
  88 + handleMaxSize(file) {
  89 + this.$Notice.warning({
  90 + title: '超出文件大小限制',
  91 + desc: '文件 ' + file.name + ' 太大,不能超过 2M。'
  92 + });
  93 + },
  94 + selected(url) {
  95 + this.uploadList = this.defaultList = [{url}];
  96 + this.$emit('success', this.id, {url});
  97 + },
  98 +
  99 + },
  100 + mounted() {
  101 + this.uploadList = this.$refs.upload.fileList;
  102 + },
  103 + watch: {
  104 + defaultFile: function(newValue) {
  105 + if (newValue) {
  106 + this.defaultList = [{url: newValue}];
  107 + }
  108 + }
  109 + },
  110 + components: {
  111 + imagePurview
  112 + }
  113 +};
  114 +</script>
  115 +<style lang="scss">
  116 +
  117 +.upload-box {
  118 + width: 100px;
  119 + height: 100px;
  120 + background: #fff;
  121 + border: 1px dashed #dddee1;
  122 + border-radius: 4px;
  123 + text-align: center;
  124 + cursor: pointer;
  125 + position: relative;
  126 + overflow: hidden;
  127 + transition: border-color 0.2s ease;
  128 + margin: 0 auto;
  129 + display: flex;
  130 + align-items: center;
  131 + justify-content: center;
  132 +
  133 + &:hover {
  134 + border-color: #2d8cf0;
  135 + }
  136 +
  137 + .ivu-upload {
  138 + height: 30px;
  139 + }
  140 +
  141 + .ivu-icon {
  142 + font-size: 30px;
  143 + margin: 0 2px;
  144 + color: #000;
  145 + transition: color 0.2s ease;
  146 +
  147 + &:hover {
  148 + color: #2d8cf0;
  149 + }
  150 + }
  151 +}
  152 +</style>
@@ -20,7 +20,7 @@ export default { @@ -20,7 +20,7 @@ export default {
20 props: { 20 props: {
21 action: { 21 action: {
22 type: String, 22 type: String,
23 - default: '/ufoPlatform/fileupload/uploadImage' 23 + default: '/ufoPlatform/fileupload/upload?debug=XYZ'
24 }, 24 },
25 data: { 25 data: {
26 type: Object, 26 type: Object,
  1 +<template>
  2 + <div class="image-purview">
  3 + <template v-if="status === 'finished' || url">
  4 + <img :src="url">
  5 + <div class="image-purview-cover">
  6 + <Icon type="ios-eye-outline" size="30" @click.native="purviewImage"></Icon>
  7 + <Icon type="ios-trash-outline" v-if="remove" size="30" @click.native="removeImage"></Icon>
  8 + </div>
  9 + </template>
  10 + <template v-else>
  11 + <Progress v-if="progress" :percent="percentage" hide-info></Progress>
  12 + </template>
  13 + </div>
  14 +</template>
  15 +
  16 +<script>
  17 +export default {
  18 + name: 'image-purview',
  19 + props: {
  20 + status: {
  21 + type: String,
  22 + default: 'finished'
  23 + },
  24 + url: {
  25 + type: String,
  26 + },
  27 + progress: {
  28 + type: Boolean,
  29 + default: false,
  30 + },
  31 + percentage: {
  32 + type: Number,
  33 + },
  34 + remove: {
  35 + type: Boolean,
  36 + default: false
  37 + }
  38 + },
  39 + data() {
  40 + return {
  41 + showModal: false
  42 + };
  43 + },
  44 + methods: {
  45 + purviewImage() {
  46 + this.showModal = true;
  47 + this.$emit('purview', this.url);
  48 + },
  49 + removeImage() {
  50 + this.$emit('remove-image', this.url);
  51 + }
  52 + },
  53 +};
  54 +</script>
  55 +
  56 +<style lang="scss">
  57 +.image-purview {
  58 + margin: 0 auto;
  59 + width: 100px;
  60 + height: 100px;
  61 + text-align: center;
  62 + border-radius: 4px;
  63 + overflow: hidden;
  64 + background: #fff;
  65 + position: relative;
  66 + box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
  67 + border: 1px solid #e8e8e8;
  68 + align-items: center;
  69 + justify-content: center;
  70 + display: flex;
  71 +
  72 + .ivu-progress {
  73 + width: 90%;
  74 + }
  75 +}
  76 +
  77 +.image-purview img {
  78 + width: 100px;
  79 + height: 100px;
  80 +}
  81 +
  82 +.image-purview-cover {
  83 + display: none;
  84 + position: absolute;
  85 + top: 0;
  86 + bottom: 0;
  87 + left: 0;
  88 + right: 0;
  89 + background: rgba(0, 0, 0, 0.6);
  90 + align-items: center;
  91 + justify-content: center;
  92 +}
  93 +
  94 +.image-purview:hover .image-purview-cover {
  95 + display: flex;
  96 +}
  97 +
  98 +.image-purview-cover i {
  99 + color: #fff;
  100 + font-size: 20px;
  101 + cursor: pointer;
  102 + margin: 0 2px;
  103 +}
  104 +</style>