wxParseImg.vue 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <image
  3. :mode="node.attr.mode"
  4. :lazy-load="node.attr.lazyLoad"
  5. :class="node.classStr"
  6. :style="newStyleStr || node.styleStr"
  7. :data-src="node.attr.src"
  8. :src="node.attr.src"
  9. @tap="wxParseImgTap"
  10. @load="wxParseImgLoad"
  11. />
  12. </template>
  13. <script>
  14. export default {
  15. name: 'wxParseImg',
  16. data() {
  17. return {
  18. newStyleStr: '',
  19. preview: true
  20. };
  21. },
  22. inject: ['parseWidth'],
  23. mounted() {},
  24. props: {
  25. node: {
  26. type: Object,
  27. default() {
  28. return {};
  29. }
  30. }
  31. },
  32. methods: {
  33. wxParseImgTap(e) {
  34. if (!this.preview) return;
  35. const { src } = e.currentTarget.dataset;
  36. if (!src) return;
  37. let parent = this.$parent;
  38. while (!parent.preview || typeof parent.preview !== 'function') {
  39. // TODO 遍历获取父节点执行方法
  40. parent = parent.$parent;
  41. }
  42. parent.preview(src, e);
  43. },
  44. // 图片视觉宽高计算函数区
  45. wxParseImgLoad(e) {
  46. const { src } = e.currentTarget.dataset;
  47. if (!src) return;
  48. let { width, height } = e.mp.detail;
  49. const recal = this.wxAutoImageCal(width, height);
  50. const { imageheight, imageWidth } = recal;
  51. const { padding, mode } = this.node.attr;//删除padding
  52. // const { mode } = this.node.attr;
  53. const { styleStr } = this.node;
  54. const imageHeightStyle = mode === 'widthFix' ? '' : `height: ${imageheight}px;`;
  55. //this.newStyleStr = ` padding: 0 ${+padding}px;`;//删除默认u安
  56. this.newStyleStr = `${styleStr}; ${imageHeightStyle}; width: ${imageWidth}px; padding: 0 ${+padding}px; margin: 0 auto;`;//删除padding
  57. // this.newStyleStr = `${styleStr}; ${imageHeightStyle}; width: ${imageWidth}px;`;
  58. },
  59. // 计算视觉优先的图片宽高
  60. wxAutoImageCal(originalWidth, originalHeight) {
  61. // 获取图片的原始长宽
  62. // const windowWidth = this.parseWidth.value;
  63. const { windowWidth, windowHeight } = uni.getSystemInfoSync();
  64. const results = {};
  65. if (originalWidth < 60 || originalHeight < 60) {
  66. const { src } = this.node.attr;
  67. let parent = this.$parent;
  68. while (!parent.preview || typeof parent.preview !== 'function') {
  69. parent = parent.$parent;
  70. }
  71. parent.removeImageUrl(src);
  72. this.preview = false;
  73. }
  74. // 判断按照那种方式进行缩放
  75. if (originalWidth > windowWidth) {
  76. // 在图片width大于手机屏幕width时候
  77. results.imageWidth = windowWidth;
  78. results.imageheight = windowWidth * (originalHeight / originalWidth);
  79. // results.imageWidth = originalWidth;
  80. // results.imageheight = originalHeight;
  81. } else {
  82. // 否则展示原来的数据
  83. results.imageWidth = originalWidth * 0.96;
  84. results.imageheight = originalHeight * 0.96;
  85. }
  86. return results;
  87. }
  88. }
  89. };
  90. </script>