u-avatar.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <template>
  2. <view
  3. class="u-avatar"
  4. :class="[`u-avatar--${shape}`]"
  5. :style="[{
  6. backgroundColor: (text || icon) ? (randomBgColor ? colors[colorIndex !== '' ? colorIndex : $u.random(0, 19)] : bgColor) : 'transparent',
  7. width: $u.addUnit(size),
  8. height: $u.addUnit(size),
  9. }, $u.addStyle(customStyle)]"
  10. @tap="clickHandler"
  11. >
  12. <slot>
  13. <!-- #ifdef MP-WEIXIN || MP-QQ || MP-BAIDU -->
  14. <open-data
  15. v-if="mpAvatar && allowMp"
  16. type="userAvatarUrl"
  17. :style="[{
  18. width: $u.addUnit(size),
  19. height: $u.addUnit(size)
  20. }]"
  21. />
  22. <!-- #endif -->
  23. <!-- #ifndef MP-WEIXIN && MP-QQ && MP-BAIDU -->
  24. <template v-if="mpAvatar && allowMp"></template>
  25. <!-- #endif -->
  26. <u-icon
  27. v-else-if="icon"
  28. :name="icon"
  29. :size="fontSize"
  30. :color="color"
  31. ></u-icon>
  32. <u--text
  33. v-else-if="text"
  34. :text="text"
  35. :size="fontSize"
  36. :color="color"
  37. align="center"
  38. customStyle="justify-content: center"
  39. ></u--text>
  40. <image
  41. class="u-avatar__image"
  42. v-else
  43. :class="[`u-avatar__image--${shape}`]"
  44. :src="avatarUrl || defaultUrl"
  45. :mode="mode"
  46. @error="errorHandler"
  47. :style="[{
  48. width: $u.addUnit(size),
  49. height: $u.addUnit(size)
  50. }]"
  51. ></image>
  52. </slot>
  53. </view>
  54. </template>
  55. <script>
  56. import props from './props.js';
  57. import mpMixin from '../../libs/mixin/mpMixin.js';
  58. import mixin from '../../libs/mixin/mixin.js';
  59. const base64Avatar =
  60. "data:image/jpg;base64,/9j/4QAYRXh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FvGzLGRGtuWvTF6NAdwAAAAAAAAAAAy5W442PVN+K59EePp5ARMvOv5MvO6QXCC4AZwAAAAAAAAAAAAAcxlKLUotprg1owN+PvORborq+7Hnwl3gUbO74VzRydt8pKn68ANcJwmqwkpLmnUDkAAAAfNy9atqtyagut0AxXt5xIV8Fbj6lRd7Am5G65V6qUvtwfyx94GMAAAAAAAAAAAAAAAAAAAOU2nVOj5gdsc3LiqRvTpyqwOxbnnrhdfpSfrQB7pnv/AGvuS9gHXPMy5/Fem1yq0v0A6W29XqwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAf//Z";
  61. /**
  62. * Avatar 头像
  63. * @description 本组件一般用于展示头像的地方,如个人中心,或者评论列表页的用户头像展示等场所。
  64. * @tutorial https://ijry.github.io/uview-plus/components/avatar.html
  65. *
  66. * @property {String} src 头像路径,如加载失败,将会显示默认头像(不能为相对路径)
  67. * @property {String} shape 头像形状 ( circle (默认) | square)
  68. * @property {String | Number} size 头像尺寸,可以为指定字符串(large, default, mini),或者数值 (默认 40 )
  69. * @property {String} mode 头像图片的裁剪类型,与uni的image组件的mode参数一致,如效果达不到需求,可尝试传widthFix值 (默认 'scaleToFill' )
  70. * @property {String} text 用文字替代图片,级别优先于src
  71. * @property {String} bgColor 背景颜色,一般显示文字时用 (默认 '#c0c4cc' )
  72. * @property {String} color 文字颜色 (默认 '#ffffff' )
  73. * @property {String | Number} fontSize 文字大小 (默认 18 )
  74. * @property {String} icon 显示的图标
  75. * @property {Boolean} mpAvatar 显示小程序头像,只对百度,微信,QQ小程序有效 (默认 false )
  76. * @property {Boolean} randomBgColor 是否使用随机背景色 (默认 false )
  77. * @property {String} defaultUrl 加载失败的默认头像(组件有内置默认图片)
  78. * @property {String | Number} colorIndex 如果配置了randomBgColor为true,且配置了此值,则从默认的背景色数组中取出对应索引的颜色值,取值0-19之间
  79. * @property {String} name 组件标识符 (默认 'level' )
  80. * @property {Object} customStyle 定义需要用到的外部样式
  81. *
  82. * @event {Function} click 点击组件时触发 index: 用户传递的标识符
  83. * @example <u-avatar :src="src" mode="square"></u-avatar>
  84. */
  85. export default {
  86. name: 'u-avatar',
  87. mixins: [mpMixin, mixin, props],
  88. data() {
  89. return {
  90. // 如果配置randomBgColor参数为true,在图标或者文字的模式下,会随机从中取出一个颜色值当做背景色
  91. colors: ['#ffb34b', '#f2bba9', '#f7a196', '#f18080', '#88a867', '#bfbf39', '#89c152', '#94d554', '#f19ec2',
  92. '#afaae4', '#e1b0df', '#c38cc1', '#72dcdc', '#9acdcb', '#77b1cc', '#448aca', '#86cefa', '#98d1ee',
  93. '#73d1f1',
  94. '#80a7dc'
  95. ],
  96. avatarUrl: this.src,
  97. allowMp: false
  98. }
  99. },
  100. watch: {
  101. // 监听头像src的变化,赋值给内部的avatarUrl变量,因为图片加载失败时,需要修改图片的src为默认值
  102. // 而组件内部不能直接修改props的值,所以需要一个中间变量
  103. src: {
  104. immediate: true,
  105. handler(newVal) {
  106. this.avatarUrl = newVal
  107. // 如果没有传src,则主动触发error事件,用于显示默认的头像,否则src为''空字符等的时候,会无内容展示
  108. if(!newVal) {
  109. this.errorHandler()
  110. }
  111. }
  112. }
  113. },
  114. computed: {
  115. imageStyle() {
  116. const style = {}
  117. return style
  118. }
  119. },
  120. created() {
  121. this.init()
  122. },
  123. emits: ["click"],
  124. methods: {
  125. init() {
  126. // 目前只有这几个小程序平台具有open-data标签
  127. // 其他平台可以通过uni.getUserInfo类似接口获取信息,但是需要弹窗授权(首次),不合符组件逻辑
  128. // 故目前自动获取小程序头像只支持这几个平台
  129. // #ifdef MP-WEIXIN || MP-QQ || MP-BAIDU
  130. this.allowMp = true
  131. // #endif
  132. },
  133. // 判断传入的name属性,是否图片路径,只要带有"/"均认为是图片形式
  134. isImg() {
  135. return this.src.indexOf('/') !== -1
  136. },
  137. // 图片加载时失败时触发
  138. errorHandler() {
  139. this.avatarUrl = this.defaultUrl || base64Avatar
  140. },
  141. clickHandler() {
  142. this.$emit('click', this.name)
  143. }
  144. }
  145. }
  146. </script>
  147. <style lang="scss" scoped>
  148. @import "../../libs/css/components.scss";
  149. .u-avatar {
  150. @include flex;
  151. align-items: center;
  152. justify-content: center;
  153. &--circle {
  154. border-radius: 100px;
  155. }
  156. &--square {
  157. border-radius: 4px;
  158. }
  159. &__image {
  160. &--circle {
  161. border-radius: 100px;
  162. }
  163. &--square {
  164. border-radius: 4px;
  165. }
  166. }
  167. }
  168. </style>