ann0707
2018-08-16 c9bc8ec61cff4076132f6396d99d383a2cdf5a03
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
<template>
    <div class="feedback-page">
        <x-header :left-options="{backText: ''}">意见反馈</x-header>
        <div class="feedback-head">
            <textarea class="mine-page-division" v-model.trim="questionText" maxlength="200" placeholder="请反馈在使用过程中遇到的问题和建议"></textarea>
            <span class="feedback-num">{{ questionText.length }}/200字</span>
        </div>
        <input class="feedback-input" v-model="mblType" type="text" maxlength='20' placeholder="请输入机型,例:红米note3(选填,方便更快解决问题)">
        <div class="feedback-foot">
            <p>添加图片(选填,提供问题截图)</p>
            <div class="upload-box">
                <!--图片列表-->
                <div class="upload-list-box">
                    <ul class="upload-list">
                        <li v-for="(item, index) in uploadImgList" class="upload-list-img">
                            <img :src="item">
                            <icon type="clear" @click.native="deleteCurrentImg(index)"></icon>
                        </li>
                        <!--上传按钮, 当上传的张数4张的时候,消失掉-->
                        <li class="upload-action-box" v-show="uploadImgList.length < 4">
                            <i class="iconfont icon-camerafill"></i>
                            <input class="upload-input" type="file" @change="handleImgUpload($event)" accept="image/*">
                        </li>
                    </ul>
                </div>
                <!--上传按钮, 当上传的张数4张的时候,消失掉-->
                <!--<div class="upload-action-box" v-show="uploadImgList.length < 4">-->
                <!--<i class="iconfont icon-camerafill"></i>-->
                <!--<input class="upload-input" type="file" @change="handleImgUpload($event)" accept="image/*">-->
                <!--</div>-->
            </div>
        </div>
        <FButton :text="'提交'" @click.native="handleSubmit"></FButton>
        <FSpace type="large"></FSpace>
    </div>
