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.

244 lines
9.8 KiB

3 years ago
  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+IDxyZGY6UkRGIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyI+IDxyZGY6RGVzY3JpcHRpb24gcmRmOmFib3V0PSIiIHhtbG5zOnhtcD0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLyIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tb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30A6Xg5i42Z+iLfqARwcyT0sz9MWvWBps7LlTf5Grce9/oBTxdtxseklHxT+uWr9AGoAB138ezfj4bsFJdD6V2MCPm7RdtJzs1uW1xXzL3gTgAAAAAAAAADRhYc8q74I6RWs5ckB6GxYtWLat21SK731sDsAAAAAAAAAAAAAAAASt021NO/YjrxuQXT1oCOAAAAAAABzGLlJRSq26JAelwsWONYjbXxcZvmwO8AAAAAAAAAAAAAAAAAAef3TEWPkVivx3NY9T6UBiAAAAAABo2+VmGXblddIJ8eivRUD0oAAAAAAAAAAAAAAAAAAAYt4tKeFKVNYNSXfRgefAAAAAAAAr7VuSSWPedKaW5v1MCsAAAAAAAAAAAAAAAAAAIe6bj96Ts2n+JPzSXzP3ATgAAAAAAAAFbbt1UUrOQ9FpC4/UwK6aaqtU+DAAAAAAAAAAAAAAA4lKMIuUmoxWrb4ARNx3R3q2rLpa4Sl0y/YCcAAAAAAAAAAANmFud7G8r89r6X0dgFvGzLGRGtuWvTF6NAdwAAAAAAAAAAAy5W442PVN+K59EePp5ARMvOv5MvO6QXCC4AZwAAAAAAAAAAAAAcxlKLUotprg1owN+PvORborq+7Hnwl3gUbO74VzRydt8pKn68ANcJwmqwkpLmnUDkAAAAfNy9atqtyagut0AxXt5xIV8Fbj6lRd7Am5G65V6qUvtwfyx94GMAAAAAAAAAAAAAAAAAAAOU2nVOj5gdsc3LiqRvTpyqwOxbnnrhdfpSfrQB7pnv/AGvuS9gHXPMy5/Fem1yq0v0A6W29XqwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAf//Z";
  25. /**
  26. * avatar 头像
  27. * @description 本组件一般用于展示头像的地方如个人中心或者评论列表页的用户头像展示等场所
  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.avatar = base64Avatar;
  127. this.error = true;
  128. } else {
  129. this.avatar = n;
  130. this.error = false;
  131. }
  132. }
  133. },
  134. computed: {
  135. wrapStyle() {
  136. let style = {};
  137. style.height = this.size == 'large' ? '120rpx' : this.size == 'default' ?
  138. '90rpx' : this.size == 'mini' ? '70rpx' : this.size + 'rpx';
  139. style.width = style.height;
  140. style.flex = `0 0 ${style.height}`;
  141. style.backgroundColor = this.bgColor;
  142. style.borderRadius = this.mode == 'circle' ? '500px' : '5px';
  143. if(this.text) style.padding = `0 6rpx`;
  144. return style;
  145. },
  146. imgStyle() {
  147. let style = {};
  148. style.borderRadius = this.mode == 'circle' ? '500px' : '5px';
  149. return style;
  150. },
  151. // 取字符串的第一个字符
  152. uText() {
  153. return String(this.text)[0];
  154. },
  155. // 性别图标的自定义样式
  156. uSexStyle() {
  157. let style = {};
  158. if(this.sexBgColor) style.backgroundColor = this.sexBgColor;
  159. return style;
  160. },
  161. // 等级图标的自定义样式
  162. uLevelStyle() {
  163. let style = {};
  164. if(this.levelBgColor) style.backgroundColor = this.levelBgColor;
  165. return style;
  166. }
  167. },
  168. methods: {
  169. // 图片加载错误时,显示默认头像
  170. loadError() {
  171. this.error = true;
  172. this.avatar = base64Avatar;
  173. },
  174. click() {
  175. this.$emit('click', this.index);
  176. }
  177. }
  178. }
  179. </script>
  180. <style lang="scss" scoped>
  181. @import "../../libs/css/style.components.scss";
  182. .u-avatar {
  183. /* #ifndef APP-NVUE */
  184. display: inline-flex;
  185. /* #endif */
  186. align-items: center;
  187. justify-content: center;
  188. font-size: 28rpx;
  189. color: $u-content-color;
  190. border-radius: 10px;
  191. position: relative;
  192. &__img {
  193. width: 100%;
  194. height: 100%;
  195. }
  196. &__sex {
  197. position: absolute;
  198. width: 32rpx;
  199. color: #ffffff;
  200. height: 32rpx;
  201. @include vue-flex;
  202. justify-content: center;
  203. align-items: center;
  204. border-radius: 100rpx;
  205. top: 5%;
  206. z-index: 1;
  207. right: -7%;
  208. border: 1px #ffffff solid;
  209. &--man {
  210. background-color: $u-type-primary;
  211. }
  212. &--woman {
  213. background-color: $u-type-error;
  214. }
  215. &--none {
  216. background-color: $u-type-warning;
  217. }
  218. }
  219. &__level {
  220. position: absolute;
  221. width: 32rpx;
  222. color: #ffffff;
  223. height: 32rpx;
  224. @include vue-flex;
  225. justify-content: center;
  226. align-items: center;
  227. border-radius: 100rpx;
  228. bottom: 5%;
  229. z-index: 1;
  230. right: -7%;
  231. border: 1px #ffffff solid;
  232. background-color: $u-type-warning;
  233. }
  234. }
  235. </style>