index.js 12 KB

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