guhongwei 4 tahun lalu
induk
melakukan
9b2880b633

+ 0 - 76
src/components/frame/upload.vue

@@ -1,76 +0,0 @@
-<template>
-  <div id="upload">
-    <el-upload
-      v-if="url"
-      ref="upload"
-      :action="url"
-      list-type="picture-card"
-      :file-list="fileList"
-      :limit="limit"
-      :on-exceed="outLimit"
-      :on-preview="handlePictureCardPreview"
-      :before-remove="handleRemove"
-      :on-success="onSuccess"
-      accept=".jpg,.jpeg,.png,.bmp,.gif,.svg"
-    >
-      <template>
-        <i class="el-icon-plus"></i>
-      </template>
-    </el-upload>
-    <el-dialog :visible.sync="dialogVisible">
-      <img width="100%" :src="dialogImageUrl" alt="" />
-    </el-dialog>
-  </div>
-</template>
-
-<script>
-export default {
-  name: 'upload',
-  props: {
-    url: { type: null },
-    limit: { type: Number },
-    data: { type: null },
-    type: { type: String },
-  },
-  components: {},
-  data: () => ({
-    dialogVisible: false,
-    dialogImageUrl: '',
-    fileList: [],
-  }),
-  created() {
-    if (this.data) {
-      this.defalutProcess(this.data);
-    }
-  },
-  watch: {
-    data: {
-      handler(val) {
-        this.defalutProcess(val);
-      },
-    },
-  },
-  computed: {},
-  methods: {
-    handlePictureCardPreview(file) {
-      this.dialogImageUrl = file.url;
-      this.dialogVisible = true;
-    },
-    handleRemove(file) {
-      return true;
-    },
-    outLimit() {
-      this.$message.error('只允许上传1张图片');
-    },
-    onSuccess(response, file, fileList) {
-      //将文件整理好传回父组件
-      this.$emit('upload', { type: this.type, data: response });
-    },
-    defalutProcess(val) {
-      this.$set(this, `fileList`, [{ name: this.type, url: `${this.data}?${new Date().getTime()}` }]);
-    },
-  },
-};
-</script>
-
-<style lang="less" scoped></style>

+ 106 - 0
src/components/frame/uploadone.vue

@@ -0,0 +1,106 @@
+<template>
+  <div id="upload">
+    <el-upload
+      v-if="url"
+      ref="upload"
+      :action="url"
+      :list-type="listType"
+      :file-list="fileList"
+      :limit="limit"
+      :on-exceed="outLimit"
+      :before-remove="handleRemove"
+      :on-success="onSuccess"
+      :before-upload="beforeUpload"
+      :show-file-list="showList"
+      :accept="accept"
+    >
+      <el-button size="small" type="primary" v-if="isBtn">点击上传</el-button>
+      <template v-else-if="uploadBtn">
+        <el-button type="primary">选择文件</el-button>
+      </template>
+      <template v-else>
+        <i class="el-icon-plus"></i>
+      </template>
+      <template #tip v-if="tip">
+        {{ tip }}
+      </template>
+    </el-upload>
+  </div>
+</template>
+
+<script>
+export default {
+  name: 'upload',
+  props: {
+    url: { type: null },
+    limit: { type: Number },
+    data: { type: null },
+    type: { type: String },
+    isBtn: { type: Boolean, default: false },
+    uploadBtn: { type: Boolean, default: false },
+    showList: { type: Boolean, default: true },
+    accept: { type: String, default: '' },
+    tip: { type: String, default: undefined },
+    listType: { type: String, default: 'picture-card' },
+    fileType: { type: String, default: '1' },
+  },
+  components: {},
+  data: () => ({
+    fileList: [],
+  }),
+  created() {
+    if (this.data) {
+      this.defalutProcess(this.data);
+    }
+  },
+  watch: {
+    data: {
+      handler(val) {
+        this.defalutProcess(val);
+      },
+    },
+  },
+  computed: {},
+  methods: {
+    handleRemove(file, fileList) {
+      if (this.fileType == '1') {
+        this.$emit('onedelete', file);
+        return true;
+      } else {
+        let index = fileList.findIndex(f => _.isEqual(f, file));
+        this.$emit('delete', index);
+      }
+    },
+    outLimit() {
+      this.$message.error(`只允许上传${this.limit}个文件`);
+    },
+    onSuccess(response, file, fileList) {
+      //将文件整理好传回父组件
+      this.$emit('upload', { type: this.type, data: { ...response, name: file.name } });
+    },
+    beforeUpload(file) {
+      const sizeLimit = file.size / 1024 / 1024 < 10;
+      if (sizeLimit) return true;
+      else {
+        this.$message.error('文件超出10M!');
+        return false;
+      }
+    },
+    defalutProcess(val) {
+      if (_.isArray(val)) {
+        let newArr = val.map(item => {
+          let object = {};
+          // object.name = item.name;
+          object.url = item ? item.url : null;
+          return object;
+        });
+        this.$set(this, `fileList`, newArr);
+      } else if (typeof val === 'string') {
+        this.$set(this, `fileList`, [{ name: '视频', url: val }]);
+      }
+    },
+  },
+};
+</script>
+
+<style lang="less" scoped></style>

