liangjin
2021-03-25 19f91bd79fa4ce2c50dfb5b44b0ebfc4f01baeae
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
<!--
 * @Descripttion: 上传合约凭证
 * @Author: TM丶
 * @LastEditors: 小明丶
 * @Date: 2019-04-08 09:16:44
 * @LastEditTime: 2019-09-03 09:25:14
 -->
<template>
  <div class="zxh-upload-box h-100-g">
    <x-header :left-options="{'backText': ''}" title="上传合约凭证" class="gradient-color"></x-header>
    <div class="up-div">
      <p>合约办理凭证截图</p>
      <van-uploader :after-read="afterRead">
        <div class="upload-div">
          <img :src="pic" alt v-if="pic" />
          <i class="iconfont iconzhaoxiang" v-else></i>
        </div>
      </van-uploader>
      <p>合约手机号</p>
      <Ipt v-model="form.contMblNo" :max="11" :placeholder="'请输入手机号'" :type="'text'"></Ipt>
    </div>
    <button class="btn" @click="submit">提交</button>
    <!-- <div class="content" v-for="(item, index) in fileTypeArr" :key="index">
            <h4 class="title">
                <span>{{item.name}}</span>
                <div @click="changeShowselect(item)">
                    <i class="iconfont scene_Staging-xiangji"></i>
                    <input ref="photo" type="file" accept="image/*" @change="getFile($event.currentTarget,item.code)">
                    <input ref="picture" type="file" accept="image/*" capture="camera"
                           @change="getFile($event.currentTarget,item.code)">
                </div>
            </h4>
            <ul class="upload">
                <li class="upload-item" v-for="(item, index) in item.fileIds" :key="index"
                    :style="{'background-image':`url(${item.fileUrl})`}">
                    <i class="iconfont scene_Staging-shibai1" @click="deleteImgItem(index)"></i>
                </li>
            </ul>
        </div>
        <group>
            <x-input title="手机号" v-model="form.contMblNo" placeholder="请输入" text-align="right" :max="11"></x-input>
        </group>
        <f-button @on-click="submit()">提交</f-button>
 
 
        <van-action-sheet v-model="showSelect" :close-on-click-overlay="false" cancel-text="取消" :actions="nav"
                          @select="handleSelect">
    </van-action-sheet> -->
  </div>
</template>
 
