index.js 12 KB

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