+ 30 - 7
src/views/company/index.vue

@@ -31,9 +31,17 @@
             </template>
           </template>
         </template>
-        <template #custom="{item}">
+        <template #custom="{item,form}">
           <template v-if="item.model === 'file'">
-            <upload :limit="1" :data="form.file" type="file" :url="'/files/file/upload'" @upload="uploadSuccess" @delete="uploadDelete"></upload>
+            <upload
+              :limit="1"
+              :data="form.file"
+              type="file"
+              :isBtn="true"
+              :url="`/files/file/upload`"
+              @upload="uploadSuccess"
+              @onedelete="uploadDeletefile"
+            ></upload>
           </template>
           <template v-else-if="item.model === 'video'">
             <upload
@@ -42,13 +50,22 @@
               type="video"
               :uploadBtn="true"
               listType=""
-              :url="'/files/video/upload'"
+              :url="`/files/video/upload`"
               @upload="uploadSuccess"
-              @delete="uploadDelete"
+              @onedelete="uploadDeletevideo"
             ></upload>
           </template>
           <template v-else-if="item.model === 'image'">
-            <upload :limit="3" :data="form.image" type="image" :url="'/files/image/upload'" @upload="uploadSuccess" @delete="uploadDelete"></upload>
+            <upload
+              :limit="2"
+              :data="form.image"
+              type="image"
+              :isBtn="true"
+              fileType="2"
+              :url="`/files/image/upload`"
+              @upload="uploadSuccess"
+              @delete="uploadDelete"
+            ></upload>
           </template>
         </template>
       </data-form>
@@ -60,7 +77,7 @@
 import breadcrumb from '@c/common/breadcrumb.vue';
 import dataTable from '@/components/frame/filter-page-table.vue';
 import dataForm from '@/components/frame/form.vue';
-import upload from '@/components/frame/upload.vue';
+import upload from '@/components/frame/uploadone.vue';
 import { mapState, createNamespacedHelpers } from 'vuex';
 const { mapActions: company } = createNamespacedHelpers('company');
 export default {
@@ -222,7 +239,13 @@ export default {
     },
     // 删除图片
     uploadDelete(index) {
-      // this.form.image.splice(index, 1);
+      this.form.image.splice(index, 1);
+    },
+    uploadDeletefile(data) {
+      this.form.file = null;
+    },
+    uploadDeletevideo(data) {
+      this.form.video = null;
     },
   },
   computed: {

+ 1 - 1
vue.config.js

@@ -23,7 +23,7 @@ module.exports = {
         target: 'http://free.liaoningdoupo.com',
       },
       '/api': {
-        target: 'http://192.168.31.194:8081',
+        target: 'http://192.168.1.43:8081',
         changeOrigin: true,
         ws: true,
       },