index.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <template>
  2. <view :style="[containerStyle, {position: 'relative', display: 'flex', alignItems: 'center', justifyContent: 'center'}]">
  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></slot>
  18. </view>
  19. </template>
  20. <script>
  21. import defaultGroupIcon from "../../static/images/contact_my_group.png";
  22. import defaultNotifyIcon from "../../static/images/default_notify_icon.png";
  23. import defaultFaceIcon from "../../static/images/default_head.png";
  24. export default {
  25. name: "MyAvatar",
  26. props: {
  27. src:{
  28. type: String,
  29. default: "",
  30. },
  31. shape: {
  32. type: String,
  33. default: "square", //square circle
  34. },
  35. size: {
  36. type: String,
  37. default: "46",
  38. },
  39. isGroup: {
  40. type: Boolean,
  41. default: false,
  42. },
  43. isNotify: {
  44. type: Boolean,
  45. default: false,
  46. },
  47. desc: String,
  48. },
  49. data() {
  50. return {
  51. avatarText: undefined,
  52. defaultImg:""
  53. };
  54. },
  55. computed: {
  56. containerStyle() {
  57. let borderRadius = '4px';
  58. if (this.shape === 'circle') {
  59. borderRadius = '50%';
  60. }
  61. return {
  62. borderRadius: borderRadius,
  63. backgroundColor: '#F4F5F7'
  64. };
  65. },
  66. getAvatarUrl() {
  67. if (this.src && this.src.trim() !== "" && this.src !== "undefined" && this.src !== "null") {
  68. return this.src;
  69. }
  70. if (this.isGroup) {
  71. return defaultGroupIcon;
  72. }
  73. if (this.isNotify) {
  74. return defaultNotifyIcon;
  75. }
  76. return defaultFaceIcon;
  77. },
  78. getDdefaultUrl() {
  79. return this.isGroup ? defaultGroupIcon : undefined;
  80. },
  81. },
  82. watch: {
  83. src(newVal, oldVal) {
  84. //console.log("qxj MyAvatar src changed", oldVal, "→", newVal);
  85. this.redirectShow();
  86. },
  87. desc() {
  88. this.redirectShow();
  89. },
  90. },
  91. methods: {
  92. errorHandle() {
  93. this.avatarText = this.desc ? this.desc.slice(0, 1) : "未知";
  94. },
  95. redirectShow() {
  96. if (this.avatarText) {
  97. this.avatarText = undefined;
  98. }
  99. },
  100. click() {
  101. this.$emit("click");
  102. },
  103. longpress() {
  104. this.$emit("longpress");
  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>