index.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. Component({
  2. options: {
  3. multipleSlots: true, // 在组件定义时的选项中启用多slot支持
  4. addGlobalClass: true,
  5. },
  6. intersectionObserverContext: null,
  7. externalClasses: [
  8. 'card-class',
  9. 'title-class',
  10. 'desc-class',
  11. 'num-class',
  12. 'thumb-class',
  13. 'specs-class',
  14. 'price-class',
  15. 'origin-price-class',
  16. 'price-prefix-class',
  17. ],
  18. properties: {
  19. hidden: {
  20. // 设置为null代表不做类型转换
  21. type: null,
  22. value: false,
  23. observer(hidden) {
  24. // null就是代表没有设置,没有设置的话不setData,防止祖先组件触发的setHidden操作被覆盖
  25. if (hidden !== null) {
  26. this.setHidden(!!hidden);
  27. }
  28. },
  29. },
  30. id: {
  31. type: String,
  32. // `goods-card-88888888`
  33. // 不能在这里写生成逻辑,如果在这里写,那么假设有多个goods-list时,他们将共享这个值
  34. value: '',
  35. observer: (id) => {
  36. this.genIndependentID(id);
  37. if (this.properties.thresholds?.length) {
  38. this.createIntersectionObserverHandle();
  39. }
  40. },
  41. },
  42. data: {
  43. type: Object,
  44. observer(goods) {
  45. // 有ID的商品才渲染
  46. if (!goods) {
  47. return;
  48. }
  49. /** 划线价是否有效 */
  50. let isValidityLinePrice = true;
  51. // 判断一次划线价格是否合理
  52. if (
  53. goods.originPrice &&
  54. goods.price &&
  55. goods.originPrice < goods.price
  56. ) {
  57. isValidityLinePrice = false;
  58. }
  59. // 敲定换行数量默认值
  60. if (goods.lineClamp === undefined || goods.lineClamp <= 0) {
  61. // tag数组长度 大于0 且 可见
  62. // 指定换行为1行
  63. if ((goods.tags?.length || 0) > 0 && !goods.hideKey?.tags) {
  64. goods.lineClamp = 1;
  65. } else {
  66. goods.lineClamp = 2;
  67. }
  68. }
  69. this.setData({ goods, isValidityLinePrice });
  70. },
  71. },
  72. layout: {
  73. type: String,
  74. value: 'horizontal',
  75. },
  76. thumbMode: {
  77. type: String,
  78. value: 'aspectFill',
  79. },
  80. priceFill: {
  81. type: Boolean,
  82. value: true,
  83. },
  84. currency: {
  85. type: String,
  86. value: '¥',
  87. },
  88. lazyLoad: {
  89. type: Boolean,
  90. value: false,
  91. },
  92. centered: {
  93. type: Boolean,
  94. value: false,
  95. },
  96. pricePrefix: {
  97. type: String,
  98. value: '',
  99. },
  100. /** 元素可见监控阈值, 数组长度大于0就创建 */
  101. thresholds: {
  102. type: Array,
  103. value: [],
  104. observer(current) {
  105. if (current && current.length) {
  106. this.createIntersectionObserverHandle();
  107. } else {
  108. this.clearIntersectionObserverHandle();
  109. }
  110. },
  111. },
  112. specsIconClassPrefix: {
  113. type: String,
  114. value: 'wr',
  115. },
  116. specsIcon: {
  117. type: String,
  118. value: 'expand_more',
  119. },
  120. addCartIconClassPrefix: {
  121. type: String,
  122. value: 'wr',
  123. },
  124. addCartIcon: {
  125. type: String,
  126. value: 'cart',
  127. },
  128. },
  129. data: {
  130. hiddenInData: false,
  131. independentID: '',
  132. goods: { id: '' },
  133. /** 保证划线价格不小于原价,否则不渲染划线价 */
  134. isValidityLinePrice: false,
  135. },
  136. lifetimes: {
  137. ready() {
  138. this.init();
  139. },
  140. detached() {
  141. this.clear();
  142. },
  143. },
  144. methods: {
  145. clickHandle() {
  146. this.triggerEvent('click', { goods: this.data.goods });
  147. },
  148. clickThumbHandle() {
  149. this.triggerEvent('thumb', { goods: this.data.goods });
  150. },
  151. clickSpecsHandle() {
  152. this.triggerEvent('specs', { goods: this.data.goods });
  153. },
  154. clickTagHandle(evt) {
  155. const { index } = evt.currentTarget.dataset;
  156. this.triggerEvent('tag', { goods: this.data.goods, index });
  157. },
  158. // 加入购物车
  159. addCartHandle(e) {
  160. const { id } = e.currentTarget;
  161. const { id: cardID } = e.currentTarget.dataset;
  162. this.triggerEvent('add-cart', {
  163. ...e.detail,
  164. id,
  165. cardID,
  166. goods: this.data.goods,
  167. });
  168. },
  169. genIndependentID(id, cb) {
  170. let independentID;
  171. if (id) {
  172. independentID = id;
  173. } else {
  174. independentID = `goods-card-${~~(Math.random() * 10 ** 8)}`;
  175. }
  176. this.setData({ independentID }, cb);
  177. },
  178. init() {
  179. const { thresholds, id, hidden } = this.properties;
  180. if (hidden !== null) {
  181. this.setHidden(!!hidden);
  182. }
  183. this.genIndependentID(id || '', () => {
  184. if (thresholds && thresholds.length) {
  185. this.createIntersectionObserverHandle();
  186. }
  187. });
  188. },
  189. clear() {
  190. this.clearIntersectionObserverHandle();
  191. },
  192. setHidden(hidden) {
  193. this.setData({ hiddenInData: !!hidden });
  194. },
  195. createIntersectionObserverHandle() {
  196. if (this.intersectionObserverContext || !this.data.independentID) {
  197. return;
  198. }
  199. this.intersectionObserverContext = wx
  200. .createIntersectionObserver(this, {
  201. thresholds: this.properties.thresholds,
  202. })
  203. .relativeToViewport();
  204. this.intersectionObserverContext.observe(
  205. `#${this.data.independentID}`,
  206. (res) => {
  207. this.intersectionObserverCB(res);
  208. },
  209. );
  210. },
  211. intersectionObserverCB(ob) {
  212. this.triggerEvent('ob', {
  213. goods: this.data.goods,
  214. context: this.intersectionObserverContext,
  215. ob,
  216. });
  217. },
  218. clearIntersectionObserverHandle() {
  219. if (this.intersectionObserverContext) {
  220. try {
  221. this.intersectionObserverContext.disconnect();
  222. } catch (e) {}
  223. this.intersectionObserverContext = null;
  224. }
  225. },
  226. },
  227. });