index.js 13 KB

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