image.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. import { SuperComponent, wxComponent } from '../common/src/index';
  8. import ImageProps from './props';
  9. import config from '../common/config';
  10. import { addUnit, getRect } from '../common/utils';
  11. const { prefix } = config;
  12. const name = `${prefix}-image`;
  13. let Image = class Image extends SuperComponent {
  14. constructor() {
  15. super(...arguments);
  16. this.externalClasses = [`${prefix}-class`, `${prefix}-class-load`];
  17. this.options = {
  18. multipleSlots: true,
  19. };
  20. this.properties = ImageProps;
  21. this.data = {
  22. prefix,
  23. isLoading: true,
  24. isFailed: false,
  25. innerStyle: '',
  26. classPrefix: name,
  27. };
  28. this.preSrc = '';
  29. this.lifetimes = {
  30. attached() {
  31. const { width, height } = this.data;
  32. let innerStyle = '';
  33. this.update();
  34. if (width) {
  35. innerStyle += `width: ${addUnit(width)};`;
  36. }
  37. if (height) {
  38. innerStyle += `height: ${addUnit(height)};`;
  39. }
  40. this.setData({
  41. innerStyle,
  42. });
  43. },
  44. };
  45. this.observers = {
  46. src() {
  47. if (this.preSrc === this.properties.src)
  48. return;
  49. this.update();
  50. },
  51. };
  52. this.methods = {
  53. onLoaded(e) {
  54. const sdkVersion = wx.getSystemInfoSync().SDKVersion;
  55. const versionArray = sdkVersion.split('.').map((v) => parseInt(v, 10));
  56. const { mode } = this.properties;
  57. const isInCompatible = versionArray[0] < 2 ||
  58. (versionArray[0] === 2 && versionArray[1] < 10) ||
  59. (versionArray[0] === 2 && versionArray[1] === 10 && versionArray[2] < 3);
  60. if (mode === 'heightFix' && isInCompatible) {
  61. const { height: picHeight, width: picWidth } = e.detail;
  62. getRect(this, '#image').then((rect) => {
  63. const { height } = rect;
  64. const resultWidth = ((height / picHeight) * picWidth).toFixed(2);
  65. this.setData({ innerStyle: `height: ${addUnit(height)}; width: ${resultWidth}px;` });
  66. });
  67. }
  68. this.setData({
  69. isLoading: false,
  70. isFailed: false,
  71. });
  72. this.triggerEvent('load', e.detail);
  73. },
  74. onLoadError(e) {
  75. this.setData({
  76. isLoading: false,
  77. isFailed: true,
  78. });
  79. this.triggerEvent('error', e.detail);
  80. },
  81. update() {
  82. const { src } = this.properties;
  83. this.preSrc = src;
  84. if (!src) {
  85. this.onLoadError({ errMsg: '图片链接为空' });
  86. }
  87. else {
  88. this.setData({
  89. isLoading: true,
  90. isFailed: false,
  91. });
  92. }
  93. },
  94. };
  95. }
  96. };
  97. Image = __decorate([
  98. wxComponent()
  99. ], Image);
  100. export default Image;