cuiqian2004
8 天以前 f57571f987f3d25730123f488fccdfa3158ec5b1
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
<template>
    <!-- <view ref="test" class="page-my-cropper-image">
        <csImageEditor ref="csImageEditor" :imageSrc="imageSrc" :circleDiameter="100" @sendUrl="updateImage"></csImageEditor>
        <view class="cropper-button">
            <button class="btn_default" @click="cancel">取消</button>
            <text class="ico fs-time" @click="mirrorImage"></text>
            <text class="ico fs-time" @click="rotateCounterClockwise"></text>
            <button type="primary" class="btn" @click="ok">确定</button>
        </view>
    </view> -->
    <!--  <view class="content">
    <cs-ImageEditor ref="csImageEditor" :imageSrc="imageSrc" @sendUrl="updateImage"></cs-ImageEditor>
    <view style="margin-top: 100rpx;">
      <button type="primary" @tap="clickCamera">选择图片</button>
      <button style="margin-top: 20rpx;" type="primary" @tap="rotateCounterClockwise">旋转</button>
      <button style="margin-top: 20rpx;" type="primary" @tap="mirrorImage">镜像</button>
      <button style="margin-top: 20rpx;" type="primary" @click="ok">保存图片到相册</button>
    </view>
  </view> -->
        <view class="page-my-cropper-image">
        <view ref="test" class="cropper-wrap">
            <ImageCropper id="image-cropper" ref="refImageCropper" :zoom="1" :angle="angle" :src="imageSrc"
                canvasBackground="red" @cropped="cropped" @afterDraw="afterDraw" @beforeDraw="beforeDraw" />
        </view>
        <view class="cropper-button">
            <button class="btn_default" @click="cancel">取消</button>
            <text class="ico fs-time" @click="onAngle"></text>
            <button type="primary" class="btn" @click="ok">确定</button>
        </view>
 
    </view>
</template>
 