</template>
<script>
    /**
     * Created by c.y on 2018/3/22.
     * 我的--意见反馈
     */
    import {XHeader, Icon, XButton} from 'vux';
    import FButton from '../../components/common/FButton';
    import FSpace from '../../components/common/FSpace';
    import validator from '../../tool/validator';
    import EXIF from 'exif-js';
    import SystemApi from '../../api/api';
    import statusCodeManage from '../../api/statusCodeManage';
 
    export default {
        name: 'f-feedback',
        data() {
            return {
                questionText: '', // 咨询的问题
                mblType: '', // 机型
                uploadImgList: [] // 上传的图片的列表
            };
        },
        methods: {
            handleSubmit() {
                let _this = this;
                if (validator.checkValEmpty(this.questionText)) {
                    this.$vux.toast.text(
                        '说点什么吧,您的建议是我们前进的动力',
                        'middle'
                    );
                    return false;
                }
                // 验证通过后
                let submitInfo = {
                    content: this.questionText
                };
                if (!validator.checkValEmpty(this.mblType)) {
                    submitInfo.mblType = this.mblType;
                }
                if (this.uploadImgList.length > 0) {
                    submitInfo.fileNames = this.uploadImgList;
                }
                SystemApi.submitFeedBack(submitInfo).then(
                    res => {
                        // 跳转至【个人】页面
                        this.$vux.toast.text(
                            '反馈已收到,我们会马上跟进',
                            'middle'
                        );
                        setTimeout(function() {
                            _this.$router.push('/f-mine');
                        }, 2000);
                    },
                    error => {
                        statusCodeManage.showTipOfStatusCode(error, _this);
                    }
                );
            },
            // 图片的删除
            deleteCurrentImg: function(index) {
                // 删除图片
                let _this = this;
                let par = {
                    fileUrl: _this.uploadImgList[index],
                    fileType: 1
                };
                SystemApi.deleteIMG(par).then(
                    function(res) {
                        if (res.errorCode === 0) {
                            _this.uploadImgList.splice(index, 1);
                        }
                    },
                    function(error) {
                        statusCodeManage.showTipOfStatusCode(error, _this);
                    }
                );
            },
            // 图片上传
            handleImgUpload(evt) {
                const files = Array.prototype.slice.call(evt.target.files);
                console.log(files);
                let that = this;
                files.forEach(function(file, i) {
                    let orientation;
                    if (!/\/(?:jpeg|png|gif)/i.test(file.type)) return;
                    // 读取图片的元信息
                    EXIF.getData(file, function() {
                        orientation = EXIF.getTag(this, 'Orientation');
                    });
                    let reader = new FileReader();
                    reader.onload = function() {
                        let result = this.result;
                        // 使用exif
                        that.getImgData(this.result, orientation, function(data) {
                            // 这里可以使用校正后的图片data了
                            var img = new Image();
                            img.src = data;
                            // 图片加载完毕之后进行压缩,然后上传
                            if (img.complete) {
                                callback();
                            } else {
                                img.onload = callback;
                            }
 
                            function callback() {
                                var data = that.compress(img);
                                that.upload(data, file.type, file.name);
                                // 发送后台请求
                            }
                        });
                    };
                    reader.readAsDataURL(file);
                });
            },
            // 压缩图片
            compress(img) {
                // 用于压缩图片的canvas
                let canvas = document.createElement('canvas');
                let ctx = canvas.getContext('2d');
                //    瓦片canvas
                var tCanvas = document.createElement('canvas');
                var tctx = tCanvas.getContext('2d');
                let width = img.width;
                let height = img.height;
                // 如果图片大于四百万像素,计算压缩比并将大小压至400万以下
                var ratio;
                if ((ratio = width * height / 4000000) > 1) {
                    ratio = Math.sqrt(ratio);
                    width /= ratio;
                    height /= ratio;
                } else {
                    ratio = 1;
                }
                canvas.width = width * 2;
                canvas.height = height * 2;
                // 铺底色
                ctx.fillStyle = '#fff';
                ctx.fillRect(0, 0, canvas.width, canvas.height);
                // 如果图片像素大于100万则使用瓦片绘制
                var count;
                if ((count = width * height / 1000000) > 1) {
                    count = ~~(Math.sqrt(count) + 1); // 计算要分成多少块瓦片
                    // 计算每块瓦片的宽和高
                    var nw = ~~(width / count);
                    var nh = ~~(height / count);
                    tCanvas.width = nw;
                    tCanvas.height = nh;
                    for (var i = 0; i < count; i++) {
                        for (var j = 0; j < count; j++) {
                            tctx.drawImage(
                                img,
                                i * nw * ratio,
                                j * nh * ratio,
                                nw * ratio * 2,
                                nh * ratio * 2,
                                0,
                                0,
                                nw,
                                nh
                            );
                            ctx.drawImage(tCanvas, i * nw, j * nh, nw * 2, nh * 2);
                        }
                    }
                } else {
                    ctx.drawImage(img, 0, 0, width * 2, height * 2);
                }
                // 进行最小压缩
                let ndata = canvas.toDataURL('image/jpeg', 0.8);
                return ndata;
            },
            // 提交到服务器
            upload(basestr) {
                let _this = this;
                let post = {
                    file: basestr.split(',')[1],
                    extName: 'jpeg',
                    fileType: 1
                };
                // 选择完毕触发事件  回调
                SystemApi.uploadIMG(post).then(
                    res => {
                        _this.uploadImgList.push(res.body);
                    },
                    err => {
                        statusCodeManage.showTipOfStatusCode(err, _this);
                    }
                );
            },
            // 图片旋转处理
            getImgData: function(img, dir, next) {
                // @param {string} img 图片的base64
                // @param {int} dir exif获取的方向信息
                // @param {function} next 回调方法,返回校正方向后的base64
                var image = new Image();
                let canvas = document.createElement('canvas');
                let ctx = canvas.getContext('2d');
                image.onload = function() {
                    let resultWidth = 0;
                    let resultHeight = 0;
                    let maxWidthOrHeight = 1024;
                    let drawWidth = this.naturalWidth;
                    let drawHeight = this.naturalHeight;
                    // 以下改变图片大小,控制最大宽度为1024
                    var maxSide = Math.max(drawWidth, drawHeight);
                    if (maxSide > maxWidthOrHeight) {
                        let minSide = Math.min(drawWidth, drawHeight);
                        minSide = minSide / maxSide * maxWidthOrHeight;
                        maxSide = maxWidthOrHeight;
                        if (drawWidth > drawHeight) {
                            resultWidth = maxSide;
                            resultHeight = minSide;
                        } else {
                            resultWidth = minSide;
                            resultHeight = maxSide;
                        }
                    } else {
                        resultWidth = drawWidth;
                        resultHeight = drawHeight;
                    }
                    if (dir === 3) {
                        canvas.width = resultWidth;
                        canvas.height = resultHeight;
                        ctx.translate(resultWidth, resultHeight);
                        ctx.rotate(Math.PI);
                    } else if (dir === 6) {
                        canvas.width = resultHeight;
                        canvas.height = resultWidth;
                        ctx.translate(resultHeight, 0);
                        ctx.rotate(Math.PI / 2);
                    } else if (dir === 8) {
                        canvas.width = resultHeight;
                        canvas.height = resultWidth;
                        ctx.translate(0, resultWidth);
                        ctx.rotate(-Math.PI / 2);
                    } else {
                        canvas.width = resultWidth;
                        canvas.height = resultHeight;
                    }
                    ctx.drawImage(this, 0, 0, resultWidth, resultHeight);
                    // 返回校正图片
                    next(canvas.toDataURL('image/jpeg', 0.8));
                };
                image.src = img;
            }
        },
        components: {
            FButton,
            XButton,
            XHeader,
            Icon,
            FSpace
        },
        activated: function() {
            this.$store.commit('UPDATE_DIRECTION', {direction: 'in'});
            this.questionText = '';
            this.mblType = '';
            this.uploadImgList = [];
        },
        deactivated() {
            this.$store.commit('UPDATE_DIRECTION', {direction: 'none'});
        }
    };
