index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. const app = getApp();
  2. import wxh from '../../utils/wxh.js';
  3. import { getIndexData, getCoupons, getTemlIds, getLiveList } 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:[],
  20. activity:[],
  21. timeList: [],
  22. killIndex: 0,//点击当前index值;
  23. seckillTimeIndex: 0, //当前秒杀index;
  24. killIndexTime: 0,//点击当前index值所对应的秒杀时间;
  25. killIndexLen: 0,//当前秒杀的产品列表长度
  26. seckillList:[],
  27. scrollLeft: 0,
  28. elementWidth:0,
  29. status:1,
  30. lovely: [],
  31. info: {
  32. fastList: [],
  33. bastBanner: [],
  34. firstList: [],
  35. bastList: []
  36. },
  37. avtiveIndex: 0,
  38. likeInfo: [],
  39. benefit:[],
  40. hostProduct: [],
  41. indicatorDots: false,
  42. circular: true,
  43. autoplay: true,
  44. intervalNew: 3500,
  45. durationNew: 700,
  46. parameter:{
  47. 'navbar':'0',
  48. 'return':'0',
  49. 'class':'5'
  50. },
  51. window: false,
  52. iShiddenTip: false,
  53. isAuto: false, //是否自动授权;
  54. iShidden: true, //是否隐藏;
  55. isGoIndex: false, //是否返回首页;
  56. navH: "",
  57. recommend:{
  58. loadend: false,
  59. loading: false,
  60. loadTitle: '加载更多',
  61. page: 1,
  62. limit:20,
  63. },
  64. where: {
  65. page: 1,
  66. limit: 20,
  67. cid: 0, //一级分类id
  68. sid: 0 //二级分类id
  69. },
  70. loadend: false,
  71. loading: false,
  72. loadTitle: '加载更多',
  73. productList: [],
  74. categoryActive: 0,
  75. tapActive:0,
  76. countDownHour: "00",
  77. countDownMinute: "00",
  78. countDownSecond: "00",
  79. seckillCont:true,
  80. interval:null,
  81. newGoodsBananr: '',
  82. liveList: [],
  83. liveInfo: {},
  84. activities: {}
  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. onColse:function(){
  127. this.setData({ window: false});
  128. console.log(this.data.activities);
  129. this.data.activities.forEach(item => {
  130. item.tabOut();
  131. });
  132. },
  133. /**
  134. * 生命周期函数--监听页面初次渲染完成
  135. */
  136. onReady: function () {
  137. },
  138. bindchange(e) {
  139. var index = e.detail.current;
  140. this.setData({ avtiveIndex: index});
  141. },
  142. /**
  143. * 生命周期函数--监听页面显示
  144. */
  145. onShow: function () {
  146. // this.getCoupon();
  147. this.getIndexConfig();
  148. // this.getSeckillTime();
  149. var activity = this.data.activities[this.data.tapActive];
  150. if(activity) {
  151. activity.tabIn();
  152. }
  153. },
  154. getIndexConfig:function(){
  155. var that = this;
  156. getIndexData().then(res=>{
  157. that.setData({
  158. banner: res.data.banner,
  159. menus: res.data.menus,
  160. activity: res.data.activity,
  161. lovely: res.data.lovely,
  162. info: res.data.info,
  163. itemNew: res.data.roll,
  164. likeInfo: res.data.likeInfo,
  165. benefit: res.data.benefit,
  166. logoUrl: res.data.logoUrl,
  167. couponList: res.data.couponList,
  168. });
  169. // 检测是否授权;scope.userInfo存在为授权;
  170. wx.getSetting({
  171. success(res) {
  172. if (!res.authSetting['scope.userInfo']) {
  173. that.setData({ window: that.data.couponList.length ? true : false });
  174. } else {
  175. that.setData({ window: false, iShidden: true});
  176. }
  177. }
  178. });
  179. })
  180. },
  181. getLiveList: function () {
  182. getLiveList(1, 20).then(res => {
  183. if (res.data.length == 1) {
  184. this.setData({ liveInfo: res.data[0] });
  185. } else {
  186. this.setData({ liveList: res.data });
  187. }
  188. }).catch(res => {
  189. })
  190. },
  191. /**
  192. * 商品详情跳转
  193. */
  194. goDetailType: function (e) {
  195. let item = e.currentTarget.dataset.items
  196. if (item.activity && item.activity.type === "1") {
  197. wx.navigateTo({
  198. url: `/pages/activity/goods_seckill_details/index?id=${item.activity.id}&time=${item.activity.time}&status=1`
  199. });
  200. } else if (item.activity && item.activity.type === "2") {
  201. wx.navigateTo({ url: `/pages/activity/goods_bargain_details/index?id=${item.activity.id}` });
  202. } else if (item.activity && item.activity.type === "3") {
  203. wx.navigateTo({
  204. url: `/pages/activity/goods_combination_details/index?id=${item.activity.id}`
  205. });
  206. } else {
  207. wx.navigateTo({ url: `/pages/goods_details/index?id=${item.id}` });
  208. }
  209. },
  210. /**
  211. * 获取我的推荐
  212. */
  213. get_host_product: function (isPage) {
  214. var that = this;
  215. if (that.data.recommend.loadend) return;
  216. if (that.data.recommend.loading) return;
  217. if (isPage === true) that.setData({ hostProduct: [] });
  218. that.setData({'recommend.loading': true, 'recommend.loadTitle': ''});
  219. getProductHot(that.data.recommend.page, that.data.recommend.limit).then(res => {
  220. let list = res.data;
  221. let hostProduct = app.SplitArray(list, that.data.hostProduct);
  222. let loadend = list.length < that.data.recommend.limit;
  223. that.setData({
  224. 'recommend.loadend': loadend,
  225. 'recommend.loading': false,
  226. 'recommend.loadTitle': loadend ? '已全部加载' : '加载更多',
  227. hostProduct: hostProduct,
  228. ['recommend.page']: that.data.recommend.page + 1,
  229. });
  230. }).catch(err => {
  231. that.setData({ loading: false, loadTitle: '加载更多' });
  232. });
  233. },
  234. categoryTap: function (event) {
  235. let that = this, tapActive = event.detail.index;
  236. that.setData({
  237. tapActive: tapActive
  238. })
  239. if (tapActive > 0) {
  240. that.setData({
  241. 'where.page':1,
  242. loadend:false,
  243. loading:false,
  244. 'where.cid': that.data.categoryOne[tapActive - 1].id,
  245. 'where.sid': that.data.categoryOne[tapActive - 1].children.length
  246. ? that.data.categoryOne[tapActive - 1].children[0].id
  247. : -1,
  248. categoryActive:0
  249. })
  250. that.get_product_list(true);
  251. }
  252. let mine = that.selectComponent('#mine');
  253. if(event.detail.title === "黑洞矿场"){
  254. if(mine && that.data.activities[tapActive] == null) {
  255. var activities = {};
  256. activities[tapActive] = mine;
  257. that.setData({
  258. activities: activities
  259. });
  260. }
  261. mine.tabIn();
  262. } else {
  263. mine.tabOut();
  264. }
  265. },
  266. productTap: function (e) {
  267. let that = this,index = e.currentTarget.dataset.indexn;
  268. that.setData({
  269. categoryActive: index,
  270. 'where.page':1,
  271. loadend:false,
  272. loading:false,
  273. 'where.sid': that.data.categoryOne[that.data.tapActive - 1].children[index].id
  274. })
  275. that.get_product_list(true);
  276. },
  277. get_product_list: function (isPage){
  278. var that = this;
  279. if (that.data.loading) return;
  280. if (that.data.loadend) return;
  281. if (isPage === true) that.setData({ productList: [] });
  282. that.setData({ loading: true, loadTitle: '' });
  283. getProductslist(that.data.where).then(res => {
  284. let list = res.data;
  285. let productList = app.SplitArray(list, that.data.productList);
  286. let loadend = list.length < that.data.where.limit;
  287. that.setData({
  288. loadend: loadend,
  289. loading: false,
  290. loadTitle: loadend ? '已全部加载' : '加载更多',
  291. productList: productList,
  292. ['where.page']: that.data.where.page + 1
  293. });
  294. }).catch(err => {
  295. that.setData({ loading: false, loadTitle: '加载更多' });
  296. });
  297. },
  298. getSeckillTime: function () {
  299. let that = this;
  300. getSeckillIndexTime().then(res => {
  301. let timeList = res.data.seckillTime, seckillTimeIndex = res.data.seckillTimeIndex;
  302. that.setData({
  303. timeList: timeList,
  304. seckillCont: res.data.seckillCont,
  305. killIndex: seckillTimeIndex,
  306. seckillTimeIndex: seckillTimeIndex,
  307. killIndexTime: timeList[that.data.killIndex].stop,
  308. status: timeList[seckillTimeIndex].status
  309. })
  310. wxh.time(timeList[that.data.killIndex].stop, that,false);
  311. that.getSeckillLists();
  312. }).catch(()=>{
  313. });
  314. },
  315. setTime: function (e) {
  316. var that = this, index = e.currentTarget.dataset.index;
  317. that.setData({
  318. killIndex: index,
  319. status: that.data.timeList[index].status
  320. })
  321. that.getSeckillLists();
  322. },
  323. getSeckillLists: function () {
  324. let that = this;
  325. let timeId = that.data.timeList[that.data.killIndex].id;
  326. getSeckillList(timeId, { page: 1, limit: 20 }).then(res => {
  327. that.setData({
  328. seckillList: res.data
  329. })
  330. if (this.data.timeList.length){
  331. let query = wx.createSelectorQuery().in(this);
  332. query.select('.timeLen').boundingClientRect(function (res) {
  333. if(res){
  334. that.setData({
  335. scrollLeft: (that.data.killIndex - 1.8) * res.width
  336. });
  337. }
  338. }).exec();
  339. if (that.data.killIndex === that.data.seckillTimeIndex) {
  340. that.setData({
  341. killIndexLen: res.data.length
  342. })
  343. }
  344. }
  345. }).catch(()=>{});
  346. },
  347. goDetail: function (e) {
  348. let index = this.data.timeList[this.data.killIndex];
  349. wx.navigateTo({
  350. url: '/pages/activity/goods_seckill_details/index?id=' + e.currentTarget.dataset.id + '&time=' + index.stop + '&status=' + index.status
  351. })
  352. },
  353. getCategoryData: function () {
  354. let that = this;
  355. getCategoryList().then(res => {
  356. that.setData({
  357. categoryOne: res.data
  358. })
  359. });
  360. },
  361. getCoupon: function () {
  362. var that = this;
  363. getCoupons({page: 1,limit: 6}).then(res => {
  364. that.setData({
  365. getCouponList: res.data
  366. })
  367. }).catch(err => {
  368. // app.Tips({
  369. // title: err
  370. // });
  371. });
  372. },
  373. receiveCoupon: function (e) {
  374. if (!app.globalData.isLog) {
  375. this.setData({
  376. iShidden: false
  377. });
  378. } else {
  379. var that = this;
  380. var list = that.data.getCouponList;
  381. var index = e.currentTarget.dataset.index;
  382. var id = that.data.getCouponList[index].id;
  383. getCouponReceive({
  384. couponId: id
  385. })
  386. .then(function () {
  387. list[index].is_use = true;
  388. that.setData({
  389. getCouponList: that.data.getCouponList
  390. })
  391. app.Tips({
  392. title: "领取成功"
  393. });
  394. })
  395. .catch(function (err) {
  396. // app.Tips({
  397. // title: err
  398. // });
  399. });
  400. }
  401. },
  402. /**
  403. * 生命周期函数--监听页面隐藏
  404. */
  405. onHide: function () {
  406. this.setData({ window:false});
  407. this.data.interval !== null && clearInterval(this.data.interval);
  408. for(var item in this.data.activities){
  409. this.data.activities[item].tabOut();
  410. };
  411. },
  412. /**
  413. * 生命周期函数--监听页面卸载
  414. */
  415. onUnload: function () {
  416. this.data.interval !== null && clearInterval(this.data.interval);
  417. },
  418. /**
  419. * 页面相关事件处理函数--监听用户下拉动作
  420. */
  421. onPullDownRefresh: function () {
  422. this.getIndexConfig();
  423. wx.stopPullDownRefresh();
  424. },
  425. /**
  426. * 页面上拉触底事件的处理函数
  427. */
  428. onReachBottom: function () {
  429. this.get_host_product();
  430. this.get_product_list();
  431. },
  432. /**
  433. * 用户点击右上角分享
  434. */
  435. onShareAppMessage: function () {
  436. }
  437. })