index.vue 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <template>
  2. <view class="nut-empty">
  3. <!-- 占位图 -->
  4. <view class="nut-empty-image" :style="imgStyle">
  5. <template v-if="$slots.image">
  6. <slot name="image"></slot>
  7. </template>
  8. <template v-else>
  9. <img v-if="imageUrl" class="img" :src="imageUrl" />
  10. </template>
  11. </view>
  12. <!-- 文本区 -->
  13. <template v-if="$slots.description">
  14. <slot name="description"></slot>
  15. </template>
  16. <template v-else>
  17. <view class="nut-empty-description">{{ description || translate('noData') }}</view>
  18. </template>
  19. <!-- 自定义slot -->
  20. <template v-if="$slots.default">
  21. <slot></slot>
  22. </template>
  23. </view>
  24. </template>
  25. <script lang="ts">
  26. import { toRefs, computed } from 'vue';
  27. import { createComponent } from '@/packages/utils/create';
  28. const { componentName, create, translate } = createComponent('empty');
  29. type statusOptions = {
  30. [key: string]: string;
  31. };
  32. /**
  33. * 内置图片地址
  34. */
  35. const defaultStatus: statusOptions = {
  36. empty: 'https://static-ftcms.jd.com/p/files/61a9e3183985005b3958672b.png',
  37. error: 'https://ftcms.jd.com/p/files/61a9e33ee7dcdbcc0ce62736.png',
  38. network: 'https://static-ftcms.jd.com/p/files/61a9e31de7dcdbcc0ce62734.png'
  39. };
  40. export default create({
  41. props: {
  42. image: {
  43. type: String,
  44. default: 'empty' //默认empty
  45. },
  46. imageSize: {
  47. type: [Number, String], // 图片大小,正方形
  48. default: ''
  49. },
  50. description: {
  51. type: String, // 文字区
  52. default: ''
  53. }
  54. },
  55. setup(props) {
  56. const { image, imageSize } = toRefs(props);
  57. /**
  58. * 根据imgSize计算行内样式
  59. */
  60. const imgStyle = computed(() => {
  61. if (!imageSize.value) {
  62. return '';
  63. }
  64. if (typeof imageSize.value === 'number') {
  65. return `width:${imageSize.value}px;height:${imageSize.value}px`;
  66. }
  67. return `width:${imageSize.value};height:${imageSize.value}`;
  68. });
  69. const isHttpUrl =
  70. image.value.startsWith('https://') || image.value.startsWith('http://') || image.value.startsWith('//');
  71. const imageUrl = isHttpUrl ? image.value : defaultStatus[image.value];
  72. return {
  73. imageUrl,
  74. imgStyle,
  75. translate
  76. };
  77. }
  78. });
  79. </script>