wxParseImg.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <image
  3. :lazy-load="newNode.attr.lazyLoad"
  4. :class="newNode.classStr"
  5. :style="newStyleStr || newNode.styleStr"
  6. :data-src="newNode.attr.src"
  7. :src="newNode.attr.src"
  8. @tap="wxParseImgTap"
  9. @load="wxParseImgLoad"
  10. />
  11. </template>
  12. <script>
  13. import {mapState} from 'vuex';
  14. export default {
  15. name: 'wxParseImg',
  16. data() {
  17. return {
  18. style: '',
  19. preview: true,
  20. windowWidth:'',
  21. };
  22. },
  23. props: {
  24. node: {
  25. type: Object,
  26. default() {
  27. return {};
  28. }
  29. },
  30. parentNode: {}
  31. },
  32. computed: {
  33. newStyleStr: function() {
  34. if (this.parentNode.styleStr && this.parentNode.styleStr.indexOf('text-align: center') > -1) {
  35. this.style += 'margin: 0 auto';
  36. }
  37. return this.style;
  38. },
  39. // ...mapState('gConfig', {
  40. // windowWidth: (state) => {
  41. // return state.imageWidth;
  42. // },
  43. // }),
  44. newNode: function() {
  45. return this.node;
  46. }
  47. },
  48. methods: {
  49. wxParseImgTap(e) {
  50. if (!this.preview) return;
  51. const { src } = e.currentTarget.dataset;
  52. if (!src) return;
  53. let parent = this.$parent;
  54. while (!parent.preview || typeof parent.preview !== 'function') {
  55. parent = parent.$parent;
  56. }
  57. parent.preview(src, e);
  58. },
  59. // 图片视觉宽高计算函数区
  60. wxParseImgLoad(e) {
  61. const { src } = e.currentTarget.dataset;
  62. if (!src) return;
  63. let { width, height } = e.mp.detail;
  64. const recal = this.wxAutoImageCal(width, height);
  65. const { imageheight, imageWidth } = recal;
  66. const { padding, mode } = this.newNode.attr;//删除padding
  67. const { styleStr } = this.newNode;
  68. const imageHeightStyle = mode === 'widthFix' ? '' : `height: ${imageheight}px;`;
  69. this.$nextTick().then(() => {
  70. this.style = `${styleStr ? styleStr: null}; ${imageHeightStyle}; width: ${imageWidth}; padding: 0 ${+padding}px;display:block;`;//删除padding
  71. });
  72. },
  73. wxAutoImageCal(originalWidth, originalHeight) {
  74. try {
  75. const res = uni.getSystemInfoSync();
  76. this.windowWidth=res.windowWidth
  77. // console.log(this.windowWidth);
  78. } catch (e) {
  79. console.log(e)
  80. }
  81. // 获取图片的原始长宽
  82. const windowWidth = this.windowWidth;
  83. const results = {};
  84. if (originalWidth < 60 || originalHeight < 60) {
  85. const { src } = this.newNode.attr;
  86. let parent = this.$parent;
  87. while (!parent.preview || typeof parent.preview !== 'function') {
  88. parent = parent.$parent;
  89. }
  90. parent.removeImageUrl(src);
  91. this.preview = false;
  92. }
  93. results.imageWidth = originalWidth;
  94. results.imageheight = originalHeight;
  95. // 判断按照那种方式进行缩放
  96. if (originalWidth > windowWidth) {
  97. // 在图片width大于手机屏幕width时候
  98. results.imageWidth = `100%`;
  99. results.imageheight = windowWidth * (uni.upx2px(originalHeight) / uni.upx2px(originalWidth));
  100. } else {
  101. // 否则展示原来的数据
  102. results.imageWidth = `100vw`;
  103. results.imageheight =windowWidth * (uni.upx2px(originalHeight) / uni.upx2px(originalWidth));
  104. }
  105. return results;
  106. }
  107. }
  108. };
  109. </script>
  110. <style lang="scss">
  111. image:host{
  112. width: 702upx;
  113. }
  114. </style>