index.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. // import defaultGroupIcon from "https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/shop/image/contact_my_group.png";
  21. // import defaultNotifyIcon from "https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/shop/image/default_notify_icon.png";
  22. const defaultNotifyIcon = "https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/shop/image/default_notify_icon.png";
  23. const defaultGroupIcon = "https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/shop/image/contact_my_group.png";
  24. export default {
  25. name: "MyAvatar",
  26. props: {
  27. src: String,
  28. shape: {
  29. type: String,
  30. default: "square",
  31. },
  32. size: {
  33. type: String,
  34. default: "46",
  35. },
  36. isGroup: {
  37. type: Boolean,
  38. default: false,
  39. },
  40. isNotify: {
  41. type: Boolean,
  42. default: false,
  43. },
  44. desc: String,
  45. },
  46. data() {
  47. return {
  48. avatarText: undefined,
  49. defaultFaceIcon: "https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/newImages/0d7f54fe8adc4d3689923f9bfc83d4c9.png"
  50. };
  51. },
  52. computed: {
  53. getAvatarUrl() {
  54. if (this.isGroup) {
  55. return defaultGroupIcon;
  56. }
  57. else if (this.isNotify) {
  58. return defaultNotifyIcon;
  59. }
  60. else{
  61. if (this.src) {
  62. return this.src;
  63. }else{
  64. return this.defaultFaceIcon;
  65. }
  66. }
  67. this.avatarText = this.desc ? this.desc.slice(0, 1) : "未知";
  68. return "";
  69. },
  70. getDdefaultUrl() {
  71. return this.isGroup ? defaultGroupIcon : undefined;
  72. },
  73. },
  74. methods: {
  75. errorHandle() {
  76. this.avatarText = this.desc ? this.desc.slice(0, 1) : "未知";
  77. },
  78. redirectShow() {
  79. if (this.avatarText) {
  80. this.avatarText = undefined;
  81. }
  82. },
  83. click() {
  84. this.$emit("click");
  85. },
  86. longpress() {
  87. this.$emit("longpress");
  88. },
  89. },
  90. watch: {
  91. src() {
  92. this.redirectShow();
  93. },
  94. desc() {
  95. this.redirectShow();
  96. },
  97. },
  98. };
  99. </script>
  100. <style>
  101. </style>