message-image.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <template>
  2. <view class="message-image" @tap="handlePreviewImage" >
  3. <image :src="data.info[1].url" :style="{height:imageHeight, width:imageWidth}" :class="['content-' + message.flow]"></image>
  4. <!-- <div class="progress" v-if="data.progress">
  5. <progress :value="data.progress" max="1"></progress>
  6. </div> -->
  7. </view>
  8. </template>
  9. <script lang="ts">
  10. import { defineComponent, watchEffect, reactive, toRefs, onMounted } from 'vue';
  11. export default defineComponent({
  12. props: {
  13. data: {
  14. type: Array,
  15. default: () => {
  16. return [];
  17. }
  18. },
  19. messageData: {
  20. type: Object,
  21. default: () => {
  22. return {};
  23. }
  24. },
  25. },
  26. setup(props:any, ctx:any) {
  27. const data = reactive({
  28. imageInfo: [],
  29. imageHeight: 0,
  30. imageWidth: 0,
  31. message: {},
  32. });
  33. watchEffect(()=>{
  34. // 等比例计算图片的 width、height
  35. const DEFAULT_MAX_SIZE = 155;
  36. let imageWidth = 0;
  37. let imageHeight = 0;
  38. data.message = props.messageData;
  39. data.imageInfo = props.data.info[1];
  40. if (data.imageInfo.width >= data.imageInfo.height) {
  41. imageWidth= DEFAULT_MAX_SIZE;
  42. imageHeight = DEFAULT_MAX_SIZE * data.imageInfo.height / data.imageInfo.width;
  43. } else {
  44. imageWidth = DEFAULT_MAX_SIZE * data.imageInfo.width / data.imageInfo.height;
  45. imageHeight = DEFAULT_MAX_SIZE;
  46. }
  47. // const imgHeight = data.imageInfo.height >= 140 ? 140 : data.imageInfo.height;
  48. // const imgWidth = imgHeight * data.imageInfo.width / data.imageInfo.height
  49. data.imageWidth = imageWidth + 'px';
  50. data.imageHeight = imageHeight + 'px';
  51. })
  52. // todo 优化 查看大图重新加载图片
  53. const handlePreviewImage = () => {
  54. console.error(props.data.info[0].url, '----linda')
  55. uni.previewImage({
  56. current: props.data.info[0].url,
  57. // 当前显示图片的http链接
  58. urls: [props.data.info[0].url],
  59. });
  60. }
  61. return {
  62. ...toRefs(data),
  63. handlePreviewImage
  64. };
  65. }
  66. });
  67. </script>
  68. <style lang="scss" scoped>
  69. .message-image {
  70. position: relative;
  71. img {
  72. max-width: 150px;
  73. }
  74. .progress {
  75. position: absolute;
  76. box-sizing: border-box;
  77. width: 100%;
  78. height: 100%;
  79. padding: 0 20px;
  80. left: 0;
  81. top: 0;
  82. background: rgba(#000000, 0.5);
  83. display: flex;
  84. align-items: center;
  85. progress {
  86. width: 100%;
  87. }
  88. }
  89. }
  90. .content {
  91. &-in {
  92. border-radius: 6px;
  93. }
  94. &-out {
  95. border-radius: 6px;
  96. }
  97. }
  98. </style>