index.js 28 KB

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