index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. Component({
  2. options: {
  3. addGlobalClass: true,
  4. },
  5. properties: {
  6. id: {
  7. type: String,
  8. value: '',
  9. observer(id) {
  10. this.genIndependentID(id);
  11. if (this.properties.thresholds?.length) {
  12. this.createIntersectionObserverHandle();
  13. }
  14. },
  15. },
  16. data: {
  17. type: Object,
  18. observer(data) {
  19. if (!data) {
  20. return;
  21. }
  22. let isValidityLinePrice = true;
  23. if (data.originPrice && data.price && data.originPrice < data.price) {
  24. isValidityLinePrice = false;
  25. }
  26. this.setData({ goods: data, isValidityLinePrice });
  27. },
  28. },
  29. currency: {
  30. type: String,
  31. value: '¥',
  32. },
  33. thresholds: {
  34. type: Array,
  35. value: [],
  36. observer(thresholds) {
  37. if (thresholds && thresholds.length) {
  38. this.createIntersectionObserverHandle();
  39. } else {
  40. this.clearIntersectionObserverHandle();
  41. }
  42. },
  43. },
  44. },
  45. data: {
  46. independentID: '',
  47. goods: { id: '' },
  48. isValidityLinePrice: false,
  49. },
  50. lifetimes: {
  51. ready() {
  52. this.init();
  53. },
  54. detached() {
  55. this.clear();
  56. },
  57. },
  58. pageLifeTimes: {},
  59. methods: {
  60. clickHandle() {
  61. this.triggerEvent('click', { goods: this.data.goods });
  62. },
  63. clickThumbHandle() {
  64. this.triggerEvent('thumb', { goods: this.data.goods });
  65. },
  66. addCartHandle(e) {
  67. const { id } = e.currentTarget;
  68. const { id: cardID } = e.currentTarget.dataset;
  69. this.triggerEvent('add-cart', {
  70. ...e.detail,
  71. id,
  72. cardID,
  73. goods: this.data.goods,
  74. });
  75. },
  76. genIndependentID(id) {
  77. let independentID;
  78. if (id) {
  79. independentID = id;
  80. } else {
  81. independentID = `goods-card-${~~(Math.random() * 10 ** 8)}`;
  82. }
  83. this.setData({ independentID });
  84. },
  85. init() {
  86. const { thresholds, id } = this.properties;
  87. this.genIndependentID(id);
  88. if (thresholds && thresholds.length) {
  89. this.createIntersectionObserverHandle();
  90. }
  91. },
  92. clear() {
  93. this.clearIntersectionObserverHandle();
  94. },
  95. intersectionObserverContext: null,
  96. createIntersectionObserverHandle() {
  97. if (this.intersectionObserverContext || !this.data.independentID) {
  98. return;
  99. }
  100. this.intersectionObserverContext = this.createIntersectionObserver({
  101. thresholds: this.properties.thresholds,
  102. }).relativeToViewport();
  103. this.intersectionObserverContext.observe(
  104. `#${this.data.independentID}`,
  105. (res) => {
  106. this.intersectionObserverCB(res);
  107. },
  108. );
  109. },
  110. intersectionObserverCB() {
  111. this.triggerEvent('ob', {
  112. goods: this.data.goods,
  113. context: this.intersectionObserverContext,
  114. });
  115. },
  116. clearIntersectionObserverHandle() {
  117. if (this.intersectionObserverContext) {
  118. try {
  119. this.intersectionObserverContext.disconnect();
  120. } catch (e) {}
  121. this.intersectionObserverContext = null;
  122. }
  123. },
  124. },
  125. });