index.js 14 KB

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