uni-events-helper-wx
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.

163 lines
8.5 KiB

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