Sfoglia il codice sorgente

feat: 素材库 - 支持更换图片

zhengxy 1 anno fa
parent
commit
0704bdfff4

+ 26 - 2
project/src/components/manage/materialLibrary/dialog/editMaterials.vue

@@ -46,6 +46,7 @@
46 46
 
47 47
           <div class="imgBtn">
48 48
             <el-button type="primary" size="mini" plain @click="openImgCutter()">图片裁剪</el-button>
49
+            <el-button type="primary" size="mini" plain @click="onClickSelectImg">更换图片</el-button>
49 50
           </div>
50 51
         </div>
51 52
       </div>
@@ -60,13 +61,23 @@
60 61
         <imgCutter v-if="imgCutterVisible" :target="oss_url" @imgCutterCallback="imgCutterCallback" source="uploadMaterials"></imgCutter>
61 62
       </el-dialog>
62 63
 
64
+      <selectImgDialog
65
+        :dialogVisible="selectImgVisible"
66
+        @confirm="onConfirmSelectImg"
67
+        @cancel="onCancelSelectImg"
68
+      />
69
+
63 70
     </el-dialog>
64 71
 </template>
65 72
 <script>
66 73
 import imgCutter from '@/components/assembly/imgCutter.vue'
74
+import selectImgDialog from './selectImgDialog.vue'
67 75
 export default {
68 76
   name: 'uploadMaterials',
69
-  components: { imgCutter },
77
+  components: {
78
+    imgCutter,
79
+    selectImgDialog,
80
+  },
70 81
   props: {
71 82
     // 控制弹框是否显示
72 83
     editVisible: {
@@ -91,7 +102,8 @@ export default {
91 102
       oss_url: '',
92 103
       loading: false,
93 104
       successFile: [],
94
-      material: {}
105
+      material: {},
106
+      selectImgVisible: false,
95 107
     }
96 108
   },
97 109
   watch:{
@@ -151,6 +163,18 @@ export default {
151 163
         this.material = Object.assign(this.material, obj.data)
152 164
       }
153 165
     },
166
+
167
+
168
+    onClickSelectImg() {
169
+      this.selectImgVisible = true
170
+    },
171
+    onConfirmSelectImg(url) {
172
+      this.material.url = url
173
+      this.selectImgVisible = false
174
+    },
175
+    onCancelSelectImg() {
176
+      this.selectImgVisible = false
177
+    },
154 178
   }
155 179
 }
156 180
 </script>

+ 149 - 0
project/src/components/manage/materialLibrary/dialog/selectImgDialog.vue

