You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
9.1 KiB

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