u-avatar.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <template>
  2. <view class="u-avatar" :style="[wrapStyle]" @tap="click">
  3. <image
  4. @error="loadError"
  5. :style="[imgStyle]"
  6. class="u-avatar__img"
  7. v-if="!uText && avatar"
  8. :src="avatar"
  9. :mode="imgMode"
  10. ></image>
  11. <text class="u-line-1" v-else-if="uText" :style="{
  12. fontSize: '38rpx'
  13. }">{{uText}}</text>
  14. <slot v-else></slot>
  15. <view class="u-avatar__sex" v-if="showSex" :class="['u-avatar__sex--' + sexIcon]" :style="[uSexStyle]">
  16. <u-icon :name="sexIcon" size="20"></u-icon>
  17. </view>
  18. <view class="u-avatar__level" v-if="showLevel" :style="[uLevelStyle]">
  19. <u-icon :name="levelIcon" size="20"></u-icon>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. let base64Avatar = "data:image/jpg;base64,/9j/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP/sABFEdWNreQABAAQAAAA8AAD/4QMraHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P3hwYWNrZXQgYmVnaW49Iu+7vyIgaWQ9Ilc1TTBNcENlaGlIenJlU3pOVGN6a2M5ZCI/PiA8eDp4bXBtZXRhIHhtbG5zOng9ImFkb2JlOm5zOm1ldGEvIiB4OnhtcHRrPSJBZG9iZSBYTVAgQ29yZSA1LjMtYzAxMSA2Ni4xNDU2NjEsIDIwMTIvMDIvMDYtMTQ6NTY6MjcgICAgICAgICI+IDxyZGY6UkRGIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc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 本组件一般用于展示头像的地方,如个人中心,或者评论列表页的用户头像展示等场所。
  28. * @tutorial https://www.uviewui.com/components/avatar.html
  29. * @property {String} bg-color 背景颜色,一般显示文字时用(默认#ffffff)
  30. * @property {String} src 头像路径,如加载失败,将会显示默认头像
  31. * @property {String Number} size 头像尺寸,可以为指定字符串(large, default, mini),或者数值,单位rpx(默认default)
  32. * @property {String} mode 显示类型,见上方说明(默认circle)
  33. * @property {String} sex-icon 性别图标,man-男,woman-女(默认man)
  34. * @property {String} level-icon 等级图标(默认level)
  35. * @property {String} sex-bg-color 性别图标背景颜色
  36. * @property {String} level-bg-color 等级图标背景颜色
  37. * @property {String} show-sex 是否显示性别图标(默认false)
  38. * @property {String} show-level 是否显示等级图标(默认false)
  39. * @property {String} img-mode 头像图片的裁剪类型,与uni的image组件的mode参数一致,如效果达不到需求,可尝试传widthFix值(默认aspectFill)
  40. * @property {String} index 用户传递的标识符值,如果是列表循环,可穿v-for的index值
  41. * @event {Function} click 头像被点击
  42. * @example <u-avatar :src="src"></u-avatar>
  43. */
  44. export default {
  45. name: 'u-avatar',
  46. props: {
  47. // 背景颜色
  48. bgColor: {
  49. type: String,
  50. default: 'transparent'
  51. },
  52. // 头像路径
  53. src: {
  54. type: String,
  55. default: ''
  56. },
  57. // 尺寸,large-大,default-中等,mini-小,如果为数值,则单位为rpx
  58. // 宽度等于高度
  59. size: {
  60. type: [String, Number],
  61. default: 'default'
  62. },
  63. // 头像模型,square-带圆角方形,circle-圆形
  64. mode: {
  65. type: String,
  66. default: 'circle'
  67. },
  68. // 文字内容
  69. text: {
  70. type: String,
  71. default: ''
  72. },
  73. // 图片的裁剪模型
  74. imgMode: {
  75. type: String,
  76. default: 'aspectFill'
  77. },
  78. // 标识符
  79. index: {
  80. type: [String, Number],
  81. default: ''
  82. },
  83. // 右上角性别角标,man-男,woman-女
  84. sexIcon: {
  85. type: String,
  86. default: 'man'
  87. },
  88. // 右下角的等级图标
  89. levelIcon: {
  90. type: String,
  91. default: 'level'
  92. },
  93. // 右下角等级图标背景颜色
  94. levelBgColor: {
  95. type: String,
  96. default: ''
  97. },
  98. // 右上角性别图标的背景颜色
  99. sexBgColor: {
  100. type: String,
  101. default: ''
  102. },
  103. // 是否显示性别图标
  104. showSex: {
  105. type: Boolean,
  106. default: false
  107. },
  108. // 是否显示等级图标
  109. showLevel: {
  110. type: Boolean,
  111. default: false
  112. }
  113. },
  114. data() {
  115. return {
  116. error: false,
  117. // 头像的地址,因为如果加载错误,需要赋值为默认图片,props值无法修改,所以需要一个中间值
  118. avatar: this.src ? this.src : base64Avatar,
  119. }
  120. },
  121. watch: {
  122. src(n) {
  123. // 用户可能会在头像加载失败时,再次修改头像值,所以需要重新赋值
  124. if(!n) {
  125. // 如果传入null或者'',或者undefined,显示默认头像
  126. this.error = true;
  127. } else {
  128. this.avatar = n;
  129. this.error = false;
  130. }
  131. }
  132. },
  133. computed: {
  134. wrapStyle() {
  135. let style = {};
  136. style.height = this.size == 'large' ? '120rpx' : this.size == 'default' ?
  137. '90rpx' : this.size == 'mini' ? '70rpx' : this.size + 'rpx';
  138. style.width = style.height;
  139. style.flex = `0 0 ${style.height}`;
  140. style.backgroundColor = this.bgColor;
  141. style.borderRadius = this.mode == 'circle' ? '500px' : '5px';
  142. if(this.text) style.padding = `0 6rpx`;
  143. return style;
  144. },
  145. imgStyle() {
  146. let style = {};
  147. style.borderRadius = this.mode == 'circle' ? '500px' : '5px';
  148. return style;
  149. },
  150. // 取字符串的第一个字符
  151. uText() {
  152. return String(this.text)[0];
  153. },
  154. // 性别图标的自定义样式
  155. uSexStyle() {
  156. let style = {};
  157. if(this.sexBgColor) style.backgroundColor = this.sexBgColor;
  158. return style;
  159. },
  160. // 等级图标的自定义样式
  161. uLevelStyle() {
  162. let style = {};
  163. if(this.levelBgColor) style.backgroundColor = this.levelBgColor;
  164. return style;
  165. }
  166. },
  167. methods: {
  168. // 图片加载错误时,显示默认头像
  169. loadError() {
  170. this.error = true;
  171. this.avatar = base64Avatar;
  172. },
  173. click() {
  174. this.$emit('click', this.index);
  175. }
  176. }
  177. }
  178. </script>
  179. <style lang="scss" scoped>
  180. @import "../../libs/css/style.components.scss";
  181. .u-avatar {
  182. /* #ifndef APP-NVUE */
  183. display: inline-flex;
  184. /* #endif */
  185. align-items: center;
  186. justify-content: center;
  187. font-size: 28rpx;
  188. color: $u-content-color;
  189. border-radius: 10px;
  190. position: relative;
  191. &__img {
  192. width: 100%;
  193. height: 100%;
  194. }
  195. &__sex {
  196. position: absolute;
  197. width: 32rpx;
  198. color: #ffffff;
  199. height: 32rpx;
  200. @include vue-flex;
  201. justify-content: center;
  202. align-items: center;
  203. border-radius: 100rpx;
  204. top: 5%;
  205. z-index: 1;
  206. right: -7%;
  207. border: 1px #ffffff solid;
  208. &--man {
  209. background-color: $u-type-primary;
  210. }
  211. &--woman {
  212. background-color: $u-type-error;
  213. }
  214. &--none {
  215. background-color: $u-type-warning;
  216. }
  217. }
  218. &__level {
  219. position: absolute;
  220. width: 32rpx;
  221. color: #ffffff;
  222. height: 32rpx;
  223. @include vue-flex;
  224. justify-content: center;
  225. align-items: center;
  226. border-radius: 100rpx;
  227. bottom: 5%;
  228. z-index: 1;
  229. right: -7%;
  230. border: 1px #ffffff solid;
  231. background-color: $u-type-warning;
  232. }
  233. }
  234. </style>