index.js 13 KB

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