<script>
    import ImageCropper from '@/components/nice-cropper/cropper.vue'
    //import csImageEditor from '@/components/csImageEditor/index.vue'
 
    //import csImageEditor from '@/uni_modules/cs-ImageEditor/components/cs-ImageEditor/cs-ImageEditor.vue'
    import {
        showModal,
        showToast
    } from "@/common/Page.js"
    import TaskInit from "@/common/extend.js"
    export default {
        name: "pagesMyCropperImage",
        components: {
            ImageCropper,
            //csImageEditor
        },
 
        data() {
            return {
                imageSrc: '/static/logo.png',
                angle: 0,
                croppedPath: "",
                maxSize: 100 * 1024,
                tempFilePath: "",
                cropperOk:false,
            }
        },
        methods: {
            setData(obj) {
                let that = this;
                let keys = [];
                let val, data;
 
                Object.keys(obj).forEach(function(key) {
                    keys = key.split(".");
                    val = obj[key];
                    data = that.$data;
                    keys.forEach(function(key2, index) {
                        if (index + 1 == keys.length) {
                            that.$set(data, key2, val);
                        } else {
                            if (!data[key2]) {
                                that.$set(data, key2, {});
                            }
                        }
                        data = data[key2];
                    });
                });
            },
            beforeDraw(context, transform) {
                context.setFillStyle('yellow')
                transform.zoom = 2
            },
            afterDraw(ctx, info) {
                console.log(`当前画布大小:${info.width}*${info.height}`)
            },
            cropped(imagePath, imageInfo) {
                console.log("cropped", imagePath, imageInfo)
                this.croppedPath = imagePath
                
                this.checkCroppedImage()
            },
 
            onAngle() {
 
                this.setData({
                    angle: (this.angle + 90) % 360
                })
                this.$refs.refImageCropper.setRotate(this.angle)
 
            },
            ok() {
                this.cropperOk = true
                if(this.$refs.csImageEditor)
                    this.$refs.csImageEditor.drawImage()
                if(this.$refs.refImageCropper)
                {
                    
                    this.$refs.refImageCropper.draw()
                }
            },
            checkCroppedImage() {
                const _this = this
                if( !this.cropperOk )return
                // #ifdef MP-WEIXIN
                uni.getFileSystemManager().getFileInfo({
                    filePath: _this.croppedPath,
                    success: function(res) {
                        if (_this.maxSize > 0) {
                            if (res.size > _this.maxSize) {
                                const limitSize = TaskInit.fileUtils.getFileSizeStr(_this.maxSize)
                                showModal(`图片大小超出限制,请限制在 ${limitSize} 以内`, "提示", false)
                                return
                            }
 
                        }
 
                        _this.corppedSuccess(_this.croppedPath)
                    },
                    fail: function(res) {
                        console.error(res)
                    }
                })
                // #endif
                // #ifndef MP-WEIXIN
                uni.getFileInfo({
                    filePath: _this.croppedPath,
                    success: (res) => {
                        if (_this.maxSize > 0) {
                            if (res.size > _this.maxSize) {
                                const limitSize = TaskInit.fileUtils.getFileSizeStr(_this.maxSize)
                                showModal(`图片大小超出限制,请限制在 ${limitSize} 以内`, "提示", false)
                                return
                            }
 
                        }
                        _this.corppedSuccess(_this.croppedPath)
                    },
                    fail: (res) => {
                        console.error(res)
                    }
                })
                // #endif
 
            },
            cancel() {
                uni.navigateBack({
                    delta: 1
                });
            },
            updateImage(obj) {
                console.log('updateImage', obj)
                this.cropperOk = true
                this.croppedPath = obj.filePath
                this.checkCroppedImage()
            },
            rotateCounterClockwise() {
                if(this.$refs.csImageEditor)
                this.$refs.csImageEditor.rotateCounterClockwise()
            },
            mirrorImage() {
                if(this.$refs.csImageEditor)
                this.$refs.csImageEditor.mirrorImage()
            },
            corppedSuccess(path) {
                const eventChannel = this.getOpenerEventChannel();
                eventChannel.emit('imageCorpped', {
                    path: path
                });
                uni.navigateBack({
                    delta: 1
                });
            },
            clickCamera() {
                const _this = this
                let sizeType = ['original', 'compressed']
                //#ifdef MP-DINGTALK
                sizeType = ['compressed']
                // #endif
                uni.chooseImage({
                    count: 1,
                    sizeType: sizeType, //original 原图,compressed 压缩图,默认二者都有
                    sourceType: ['album', 'camera'], //album 从相册选图,camera 使用相机,默认二者都有。
                    success: (res) => {
                        if (res.tempFilePaths) {
                            _this.setData({imageSrc: res.tempFilePaths[0]})
                            console.log('chooseImage ', _this.imageSrc,res)
                            if(_this.$refs.csImageEditor)
                            _this.$refs.csImageEditor.init()
                        }
                    },
                    fail: (err) => {
                        console.log('chooseImage fail', err)
                        if (this.isMobilePlatform && err.errMsg.indexOf('cancel') < 0 && err.errMsg.indexOf(
                                '取消') < 0) {
                            console.log('chooseImage 取消')
                            uni.getSetting({
                                success: (res) => {
                                    let authStatus = res.authSetting['scope.album'];
                                    if (!authStatus) {
                                        showModal("需要从您的相册获取图片,请在设置界面打开相关权限", "授权失败").then((
                                            res) => {
                                            if (res) {
                                                uni.openSetting()
                                                uni.navigateBack({
                                                    delta: 1
                                                });
                                            }
                                        })
                                    }
                                },
                                fail: (res) => {
                                    uni.navigateBack({
                                        delta: 1
                                    });
                                }
                            })
                        } else {
                            uni.navigateBack({
                                delta: 1
                            });
                        }
                    }
                })
 
            }
 
        },
        onLoad(option) {
            // #ifdef MP-DINGTALK
            
            my.setNavigationBar({
                backgroundColor: "#007AFF",
                frontColor: "#ffffff"
            })
            // #endif
            this.setData({
                // imageSrc: option.imageSrc,
                maxSize: option.maxSize || 100 * 1024,
            })
            this.clickCamera()
 
 
        },
 
    }
</script>
 
<style lang="less">
    page {
        /* #ifdef MP-ALIPAY */
        position: absolute;
        width: 100%;
        /* #endif */
        height: 100%;
        box-sizing: border-box;
    }
 
    .page-my-cropper-image {
        height: 100%;
 
        .cropper-wrap {
            height: 100%;
        }
 
        .cropper-button {
            display: flex;
            flex-direction: row;
            position: absolute;
            bottom: 30rpx;
            left: 0;
            right: 0;
            height: 50px;
            padding: 0 10px;
            justify-content: space-between;
            color: #fff;
 
            .ico {
                font-size: 32px;
                padding: 5px;
                margin-top: 5px;
            }
 
            .btn_default {
                padding: 0 8px;
                border: 0;
                background: transparent;
                color: #fff;
            }
 
            .btn {
                padding: 0 8px;
                border-radius: 5px;
 
            }
 
        }
    }
</style>