zhangxiaoxu123456
2021-12-06 f32f7d699607bb159d25dcf441942370aba2c968
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
<template>
    <view class="u-demo">
        <view class="u-demo-wrap">
            <view class="u-demo-title">演示效果</view>
            <view class="u-demo-area">
                <u-toast ref="uToast"></u-toast>
                <view class="u-avatar-wrap">
                    <image @tap="preAvatar" class="u-avatar-demo" v-if="avatar" :src="avatar" mode="aspectFill"></image>
                </view>
                <u-button @click="chooseAvatar">选择图片</u-button>
            </view>
        </view>
        <!-- <view class="u-config-wrap">
            <view class="u-config-title u-border-bottom">
                参数配置
            </view>
            <view class="u-config-item">
                <view class="u-item-title">生成图片质量</view>
                <u-subsection :current="1" :list="['0.3', '0.7', '1']" @change="qualityChange"></u-subsection>
            </view>
            <view class="u-config-item">
                <view class="u-item-title">自定义参数</view>
                <u-subsection :current="1" :list="['是', '否']" @change="styleChange"></u-subsection>
            </view>
        </view> -->
    </view>
</template>
 
<script>
    export default {
        data() {
            return {
                avatar: 'https://cdn.uviewui.com/uview/common/logo.png',
            }
        },
        created() {
            uni.$on('uAvatarCropper', path => {
                this.avatar = path;
                // 可以在此上传到服务端
                // uni.uploadFile({
                //     url: 'http://192.168.100.17/index.php/index/index/upload',
                //     filePath: path,
                //     name: 'file',
                //     complete: (res) => {
                //         console.log(res);
                //     }
                // });
            })
        },
        methods: {
            chooseAvatar() {
                this.$u.route({
                    url: '/uview-ui/components/u-avatar-cropper/u-avatar-cropper',
                    params: {
                        // 输出图片宽度,高等于宽,单位px
                        destWidth: 300,
                        // 裁剪框宽度,高等于宽,单位px
                        rectWidth: 200,
                        // 输出的图片类型,如果'png'类型发现裁剪的图片太大,改成"jpg"即可
                        fileType: 'jpg',
                    }
                })
            },
            qualityChange(index) {
                this.quality = index == 0 ? 0.3 : index == 1 ? 0.7 : 1;
            },
            styleChange(index) {
                if (index == 0) {
                    this.rectHeight = this.rectWidth = this.destHeight = this.destWidth = 200;
                    this.boundStyle = {
                        lineWidth: 8,
                        borderColor: this.$u.color['error'],
                        mask: 'rgba(0, 0, 0, 0.8)'
                    }
                } else {
                    this.rectHeight = this.rectWidth = this.destHeight = this.destWidth = 400;
                    this.boundStyle = {
                        lineWidth: 4,
                        borderColor: 'rgb(245, 245, 245)',
                        mask: 'rgba(0, 0, 0, 0.35)'
                    }
                }
            },
            // 预览图片
            preAvatar() {
                wx.previewImage({
                    current: '', // 当前显示图片的 http 链接
                    urls: [this.avatar] // 需要预览的图片 http 链接列表
                })
            }
        }
    }
</script>
 
<style lang="scss" scoped>
    .wrap {
        padding: 24rpx;
    }
 
    .u-avatar-wrap {
        overflow: hidden;
        margin-bottom: 20rpx;
    }
 
    .u-avatar-demo {
        width: 150rpx;
        height: 150rpx;
        border-radius: 100rpx;
    }
</style>