@@ -0,0 +1,149 @@
1
+<template>
2
+  <el-dialog
3
+    :visible.sync="dialogVisible"
4
+    :before-close="handleCancel"
5
+    class="selectImg-dialog"
6
+    :title="title"
7
+    width="550px"
8
+    :close-on-click-modal="false"
9
+    append-to-body
10
+  >
11
+    <div class="content-wrap" v-loading="loading">
12
+      <el-upload
13
+        ref="upload"
14
+        class="upload-demo"
15
+        :action="URL.BASEURL + URL.media_upload"
16
+        :before-upload="beforeAvatarUpload"
17
+        :on-success="uploadSuccess"
18
+        :on-progress='beforeUploadUpload'
19
+        :on-error='uploadFail'
20
+        :on-exceed="handleExceed"
21
+        :file-list="fileList"
22
+        accept=".jpg,.jpeg,.png"
23
+        name="file"
24
+        :data="fileData"
25
+        :show-file-list="true"
26
+        list-type="picture"
27
+        :multiple="false"
28
+        :limit="1"
29
+        drag
30
+      >
31
+        <div>
32
+          <i class="el-icon-upload"></i>
33
+          <div class="el-upload__text">将图片拖到此处,或<em>点击上传</em></div>
34
+          <div slot="tip" class="el-upload__tip">*图片仅支持png或jpg格式,且文件大小不得超过10M。</div>
35
+        </div>
36
+      </el-upload>
37
+    </div>
38
+    <div slot="footer" class="dialog-footer">
39
+      <el-button size="mini" @click="handleCancel">关 闭</el-button>
40
+      <el-button size="mini" type="primary" @click="handleConfirm">确 定</el-button>
41
+    </div>
42
+  </el-dialog>
43
+</template>
44
+
45
+<script>
46
+export default {
47
+  props: {
48
+    // 控制弹框是否显示
49
+    dialogVisible: {
50
+      type: Boolean,
51
+      default: () => false
52
+    },
53
+  },
54
+  data() {
55
+    return {
56
+      loading: false,
57
+      fileList: [],
58
+      fileData: {
59
+        admin_id: this.$cookie.getCookie('admin_id'),
60
+        ttl: this.$cookie.getCookie('ttl'),
61
+        sign: this.$cookie.getCookie('sign'),
62
+        corpid: this.$localSelfStore.getLocal('defaultCorp') && this.$localSelfStore.getLocal('defaultCorp') != 'undefined' ? JSON.parse(this.$localSelfStore.getLocal('defaultCorp')).corpid : '',
63
+        type: 1,//素材类型。1图片 2语音 3视频
64
+      },
65
+
66
+    }
67
+  },
68
+  computed: {
69
+    title() {
70
+      return `更换图片`
71
+    },
72
+  },
73
+  watch: {
74
+    dialogVisible(isShow) {
75
+      if (isShow) {
76
+        // this.$refs.upload.clearFiles()
77
+      } else {
78
+        this.$refs.upload.clearFiles()
79
+      }
80
+    },
81
+  },
82
+  methods: {
83
+    // 限制上传一个文件,重新选择文件替换原来的文件
84
+    handleExceed(files, fileList) {
85
+      this.$message.error('最多只能上传一张图片')
86
+    },
87
+    beforeAvatarUpload(file) {
88
+      const isLt10M = file.size / 1024 / 1024 < 10;
89
+      if (!isLt10M) {
90
+        this.$message.error('上传头像图片大小不能超过 10MB!');
91
+      }
92
+      return isLt10M;
93
+    },
94
+    // 上传成功
95
+    uploadSuccess (res, file) {
96
+      this.$loading(this.$loadingConfig).close();
97
+      if (res.errno == 0) {
98
+        this.$message({
99
+          message: '上传成功',
100
+          type: 'success'
101
+        })
102
+      } else {
103
+        this.$message({
104
+          message: res.err,
105
+          type: 'warning'
106
+        })
107
+      }
108
+    },
109
+    beforeUploadUpload (file) {
110
+      this.$loading(this.$loadingConfig);
111
+    },
112
+    // 上传失败
113
+    uploadFail (file) {
114
+      this.$loading(this.$loadingConfig).close();
115
+      this.$message({
116
+        message: '上传失败',
117
+        type: 'fail'
118
+      })
119
+      let idx = this.$refs.upload.uploadFiles.findIndex(item => item.uid === file.uid)
120
+      this.$refs.upload.uploadFiles.splice(idx, 1)
121
+    },
122
+    handleCancel() {
123
+      this.$emit('cancel')
124
+    },
125
+    handleConfirm() {
126
+      console.log('this.$refs.upload.uploadFiles => ', this.$refs.upload.uploadFiles)
127
+      console.log('fileList => ', this.fileList)
128
+      const { uploadFiles } = this.$refs.upload
129
+      if (!uploadFiles || !uploadFiles.length) {
130
+        return this.$message.warning('请上传更换图片')
131
+      }
132
+      this.$emit('confirm', uploadFiles[0].response.rst.url)
133
+    },
134
+  },
135
+};
136
+</script>
137
+
138
+<style lang="scss" scoped>
139
+.selectImg-dialog {
140
+  .content-wrap {
141
+    display: flex;
142
+    align-items: center;
143
+    justify-content: center;
144
+  }
145
+  .dialog-footer {
146
+    text-align: center;
147
+  }
148
+}
149
+</style>