index.js 28 KB

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