Browse Source

图片截取

xiuli.gao 1 year ago
parent
commit
fe6b1b2834

+ 1 - 0
project/src/assets/config/interface_api.js

@@ -396,6 +396,7 @@ var api = {
396 396
 
397 397
   order_platformList: "/api/order/platformList", // 付费来源
398 398
   playlet_batchCreateLink: "/api/platform/batchCreateLink", // 批量生成推广链接
399
+  material_uploadFileByUrl: '/api/material/uploadFileByUrl',
399 400
 };
400 401
 
401 402
 export { api };

+ 46 - 4
project/src/components/assembly/editCon.vue

@@ -200,7 +200,11 @@
200 200
             <img v-if="miniprogram.pic_url" :src="miniprogram.pic_url" class="fm_img">
201 201
             <i v-else class="el-icon-plus avatar-uploader-icon"></i>
202 202
           </el-upload>
203
-          <el-button type="primary" plain size="mini">裁剪</el-button>
203
+          <div style="margin-left:40px;">
204
+            <el-button type="danger" size="mini" style="height:30px;margin-bottom:0px;" @click="openImgCutter">图片裁剪</el-button>
205
+            <p class="uploadImgHint" style="margin:0">可根据需要对图片进行裁剪。</p>
206
+          </div>
207
+
204 208
         </div>
205 209
         <p class="uploadImgHint" style="margin-left:20px;">*图片仅支持png或jpg格式,且文件大小不得超过10M。封面图建议尺寸为520*416。</p>
206 210
         <div class="buttons">
@@ -276,8 +280,10 @@
276 280
       @confirm="onConfirmMultipleLink"
277 281
       @cancel="onCancelMultipleLink"
278 282
     />
279
-
280
-    <!-- <imgCutter></imgCutter> -->
283
+    <!-- 图片裁剪弹框 -->
284
+    <el-dialog width="840px" title="图片裁剪" :visible.sync="imgCutterVisible" append-to-body>
285
+      <imgCutter v-if="imgCutterVisible" :target="oss_url"></imgCutter>
286
+    </el-dialog>
281 287
   </div>
282 288
 </template>
283 289
 <script>
@@ -285,9 +291,10 @@ import draggable from "vuedraggable"
285 291
 import phonePreview from './phonePreview.vue'
286 292
 import radarEntrance from './radarEntrance.vue'
287 293
 import multipleLink from './multipleLink.vue'
