index.js 13 KB

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