<script>
import { compress } from "@/utils/index";
import { Actionsheet } from "vux";
import Vue from "vue";
import { Uploader } from "vant";
import Ipt from "../../../../components/common/h-ipt";
Vue.use(Uploader);
export default {
  name: "xyg_upload",
  data() {
    return {
      pic: "",
      showSelect: false,
      nav: [
        {
          name: "拍照"
        },
        {
          name: "从手机相册选择"
        }
      ],
      form: {
        orderId: "",
        contMblNo: "",
        fileIds: []
      },
      fileTypeArr: [],
      currentItem: {}
    };
  },
  components: {
    Actionsheet,
    Ipt
  },
  props: ["orderId"],
  created() {
    this.form.orderId = this.orderId;
    this.$api.h_getXygFileType().then(res => {
      let list = res.body || [];
      this.fileTypeArr = list.map(item => {
        return {
          ...item,
          fileIds: []
        };
      });
      console.log(this.fileTypeArr)
    });
  },
  methods: {
    uploadSectionFile(f, t) {
      //    图片上传
      let self = this;
      let Orientation;
      let ndata;
      let pic;
      if (t.size <= 1 * 1024 * 1024) {
        console.log("未压缩");
        //判断图片是否小于1M,是就直接上传
        ndata = f;
        self.postImg(ndata);
      } else {
        //反之压缩图片
        //BASE64转图片
        var arr = f.split(","),
          mime = arr[0].match(/:(.*?);/)[1],
          bstr = atob(arr[1]),
          n = bstr.length,
          u8arr = new Uint8Array(n);
        while (n--) {
          u8arr[n] = bstr.charCodeAt(n);
        }
        pic = new File([u8arr], t.name, { type: mime });
        let reader = new FileReader();
        // 将图片将转成 base64 格式
        reader.readAsDataURL(pic);
        // 读取成功后的回调
        reader.onloadend = function() {
          let result = this.result;
          //let result = f;
          let img = new Image();
          img.src = result;
          img.onload = function() {
            let data = self.compress(img, Orientation);
            self.headerImage = data;
            ndata = self.compress(img, Orientation);
            //console.log('111',self.headerImage)
            self.postImg(ndata);
          };
        };
      }
    },
 
    async postImg(ndata) {
      let self = this;
      var FileUpload = function(ndata) {
        //console.log(index)
        self.$api
          .uploadFile({
            fileType:6304,
            base64Data: ndata.split(",")[1], // 文件base64
            suffix: ndata
              .split(",")[0]
              .split(";")
              ["0"].split("/")[1]
          })
          .then(res => {
            console.log("res", res.body);
            console.log("self.fileTypeAr", self.fileTypeAr);
            self.pic = ndata
            //self.currentItem.fileIds.push(res.body)
            //self.fileTypeArr.fileIds.push(res.body)
            if(self.form.fileIds.length<1){
                self.form.fileIds.push(res.body.id)
            }else{
                self.form.fileIds.shift()
                self.form.fileIds.push(res.body.id)
            }
          });
      };
      await FileUpload(ndata);
    },
    compress(img, Orientation) {
      let canvas = document.createElement("canvas");
      let ctx = canvas.getContext("2d");
      //瓦片canvas
      let tCanvas = document.createElement("canvas");
      let tctx = tCanvas.getContext("2d");
      let initSize = img.src.length;
      let width = img.width;
      let height = img.height;
      //如果图片大于四百万像素,计算压缩比并将大小压至400万以下
      let ratio;
      if ((ratio = (width * height) / 4000000) > 1) {
        console.log("大于400万像素");
        ratio = Math.sqrt(ratio);
        width /= ratio;
        height /= ratio;
      } else {
        ratio = 1;
      }
      canvas.width = width;
      canvas.height = height;
      //         铺底色
      ctx.fillStyle = "#fff";
      ctx.fillRect(0, 0, canvas.width, canvas.height);
      //如果图片像素大于100万则使用瓦片绘制
      let count;
      if ((count = (width * height) / 1000000) > 1) {
        console.log("超过100W像素");
        count = ~~(Math.sqrt(count) + 1); //计算要分成多少块瓦片
        //            计算每块瓦片的宽和高
        let nw = ~~(width / count);
        let nh = ~~(height / count);
        tCanvas.width = nw;
        tCanvas.height = nh;
        for (let i = 0; i < count; i++) {
          for (let j = 0; j < count; j++) {
            tctx.drawImage(
              img,
              i * nw * ratio,
              j * nh * ratio,
              nw * ratio,
              nh * ratio,
              0,
              0,
              nw,
              nh
            );
            ctx.drawImage(tCanvas, i * nw, j * nh, nw, nh);
          }
        }
      } else {
        ctx.drawImage(img, 0, 0, width, height);
      }
      //修复ios上传图片的时候 被旋转的问题
      if (Orientation != "" && Orientation != 1) {
        switch (Orientation) {
          case 6: //需要顺时针(向左)90度旋转
            this.rotateImg(img, "left", canvas);
            break;
          case 8: //需要逆时针(向右)90度旋转
            this.rotateImg(img, "right", canvas);
            break;
          case 3: //需要180度旋转
            this.rotateImg(img, "right", canvas); //转两次
            this.rotateImg(img, "right", canvas);
            break;
        }
      }
      //进行最小压缩
      let ndata = canvas.toDataURL("image/jpeg", 0.1);
 
      //console.log("压缩前:" + initSize);
      //console.log("压缩后:" + ndata.length);
      //console.log("ndata:" + ndata);
 
      console.log(
        "压缩率:" + ~~((100 * (initSize - ndata.length)) / initSize) + "%"
      );
      tCanvas.width = tCanvas.height = canvas.width = canvas.height = 0;
 
      return ndata;
    },
    afterRead(file) {
       this.uploadSectionFile(file.content, file.file) 
    },
    changeShowselect(item) {
      if (item.fileIds.length) {
        this.$tool.toast(`最多上传一张照片~`);
        return;
      }
      this.showSelect = true;
      this.currentItem = item;
    },
    imgCompress(path, options = {}) {
      return new Promise((resolve, reject) => {
        let img = new Image();
        img.src = path;
        img.onload = function() {
          let that = this,
            w = that.width,
            h = that.height,
            scale = w / h, // 默认按比例压缩
            quality = options.quality || 0.92,
            canvas = document.createElement("canvas"),
            ctx = canvas.getContext("2d");
 
          w = options.width || w;
          h = options.height || w / scale;
          canvas.width = w;
          canvas.height = h;
          ctx.drawImage(that, 0, 0, w, h);
          if (options.quality && options.quality <= 1 && options.quality > 0) {
            quality = options.quality;
          }
          let base64 = canvas.toDataURL("image/jpeg", quality);
          resolve(base64);
        };
      });
    },
    handleSelect(item, index) {
      if (index === 0) {
        this.$refs["picture"][0].click();
      } else {
        this.$refs["photo"][0].click();
      }
      this.showSelect = false;
    },
    // 删除图片
    deleteImgItem(index) {
      this.currentItem.fileIds.splice(index, 1);
    },
    // 获取base64
    getFile(el, fileType) {
      let file = el.files[0];
      if (typeof file === "undefined") return;
      let reader = new FileReader(),
        that = this;
      reader.readAsDataURL(file);
      reader.onload = function() {
        that.imgCompress(this.result).then(base64 => {
          that.upload(base64.split(",")[1], file.type.split("/")[1], fileType);
        });
      };
    },
    //上传文件
    upload(base64, suffix, fileType) {
      let params = {
        fileType,
        suffix,
        base64Data: base64
      };
      console.log(fileType)
      this.$api.uploadFile(params).then(res => {
        this.currentItem.fileIds.push(res.body);
      });
    },
    // 提交保存
    submit() {
      let form = this.form,
        tel = form.contMblNo;
 
      let len = this.fileTypeArr.length;
      console.log(this.fileTypeArr)
    //   for (let index = 0; index < len; index++) {
    //     const item = this.fileTypeArr[index];
    //     if (item.fileIds.length === 0) {
    //       this.$tool.toast(`请上传${item.name}`);
    //       return;
    //     }
    //   }
        if(form.fileIds.length<1){
            this.$tool.toast(`请上传合约凭证截图`)
            return
        }
      if (tel.length === 0) {
        this.$tool.toast("请输入电话号码");
        return;
      }
 
      if (!this.$tool.checkPhone(tel)) {
        this.$tool.toast("请输入正确的电话号码");
        return;
      }
      let params = {
        orderId: form.orderId,
        contMblNo: tel,
        fileIds: this.form.fileIds
      };
        
    //   let fileIds = [];
 
    //   for (let k = 0; k < len; k++) {
    //     const item = this.fileTypeArr[k];
    //     let arr = item.fileIds;
    //     arr.forEach(child => {
    //       fileIds.push(child.id);
    //     });
    //     // fileIds.push(...item.fileIds);
    //   }
 
      //params.fileIds = fileIds;
      this.$api.h_uploadXygProtocol(params).then(res => {
        this.$tool.toast("提交成功");
        this.$router.go(-1);
      });
    }
  }
};
</script>
 
