index.js 14 KB

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