image.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  2. <view
  3. :class="classList"
  4. v-if="data.data"
  5. >
  6. <image
  7. :src="imagePath"
  8. :class="data.data.class"
  9. :style="styles"
  10. :alt="data.data.attributes.alt || ''"
  11. class="img"
  12. mode="aspectFill"
  13. @load="imageLoad"
  14. @click="imagePreview"
  15. ></image>
  16. </view>
  17. </template>
  18. <script>
  19. import {parseImageUrl} from "@/uni_modules/uni-cms-article/common/parse-image-url.js";
  20. export default {
  21. name: "render-image",
  22. props: {
  23. data: {
  24. type: Object,
  25. default () {
  26. return {}
  27. }
  28. },
  29. className: String,
  30. reset: false
  31. },
  32. data () {
  33. return {
  34. width: 0,
  35. height: 0,
  36. imagePath: ''
  37. }
  38. },
  39. computed: {
  40. classList () {
  41. return [
  42. 'image',
  43. this.reset ? 'reset': '',
  44. this.className
  45. ]
  46. },
  47. styles () {
  48. let style = this.data.data.style
  49. if (this.width) {
  50. style += `;width:${this.width}px`
  51. }
  52. if (this.height) {
  53. style += `;height:${this.height}px`
  54. }
  55. return style
  56. }
  57. },
  58. mounted () {
  59. this.loadImagePath()
  60. },
  61. methods: {
  62. async loadImagePath () {
  63. const parseImages = await parseImageUrl({
  64. insert: {image: this.data.data.value},
  65. attributes: this.data.data.attributes,
  66. }, "editor")
  67. this.imagePath = parseImages[0].src
  68. },
  69. imagePreview () {
  70. uni.$emit('imagePreview', this.data.data.value)
  71. },
  72. // 图片加载完成
  73. imageLoad(e) {
  74. const recal = this.wxAutoImageCal(e.detail.width, e.detail.height, 15) // 计算图片宽高
  75. // const image = this.data
  76. // ::TODO 关注一下在多端得表现情况
  77. // if (!image.data.attributes.width || Number(image.data.attributes.width) > recal.imageWidth) {
  78. // 如果图片宽度不存在或者图片宽度大于计算出来的宽度,则设置图片宽高
  79. this.width = recal.imageWidth
  80. this.height = recal.imageHeight
  81. // }
  82. },
  83. // 计算图片宽高
  84. wxAutoImageCal(originalWidth, originalHeight, imagePadding = 0) {
  85. // 获取系统信息
  86. const systemInfo = uni.getSystemInfoSync()
  87. let windowWidth = 0, windowHeight = 0;
  88. let autoWidth = 0, autoHeight = 0;
  89. let results = {};
  90. // 计算图片宽度
  91. windowWidth = systemInfo.windowWidth - 2 * imagePadding;
  92. windowHeight = systemInfo.windowHeight;
  93. if (originalWidth > windowWidth) {//在图片width大于手机屏幕width时候
  94. autoWidth = windowWidth;
  95. autoHeight = (autoWidth * originalHeight) / originalWidth;
  96. results.imageWidth = autoWidth;
  97. results.imageHeight = autoHeight;
  98. } else {//否则展示原来的数据
  99. results.imageWidth = originalWidth;
  100. results.imageHeight = originalHeight;
  101. }
  102. return results;
  103. }
  104. }
  105. }
  106. </script>
  107. <style scoped lang="scss">
  108. .image {
  109. margin-bottom: 40rpx;
  110. &.reset {
  111. margin-bottom: 0;
  112. }
  113. .img {
  114. // #ifdef APP-PLUS
  115. display: block;
  116. // #endif
  117. // #ifndef APP-PLUS
  118. display: flex;
  119. // #endif
  120. border-radius: 12rpx;
  121. margin: 0 auto;
  122. }
  123. }
  124. </style>