index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. Page({
  27. /**
  28. * 页面的初始数据
  29. */
  30. data: {
  31. logoUrl: '',
  32. categoryOne: [],
  33. banner: [],
  34. itemNew: [], // 跑马灯列表
  35. menus: [],
  36. getCouponList: [], //@deprecated
  37. activity: [],
  38. timeList: [], //@deprecated
  39. killIndex: 0, //@deprecated 点击当前index值;
  40. seckillTimeIndex: 0, //@deprecated 当前秒杀index;
  41. killIndexTime: 0, //@deprecated 点击当前index值所对应的秒杀时间;
  42. killIndexLen: 0, //@deprecated 当前秒杀的产品列表长度
  43. seckillList: [], //@deprecated
  44. scrollLeft: 0, //@deprecated
  45. status: 1, //@deprecated
  46. lovely: [], //@deprecated
  47. info: {
  48. fastList: [],
  49. bastBanner: [],
  50. firstList: [],
  51. bastList: []
  52. },
  53. avtiveIndex: 0, // 轮播图索引
  54. likeInfo: [],
  55. benefit: [],
  56. hostProduct: [],
  57. indicatorDots: false, // 跑马灯轮播图不带点
  58. circular: true,
  59. autoplay: true, // 跑马灯轮播图自动播放
  60. intervalNew: 3500,
  61. durationNew: 700,
  62. parameter: {
  63. 'navbar': '0',
  64. 'return': '0',
  65. 'class': '5'
  66. },
  67. window: false, // 是否显示优惠券窗口
  68. iShiddenTip: false, // 是否显示提示
  69. isAuto: true, // 是否自动授权;
  70. iShidden: true, // 是否隐藏;
  71. isGoIndex: false, // 是否返回首页;
  72. navH: "",
  73. recommend: {
  74. loadend: false,
  75. loading: false,
  76. loadTitle: '加载更多',
  77. page: 1,
  78. limit: 20,
  79. },
  80. where: {
  81. page: 1,
  82. limit: 20,
  83. cid: 0, //一级分类id
  84. sid: 0 //二级分类id
  85. },
  86. loadend: false, // 是否已全部加载
  87. loading: false, // 是否正在加载
  88. loadTitle: '加载更多',
  89. productList: [],
  90. categoryActive: 0,
  91. activeTabIndex: 0,
  92. countDownHour: "00",
  93. countDownMinute: "00",
  94. countDownSecond: "00",
  95. seckillCont: true, //@deprecated
  96. interval: null,
  97. timerNotification: null,
  98. newGoodsBananr: '', //@deprecated
  99. cpnMine: null, // 挖礦組件
  100. liveList: [],
  101. liveInfo: {}
  102. },
  103. closeTip: function () {
  104. wx.setStorageSync('msg_key', true);
  105. this.setData({
  106. iShiddenTip: true
  107. })
  108. },
  109. // 挖矿组件绑定
  110. onNeedLogin: function () {
  111. this.setData({
  112. iShidden: false
  113. })
  114. },
  115. /**
  116. * 生命周期函数--监听页面加载
  117. */
  118. onLoad: function (options) {
  119. if (options.a && options.a == 'share' && options.id && options.spid) {
  120. wx.navigateTo({
  121. url: '/pages/goods_details/index?id=' + options.id + '&spid=' + options.spid,
  122. })
  123. }
  124. wxh.selfLocation(1);
  125. this.getCategoryData();
  126. // this.getCoupon();
  127. this.get_hot_product();
  128. this.get_product_list();
  129. this.setData({
  130. navH: app.globalData.navHeight
  131. });
  132. if (options.spid) {
  133. app.globalData.spid = options.spid;
  134. }
  135. if (options.scene) {
  136. app.globalData.code = decodeURIComponent(options.scene);
  137. }
  138. if (wx.getStorageSync('msg_key')) {
  139. this.setData({
  140. iShiddenTip: true
  141. });
  142. }
  143. this.getTemlIds();
  144. this.getLiveList();
  145. },
  146. getTemlIds() {
  147. let messageTmplIds = wx.getStorageSync(CACHE_SUBSCRIBE_MESSAGE);
  148. if (!messageTmplIds) {
  149. getTemlIds().then(res => {
  150. if (res.data) {
  151. wx.setStorageSync(CACHE_SUBSCRIBE_MESSAGE, JSON.stringify(res.data));
  152. }
  153. }).catch(() => {})
  154. }
  155. },
  156. //授权
  157. onLoadFun: function () {
  158. // 挖矿组件认证回调
  159. if (this.data.cpnMine != null) {
  160. this.data.cpnMine.onLoadFun();
  161. }
  162. },
  163. catchTouchMove: function (res) {
  164. return false
  165. },
  166. /** 关闭优惠券窗口 */
  167. onClose: function () {
  168. this.setData({
  169. window: false
  170. });
  171. },
  172. /**
  173. * 生命周期函数--监听页面初次渲染完成
  174. */
  175. onReady: function () {},
  176. /**
  177. * 轮播监听
  178. */
  179. bindchange(e) {
  180. var index = e.detail.current;
  181. this.setData({
  182. avtiveIndex: index
  183. });
  184. },
  185. /**
  186. * 生命周期函数--监听页面显示
  187. */
  188. onShow: function () {
  189. // this.getCoupon();
  190. this.getIndexConfig();
  191. // this.getSeckillTime();
  192. var that = this
  193. this.setData({
  194. timerNotification: setInterval(function () {
  195. that.loadNotifications()
  196. }, 1000 * 60 * 3)
  197. })
  198. app.updateUnread()
  199. },
  200. // 加载通知,目前只有跑马灯
  201. loadNotifications: function () {
  202. var that = this
  203. getNotifications().then(res => {
  204. if (res.data.carousel.length > 0) {
  205. that.setData({
  206. itemNew: res.data.carousel
  207. })
  208. }
  209. })
  210. },
  211. getIndexConfig: function () {
  212. var that = this;
  213. getIndexData().then(res => {
  214. that.setData({
  215. banner: res.data.banner,
  216. menus: res.data.menus,
  217. activity: res.data.activity,
  218. lovely: res.data.lovely,
  219. info: res.data.info,
  220. itemNew: res.data.roll,
  221. likeInfo: res.data.likeInfo,
  222. benefit: res.data.benefit,
  223. logoUrl: res.data.logoUrl,
  224. couponList: res.data.couponList,
  225. });
  226. app.setUnread(res.data.notice);
  227. })
  228. },
  229. getLiveList: function () {
  230. getLiveList(1, 20).then(res => {
  231. if (res.data.length == 1) {
  232. this.setData({
  233. liveInfo: res.data[0]
  234. });
  235. } else {
  236. this.setData({
  237. liveList: res.data
  238. });
  239. }
  240. }).catch(res => {
  241. })
  242. },
  243. /**
  244. * 商品详情跳转
  245. */
  246. goDetailType: function (e) {
  247. let item = e.currentTarget.dataset.items
  248. if (item.activity && item.activity.type === "1") {
  249. wx.navigateTo({
  250. url: `/pages/activity/goods_seckill_details/index?id=${item.activity.id}&time=${item.activity.time}&status=1`
  251. });
  252. } else if (item.activity && item.activity.type === "2") {
  253. wx.navigateTo({
  254. url: `/pages/activity/goods_bargain_details/index?id=${item.activity.id}`
  255. });
  256. } else if (item.activity && item.activity.type === "3") {
  257. wx.navigateTo({
  258. url: `/pages/activity/goods_combination_details/index?id=${item.activity.id}`
  259. });
  260. } else {
  261. wx.navigateTo({
  262. url: `/pages/goods_details/index?id=${item.id}`
  263. });
  264. }
  265. },
  266. /**
  267. * 获取我的推荐
  268. */
  269. get_hot_product: function (isPage) {
  270. var that = this;
  271. if (that.data.recommend.loadend) return;
  272. if (that.data.recommend.loading) return;
  273. if (isPage === true) that.setData({
  274. hostProduct: []
  275. });
  276. that.setData({
  277. 'recommend.loading': true,
  278. 'recommend.loadTitle': ''
  279. });
  280. getProductHot(that.data.recommend.page, that.data.recommend.limit).then(res => {
  281. let list = res.data;
  282. let hostProduct = app.SplitArray(list, that.data.hostProduct);
  283. let loadend = list.length < that.data.recommend.limit;
  284. that.setData({
  285. 'recommend.loadend': loadend,
  286. 'recommend.loading': false,
  287. 'recommend.loadTitle': loadend ? '已全部加载' : '加载更多',
  288. hostProduct: hostProduct,
  289. ['recommend.page']: that.data.recommend.page + 1,
  290. });
  291. }).catch(err => {
  292. that.setData({
  293. loading: false,
  294. loadTitle: '加载更多'
  295. });
  296. });
  297. },
  298. /** 点击上方活动标签按钮 */
  299. onTapCategoryTabItem: function (event) {
  300. let activeTabIndex = event.detail.index;
  301. if (activeTabIndex <= 0 || activeTabIndex > this.data.categoryOne.children.length) {
  302. return
  303. }
  304. this.setData({
  305. activeTabIndex: activeTabIndex
  306. })
  307. if (this.data.categoryOne.children[activeTabIndex - 1].id === 199) {
  308. // 黑洞
  309. if (this.data.cpnMine == null) {
  310. this.setData({
  311. cpnMine: this.selectComponent('#mine'),
  312. })
  313. }
  314. var _ = this.data.cpnMine && this.data.cpnMine.show()
  315. return
  316. } else {
  317. var _ = this.data.cpnMine && this.data.cpnMine.hide()
  318. }
  319. this.setData({
  320. 'where.page': 1,
  321. loadend: false,
  322. loading: false,
  323. 'where.cid': this.data.categoryOne.id,
  324. 'where.sid': this.data.categoryOne.children[activeTabIndex - 1].id,
  325. categoryActive: 0
  326. })
  327. this.get_product_list(true);
  328. },
  329. productTap: function (e) {
  330. let index = e.currentTarget.dataset.indexn;
  331. this.setData({
  332. categoryActive: index,
  333. 'where.page': 1,
  334. loadend: false,
  335. loading: false,
  336. 'where.sid': this.data.categoryOne[this.data.activeTabIndex - 1].children[index].id
  337. })
  338. this.get_product_list(true);
  339. },
  340. get_product_list: function (isPage) {
  341. var that = this;
  342. if (that.data.loading) return;
  343. if (that.data.loadend) return;
  344. if (isPage === true) that.setData({
  345. productList: []
  346. });
  347. that.setData({
  348. loading: true,
  349. loadTitle: ''
  350. });
  351. getProductslist(that.data.where).then(res => {
  352. let list = res.data;
  353. let productList = app.SplitArray(list, that.data.productList);
  354. let loadend = list.length < that.data.where.limit;
  355. that.setData({
  356. loadend: loadend,
  357. loading: false,
  358. loadTitle: loadend ? '已全部加载' : '加载更多',
  359. productList: productList,
  360. ['where.page']: that.data.where.page + 1
  361. });
  362. }).catch(err => {
  363. that.setData({
  364. loading: false,
  365. loadTitle: '加载更多'
  366. });
  367. });
  368. },
  369. // getSeckillTime: function () {
  370. // let that = this;
  371. // getSeckillIndexTime().then(res => {
  372. // let timeList = res.data.seckillTime, seckillTimeIndex = res.data.seckillTimeIndex;
  373. // that.setData({
  374. // timeList: timeList,
  375. // seckillCont: res.data.seckillCont,
  376. // killIndex: seckillTimeIndex,
  377. // seckillTimeIndex: seckillTimeIndex,
  378. // killIndexTime: timeList[that.data.killIndex].stop,
  379. // status: timeList[seckillTimeIndex].status
  380. // })
  381. // wxh.time(timeList[that.data.killIndex].stop, that,false);
  382. // that.getSeckillLists();
  383. // }).catch(()=>{
  384. // });
  385. // },
  386. // 页面事件, 不再使用
  387. setTime: function (e) {
  388. let index = e.currentTarget.dataset.index;
  389. this.setData({
  390. killIndex: index,
  391. status: that.data.timeList[index].status
  392. })
  393. // that.getSeckillLists();
  394. },
  395. // 不再触发
  396. // getSeckillLists: function () {
  397. // let that = this;
  398. // let timeId = that.data.timeList[that.data.killIndex].id;
  399. // getSeckillList(timeId, {
  400. // page: 1,
  401. // limit: 20
  402. // }).then(res => {
  403. // that.setData({
  404. // seckillList: res.data
  405. // })
  406. // if (this.data.timeList.length) {
  407. // let query = wx.createSelectorQuery().in(this);
  408. // query.select('.timeLen').boundingClientRect(function (res) {
  409. // if (res) {
  410. // that.setData({
  411. // scrollLeft: (that.data.killIndex - 1.8) * res.width
  412. // });
  413. // }
  414. // }).exec();
  415. // if (that.data.killIndex === that.data.seckillTimeIndex) {
  416. // that.setData({
  417. // killIndexLen: res.data.length
  418. // })
  419. // }
  420. // }
  421. // }).catch(() => {});
  422. // },
  423. /** 获取分类列表 */
  424. getCategoryData: function () {
  425. let that = this;
  426. getCategoryList().then(res => {
  427. that.setData({
  428. categoryOne: res.data.length > 0 ? res.data[0] : []
  429. })
  430. });
  431. },
  432. /** 获取优惠券列表 不再触发*/
  433. // getCoupon: function () {
  434. // var that = this;
  435. // getCoupons({
  436. // page: 1,
  437. // limit: 6
  438. // }).then(res => {
  439. // that.setData({
  440. // getCouponList: res.data
  441. // })
  442. // }).catch(err => {
  443. // // app.Tips({
  444. // // title: err
  445. // // });
  446. // });
  447. // },
  448. // 页面回调,不再触发
  449. // receiveCoupon: function (e) {
  450. // if (!app.globalData.isLog) {
  451. // this.setData({
  452. // iShidden: false
  453. // });
  454. // } else {
  455. // var that = this;
  456. // var list = that.data.getCouponList;
  457. // var index = e.currentTarget.dataset.index;
  458. // var id = that.data.getCouponList[index].id;
  459. // getCouponReceive({
  460. // couponId: id
  461. // })
  462. // .then(function () {
  463. // list[index].is_use = true;
  464. // that.setData({
  465. // getCouponList: that.data.getCouponList
  466. // })
  467. // app.Tips({
  468. // title: "领取成功"
  469. // });
  470. // })
  471. // .catch(function (err) {
  472. // // app.Tips({
  473. // // title: err
  474. // // });
  475. // });
  476. // }
  477. // },
  478. /**
  479. * 生命周期函数--监听页面隐藏
  480. */
  481. onHide: function () {
  482. this.setData({
  483. window: false
  484. });
  485. this.data.interval !== null && clearInterval(this.data.interval);
  486. if (this.data.timerNotification != null) {
  487. clearInterval(this.data.timerNotification);
  488. this.setData({
  489. timerNotification: null,
  490. })
  491. }
  492. var _ = this.data.cpnMine && this.data.cpnMine.hide()
  493. },
  494. /**
  495. * 生命周期函数--监听页面卸载
  496. */
  497. onUnload: function () {
  498. this.data.interval !== null && clearInterval(this.data.interval);
  499. },
  500. /**
  501. * 页面相关事件处理函数--监听用户下拉动作
  502. */
  503. onPullDownRefresh: function () {
  504. this.getIndexConfig();
  505. wx.stopPullDownRefresh();
  506. },
  507. /**
  508. * 页面上拉触底事件的处理函数
  509. */
  510. onReachBottom: function () {
  511. this.get_hot_product();
  512. this.get_product_list();
  513. },
  514. /**
  515. * 用户点击右上角分享
  516. */
  517. onShareAppMessage: function () {
  518. }
  519. })