zhangwei
2025-01-16 e3f6581c48b1dfb65c55e8a1a6ce1761a7dc26b4
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
<template>
    <!--本文件由FirstUI授权予四川政采招投标咨询有限公司(会员ID:  163,营业执照号:915101   31 3  32  0  0  6  19   3K)专用,请尊重知识产权,勿私下传播,违者追究法律责任。-->
    <view class="fui-avatar__wrap" :class="[width?'':'fui-avatar__size-'+size,radius===-1?'fui-avatar__'+shape:'']"
        :style="wrapStyles" @tap="handleClick">
        <image class="fui-avatar__img" :style="styles"
            :class="[radius===-1?'fui-avatar__'+shape:'',width?'':'fui-avatar__size-'+size]" :src="showImg" :mode="mode"
            v-if="src && src!==true" :webp="webp" :lazy-load="lazyLoad" @error="handleError"></image>
        <text class="fui-avatar__text" :class="[width?'':'fui-avatar__text-'+size]" v-if="!src && src!==true && text"
            :style="textStyles">{{text}}</text>
        <slot></slot>
    </view>
</template>
 
<script>
    export default {
        name: "fui-avatar",
        emits: ['click', 'error'],
        props: {
            src: {
                type: String,
                default: ''
            },
            errorSrc: {
                type: String,
                default: ''
            },
            mode: {
                type: String,
                default: 'scaleToFill'
            },
            //微信小程序、百度小程序、字节跳动小程序
            //图片懒加载。只针对page与scroll-view下的image有效
            lazyLoad: {
                type: Boolean,
                default: true
            },
            //默认不解析 webP 格式,只支持网络资源 微信小程序2.9.0
            webp: {
                type: Boolean,
                default: false
            },
            background: {
                type: String,
                default: '#D1D1D1'
            },
            //small(64)、middle(96)、large(128)
            size: {
                type: String,
                default: 'middle'
            },
            //图片宽度,设置大于0的数值生效,默认使用size
            width: {
                type: [Number, String],
                default: 0
            },
            //默认等宽,设置图大于0的数值且设置了图片宽度生效
            height: {
                type: [Number, String],
                default: 0
            },
            //指定头像的形状,可选值为 circle、square
            shape: {
                type: String,
                default: 'circle'
            },
            //图片圆角值,默认使用shape,当设置大于等于0的数值,shape失效
            radius: {
                type: [Number, String],
                default: -1
            },
            //没有src时可以使用文本代替
            text: {
                type: String,
                default: ''
            },
            color: {
                type: String,
                default: '#fff'
            },
            //默认使用size下字体大小
            fontSize: {
                type: [Number, String],
                default: 0
            },
            fontWeight: {
                type: [Number, String],
                default: 600
            },
            marginRight: {
                type: [Number, String],
                default: 0
            },
            marginBottom: {
                type: [Number, String],
                default: 0
            },
            //在列表中的索引值
            index: {
                type: Number,
                default: 0
            },
            //其他参数
            params: {
                type: [Number, String],
                default: 0
            }
        },
        computed: {
            styles() {
                let styles = '';
                if (this.width) {
                    styles = `width:${this.width}rpx;height:${this.height || this.width}rpx;`
                }
                if (this.radius !== -1) {
                    styles += `border-radius:${this.radius}rpx;`
                }
                return styles;
            },
            wrapStyles() {
                return `background:${this.background};margin-right:${this.marginRight}rpx;margin-bottom:${this.marginBottom}rpx;${this.styles};`
            },
            textStyles() {
                let styles = `color:${this.color};font-weight:${this.fontWeight};`;
                if (this.fontSize) {
                    styles += `font-size:${this.fontSize}rpx;`
                }
                return styles;
            }
        },
        watch: {
            src(val) {
                this.src && (this.showImg = this.src);
            }
        },
        data() {
            return {
                showImg: ''
            }
        },
        created() {
            this.src && (this.showImg = this.src);
        },
        methods: {
            handleError(e) {
                if (this.src) {
                    this.errorSrc && (this.showImg = this.errorSrc);
                    this.$emit('error', {
                        index: this.index,
                        params: this.params
                    })
                }
            },
            handleClick() {
                this.$emit('click', {
                    index: this.index,
                    params: this.params
                })
            }
        }
    }
</script>
 
<style scoped>
    .fui-avatar__wrap {
        position: relative;
        /* #ifndef APP-NVUE */
        display: inline-flex;
        overflow: hidden;
        flex-shrink: 0;
        z-index: 3;
        /* #endif */
        flex-direction: row;
        align-items: center;
        justify-content: center;
    }
 
    .fui-avatar__img {
        flex: 1;
        /* #ifndef APP-NVUE */
        display: block;
        object-fit: cover;
        flex-shrink: 0;
        /* #endif */
    }
 
    .fui-avatar__text {
        flex: 1;
        /* #ifndef APP-NVUE */
        display: block;
        white-space: nowrap;
        box-sizing: border-box;
        /* #endif */
        /* #ifdef APP-NVUE */
        lines: 1;
        /* #endif */
        overflow: hidden;
        text-overflow: ellipsis;
        text-align: center;
    }
 
    .fui-avatar__size-small {
        width: 64rpx !important;
        height: 64rpx !important;
 
    }
 
    .fui-avatar__text-small {
        font-size: 32rpx !important;
    }
 
    .fui-avatar__size-middle {
        width: 96rpx !important;
        height: 96rpx !important;
    }
 
    .fui-avatar__text-middle {
        font-size: 44rpx !important;
    }
 
    .fui-avatar__size-large {
        width: 128rpx !important;
        height: 128rpx !important;
    }
 
    .fui-avatar__text-large {
        font-size: 56rpx !important;
    }
 
    .fui-avatar__circle {
        /* #ifdef APP-NVUE */
        border-radius: 500px !important;
        /* #endif */
 
        /* #ifndef APP-NVUE */
        border-radius: 50% !important;
        /* #endif */
    }
 
    .fui-avatar__square {
        border-radius: 8rpx !important;
    }
</style>