<style lang="less" scoped>
.zxh-upload-box {
  padding-top: 56px;
  background-color: #f1f1f1;
  .up-div {
    width: 100%;
    height: 250px;
    background: rgba(255, 255, 255, 1);
    box-shadow: 1px 5px 13px 0px rgba(0, 0, 0, 0.08);
    padding: 20px;
    box-sizing: border-box;
    p:nth-of-type(1) {
      font-size: 14px;
      font-family: PingFang SC;
      font-weight: 500;
      color: rgba(51, 51, 51, 1);
      margin-bottom: 10px;
    }
    .upload-div {
      width: 100px;
      height: 100px;
      background: rgba(249, 250, 255, 1);
      border: 1px dashed rgba(205, 211, 246, 1);
      border-radius: 4px;
      text-align: center;
      line-height: 100px;
      img {
        width: 100%;
        height: 100%;
      }
      i {
        font-size: 30px;
        color: #8365e1;
      }
    }
    p:nth-of-type(2) {
      margin: 16px 0 10px 0;
      font-size: 13px;
      font-family: PingFang SC;
      font-weight: 500;
      color: rgba(153, 153, 153, 1);
    }
  }
  .btn {
    width: 88%;
    height: 44px;
    background: rgba(137, 110, 219, 1);
    border-radius: 22px;
    color: white;
    outline: none;
    border: 0;
    margin-left: 6%;
    margin-top: 37px;
  }
}
 
.content {
  margin-bottom: 10px;
  padding-bottom: 12px;
  background-color: @color-white;
 
  .title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 12px;
    height: 49px;
    line-height: 49px;
    font-size: @font-size-base;
    font-weight: @font-weight-base;
 
    input {
      display: none;
    }
  }
}
 
.upload {
  margin: 5px 12px 0;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-wrap: wrap;
 
  .upload-item {
    margin-right: 12px;
    width: 109px;
    height: 109px;
    position: relative;
    background-position: center;
    background-size: contain;
    background-repeat: no-repeat;
    margin-bottom: 12px;
 
    &:nth-child(3n) {
      margin-right: 0;
    }
 
    .iconfont {
      position: absolute;
      right: 3px;
      top: 0;
    }
  }
}
 
#file {
  display: none;
}
 
.scene_Staging-xiangji {
  fill: @color-border-theme;
  color: @color-border-theme;
  font-size: 20px;
}
</style>