index.js 14 KB

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