index.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <template>
  2. <view style="position: relative;">
  3. <u-avatar
  4. @longpress="longpress"
  5. @click="click"
  6. @onError="errorHandle"
  7. :src="getAvatarUrl"
  8. :text="avatarText"
  9. bgColor="#e5e5e5"
  10. :defaultUrl="getDdefaultUrl"
  11. :shape="shape"
  12. :size="size"
  13. mode="aspectFill"
  14. font-size="14">
  15. </u-avatar>
  16. <slot></slot>
  17. </view>
  18. </template>
  19. <script>
  20. const defaultGroupIcon = "https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/shop/image/contact_my_group.png";
  21. const defaultNotifyIcon = "https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/shop/image/default_notify_icon.png";
  22. const defaultFaceIcon = "https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/shop/image/default_head.png";
  23. export default {
  24. name: "MyAvatar",
  25. props: {
  26. src: String,
  27. shape: {
  28. type: String,
  29. default: "square",
  30. },
  31. size: {
  32. type: String,
  33. default: "46",
  34. },
  35. isGroup: {
  36. type: Boolean,
  37. default: false,
  38. },
  39. isNotify: {
  40. type: Boolean,
  41. default: false,
  42. },
  43. desc: String,
  44. },
  45. data() {
  46. return {
  47. avatarText: undefined,
  48. };
  49. },
  50. computed: {
  51. getAvatarUrl() {
  52. if (this.src) {
  53. return this.src;
  54. }else{
  55. return defaultFaceIcon;
  56. }
  57. if (this.isGroup) {
  58. return defaultGroupIcon;
  59. }
  60. if (this.isNotify) {
  61. return defaultNotifyIcon;
  62. }
  63. this.avatarText = this.desc ? this.desc.slice(0, 1) : "未知";
  64. return "";
  65. },
  66. getDdefaultUrl() {
  67. return this.isGroup ? defaultGroupIcon : undefined;
  68. },
  69. },
  70. methods: {
  71. errorHandle() {
  72. this.avatarText = this.desc ? this.desc.slice(0, 1) : "未知";
  73. },
  74. redirectShow() {
  75. if (this.avatarText) {
  76. this.avatarText = undefined;
  77. }
  78. },
  79. click() {
  80. this.$emit("click");
  81. },
  82. longpress() {
  83. this.$emit("longpress");
  84. },
  85. },
  86. watch: {
  87. src() {
  88. this.redirectShow();
  89. },
  90. desc() {
  91. this.redirectShow();
  92. },
  93. },
  94. };
  95. </script>
  96. <style>
  97. </style>