index.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. <!-- <image @longpress="longpress" @click="click" :src="getAvatarUrl" class="headImg"></image> -->
  17. <slot >
  18. </slot>
  19. </view>
  20. </template>
  21. <script>
  22. import defaultGroupIcon from "../../static/images/contact_my_group.png";
  23. import defaultNotifyIcon from "../../static/images/default_notify_icon.png";
  24. import defaultFaceIcon from "../../static/images/default_head.png";
  25. export default {
  26. name: "MyAvatar",
  27. props: {
  28. src:{
  29. type: String,
  30. default: "",
  31. },
  32. shape: {
  33. type: String,
  34. default: "square", //square circle
  35. },
  36. size: {
  37. type: String,
  38. default: "44",
  39. },
  40. isGroup: {
  41. type: Boolean,
  42. default: false,
  43. },
  44. isNotify: {
  45. type: Boolean,
  46. default: false,
  47. },
  48. desc: String,
  49. },
  50. data() {
  51. return {
  52. avatarText: undefined,
  53. defaultImg:""
  54. };
  55. },
  56. computed: {
  57. getAvatarUrl() {
  58. if (this.src) {
  59. return this.src;
  60. }
  61. else if (this.isGroup) {
  62. return defaultGroupIcon;
  63. }
  64. else if (this.isNotify) {
  65. return defaultNotifyIcon;
  66. }
  67. else{
  68. return defaultFaceIcon;
  69. }
  70. // this.avatarText = this.desc ? this.desc.slice(0, 1) : "未知";
  71. // return "";
  72. },
  73. getDdefaultUrl() {
  74. return this.isGroup ? defaultGroupIcon : undefined;
  75. },
  76. },
  77. methods: {
  78. getImgSrc(){
  79. if(!this.src){
  80. return defaultFaceIcon;
  81. }
  82. return this.src;
  83. },
  84. errorHandle() {
  85. this.avatarText = this.desc ? this.desc.slice(0, 1) : "未知";
  86. },
  87. redirectShow() {
  88. if (this.avatarText) {
  89. this.avatarText = undefined;
  90. }
  91. },
  92. click() {
  93. this.$emit("click");
  94. },
  95. longpress() {
  96. this.$emit("longpress");
  97. },
  98. },
  99. watch: {
  100. src() {
  101. this.redirectShow();
  102. },
  103. desc() {
  104. this.redirectShow();
  105. },
  106. },
  107. };
  108. </script>
  109. <style lang="scss">
  110. .headImg{
  111. background: #f5f5f5;
  112. width: 60px;
  113. height: 60px;
  114. border-radius:10px ;
  115. }
  116. </style>