index.js 17 KB

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