</script>
 
<style lang="less">
@import '../../style/mixin.less';
 
.feedback-page {
    .input-btnbox {
        padding: 0 12px;
        .weui-btn {
            height: 44px;
            .color-linear-gradient(@color-primary-light, @color-primary, 90deg);
            color: @color-white;
            font-size: 16px;
        }
        .weui-btn:after {
            display: none;
        }
    }
    input::-webkit-input-placeholder {
        color: #999 !important; /* WebKit browsers */
    }
    .search_input::-webkit-input-placeholder {
        color: #999 !important;
    }
    background-color: @color-background-default;
    .vux-header {
        .color-linear-gradient(@color-primary-light, @color-primary, 90deg);
        .vux-header-title {
            font-size: 18px;
        }
        .vux-header-left {
            .left-arrow:before {
                border: solid 1px @color-white;
                border-width: 2px 0 0 2px;
            }
        }
    }
    .feedback-head {
        position: relative;
        margin-bottom: 20px;
        height: 215px;
        .mine-page-division {
            width: 100%;
            height: 215px;
            border-width: 0px;
            margin-top: 10px;
            resize: none;
            outline: none;
            padding: 15px 12px 10px 12px;
            box-sizing: border-box;
            font-size: @font-size-medium;
        }
        .feedback-num {
            position: absolute;
            right: 12px;
            bottom: 0px;
            color: @color-text-third;
        }
    }
    .feedback-input {
        width: 100%;
        box-sizing: border-box;
        border-width: 0px;
        outline: none;
        padding: 0 12px;
        height: 44px;
        margin-bottom: 10px;
    }
    .feedback-foot {
        padding: 15px 12px;
        background-color: @color-white;
        p {
            color: @color-text-third;
            margin-bottom: 16px;
        }
        .feedback-add {
            width: 100%;
            height: 84px;
            img {
                width: 79px;
                height: 79px;
            }
        }
    }
    .f-button {
        margin-top: 45px;
    }
    .upload-box {
        background-color: @color-white;
        padding-bottom: 10px;
        .flexLayout();
    }
    .upload-list {
        flex: 1;
        background-color: @color-white;
        .upload-list-img {
            position: relative;
            display: inline-block;
            vertical-align: middle;
            width: 78px;
            height: 80px;
            margin-right: 10px;
            margin-bottom: 20px;
            .weui-icon {
                position: absolute;
                right: -14px;
                top: -10px;
                font-size: 20px;
                /*background-color: #fff;*/
                color: #ff4d53;
            }
            &:first-child {
                margin-left: 0;
            }
            &:last-child {
                margin-right: 0;
            }
            img {
                width: 80px;
                height: 80px;
            }
        }
    }
    .upload-action-box {
        position: relative;
        width: 80px;
        height: 80px;
        margin-bottom: 20px;
        list-style: none;
        display: inline-block;
        vertical-align: middle;
        text-align: center;
        background: @color-background-default;
        border: 1px solid @color-divider-regular;
        box-sizing: border-box;
        .iconfont {
            font-size: 30px;
            line-height: 80px;
            color: @color-divider-regular;
        }
        .upload-input {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            opacity: 0;
        }
    }
}
</style>