index.js 14 KB

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