index.js 14 KB

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