Files
JoyD/Claw/client/wechat_app/components/user-avatar/user-avatar.js
2026-03-16 15:47:55 +08:00

117 lines
2.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 用户头像组件逻辑
const { DEFAULT_AVATAR } = require('../../utils/constant.js')
Component({
properties: {
// 头像图片地址
src: {
type: String,
value: ''
},
// 默认头像
defaultAvatar: {
type: String,
value: DEFAULT_AVATAR
},
// 尺寸small, medium, large, xlarge
size: {
type: String,
value: 'medium'
},
// 形状circle, rounded, square
shape: {
type: String,
value: 'circle'
},
// 图片裁剪模式
mode: {
type: String,
value: 'aspectFill'
},
// 是否懒加载
lazyLoad: {
type: Boolean,
value: true
},
// 是否显示在线状态
showStatus: {
type: Boolean,
value: false
},
// 在线状态online, offline, busy, away
status: {
type: String,
value: 'offline'
},
// 徽章数量
badge: {
type: Number,
value: 0
},
// 是否显示加载状态
loading: {
type: Boolean,
value: false
},
// 自定义样式
customStyle: {
type: String,
value: ''
}
},
data: {
imageLoaded: false,
imageError: false
},
methods: {
// 图片加载成功
onImageLoad() {
this.setData({
imageLoaded: true,
imageError: false
})
this.triggerEvent('load')
},
// 图片加载失败
onImageError(e) {
console.error('头像加载失败:', e)
this.setData({
imageLoaded: false,
imageError: true
})
this.triggerEvent('error', e)
},
// 点击头像
onAvatarTap() {
this.triggerEvent('tap', {
src: this.properties.src,
status: this.properties.status,
badge: this.properties.badge
})
},
// 长按头像
onAvatarLongPress() {
this.triggerEvent('longpress', {
src: this.properties.src,
status: this.properties.status,
badge: this.properties.badge
})
},
// 获取头像状态文本
getStatusText() {
const statusMap = {
online: '在线',
offline: '离线',
busy: '忙碌',
away: '离开'
}
return statusMap[this.properties.status] || '未知'
}
}
})