index.js 5.9 KB

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