index.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. const app = getApp();
  2. import wxh from '../../utils/wxh.js';
  3. import {
  4. getIndexData,
  5. getCoupons,
  6. getTemlIds,
  7. getLiveList,
  8. getNotifications
  9. } from '../../api/api.js';
  10. import {
  11. CACHE_SUBSCRIBE_MESSAGE
  12. } from '../../config.js';
  13. import {
  14. getCategoryList,
  15. getProductHot,
  16. getProductslist
  17. } from '../../api/store.js';
  18. import {
  19. getCouponReceive
  20. } from '../../api/user.js';
  21. import {
  22. getSeckillIndexTime,
  23. getSeckillList
  24. } from '../../api/activity.js';
  25. import Util from '../../utils/util.js';
  26. import d from '../../utils/d.js';
  27. const CATE_BLACKHOLE_ID = 199
  28. const PAGE_LIMIT = 20
  29. const THROTTLE_TIME = 100 // 滚动回调限流 ms
  30. const HEIGHT_OF_ROW = 500 // 摘抄自 css, 商品高度,包含图片文字 margin. rpx
  31. const ITEM_NUM_OF_ROW = 2 // 设计为一行 2 个元素
  32. Page({
  33. /**
  34. * 页面的初始数据
  35. */
  36. data: {
  37. CATE_BLACKHOLE_ID: CATE_BLACKHOLE_ID,
  38. logoUrl: '', // 左上方 logo
  39. prodCates: [], // 分类列表/活动列表 (目前只有一个,就是“活动”)
  40. banner: [], // 顶部 banner
  41. itemNew: [], // 跑马灯列表
  42. menus: [],
  43. getCouponList: [], //@deprecated
  44. activity: [],
  45. timeList: [], //@deprecated
  46. killIndex: 0, //@deprecated 点击当前index值;
  47. seckillTimeIndex: 0, //@deprecated 当前秒杀index;
  48. killIndexTime: 0, //@deprecated 点击当前index值所对应的秒杀时间;
  49. killIndexLen: 0, //@deprecated 当前秒杀的产品列表长度
  50. seckillList: [], //@deprecated
  51. scrollLeft: 0, //@deprecated
  52. status: 1, //@deprecated
  53. lovely: [], //@deprecated
  54. info: {
  55. fastList: [],
  56. bastBanner: [], // 精品推荐 banner
  57. firstList: [], // 首发新品
  58. bastList: [] // 精品推荐
  59. },
  60. avtiveIndex: 0, // 轮播图索引
  61. likeInfo: [], // 热门榜单
  62. benefit: [], // 促销单品
  63. hotProducts: [], // 海量精品 ?
  64. indicatorDots: false, // 跑马灯轮播图不带点
  65. circular: true, // 轮播图圆点
  66. autoplay: true, // 跑马灯轮播图自动播放
  67. intervalNew: 3500, // 轮播图自动播放间隔
  68. durationNew: 700, // 轮播图自动播放时长
  69. parameter: {
  70. 'navbar': '0',
  71. 'return': '0',
  72. 'class': '5'
  73. },
  74. window: false, // 是否显示优惠券窗口
  75. iShiddenTip: false, // 是否显示提示
  76. isAuto: true, // 是否自动授权;
  77. iShidden: true, // 是否隐藏;
  78. isGoIndex: false, // 是否返回首页;
  79. recommend: {
  80. loadend: false, // 已全部加载
  81. loading: false, // 正在加载
  82. loadTitle: '加载更多',
  83. page: 1,
  84. limit: 20,
  85. },
  86. /// 以下为拉洋片算法参数
  87. navH: 0, // 标题栏高度
  88. navPxH: 0, //
  89. rowHeight: HEIGHT_OF_ROW,
  90. rowPxHeight: HEIGHT_OF_ROW,
  91. rowProdNum: ITEM_NUM_OF_ROW,
  92. rowCacheNum: 5, // 拉洋片缓冲商品: 3 行
  93. ///
  94. activeTabIndex: 0,
  95. countDownHour: "00",
  96. countDownMinute: "00",
  97. countDownSecond: "00",
  98. seckillCont: true, //@deprecated
  99. interval: null,
  100. timerNotification: null,
  101. newGoodsBananr: '', //@deprecated
  102. cpnMine: null, // 挖礦組件
  103. liveList: [], // 直播列表
  104. liveInfo: {}
  105. },
  106. // 关闭提示添加到小程序窗口
  107. closeTip: function () {
  108. wx.setStorageSync('msg_key', true);
  109. this.setData({
  110. iShiddenTip: true
  111. })
  112. },
  113. // 挖矿组件绑定
  114. onNeedLogin: function () {
  115. this.setData({
  116. iShidden: false
  117. })
  118. },
  119. /**
  120. * 生命周期函数--监听页面加载
  121. */
  122. onLoad: function (options) {
  123. // 由分享链接跳转过来
  124. // a === 'share'
  125. // id 商品ID
  126. // spid spread_id
  127. if (options.a && options.a == 'share' && options.id && options.spid) {
  128. wx.navigateTo({
  129. url: '/pages/goods_details/index?id=' + options.id + '&spid=' + options.spid,
  130. })
  131. }
  132. // 获取 nav 高度等信息
  133. let query = wx.createSelectorQuery()
  134. query.select('.indexs').boundingClientRect((rect) => {
  135. let width = rect.width
  136. let ratio = 750 / width // px -> rpx 需要乘以的数
  137. this.setData({
  138. rowPxHeight: Math.floor(this.data.rowHeight / ratio),
  139. navH: app.globalData.navHeight,
  140. navPxH: Math.floor(app.globalData.navHeight / ratio),
  141. })
  142. d.debug('width:', width, 'ratio:', ratio)
  143. d.debug('rowHeight:', this.data.rowPxHeight, 'navH:', this.data.navH, 'navPxH:', this.data.navPxH)
  144. }).exec()
  145. wxh.selfLocation(1);
  146. this.getCategoryData();
  147. // this.getCoupon();
  148. this.get_hot_product();
  149. // this.get_product_list();
  150. if (options.spid) {
  151. app.globalData.spid = options.spid;
  152. }
  153. if (options.scene) {
  154. app.globalData.code = decodeURIComponent(options.scene);
  155. }
  156. if (wx.getStorageSync('msg_key')) {
  157. this.setData({
  158. iShiddenTip: true
  159. });
  160. }
  161. this.getTemlIds();
  162. this.getLiveList();
  163. },
  164. getTemlIds() {
  165. let messageTmplIds = wx.getStorageSync(CACHE_SUBSCRIBE_MESSAGE);
  166. if (!messageTmplIds) {
  167. getTemlIds().then(res => {
  168. if (res.data) {
  169. wx.setStorageSync(CACHE_SUBSCRIBE_MESSAGE, JSON.stringify(res.data));
  170. }
  171. }).catch(err => {
  172. d.error('err:', err)
  173. })
  174. }
  175. },
  176. //授权
  177. onLoadFun: function () {
  178. // 挖矿组件认证回调
  179. if (this.data.cpnMine != null) {
  180. this.data.cpnMine.onLoadFun();
  181. }
  182. },
  183. catchTouchMove: function (res) {
  184. return false
  185. },
  186. /** 关闭优惠券窗口 */
  187. onClose: function () {
  188. this.setData({
  189. window: false
  190. });
  191. },
  192. /**
  193. * 生命周期函数--监听页面初次渲染完成
  194. */
  195. onReady: function () {},
  196. /**
  197. * 轮播监听
  198. */
  199. bindchange(e) {
  200. var index = e.detail.current;
  201. this.setData({
  202. avtiveIndex: index
  203. });
  204. },
  205. /**
  206. * 生命周期函数--监听页面显示
  207. */
  208. onShow: function () {
  209. // this.getCoupon();
  210. this.getIndexConfig();
  211. // this.getSeckillTime();
  212. var that = this
  213. this.setData({
  214. timerNotification: setInterval(function () {
  215. that.loadNotifications()
  216. }, 1000 * 60 * 3)
  217. })
  218. app.updateUnread()
  219. },
  220. // 加载通知,目前只有跑马灯
  221. loadNotifications: function () {
  222. var that = this
  223. getNotifications().then(res => {
  224. if (res.data.carousel.length > 0) {
  225. that.setData({
  226. itemNew: res.data.carousel
  227. })
  228. }
  229. })
  230. },
  231. getIndexConfig: function () {
  232. var that = this;
  233. getIndexData().then(res => {
  234. that.setData({
  235. banner: res.data.banner,
  236. menus: res.data.menus,
  237. activity: res.data.activity,
  238. lovely: res.data.lovely,
  239. info: res.data.info,
  240. itemNew: res.data.roll,
  241. likeInfo: res.data.likeInfo,
  242. benefit: res.data.benefit,
  243. logoUrl: res.data.logoUrl,
  244. couponList: res.data.couponList,
  245. });
  246. app.setUnread(res.data.notice);
  247. })
  248. },
  249. getLiveList: function () {
  250. let that = this
  251. getLiveList(1, 20).then(res => {
  252. if (res.data.length == 1) {
  253. that.setData({
  254. liveInfo: res.data[0]
  255. });
  256. } else {
  257. that.setData({
  258. liveList: res.data
  259. });
  260. }
  261. }).catch(err => {
  262. d.error('error:', err)
  263. })
  264. },
  265. /**
  266. * 商品详情跳转 商品点击事件
  267. */
  268. goDetailType: function (e) {
  269. let item = e.currentTarget.dataset.items
  270. if (item.activity && item.activity.type === "1") {
  271. wx.navigateTo({
  272. url: `/pages/activity/goods_seckill_details/index?id=${item.activity.id}&time=${item.activity.time}&status=1`
  273. });
  274. } else if (item.activity && item.activity.type === "2") {
  275. wx.navigateTo({
  276. url: `/pages/activity/goods_bargain_details/index?id=${item.activity.id}`
  277. });
  278. } else if (item.activity && item.activity.type === "3") {
  279. wx.navigateTo({
  280. url: `/pages/activity/goods_combination_details/index?id=${item.activity.id}`
  281. });
  282. } else {
  283. wx.navigateTo({
  284. url: `/pages/goods_details/index?id=${item.id}`
  285. });
  286. }
  287. },
  288. /**
  289. * 获取我的推荐
  290. */
  291. get_hot_product: function (isReload) {
  292. if (this.data.recommend.loadend) return;
  293. if (this.data.recommend.loading) return;
  294. if (isReload) {
  295. this.setData({
  296. 'recommend.page': 1,
  297. hotProducts: []
  298. });
  299. }
  300. this.setData({
  301. 'recommend.loading': true,
  302. 'recommend.loadTitle': ''
  303. });
  304. let that = this;
  305. getProductHot(this.data.recommend.page, this.data.recommend.limit).then(res => {
  306. let list = res.data;
  307. let hotProducts = app.SplitArray(list, that.data.hotProducts);
  308. let loadend = list.length < that.data.recommend.limit;
  309. that.setData({
  310. 'recommend.loadend': loadend,
  311. 'recommend.loading': false,
  312. 'recommend.loadTitle': loadend ? '已全部加载' : '加载更多',
  313. hotProducts: hotProducts,
  314. ['recommend.page']: that.data.recommend.page + 1,
  315. });
  316. }).catch(err => {
  317. that.setData({
  318. 'recommend.loading': false,
  319. 'recommend.loadTitle': '加载更多'
  320. });
  321. });
  322. },
  323. /** 点击上方活动标签按钮 */
  324. onTapCategoryTabItem: function (event) {
  325. let tabIndex = event.detail.index;
  326. d.debug('this.data.prodCates.children.length:', this.data.prodCates.children.length, 'current tab index:', tabIndex)
  327. // 不能过界 0 是主场,不包含在 prodCates 内,
  328. // 主场不处理
  329. if (tabIndex <= 0 || tabIndex > this.data.prodCates.children.length) {
  330. d.debug('tab index out of range:', tabIndex)
  331. this.setData({
  332. activeTabIndex: tabIndex
  333. })
  334. return
  335. }
  336. if (this.data.prodCates.children[tabIndex - 1].id === CATE_BLACKHOLE_ID) {
  337. // 黑洞
  338. let mine = null
  339. if (this.data.cpnMine == null) {
  340. mine = this.selectComponent('#mine')
  341. }
  342. this.setData({
  343. activeTabIndex: tabIndex,
  344. cpnMine: mine,
  345. })
  346. var _ = this.data.cpnMine && this.data.cpnMine.show()
  347. return
  348. }
  349. var _ = this.data.cpnMine && this.data.cpnMine.hide()
  350. this.setData({
  351. activeTabIndex: tabIndex
  352. })
  353. // 首屏
  354. if (this.data.prodCates.children[tabIndex - 1].page <= 1) {
  355. this.get_product_list(tabIndex);
  356. }
  357. wx.pageScrollTo({
  358. scrollTop: this.data.prodCates.children[tabIndex - 1].scrollPos
  359. })
  360. },
  361. // 获取商品列表
  362. // @tabIndex 活动标签 @isReload 是否重新加载
  363. get_product_list: function (tabIndex, isReload) {
  364. // 不处理主场和黑洞星球
  365. if (tabIndex <= 0 || tabIndex > this.data.prodCates.children.length) {
  366. d.debug('invalid tab index:', tabIndex)
  367. return
  368. }
  369. // 大类 ID
  370. let cid = this.data.prodCates.id
  371. // 子类
  372. let act = this.data.prodCates.children[tabIndex - 1]
  373. if (act.loading) return
  374. if (act.loaded && !isReload) return
  375. let cindex = tabIndex - 1 // tab index 转换到 数据的 index
  376. if (isReload) {
  377. this.setData({
  378. [`prodCates.children[${cindex}].page`]: 1,
  379. [`prodCates.children[${cindex}].products`]: [],
  380. [`prodCates.children[${cindex}].aboveShowRowIndex`]: 0,
  381. [`prodCates.children[${cindex}].belowShowRowNum`]: 0
  382. })
  383. }
  384. this.setData({
  385. [`prodCates.children[${cindex}].loading`]: true,
  386. [`prodCates.children[${cindex}].loadTitle`]: ''
  387. });
  388. var that = this;
  389. getProductslist({
  390. page: act.page,
  391. limit: PAGE_LIMIT,
  392. cid: cid,
  393. sid: act.id,
  394. }).then(res => {
  395. let list = [] // res.data
  396. // 过滤产品列表, 只保留本页用的字段
  397. let counter = act.products.length
  398. res.data.forEach((prod) => {
  399. counter += 1
  400. list.push({
  401. id: prod.id,
  402. image: prod.image,
  403. activity: prod.activity,
  404. store_name: prod.store_name,
  405. price: prod.price,
  406. vip_price: prod.vip_price,
  407. sales: prod.sales,
  408. isShow: (counter / 2 - act.aboveShowRowIndex) <= 2 * this.data.rowCacheNum, // 是否显示
  409. })
  410. })
  411. // console.log('list.length:', list.length, 'res.data.length:', res.data.length)
  412. let products = app.SplitArray(list, act.products)
  413. let loaded = list.length < PAGE_LIMIT
  414. that.setData({
  415. [`prodCates.children[${cindex}].loaded`]: loaded,
  416. [`prodCates.children[${cindex}].loading`]: false,
  417. [`prodCates.children[${cindex}].loadTitle`]: loaded ? '已全部加载' : '加载更多',
  418. [`prodCates.children[${cindex}].products`]: products,
  419. [`prodCates.children[${cindex}].page`]: act.page + 1
  420. });
  421. }).catch(err => {
  422. d.error(err)
  423. that.setData({
  424. [`prodCates.children[${cindex}].loading`]: false,
  425. [`prodCates.children[${cindex}].loadTitle`]: '加载更多'
  426. });
  427. });
  428. },
  429. // getSeckillTime: function () {
  430. // let that = this;
  431. // getSeckillIndexTime().then(res => {
  432. // let timeList = res.data.seckillTime, seckillTimeIndex = res.data.seckillTimeIndex;
  433. // that.setData({
  434. // timeList: timeList,
  435. // seckillCont: res.data.seckillCont,
  436. // killIndex: seckillTimeIndex,
  437. // seckillTimeIndex: seckillTimeIndex,
  438. // killIndexTime: timeList[that.data.killIndex].stop,
  439. // status: timeList[seckillTimeIndex].status
  440. // })
  441. // wxh.time(timeList[that.data.killIndex].stop, that,false);
  442. // that.getSeckillLists();
  443. // }).catch(()=>{
  444. // });
  445. // },
  446. // 页面事件, 不再使用
  447. // setTime: function (e) {
  448. // let index = e.currentTarget.dataset.index;
  449. // this.setData({
  450. // killIndex: index,
  451. // status: that.data.timeList[index].status
  452. // })
  453. // // that.getSeckillLists();
  454. // },
  455. // 不再触发
  456. // getSeckillLists: function () {
  457. // let that = this;
  458. // let timeId = that.data.timeList[that.data.killIndex].id;
  459. // getSeckillList(timeId, {
  460. // page: 1,
  461. // limit: 20
  462. // }).then(res => {
  463. // that.setData({
  464. // seckillList: res.data
  465. // })
  466. // if (this.data.timeList.length) {
  467. // let query = wx.createSelectorQuery().in(this);
  468. // query.select('.timeLen').boundingClientRect(function (res) {
  469. // if (res) {
  470. // that.setData({
  471. // scrollLeft: (that.data.killIndex - 1.8) * res.width
  472. // });
  473. // }
  474. // }).exec();
  475. // if (that.data.killIndex === that.data.seckillTimeIndex) {
  476. // that.setData({
  477. // killIndexLen: res.data.length
  478. // })
  479. // }
  480. // }
  481. // }).catch(() => {});
  482. // },
  483. // 获取分类列表
  484. getCategoryData: function () {
  485. let that = this;
  486. getCategoryList().then(res => {
  487. let cates = res.data.length > 0 ? res.data[0] : {}
  488. that.setData({
  489. prodCates: that._rebuildProdCates(cates)
  490. })
  491. d.debug(this.data.prodCates)
  492. });
  493. },
  494. // 整理 this.data.prodCates
  495. _rebuildProdCates: function (cates) {
  496. cates.children.forEach(cate => {
  497. // 不处理黑洞星球
  498. if (cate.id == CATE_BLACKHOLE_ID) {
  499. return
  500. }
  501. // 增加属性
  502. cate.loading = false
  503. cate.loaded = false
  504. cate.loadTitle = '加载更多'
  505. cate.products = []
  506. cate.page = 1 // 当前第几页
  507. cate.scrollPos = 0 // 上次滚动条位置
  508. cate.aboveShowRowIndex = 0 // 可见区域上部行索引
  509. cate.belowShowRowNum = 0 // 底部隐藏行数
  510. })
  511. return cates;
  512. },
  513. /** 获取优惠券列表 不再触发*/
  514. // getCoupon: function () {
  515. // var that = this;
  516. // getCoupons({
  517. // page: 1,
  518. // limit: 6
  519. // }).then(res => {
  520. // that.setData({
  521. // getCouponList: res.data
  522. // })
  523. // }).catch(err => {
  524. // // app.Tips({
  525. // // title: err
  526. // // });
  527. // });
  528. // },
  529. // 页面回调,不再触发
  530. // receiveCoupon: function (e) {
  531. // if (!app.globalData.isLog) {
  532. // this.setData({
  533. // iShidden: false
  534. // });
  535. // } else {
  536. // var that = this;
  537. // var list = that.data.getCouponList;
  538. // var index = e.currentTarget.dataset.index;
  539. // var id = that.data.getCouponList[index].id;
  540. // getCouponReceive({
  541. // couponId: id
  542. // })
  543. // .then(function () {
  544. // list[index].is_use = true;
  545. // that.setData({
  546. // getCouponList: that.data.getCouponList
  547. // })
  548. // app.Tips({
  549. // title: "领取成功"
  550. // });
  551. // })
  552. // .catch(function (err) {
  553. // // app.Tips({
  554. // // title: err
  555. // // });
  556. // });
  557. // }
  558. // },
  559. /**
  560. * 生命周期函数--监听页面隐藏
  561. */
  562. onHide: function () {
  563. this.setData({
  564. window: false
  565. });
  566. this.data.interval !== null && clearInterval(this.data.interval);
  567. if (this.data.timerNotification != null) {
  568. clearInterval(this.data.timerNotification);
  569. this.setData({
  570. timerNotification: null,
  571. })
  572. }
  573. var _ = this.data.cpnMine && this.data.cpnMine.hide()
  574. },
  575. /**
  576. * 生命周期函数--监听页面卸载
  577. */
  578. onUnload: function () {
  579. this.data.interval !== null && clearInterval(this.data.interval);
  580. },
  581. /**
  582. * 页面相关事件处理函数--监听用户下拉动作
  583. */
  584. onPullDownRefresh: function () {
  585. let tabIndex = this.data.activeTabIndex
  586. // 主场刷新
  587. // 其他场刷新
  588. if (tabIndex == 0) {
  589. this.getIndexConfig();
  590. } else if (tabIndex > this.data.prodCates.children.length) {
  591. d.error('invalid tab index:', tabIndex)
  592. } else {
  593. if (!this.data.prodCates.children[tabIndex - 1].id == CATE_BLACKHOLE_ID) {
  594. this.get_product_list(tabIndex, true)
  595. }
  596. }
  597. this.get_hot_product(true)
  598. wx.stopPullDownRefresh();
  599. },
  600. /**
  601. * 页面上拉触底事件的处理函数
  602. */
  603. onReachBottom: function () {
  604. let tabIndex = this.data.activeTabIndex
  605. // 主场只加载热门
  606. // 其他场加载商品列表
  607. if (tabIndex == 0) {
  608. this.get_hot_product();
  609. } else if (tabIndex > 0 &&
  610. tabIndex <= this.data.prodCates.children.length &&
  611. this.data.prodCates.children[tabIndex - 1].id != CATE_BLACKHOLE_ID) {
  612. this.get_product_list(this.data.activeTabIndex);
  613. if (this.data.prodCates.children[tabIndex - 1].loadend) {
  614. this.get_hot_product();
  615. }
  616. }
  617. },
  618. onPageScroll: throttle(function (obj) {
  619. let tabIndex = this.data.activeTabIndex
  620. if (tabIndex <= 0 ||
  621. tabIndex > this.data.prodCates.children.length ||
  622. this.data.prodCates.children[tabIndex - 1].id == CATE_BLACKHOLE_ID) {
  623. d.debug('ignore tab scroll event. tab index:', tabIndex)
  624. return
  625. }
  626. let pos = obj[0].scrollTop //
  627. let calcPos = pos //- this.data.navPxH
  628. calcPos = calcPos > 0 ? calcPos : 0
  629. let rowNum = Math.floor(calcPos / this.data.rowPxHeight) // 滚动过去的总行数, 也是当前 viewport 的开始索引位置
  630. let clearRowNum = rowNum - this.data.rowCacheNum // 隐藏总行数
  631. let cindex = tabIndex - 1
  632. let tabData = this.data.prodCates.children[cindex]
  633. let changingData = {}
  634. if (pos - tabData.scrollPos > 0) { // 向下
  635. // d.debug('DOWN pos:', pos, 'rowPxHeight', this.data.rowPxHeight,
  636. // 'rowNum:', rowNum, 'clearRowNum:', clearRowNum, 'aboveShowRowIndex:', tabData.aboveShowRowIndex)
  637. if (clearRowNum > 0) {
  638. // aboveShowRowIndex 索引的行之前都不显示
  639. for (let i = tabData.aboveShowRowIndex; i < clearRowNum; i++) {
  640. let leftIndex = i * 2
  641. changingData[[`prodCates.children[${cindex}].products[${leftIndex}].isShow`]] = false
  642. changingData[[`prodCates.children[${cindex}].products[${leftIndex + 1}].isShow`]] = false
  643. let belowShowRowIndex = i + 2 * this.data.rowCacheNum
  644. if (belowShowRowIndex < Math.floor(tabData.products.length / 2)) {
  645. leftIndex = belowShowRowIndex * 2
  646. changingData[[`prodCates.children[${cindex}].products[${leftIndex}].isShow`]] = true
  647. changingData[[`prodCates.children[${cindex}].products[${leftIndex + 1}].isShow`]] = true
  648. }
  649. }
  650. }
  651. } else { // 向上
  652. // d.debug('UP pos:', pos, 'rowPxHeight', this.data.rowPxHeight,
  653. // 'rowNum:', rowNum, 'clearRowNum:', clearRowNum, 'aboveShowRowIndex:', tabData.aboveShowRowIndex)
  654. if (clearRowNum >= 0) {
  655. for (let i = tabData.aboveShowRowIndex - 1; i >= clearRowNum; i--) {
  656. let leftIndex = i * 2
  657. changingData[[`prodCates.children[${cindex}].products[${leftIndex}].isShow`]] = true
  658. changingData[[`prodCates.children[${cindex}].products[${leftIndex + 1}].isShow`]] = true
  659. let belowShowRowIndex = i + 2 * this.data.rowCacheNum
  660. if (belowShowRowIndex < tabData.products.length / 2) {
  661. leftIndex = belowShowRowIndex * 2
  662. changingData[[`prodCates.children[${cindex}].products[${leftIndex}].isShow`]] = false
  663. changingData[[`prodCates.children[${cindex}].products[${leftIndex + 1}].isShow`]] = false
  664. }
  665. }
  666. } else {
  667. if (tabData.aboveShowRowIndex > 0) {
  668. for (let i = 0; i < tabData.aboveShowRowIndex; i++) {
  669. this.setData({
  670. [`prodCates.children[${cindex}].products[${i * 2}].isShow`]: true,
  671. [`prodCates.children[${cindex}].products[${i * 2 + 1}].isShow`]: true,
  672. })
  673. // changingData[[`prodCates.children[${cindex}].products[${i * 2}].isShow`]] = true
  674. // changingData[[`prodCates.children[${cindex}].products[${i * 2 + 1}].isShow`]] = true
  675. }
  676. }
  677. }
  678. } //
  679. clearRowNum = clearRowNum > 0 ? clearRowNum : 0;
  680. if (clearRowNum >= 0 &&
  681. !(clearRowNum > 0 && clearRowNum == tabData.aboveShowRowIndex)) {
  682. changingData[[`prodCates.children[${cindex}].aboveShowRowIndex`]] = clearRowNum
  683. let belowShowRowNum = tabData.products.length / 2 - (2 * this.data.rowCacheNum + clearRowNum)
  684. belowShowRowNum = belowShowRowNum > 0 ? belowShowRowNum : 0
  685. // console.log('belowShowRowNum:', belowShowRowNum, 'tabData.products.length:', tabData.products.length, 'clearRowNum:', clearRowNum)
  686. if (belowShowRowNum >= 0) {
  687. changingData[[`prodCates.children[${cindex}].belowShowRowNum`]] = belowShowRowNum
  688. // for (let i = 0; i < belowShowRowNum; i++) {
  689. // let leftIndex = (tabData.products.length / 2 - i - 1) * 2;
  690. // changingData[[`prodCates.children[${cindex}].products[${leftIndex}].isShow`]] = false
  691. // changingData[[`prodCates.children[${cindex}].products[${leftIndex + 1}].isShow`]] = false
  692. // }
  693. }
  694. this.setData(changingData)
  695. }
  696. if (cindex >= 0 && cindex < this.data.prodCates.children.length) {
  697. this.setData({
  698. ['prodCates.children[' + cindex + '].scrollPos']: pos
  699. })
  700. }
  701. // this._debugProducts()
  702. }),
  703. _debugProducts: function () {
  704. let all = [],
  705. display = [],
  706. tabIndex = this.data.activeTabIndex
  707. if (tabIndex <= 0 || tabIndex > this.data.prodCates.children.length) {
  708. return
  709. }
  710. let tabData = this.data.prodCates.children[tabIndex - 1]
  711. for (let i = 0; i < tabData.products.length; i++) {
  712. if (tabData.products[i].isShow) {
  713. display.push(i)
  714. }
  715. all.push(i)
  716. }
  717. d.debug('all:', all)
  718. d.debug('display:', display)
  719. },
  720. /**
  721. * 用户点击右上角分享
  722. */
  723. onShareAppMessage: function () {
  724. }
  725. })
  726. function throttle(fn) {
  727. let valid = true
  728. return function () {
  729. if (!valid) {
  730. return false
  731. }
  732. valid = false
  733. return setTimeout(() => {
  734. fn.call(this, arguments)
  735. valid = true
  736. }, THROTTLE_TIME)
  737. }
  738. }