index.js 13 KB

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