coupon.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // pages/sub_index_page/coupon/coupon.js
  2. // 获取应用实例
  3. const app = getApp()
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. tabIndex: 0,
  10. coupon: [],
  11. token: '',
  12. isLogin: false,
  13. myCoupon: null
  14. },
  15. //点击标题
  16. handleTitleTap(e) {
  17. var index = e.currentTarget.dataset.index;
  18. var isLogin = this.data.isLogin;
  19. if (index != 0 && !isLogin) {
  20. wx.showToast({
  21. title: '请先登录',
  22. icon: 'error'
  23. })
  24. return;
  25. }
  26. if (index == 1) {
  27. //查询可以使用优惠券
  28. this.getAvailableCoupons(0, 5);
  29. } else if (index == 2) {
  30. this.getExpireCoupons(0, 5);
  31. }
  32. this.setData({
  33. tabIndex: index
  34. })
  35. },
  36. /**
  37. * 获取可以使用优惠券
  38. */
  39. getAvailableCoupons(pageNo, pageSize) {
  40. app.showLoading("加载中")
  41. var _this = this;
  42. wx.request({
  43. url: app.globalData.rootUrl + '/user/coupon/valid',
  44. method: "POST",
  45. data: {
  46. pageNo: pageNo,
  47. pageSize: pageSize
  48. },
  49. header: {
  50. token: _this.data.token
  51. },
  52. success(e) {
  53. if (e.data.success) {
  54. _this.setData({
  55. myCoupon: e.data.data
  56. })
  57. } else {
  58. _this.showFail()
  59. }
  60. wx.hideLoading()
  61. },
  62. fail(){
  63. wx.hideLoading()
  64. }
  65. })
  66. },
  67. //查询已过期的优惠券
  68. getExpireCoupons(pageNo, pageSize) {
  69. app.showLoading("加载中")
  70. var _this = this;
  71. wx.request({
  72. url: app.globalData.rootUrl + '/user/coupon/expire',
  73. method: "POST",
  74. data: {
  75. pageNo: pageNo,
  76. pageSize: pageSize
  77. },
  78. header: {
  79. token: _this.data.token
  80. },
  81. success(e) {
  82. if (e.data.success) {
  83. _this.setData({
  84. myCoupon: e.data.data
  85. })
  86. } else {
  87. _this.showFail()
  88. }
  89. wx.hideLoading()
  90. },
  91. fail(){
  92. wx.hideLoading()
  93. }
  94. })
  95. },
  96. //游客用户
  97. getVisitCoupons(pageNo, pageSize) {
  98. var _this = this;
  99. app.showLoading("加载中")
  100. wx.request({
  101. url: app.globalData.rootUrl + '/coupon/visit/get',
  102. method: "POST",
  103. data: {
  104. pageNo: pageNo,
  105. pageSize: pageSize
  106. },
  107. success(e) {
  108. if (e.data.success) {
  109. _this.setData({
  110. coupon: e.data.data
  111. })
  112. } else {
  113. _this.showFail()
  114. }
  115. wx.hideLoading()
  116. },
  117. fail() {
  118. wx.hideLoading()
  119. }
  120. })
  121. },
  122. showFail() {
  123. wx.showToast({
  124. title: '获取失败',
  125. icon: 'error'
  126. })
  127. },
  128. //获取优惠券,已登录用户
  129. getLoginCoupons(pageNo, pageSize) {
  130. app.showLoading("加载中")
  131. var _this = this
  132. var token = _this.data.token;
  133. wx.request({
  134. url: app.globalData.rootUrl + '/coupon/get',
  135. method: "POST",
  136. header: {
  137. token: token
  138. },
  139. data: {
  140. pageNo: pageNo,
  141. pageSize: pageSize
  142. },
  143. success(e) {
  144. if (e.data.success) {
  145. _this.setData({
  146. coupon: e.data.data
  147. })
  148. } else {
  149. _this.showFail()
  150. }
  151. wx.hideLoading()
  152. },
  153. fail(e) {
  154. wx.hideLoading()
  155. }
  156. })
  157. },
  158. //点击优惠券,获取优惠券
  159. handleCouponTap(e) {
  160. var id = e.currentTarget.dataset.id;
  161. var isLogin = this.data.isLogin;
  162. //没有登录
  163. if (!isLogin) {
  164. wx.showToast({
  165. title: '请先登录',
  166. icon: 'error'
  167. })
  168. } else {
  169. this.getCoupons(id);
  170. }
  171. },
  172. /**
  173. * 已登录用户,领取优惠券
  174. * @param {*} couponId 优惠券id
  175. */
  176. getCoupons(couponId) {
  177. // console.log(couponId)
  178. wx.request({
  179. url: app.globalData.rootUrl + '/user/coupon/get',
  180. method: "GET",
  181. header: {
  182. token: this.data.token
  183. },
  184. data: {
  185. couponId: couponId
  186. },
  187. success(e) {
  188. wx.showToast({
  189. title: e.data.message,
  190. icon: 'none'
  191. })
  192. }
  193. })
  194. },
  195. //获取更多
  196. getNextPageCoupon(pageNo, pageSize, url) {
  197. app.showLoading("加载中")
  198. var _this = this
  199. var token = _this.data.token;
  200. wx.request({
  201. url: app.globalData.rootUrl + url,
  202. method: "POST",
  203. header: {
  204. token: token
  205. },
  206. data: {
  207. pageNo: pageNo,
  208. pageSize: pageSize
  209. },
  210. success(e) {
  211. if (e.data.success) {
  212. //加入已有的数据集合中
  213. _this.pushMoreCoupons(e.data.data)
  214. } else {
  215. _this.showFail()
  216. }
  217. wx.hideLoading()
  218. },
  219. fail(e) {
  220. wx.hideLoading()
  221. }
  222. })
  223. },
  224. /**
  225. * 放入已有coupon页面对象中
  226. * @param {JSON} pageData
  227. */
  228. pushMoreCoupons(pageData) {
  229. var couponPage = this.data.coupon;
  230. couponPage.pageNum = pageData.pageNum;
  231. couponPage.pageSize = pageData.pageSize;
  232. couponPage.pages = pageData.pages;
  233. couponPage.total = pageData.total;
  234. pageData.data.forEach(page => {
  235. couponPage.data.push(page);
  236. })
  237. this.setData({
  238. coupon: couponPage
  239. })
  240. },
  241. getNextMyCoupon(pageNo, pageSize, url) {
  242. app.showLoading("加载中")
  243. var _this = this;
  244. wx.request({
  245. url: app.globalData.rootUrl + url,
  246. method: "POST",
  247. data: {
  248. pageNo: pageNo,
  249. pageSize: pageSize
  250. },
  251. header: {
  252. token: _this.data.token
  253. },
  254. success(e) {
  255. if (e.data.success) {
  256. _this.pushMoreMyCoupons(e.data.data);
  257. } else {
  258. _this.showFail()
  259. }
  260. wx.hideLoading()
  261. },
  262. fail(){
  263. wx.hideLoading()
  264. }
  265. })
  266. },
  267. pushMoreMyCoupons(pageData) {
  268. var couponPage = this.data.myCoupon;
  269. couponPage.pageNum = pageData.pageNum;
  270. couponPage.pageSize = pageData.pageSize;
  271. couponPage.pages = pageData.pages;
  272. couponPage.total = pageData.total;
  273. pageData.data.forEach(page => {
  274. couponPage.data.push(page);
  275. })
  276. this.setData({
  277. myCoupon: couponPage
  278. })
  279. },
  280. /**
  281. * 生命周期函数--监听页面加载
  282. */
  283. onLoad: function (options) {
  284. var token = wx.getStorageSync('token');
  285. if (token) {
  286. //已登录用户
  287. this.setData({
  288. token: token,
  289. isLogin: true
  290. })
  291. this.getLoginCoupons(0, 5);
  292. } else {
  293. this.getVisitCoupons(0, 5);
  294. }
  295. },
  296. /**
  297. * 页面上拉触底事件的处理函数
  298. */
  299. onReachBottom: function () {
  300. var tabIndex = this.data.tabIndex;
  301. var isLogin = this.data.isLogin
  302. var coupon = this.data.coupon;
  303. var myCoupon = this.data.myCoupon;
  304. if (tabIndex == 0) {
  305. //已到最后一页
  306. if (coupon.pageNum >= coupon.pages) {
  307. return;
  308. }
  309. if (isLogin) {
  310. this.getNextPageCoupon(coupon.pageNum + 1, coupon.pageSize, '/coupon/get');
  311. } else {
  312. //TODO
  313. this.getNextPageCoupon(coupon.pageNum + 1, coupon.pageSize, '/coupon/visit/get');
  314. }
  315. } else if (tabIndex == 1) {
  316. if (myCoupon.pageNum >= myCoupon.pages) {
  317. return;
  318. }
  319. this.getNextMyCoupon(myCoupon.pageNum + 1, myCoupon.pageSize, '/user/coupon/valid');
  320. } else {
  321. if (myCoupon.pageNum >= myCoupon.pages) {
  322. return;
  323. }
  324. this.getNextMyCoupon(myCoupon.pageNum + 1, myCoupon.pageSize, '/user/coupon/expire');
  325. }
  326. },
  327. /**
  328. * 用户点击右上角分享
  329. */
  330. onShareAppMessage: function () {
  331. }
  332. })