VimAvatar.vue 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <template>
  2. <image v-if="url" :src="url" class="cu-avatar radius" :class="[size,{'grid-image':isGroup}]" ></image>
  3. <view v-if="!url" class="cu-avatar radius avatar-name bg-olive" :class="[size,{'grid-image':isGroup}]">{{start}}</view>
  4. </template>
  5. <script setup lang="ts">
  6. import {computed, ref} from "vue";
  7. import FetchRequest from "@/api/FetchRequest";
  8. const host = ref(FetchRequest.getHost())
  9. interface IProps {
  10. size?: string
  11. img?: string
  12. name?: string
  13. isGroup?: boolean
  14. }
  15. const props = withDefaults(defineProps<IProps>(), {
  16. size: 'lg' ,
  17. img: '',
  18. name: '',
  19. isGroup: false,
  20. })
  21. const url = computed(() => {
  22. if (props.img?.indexOf('http') > -1) {
  23. return props.img
  24. } else if (props.img?.trim() === '') {
  25. return null
  26. } else {
  27. return host.value + props.img
  28. }
  29. })
  30. const start = computed(() => {
  31. return props.name ? props.name.slice(0,2) : ''
  32. })
  33. </script>
  34. <style scoped>
  35. .avatar-name{
  36. font-size: 1rem;
  37. font-weight: bold;
  38. }
  39. .grid-image {
  40. position: relative;
  41. overflow: hidden; /* 防止裁剪部分之外的内容显示出来 */
  42. }
  43. .grid-image img {
  44. width: 100%;
  45. height: 100%;
  46. display: block;
  47. position: relative;
  48. z-index: 1;
  49. }
  50. .grid-image::before,
  51. .grid-image::after {
  52. content: '';
  53. position: absolute;
  54. z-index: 2;
  55. background: rgba(255, 255, 255, 0.5); /* 根据需要调整蒙板颜色 */
  56. }
  57. .grid-image::before {
  58. top: 0;
  59. left: 0;
  60. right: calc(51% - 1px);
  61. bottom: calc(51% - 1px);
  62. border-right: 1px solid /* 边框颜色 */;
  63. border-bottom: 1px solid /* 边框颜色 */;
  64. }
  65. .grid-image::after {
  66. top: 50%;
  67. left: 50%;
  68. right: 0;
  69. bottom: 0;
  70. border-left: 1px solid /* 边框颜色 */;
  71. border-top: 1px solid /* 边框颜色 */;
  72. }
  73. </style>