288
-// import imgCutter from './imgCutter.vue'
294
+import imgCutter from './imgCutter.vue'
289 295
 export default {
290 296
   components: {
297
+    imgCutter,
291 298
     draggable,
292 299
     phonePreview,
293 300
     radarEntrance,
@@ -376,6 +383,8 @@ export default {
376 383
       has_attachLen: 0,//除雷达外,附件的长度
377 384
 
378 385
       multipleLinkVisible: false,
386
+      imgCutterVisible: false,
387
+      oss_url: '', // 需要裁剪并转化后的图片链接
379 388
     }
380 389
   },
381 390
   created () {
@@ -385,6 +394,39 @@ export default {
385 394
     }
386 395
   },
387 396
   methods: {
397
+    openImgCutter(){ // 打开图片裁剪
398
+      if ( this.miniprogram.pic_url ) {
399
+        this.$loading(this.$loadingConfig)
400
+        if ( this.miniprogram.pic_url.indexOf('http://playlet.oss-cn-beijing.aliyuncs.com/') != -1 ){
401
+          this.oss_url = this.miniprogram.pic_url.replace('http://playlet.oss-cn-beijing.aliyuncs.com/','http://dj.wenxingshuju.com/oss_image/');
402
+          this.imgCutterVisible = true
403
+          this.$loading(this.$loadingConfig).close()
404
+        } else {
405
+          if (this.oss_url) { // oss_url存在,说明已经通过服务端转化过,不需要重复转化
406
+            this.imgCutterVisible = true
407
+            this.$loading(this.$loadingConfig).close()
408
+            return
409
+          }
410
+          // 图片通过服务器转化,解决图片裁剪跨域问题
411
+          this.$axios.post(this.URL.BASEURL + this.URL.material_uploadFileByUrl, {
412
+            img_url: this.miniprogram.pic_url
413
+          }).then((res) => {
414
+            var res = res.data
415
+            if (res && res.errno == 0) {
416
+              this.oss_url = res.rst.oss_url.replace('http://playlet.oss-cn-beijing.aliyuncs.com/','http://dj.wenxingshuju.com/oss_image/');
417
+              this.imgCutterVisible = true
418
+              this.$loading(this.$loadingConfig).close()
419
+            } else if (res.errno != 4002) {
420
+              this.$message({ message: res.err, type: "error" })
421
+            }
422
+          }).catch((err) => {
423
+            this.$message({ message: '服务器错误,获取裁剪图片失败', type: "error" })
424
+          });
425
+        }
426
+      } else {
427
+        this.imgCutterVisible = true
428
+      }
429
+    },
388 430
     radarids_reset () { //雷达id回显
389 431
       this.has_attachLen = 0
390 432
       this.radarIds = []

+ 90 - 4
project/src/components/assembly/imgCutter.vue

@@ -1,26 +1,112 @@
1 1
 <template>
2 2
 <div>
3
-  <ImgCutter @cutDown="cutDown" :isModal="false" :sizeChange="false" :scaleAble="false" :originalGraph="true" cutWidth="520" cutHeight="206"></ImgCutter>
3
+  <ImgCutter ref="imgCutterModal" v-loading="loadCut" :isModal="false" :sizeChange="true" :scaleAble="false" :originalGraph="true" rate="65:52" @cutDown="cutDown" @onClearAll="onClearAll">
4
+    <template slot="open"><slot></slot></template>
5
+  </ImgCutter>
4 6
 </div>
5 7
 </template>
6 8
 <script>
7 9
 import ImgCutter from 'vue-img-cutter'
8
-
10
+import axios from 'axios'
9 11
 export default {
10 12
     name: 'imgCutter',
13
+    props:{
14
+      target:{
15
+        type: String
16
+      }
17
+    },
11 18
     components:{
12 19
         ImgCutter
13 20
     },
14 21
     data() {
15 22
        return {
16
-
23
+        loadCut: false,
24
+        base64_url: '', // base64
25
+        firstCome: true, // 判断是否初次进来
26
+        oss_url: '', // 域名更新后的地址
17 27
        }
18 28
     },
29
+    watch: {
30
+      target(){
31
+        this.target && this.target != '' && this.getBase64Image(this.target);
32
+      }
33
+    },
34
+    mounted(){
35
+      if(this.firstCome && this.target && this.target != ''){
36
+        this.getBase64Image (this.target)
37
+      }
38
+    },
19 39
     methods: {
40
+      getBase64Image (url, ref) {
41
+        this.loadCut = true
42
+        this.firstCome = false ;
43
+        var that = this
44
+        var image = new Image()
45
+        image.src = url + '?v=' + Math.random() // 处理缓存
46
+        image.crossOrigin = 'anonymous' // 支持跨域图片
47
+        image.onload = function () {
48
+          var base64 = that.drawBase64Image(image)
49
+          that.loadCut = false;
50
+          that.base64_url = base64;
51
+          that.forIe9();
52
+        }
53
+      },
54
+      drawBase64Image (img) {
55
+        var canvas = document.createElement('canvas')
56
+        canvas.width = img.width
57
+        canvas.height = img.height
58
+        var ctx = canvas.getContext('2d')
59
+        ctx.drawImage(img, 0, 0, img.width, img.height)
60
+        var dataURL = canvas.toDataURL('image/png')
61
+        return dataURL
62
+      },
63
+      //裁剪
64
+      forIe9 () {
65
+        this.$refs.imgCutterModal.handleOpen({
66
+          name: 'img',
67
+          src: this.base64_url,
68
+          width: 400,
69
+          height: 400,
70
+        });
71
+      },
20 72
        //通过cutDown事件接收选择的图片信息
21 73
        cutDown(event) {
22 74
            console.log(event)
23
-       }
75
+          if (!event.file) {
76
+            this.$message({ message: '图片剪裁失败!', type: "error" })
77
+            return
78
+          };
79
+          let formData = new FormData();
80
+          formData.append("material_file",event.file);  //上传一个files对象
81
+          formData.append("type",1);
82
+          formData.append("need_media_id",0);
83
+          formData.append("admin_id",this.$cookie.getCookie('admin_id'));
84
+          formData.append("ttl",this.$cookie.getCookie('ttl'));
85
+          formData.append("sign",this.$cookie.getCookie('sign'));
86
+          formData.append("corpid",this.$localSelfStore.getLocal('defaultCorp') && this.$localSelfStore.getLocal('defaultCorp') != 'undefined' ? JSON.parse(this.$localSelfStore.getLocal('defaultCorp')).corpid : '');
87
+
88
+          let instance= axios.create({
89
+            baseURL: '',
90
+            data: formData,
91
+            headers: { 'Content-Type': 'multipart/form-data'}
92
+          })
93
+
94
+          instance.post(this.URL.BASEURL + this.URL.material_upload,formData).then((res) => {
95
+            var res = res.data
96
+            if (res && res.errno == 0) {
97
+
98
+            } else if (res.errno != 4002) {
99
+              this.$message({ message: res.err, type: "error" })
100
+            }
101
+          }).catch((err) => {
102
+            this.$message({ message: '服务器错误,获取裁剪图片失败', type: "error" })
103
+          });
104
+
105
+       },
106
+      // 清空画布
107
+      onClearAll(){
108
+        console.log('点击了取消')
109
+      },
24 110
     }
25 111
 
26 112
 }