index.js 24 KB

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