洛阳学员端
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.

172 lines
8.9 KiB

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