index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import util from '../../../utils/util.js'
  2. const App = getApp()
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. // 当前tab索引
  9. currentTab: 0,
  10. // 列表容器高度
  11. scrollHeight: null,
  12. // 列表容器滚动的位置
  13. scrollTop: 0,
  14. noMore: false, // 没有更多数据
  15. isLoading: true, // 是否正在加载中
  16. page: 1, // 当前页码
  17. // 时间记录
  18. countDownList: [],
  19. // 砍价会场商品列表
  20. activeList: [],
  21. // 我的砍价列表
  22. myList: [],
  23. },
  24. /**
  25. * 生命周期函数--监听页面加载
  26. */
  27. onLoad(options) {
  28. let _this = this;
  29. // 设置scroll-view高度
  30. _this._setListHeight();
  31. // 设置当前tab索引
  32. _this._setCurrentTab(options);
  33. },
  34. /**
  35. * 生命周期函数--监听页面显示
  36. */
  37. onShow() {
  38. let _this = this;
  39. if (_this.data.scrollTop == 0) {
  40. // 获取列表数据
  41. _this._getList();
  42. }
  43. },
  44. /**
  45. * 设置当前tab索引
  46. */
  47. _setCurrentTab(options) {
  48. let _this = this;
  49. if (options.hasOwnProperty('tab')) {
  50. _this.setData({
  51. currentTab: options.tab
  52. });
  53. }
  54. },
  55. /**
  56. * 设置商品列表高度
  57. */
  58. _setListHeight() {
  59. let _this = this;
  60. let systemInfo = wx.getSystemInfoSync(),
  61. rpx = systemInfo.windowWidth / 750, // 计算rpx
  62. tapHeight = Math.floor(rpx * (92 + 20)), // tap高度
  63. scrollHeight = systemInfo.windowHeight - tapHeight; // swiper高度
  64. _this.setData({
  65. scrollHeight
  66. });
  67. },
  68. /**
  69. * 获取砍价活动列表
  70. */
  71. getActiveList(isPage) {
  72. let _this = this;
  73. App._get('bargain.active/lists', {
  74. page: _this.data.page || 1,
  75. }, (result) => {
  76. let resList = result.data.activeList,
  77. dataList = _this.data.activeList;
  78. if (isPage == true) {
  79. _this.setData({
  80. 'activeList.data': dataList.data.concat(resList.data),
  81. isLoading: false,
  82. });
  83. } else {
  84. _this.setData({
  85. activeList: resList,
  86. isLoading: false,
  87. });
  88. }
  89. });
  90. },
  91. /**
  92. * 获取我的砍价列表
  93. */
  94. getMyList(isPage) {
  95. let _this = this;
  96. App._get('bargain.task/lists', {
  97. page: _this.data.page || 1,
  98. }, (result) => {
  99. let resList = result.data.myList,
  100. dataList = _this.data.myList;
  101. if (isPage == true) {
  102. _this.setData({
  103. 'myList.data': dataList.data.concat(resList.data),
  104. isLoading: false,
  105. });
  106. } else {
  107. _this.setData({
  108. myList: resList,
  109. isLoading: false,
  110. });
  111. }
  112. // 初始化倒计时组件
  113. _this._initCountDownData(result.data);
  114. });
  115. },
  116. /**
  117. * 初始化倒计时组件
  118. */
  119. _initCountDownData(data) {
  120. // let _this = this;
  121. // // 记录活动到期时间
  122. // let countDownList = _this.data.countDownList;
  123. // data.myList.data.forEach((item) => {
  124. // countDownList.push({
  125. // date: item.end_time
  126. // })
  127. // })
  128. },
  129. /**
  130. * 记录滚动的位置
  131. */
  132. onScrollEvent(e) {
  133. let _this = this;
  134. _this.setData({
  135. scrollTop: e.detail.scrollTop
  136. })
  137. },
  138. /**
  139. * 切换tabbar
  140. */
  141. onToggleTab(e) {
  142. let _this = this;
  143. // 设置当前tabbar索引,并重置数据
  144. _this.setData({
  145. currentTab: e.currentTarget.dataset.index,
  146. activeList: [],
  147. myList: [],
  148. page: 1,
  149. isLoading: true,
  150. noMore: false,
  151. });
  152. // 获取列表数据
  153. _this._getList();
  154. },
  155. /**
  156. * 跳转到砍价商品详情
  157. */
  158. onTargetActive(e) {
  159. wx.navigateTo({
  160. url: `../goods/index?active_id=${e.detail.target.dataset.id}`,
  161. })
  162. },
  163. /**
  164. * 跳转到砍价任务详情
  165. */
  166. onTargetTask(e) {
  167. wx.navigateTo({
  168. url: `../task/index?task_id=${e.detail.target.dataset.id}`,
  169. })
  170. },
  171. /**
  172. * 下拉到底部加载下一页
  173. */
  174. onScrollToLower() {
  175. let _this = this,
  176. listData = _this.data.currentTab == 0 ? _this.data.activeList : _this.data.myList;
  177. // 已经是最后一页
  178. if (_this.data.page >= listData.last_page) {
  179. _this.setData({
  180. noMore: true
  181. });
  182. return false;
  183. }
  184. // 加载下一页列表
  185. _this.setData({
  186. page: ++_this.data.page
  187. });
  188. _this._getList(true);
  189. },
  190. /**
  191. * 获取列表数据
  192. */
  193. _getList(isPage) {
  194. let _this = this;
  195. _this.data.currentTab == 0 ? _this.getActiveList(isPage) : _this.getMyList(isPage);
  196. },
  197. /**
  198. * 分享当前页面
  199. */
  200. onShareAppMessage() {
  201. const _this = this;
  202. // 构建页面参数
  203. const params = App.getShareUrlParams();
  204. return {
  205. title: '砍价专区',
  206. path: `/pages/bargain/index/index?${params}`
  207. };
  208. },
  209. /**
  210. * 分享到朋友圈
  211. * 本接口为 Beta 版本,暂只在 Android 平台支持,详见分享到朋友圈 (Beta)
  212. * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
  213. */
  214. onShareTimeline() {
  215. const _this = this;
  216. // 构建页面参数
  217. const params = App.getShareUrlParams();
  218. return {
  219. title: '砍价专区',
  220. path: `/pages/bargain/index/index?${params}`
  221. };
  222. },
  223. })