test.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import {
  2. get,
  3. } from '../../utils/http';
  4. import {
  5. formatActivity,
  6. } from '../../utils/time';
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. list: [],
  13. page: 1,
  14. total: 0,
  15. coupon: null,
  16. homeData: null,
  17. visible: false,
  18. posterPopup: false,
  19. isLoading: true,
  20. btmToast: true,
  21. activity: {}
  22. },
  23. /**
  24. * 生命周期函数--监听页面加载
  25. */
  26. onLoad: function (options) {
  27. this.setData({
  28. visible: getApp().globalData.couponPopup,
  29. coupon: getApp().globalData.coupon
  30. })
  31. this.getHomeData()
  32. if(wx.getStorageSync('day') != new Date().getDate()) {
  33. this.setData({
  34. posterPopup: true
  35. })
  36. }
  37. wx.setStorageSync('day', new Date().getDate())
  38. },
  39. /**
  40. * 生命周期函数--监听页面初次渲染完成
  41. */
  42. onReady: function () {
  43. },
  44. /**
  45. * 生命周期函数--监听页面显示
  46. */
  47. onShow: function (e) {
  48. this.getUserActivity()
  49. },
  50. /**
  51. * 生命周期函数--监听页面隐藏
  52. */
  53. onHide: function () {
  54. },
  55. /**
  56. * 生命周期函数--监听页面卸载
  57. */
  58. onUnload: function () {
  59. if(this.timer_) {
  60. clearInterval(this.timer_);
  61. }
  62. },
  63. /**
  64. * 页面相关事件处理函数--监听用户下拉动作
  65. */
  66. onPullDownRefresh: function () {
  67. },
  68. /**
  69. * 页面上拉触底事件的处理函数
  70. */
  71. onReachBottom: function () {
  72. if(this.data.page * 10 < this.data.total) {
  73. this.getProjectList(++this.data.page)
  74. }
  75. },
  76. /**
  77. * 用户点击右上角分享
  78. */
  79. onShareAppMessage: function () {
  80. },
  81. /**
  82. * 关闭弹框
  83. */
  84. onClose(e,_bool) {
  85. let key = _bool ? e : e.currentTarget.dataset.key
  86. this.setData({
  87. [key]: false,
  88. })
  89. getApp().globalData.couponPopup = false;
  90. },
  91. /**
  92. * 确认领取
  93. */
  94. onConfirm() {
  95. wx.showToast({
  96. title: '领取成功',
  97. icon: 'none',
  98. })
  99. getApp().globalData.couponPopup = false;
  100. this.onClose("visible",true)
  101. // this.setData({ visible: false })
  102. },
  103. /**
  104. * 首页数据
  105. */
  106. getHomeData() {
  107. get('api/home',{},(res) => {
  108. this.setData({
  109. homeData: res.data
  110. },() => {
  111. this.getProjectList(1)
  112. })
  113. })
  114. },
  115. /**
  116. * 首页列表
  117. */
  118. getProjectList(_page) {
  119. let { list,page } = this.data;
  120. get('api/project',{
  121. store_id: this.data.homeData.store.id,
  122. page: _page || page,
  123. limit: 10
  124. },(res) => {
  125. if(_page == 1 || page == 1) {
  126. list = []
  127. this.data.page = 1
  128. }
  129. list.push(...res.data.list)
  130. this.setData({
  131. list,
  132. isLoading: false,
  133. total: res.data.total
  134. })
  135. })
  136. },
  137. /**
  138. * 店铺信息
  139. * /api/store/info
  140. */
  141. getStoreInfo(store_id) {
  142. get('api/store/info',{
  143. store_id
  144. },(res) => {
  145. this.setData({
  146. ['homeData.store']: res.data,
  147. },() => {
  148. this.getProjectList(1)
  149. })
  150. console.log(res)
  151. })
  152. },
  153. /**
  154. * 首页广告跳转
  155. */
  156. goToUrl(e) {
  157. if(e.currentTarget.dataset.url) {
  158. wx.navigateTo({
  159. url: e.currentTarget.dataset.url,
  160. })
  161. }
  162. this.setData({
  163. posterPopup: false
  164. })
  165. },
  166. /**
  167. * 跳转店铺列表
  168. */
  169. goToStore() {
  170. wx.navigateTo({
  171. url: '/pages/store/store?defaultStore='+ JSON.stringify(this.data.homeData.store),
  172. })
  173. },
  174. /**
  175. * 跳转项目详情
  176. */
  177. goToProject(e) {
  178. let item = e.currentTarget.dataset.item || {}
  179. wx.navigateTo({
  180. url: `/pages/projectDetail/projectDetail?project_id=${item.id}&store_id=${this.data.homeData.store.id}`
  181. })
  182. },
  183. /**
  184. * 获取首次开卡优惠
  185. * api/user/activity
  186. */
  187. getUserActivity() {
  188. if(this.timer) {
  189. clearInterval(this.timer_);
  190. }
  191. get('api/user/activity',{},(res) => {
  192. if(res.data) {
  193. res.data.currentTime = formatActivity(res.data.expire_time_seconds)
  194. this.setData({
  195. activity: res.data
  196. })
  197. this.timer_ = setInterval(() => {
  198. if(res.data.expire_time_seconds <= 0) {
  199. clearInterval(this.timer_);
  200. }
  201. res.data.expire_time_seconds--
  202. res.data.currentTime = formatActivity(res.data.expire_time_seconds)
  203. this.setData({
  204. activity: res.data
  205. })
  206. }, 1000);
  207. }
  208. })
  209. },
  210. /**
  211. * 开卡优惠关闭
  212. */
  213. onCloseToast() {
  214. this.setData({
  215. btmToast: false
  216. })
  217. }
  218. })