index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import util from '../../../utils/util.js';
  2. import StateEnum from '../../../utils/enum/sharp/ActiveStatus.js';
  3. const App = getApp()
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. // 当前tab索引
  10. curTabIndex: 0,
  11. noMore: false, // 没有更多数据
  12. isLoading: true, // 是否正在加载中
  13. page: 1, // 当前页码
  14. StateEnum, // 枚举类:秒杀会场活动状态
  15. countDownTime: false, // 倒计时日期
  16. // 秒杀活动场次
  17. tabbar: [],
  18. // 秒杀商品列表
  19. goodsList: [],
  20. },
  21. /**
  22. * 生命周期函数--监听页面加载
  23. */
  24. onLoad(options) {
  25. const _this = this;
  26. _this.onRefreshPage()
  27. },
  28. /**
  29. * 生命周期函数--监听页面显示
  30. */
  31. onShow() {
  32. // const _this = this;
  33. // if (_this.data.curTabIndex == 0) {
  34. // // 刷新页面
  35. // _this.onRefreshPage()
  36. // }
  37. },
  38. /**
  39. * 刷新页面数据
  40. */
  41. onRefreshPage() {
  42. const _this = this
  43. return new Promise((resolve, reject) => {
  44. // 获取列表数据
  45. _this.getApiData().then(() => {
  46. resolve()
  47. })
  48. })
  49. },
  50. /**
  51. * 下拉刷新
  52. */
  53. onPullDownRefresh() {
  54. // 获取首页数据
  55. this.onRefreshPage().then(() => {
  56. wx.stopPullDownRefresh()
  57. })
  58. },
  59. /**
  60. * 获取页面数据
  61. */
  62. getApiData() {
  63. const app = this;
  64. return new Promise((resolve, reject) => {
  65. App._get('sharp.index/index', {}, (result) => {
  66. const data = result.data
  67. app.setData(data);
  68. // 初始化倒计时组件
  69. app._initCountDownData();
  70. resolve(data)
  71. });
  72. })
  73. },
  74. /**
  75. * 初始化倒计时组件
  76. */
  77. _initCountDownData() {
  78. const app = this,
  79. curTabbar = app.data.tabbar[app.data.curTabIndex];
  80. // 记录倒计时的时间
  81. app.setData({
  82. countDownTime: curTabbar.count_down_time
  83. })
  84. },
  85. // 倒计时结束刷新页面
  86. onCountDownEnd() {
  87. console.log('onCountDownEnd')
  88. const app = this
  89. setTimeout(() => {
  90. app.onRefreshPage()
  91. }, 200)
  92. },
  93. /**
  94. * 切换tabbar
  95. */
  96. onToggleTab(e) {
  97. let _this = this;
  98. // 设置当前tabbar索引,并重置数据
  99. const curTabIndex = e.currentTarget.dataset.index
  100. _this.setData({
  101. curTabIndex,
  102. goodsList: [],
  103. page: 1,
  104. isLoading: true,
  105. noMore: false,
  106. });
  107. // 获取列表数据
  108. _this.getGoodsList();
  109. // 初始化倒计时组件
  110. _this._initCountDownData();
  111. },
  112. /**
  113. * 跳转到秒杀商品详情
  114. */
  115. onTargetActive(e) {
  116. let _this = this,
  117. curTabbar = _this.data.tabbar[_this.data.curTabIndex];
  118. let query = util.urlEncode({
  119. active_time_id: curTabbar.active_time_id,
  120. sharp_goods_id: e.detail.target.dataset.id,
  121. });
  122. console.log(query);
  123. wx.navigateTo({
  124. url: `../goods/index?${query}`,
  125. })
  126. },
  127. /**
  128. * 下拉到底部加载下一页
  129. */
  130. onReachBottom() {
  131. let _this = this,
  132. listData = _this.data.goodsList;
  133. // 已经是最后一页
  134. if (_this.data.page >= listData.last_page) {
  135. _this.setData({
  136. noMore: true
  137. });
  138. return false;
  139. }
  140. // 加载下一页列表
  141. _this.setData({
  142. page: ++_this.data.page
  143. });
  144. _this.getGoodsList(true);
  145. },
  146. /**
  147. * 获取列表数据
  148. */
  149. getGoodsList(isNextPage) {
  150. let _this = this,
  151. curTabbar = _this.data.tabbar[_this.data.curTabIndex];
  152. App._get('sharp.goods/lists', {
  153. page: _this.data.page || 1,
  154. active_time_id: curTabbar.active_time_id
  155. }, (result) => {
  156. let resList = result.data.list,
  157. dataList = _this.data.goodsList;
  158. if (isNextPage == true) {
  159. _this.setData({
  160. 'goodsList.data': dataList.data.concat(resList.data),
  161. isLoading: false,
  162. });
  163. } else {
  164. _this.setData({
  165. goodsList: resList,
  166. isLoading: false,
  167. });
  168. }
  169. });
  170. },
  171. /**
  172. * 分享当前页面
  173. */
  174. onShareAppMessage() {
  175. const _this = this;
  176. // 构建页面参数
  177. const params = App.getShareUrlParams();
  178. return {
  179. title: '整点秒杀',
  180. path: `/pages/sharp/index/index?${params}`
  181. };
  182. },
  183. /**
  184. * 分享到朋友圈
  185. * 本接口为 Beta 版本,暂只在 Android 平台支持,详见分享到朋友圈 (Beta)
  186. * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
  187. */
  188. onShareTimeline() {
  189. const _this = this;
  190. // 构建页面参数
  191. const params = App.getShareUrlParams();
  192. return {
  193. title: '整点秒杀',
  194. path: `/pages/sharp/index/index?${params}`
  195. };
  196. },
  197. })