fs-avatar.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <template>
  2. <view
  3. class="fs-avatar"
  4. :class="[
  5. shape,
  6. {
  7. radius,
  8. fixed
  9. }
  10. ]"
  11. :style="{
  12. width: width || size,
  13. height: height || size,
  14. right: fixed ? right : 0,
  15. bottom: fixed ? bottom : 0,
  16. border: borderStyle,
  17. 'margin-left': avatarGroup.margin + 'rpx'
  18. }"
  19. @click="handleClick"
  20. >
  21. <image
  22. class="fs-avatar-img"
  23. :src="errImg"
  24. v-if="errImg"
  25. :lazy-load="lazyLoad"
  26. :mode="imageMode"
  27. @click="handlePreview"
  28. />
  29. <image
  30. class="fs-avatar-img"
  31. :src="src"
  32. v-if="src"
  33. :lazy-load="lazyLoad"
  34. :mode="imageMode"
  35. :show-menu-by-longpress="showMenu"
  36. @click="handlePreview"
  37. @error="handleError"
  38. />
  39. <view v-else class="fs-avatar-slot" :class="['bg-' + bgColorType]" :style="{ backgroundColor: bgColor }">
  40. <slot></slot>
  41. </view>
  42. </view>
  43. </template>
  44. <script>
  45. /**
  46. * 头像组件
  47. * @description 头像组件
  48. * @property {String} src 头像地址
  49. * @property {String} shape = [circle | square] 头像类型
  50. * @property {String} size 头像大小
  51. * @property {String} width 头像宽
  52. * @property {String} height 头像高
  53. * @property {String} bgColor 背景颜色
  54. * @property {String} bgColorType = [primary | danger | warning | info | success] 背景颜色类型
  55. * @property {Boolean} border 是否展示边框
  56. * @property {String} borderWidth 边框宽
  57. * @property {String} borderColor 边框颜色
  58. * @property {Boolean} lazyLoad 是否懒加载
  59. * @property {String} imageMode 图片模式
  60. * @property {Boolean} preview 是否预览图片
  61. * @property {Boolean} radius 是否圆角
  62. * @property {String} link 跳转地址
  63. * @property {String} linkType 跳转类型
  64. * @property {Boolean} fixed 是否悬浮
  65. * @property {String} right 悬浮右边距(仅在fixed为true时有效)
  66. * @property {String} bottom 悬浮下边距(仅在fixed为true时有效)
  67. * @property {Boolean} showMenu 长按图片显示菜单
  68. * @property {String} defaultErrorImg 图片加载失败显示的默认图片
  69. * @example <fs-avatar size="100rpx" src="***"></fs-avatar>
  70. */
  71. export default {
  72. name: 'fs-avatar'
  73. }
  74. </script>
  75. <script setup>
  76. import { computed, inject, ref } from 'vue'
  77. import config from '@/utils/config'
  78. const props = defineProps({
  79. src: String,
  80. shape: {
  81. type: String,
  82. default: 'circle', // square, circle
  83. validator(value) {
  84. return ['circle', 'square'].includes(value)
  85. }
  86. },
  87. size: {
  88. type: String,
  89. default: '80rpx'
  90. },
  91. width: String,
  92. height: String,
  93. bgColor: String,
  94. bgColorType: {
  95. type: String,
  96. default: 'primary',
  97. validator(value) {
  98. return ['primary', 'success', 'info', 'warning', 'danger'].includes(value)
  99. }
  100. },
  101. border: Boolean,
  102. borderWidth: {
  103. type: String,
  104. default: '4rpx'
  105. },
  106. borderColor: {
  107. type: String,
  108. default: '#fff'
  109. },
  110. lazyLoad: Boolean,
  111. imageMode: {
  112. type: String,
  113. default: 'scaleToFill'
  114. },
  115. preview: Boolean,
  116. radius: Boolean,
  117. link: String,
  118. linkType: {
  119. type: String,
  120. default: 'navigateTo'
  121. },
  122. fixed: Boolean,
  123. right: {
  124. type: String,
  125. default: '40rpx'
  126. },
  127. bottom: {
  128. type: String,
  129. default: '60rpx'
  130. },
  131. showMenu: Boolean,
  132. defaultErrorImg: {
  133. type: String,
  134. default: config.defaultErrorImg
  135. }
  136. })
  137. const emits = defineEmits(['click'])
  138. const avatarGroup = inject('avatarGroup', {})
  139. const borderStyle = computed(() => {
  140. return props.border || avatarGroup.border ? `${props.borderWidth} solid ${props.borderColor}` : 'none'
  141. })
  142. const handleClick = () => {
  143. if (props.link) {
  144. uni[props.linkType]({
  145. url: props.link
  146. })
  147. }
  148. emits('click')
  149. }
  150. const handlePreview = () => {
  151. if (props.preview) {
  152. uni.previewImage({
  153. urls: [props.src || errImg.value]
  154. })
  155. }
  156. }
  157. const errImg = ref('')
  158. const handleError = e => {
  159. errImg.value = props.defaultErrorImg
  160. }
  161. </script>
  162. <style lang="scss" scoped>
  163. .fs-avatar {
  164. display: inline-block;
  165. white-space: nowrap;
  166. position: relative;
  167. overflow: hidden;
  168. vertical-align: middle;
  169. text-align: center;
  170. &.radius {
  171. border-radius: var(--radius);
  172. }
  173. &.circle,
  174. &.circle &-img {
  175. border-radius: 50%;
  176. }
  177. &.fixed {
  178. position: fixed;
  179. z-index: 50;
  180. margin-bottom: var(--window-bottom);
  181. }
  182. &-img {
  183. width: 100%;
  184. height: 100%;
  185. object-fit: cover;
  186. }
  187. &-slot {
  188. width: 100%;
  189. height: 100%;
  190. display: flex;
  191. justify-content: center;
  192. align-items: center;
  193. color: #fff;
  194. white-space: normal;
  195. line-height: 1;
  196. }
  197. }
  198. </style>