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
| <template>
| <view
| class="tn-avatar-class tn-avatar"
| :class="[backgroundColorClass,avatarClass]"
| :style="[avatarStyle]"
| @tap="click"
| >
| <image
| v-if="showImg"
| class="tn-avatar__img"
| :class="[imgClass]"
| :src="src"
| :mode="imgMode || 'aspectFill'"
| @error="loadImageError"
| ></image>
| <view v-else class="tn-avatar__text" >
| <view v-if="text">{{ text }}</view>
| <view v-else :class="[`tn-icon-${icon}`]"></view>
| </view>
|
| <!-- 角标 -->
| <tn-badge
| v-if="badge && (badgeIcon || badgeText)"
| :radius="badgeSize"
| :backgroundColor="badgeBgColor"
| :fontColor="badgeColor"
| :fontSize="badgeSize - 8"
| :absolute="true"
| :top="badgePosition[0]"
| :right="badgePosition[1]"
| >
| <view v-if="badgeIcon && badgeText === ''">
| <view :class="[`tn-icon-${badgeIcon}`]"></view>
| </view>
| <view v-else>
| {{ badgeText }}
| </view>
| </tn-badge>
| </view>
| </template>
|
| <script>
| import componentsColorMixin from '../../libs/mixin/components_color.js'
| export default {
| mixins: [componentsColorMixin],
| name: 'tn-avatar',
| props: {
| // 序号
| index: {
| type: [Number, String],
| default: 0
| },
| // 头像类型
| // square 带圆角正方形 circle 圆形
| shape: {
| type: String,
| default: 'circle'
| },
| // 大小
| // sm 小头像 lg 大头像 xl 加大头像
| // 如果为其他则认为是直接设置大小
| size: {
| type: [Number, String],
| default: ''
| },
| // 是否显示阴影
| shadow: {
| type: Boolean,
| default: false
| },
| // 是否显示边框
| border: {
| type: Boolean,
| default: false
| },
| // 边框颜色
| borderColor: {
| type: String,
| default: 'rgba(0, 0, 0, 0.1)'
| },
| // 边框大小, rpx
| borderSize: {
| type: Number,
| default: 2
| },
| // 头像路径
| src: {
| type: String,
| default: ''
| },
| // 文字
| text: {
| type: String,
| default: ''
| },
| // 图标
| icon: {
| type: String,
| default: ''
| },
| // 当设置为显示头像信息时,
| // 图片的裁剪模式
| imgMode: {
| type: String,
| default: 'aspectFill'
| },
| // 是否显示角标
| badge: {
| type: Boolean,
| default: false
| },
| // 设置显示角标后,角标大小
| badgeSize: {
| type: Number,
| default: 0
| },
| // 角标背景颜色
| badgeBgColor: {
| type: String,
| default: '#AAAAAA'
| },
| // 角标字体颜色
| badgeColor: {
| type: String,
| default: '#FFFFFF'
| },
| // 角标图标
| badgeIcon: {
| type: String,
| default: ''
| },
| // 角标文字,优先级比icon高
| badgeText: {
| type: String,
| default: ''
| },
| // 角标坐标
| // [top, right]
| badgePosition: {
| type: Array,
| default() {
| return [0, 0]
| }
| }
| },
| data() {
| return {
| // 图片显示是否发生错误
| imgLoadError: false
| }
| },
| computed: {
| showImg() {
| // 如果设置了图片地址,则为显示图片,否则为显示文本
| return this.text === '' && this.icon === ''
| },
| avatarClass() {
| let clazz = ''
| clazz += ` tn-avatar--${this.shape}`
|
| if (this._checkSizeIsInline()) {
| clazz += ` tn-avatar--${this.size}`
| }
|
| if (this.shadow) {
| clazz += ' tn-avatar--shadow'
| }
|
| return clazz
| },
| avatarStyle() {
| let style = {}
|
| if (this.backgroundColorStyle) {
| style.background = this.backgroundColorStyle
| } else if (this.shadow && this.showImg) {
| style.backgroundImage = `url(${this.src})`
| }
|
| if (this.border) {
| style.border = `${this.borderSize}rpx solid ${this.borderColor}`
| }
|
| if (!this._checkSizeIsInline()) {
| style.width = this.size
| style.height = this.size
| }
|
| return style
| },
| imgClass() {
| let clazz = ''
| clazz += ` tn-avatar__img--${this.shape}`
|
| return clazz
| }
| },
| methods: {
| // 加载图片失败
| loadImageError() {
| this.imgLoadError = true
| },
| // 点击事件
| click() {
| this.$emit("click", this.index)
| },
|
| // 检查是否使用内置的大小进行设置
| _checkSizeIsInline() {
| if (/^(xs|sm|md|lg|xl|xxl)$/.test(this.size)) return true
| else return false
| }
| }
| }
| </script>
|
| <style lang="scss" scoped>
|
| .tn-avatar {
| /* #ifndef APP-NVUE */
| display: inline-flex;
| /* #endif */
| margin: 0;
| padding: 0;
| text-align: center;
| align-items: center;
| justify-content: center;
| background-color: $tn-font-holder-color;
| // color: #FFFFFF;
| white-space: nowrap;
| position: relative;
| width: 64rpx;
| height: 64rpx;
| z-index: 1;
|
| &--sm {
| width: 48rpx;
| height: 48rpx;
| }
| &--lg {
| width: 96rpx;
| height: 96rpx;
| }
| &--xl {
| width: 128rpx;
| height: 128rpx;
| }
|
| &--square {
| border-radius: 10rpx;
| }
|
| &--circle {
| border-radius: 5000rpx;
| }
|
| &--shadow {
| position: relative;
|
| &::after {
| content: " ";
| display: block;
| background: inherit;
| filter: blur(10rpx);
| position: absolute;
| width: 100%;
| height: 100%;
| top: 10rpx;
| left: 10rpx;
| z-index: -1;
| opacity: 0.4;
| transform-origin: 0 0;
| border-radius: inherit;
| transform: scale(1, 1);
| }
| }
|
| &__img {
| width: 100%;
| height: 100%;
|
| &--square {
| border-radius: 10rpx;
| }
|
| &--circle {
| border-radius: 5000rpx;
| }
| }
|
| &__text {
| display: flex;
| flex-direction: row;
| align-items: center;
| justify-content: center;
| }
| }
| </style>
|
|