wxParseImg.vue 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. style="display: inline-block !important;"
  8. :data-src="node.attr.src"
  9. :src="node.attr.src"
  10. @tap="wxParseImgTap"
  11. @load="wxParseImgLoad"
  12. />
  13. </template>
  14. <script>
  15. export default {
  16. name: 'wxParseImg',
  17. data() {
  18. return {
  19. newStyleStr: '',
  20. preview: true
  21. };
  22. },
  23. inject: ['parseWidth'],
  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 = `${styleStr}; ${imageHeightStyle}; width: ${imageWidth}px; padding: 0 ${+padding}px;`;//删除padding
  56. // this.newStyleStr = `${styleStr}; ${imageHeightStyle}; width: ${imageWidth}px;`;
  57. console.log(this.node,'this.node')
  58. },
  59. // 计算视觉优先的图片宽高
  60. wxAutoImageCal(originalWidth, originalHeight) {
  61. // 获取图片的原始长宽
  62. const windowWidth = this.parseWidth.value;
  63. const results = {};
  64. if (originalWidth < 60 || originalHeight < 60) {
  65. const { src } = this.node.attr;
  66. let parent = this.$parent;
  67. while (!parent.preview || typeof parent.preview !== 'function') {
  68. parent = parent.$parent;
  69. }
  70. parent.removeImageUrl(src);
  71. this.preview = false;
  72. }
  73. // 判断按照那种方式进行缩放
  74. if (originalWidth > windowWidth) {
  75. // 在图片width大于手机屏幕width时候
  76. results.imageWidth = windowWidth;
  77. results.imageheight = windowWidth * (originalHeight / originalWidth);
  78. } else {
  79. // 否则展示原来的数据
  80. results.imageWidth = originalWidth;
  81. results.imageheight = originalHeight;
  82. }
  83. return results;
  84. }
  85. }
  86. };
  87. </script>