couponList.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. var util = require('../../../utils/util.js');
  2. var api = require('../../../config/api.js');
  3. var app = getApp();
  4. Page({
  5. data: {
  6. couponList: [],
  7. code: '',
  8. codeChannelType: '', //2是抖音 其他小商城
  9. status: 0,
  10. page: 1,
  11. limit: 20,
  12. count: 0,
  13. scrollTop: 0,
  14. showPage: false,
  15. currentType: 0,
  16. typeList: [{
  17. name: "全部",
  18. type: 0
  19. },
  20. // {
  21. // name: "会员专享",
  22. // type: 3
  23. // },
  24. {
  25. name: "立减券",
  26. type: 1
  27. },
  28. {
  29. name: "折扣券",
  30. type: 2
  31. },
  32. ],
  33. totalList: [],
  34. options: '' //页面参数
  35. },
  36. /**
  37. * 生命周期函数--监听页面加载
  38. */
  39. onLoad: function (options) {
  40. if (this.data.options == '') {
  41. this.setData({
  42. options: options
  43. });
  44. }
  45. console.info(options)
  46. },
  47. /**
  48. * 生命周期函数--监听页面显示
  49. */
  50. onShow: function () {
  51. if (app.globalData.hasLogin == false) {
  52. wx.navigateTo({
  53. url: "/pages/auth/login/login"
  54. });
  55. return;
  56. }
  57. setTimeout(() => {
  58. this.exchange(this.data.options);
  59. this.setData({
  60. options: ''
  61. })
  62. }, 500);
  63. },
  64. exchange(options) {
  65. let that = this;
  66. if (options.scene) { // id,12;type,0
  67. var paras = decodeURIComponent(options.scene).split(";");
  68. console.info(paras)
  69. var id = paras[0].replace("id,", "");
  70. console.info(id)
  71. //开始领取优惠券
  72. util.request(api.CouponReceive, {
  73. couponId: id
  74. }, "POST").then(res => {
  75. if (res.errno == 0) {
  76. wx.showToast({
  77. title: '领取成功',
  78. icon: 'none',
  79. duration: 3000
  80. })
  81. } else util.showErrorToast(res.errmsg);
  82. this.getCouponList();
  83. })
  84. } else if (options.q) { //扫码url二维码过来的
  85. let url = decodeURIComponent(options.q);
  86. console.info(url)
  87. if (url.indexOf("?coupon=") >= 0) {
  88. let coupon = url.substring(url.indexOf("?coupon=") + 8);
  89. console.info('兑换码:' + coupon)
  90. that.setData({
  91. code: coupon
  92. })
  93. //判断是否登录
  94. if (app.globalData.hasLogin) {
  95. that.goExchange();
  96. } else {
  97. wx.navigateTo({
  98. url: "/pages/auth/login/login"
  99. });
  100. }
  101. }
  102. } else if (options.type) { //2抖音 1其他
  103. that.setData({
  104. code: options.coupon,
  105. codeChannelType: options.type
  106. });
  107. if (app.globalData.hasLogin) {
  108. if (options.type == 2)
  109. that.goDouyinExchange();
  110. else that.goExchange();
  111. } else {
  112. wx.navigateTo({
  113. url: "/pages/auth/login/login"
  114. });
  115. }
  116. //util.showErrorToast(options.coupon);
  117. } else
  118. that.getCouponList();
  119. },
  120. goExchange: function () {
  121. if (!this.data.code || this.data.code.length === 0) {
  122. util.showErrorToast("请输入兑换码");
  123. return;
  124. }
  125. let that = this;
  126. wx.showModal({
  127. title: '提示',
  128. content: '确定要兑换吗?兑换后将无法撤回',
  129. success: (res) => {
  130. if (res.confirm) {
  131. util.request(api.CouponExchange, {
  132. code: this.data.code
  133. }, 'POST').then(function (res) {
  134. if (res.errno === 0) {
  135. that.getCouponList();
  136. that.clearExchange();
  137. wx.showToast({
  138. title: "领取成功",
  139. duration: 2000
  140. })
  141. } else {
  142. util.showErrorToast(res.errmsg);
  143. }
  144. });
  145. }
  146. }
  147. });
  148. },
  149. /**
  150. * 生命周期函数--监听页面初次渲染完成
  151. */
  152. onReady: function () {
  153. },
  154. /**
  155. * 生命周期函数--监听页面隐藏
  156. */
  157. onHide: function () {
  158. },
  159. /**
  160. * 生命周期函数--监听页面卸载
  161. */
  162. onUnload: function () {
  163. },
  164. goService(e) {
  165. let goodsvalue = e.currentTarget.dataset.goodsvalue;
  166. let id = e.currentTarget.dataset.id;
  167. console.log(goodsvalue);
  168. if (goodsvalue.length == 1) {
  169. wx.navigateTo({
  170. url: '/pages/goods/goods?id=' + goodsvalue[0],
  171. })
  172. } else if (goodsvalue.length > 1) {
  173. wx.navigateTo({
  174. url: '/pages/ucenter/couponGoods/couponGoods?id=' + id
  175. })
  176. } else {
  177. wx.switchTab({
  178. url: '/pages/tabBar/catalog/catalog',
  179. })
  180. }
  181. },
  182. /**
  183. * 页面相关事件处理函数--监听用户下拉动作
  184. */
  185. onPullDownRefresh() {
  186. wx.showNavigationBarLoading() //在标题栏中显示加载
  187. this.getCouponList();
  188. wx.hideNavigationBarLoading() //完成停止加载
  189. wx.stopPullDownRefresh() //停止下拉刷新
  190. },
  191. /**
  192. * 页面上拉触底事件的处理函数
  193. */
  194. onReachBottom: function () {
  195. },
  196. /**
  197. * 用户点击右上角分享
  198. */
  199. onShareAppMessage: function () {
  200. },
  201. getCouponList: function () {
  202. let that = this;
  203. that.setData({
  204. scrollTop: 0,
  205. showPage: false,
  206. couponList: []
  207. });
  208. util.request(api.CouponMyList, {
  209. status: that.data.status,
  210. page: that.data.page,
  211. limit: that.data.limit
  212. }).then(function (res) {
  213. if (res.errno === 0) {
  214. that.setData({
  215. scrollTop: 0,
  216. couponList: res.data.list,
  217. showPage: res.data.total > that.data.limit,
  218. count: res.data.total
  219. });
  220. if (that.data.status == 0) {
  221. that.setData({
  222. totalList: res.data.list
  223. });
  224. }
  225. }
  226. });
  227. },
  228. bindExchange: function (e) {
  229. this.setData({
  230. code: e.detail.value
  231. });
  232. },
  233. clearExchange: function () {
  234. this.setData({
  235. code: ''
  236. });
  237. },
  238. goDouyinExchange: function () {
  239. if (this.data.code.length === 0) {
  240. util.showErrorToast("请输入兑换码");
  241. return;
  242. }
  243. let that = this;
  244. wx.showModal({
  245. title: '提示',
  246. content: '确定要兑换抖音券吗?兑换后将无法撤回',
  247. success: (res) => {
  248. if (res.confirm) {
  249. util.request(api.CouponDouyinExchange, {
  250. code: this.data.code
  251. }, 'POST').then(function (res) {
  252. if (res.errno === 0) {
  253. that.getCouponList();
  254. that.clearExchange();
  255. wx.showToast({
  256. title: "领取成功",
  257. duration: 2000
  258. })
  259. } else {
  260. util.showErrorToast(res.errmsg);
  261. }
  262. });
  263. }
  264. }
  265. });
  266. },
  267. nextPage: function (event) {
  268. var that = this;
  269. if (this.data.page > that.data.count / that.data.limit) {
  270. return true;
  271. }
  272. that.setData({
  273. page: that.data.page + 1,
  274. currentType: 0
  275. });
  276. this.getCouponList();
  277. },
  278. prevPage: function (event) {
  279. if (this.data.page <= 1) {
  280. return false;
  281. }
  282. var that = this;
  283. that.setData({
  284. page: that.data.page - 1,
  285. currentType: 0
  286. });
  287. this.getCouponList();
  288. },
  289. switchTab: function (e) {
  290. this.setData({
  291. couponList: [],
  292. status: e.currentTarget.dataset.index,
  293. page: 1,
  294. limit: 20,
  295. count: 0,
  296. scrollTop: 0,
  297. showPage: false
  298. });
  299. this.getCouponList();
  300. },
  301. switchType(e) {
  302. let type = e.currentTarget.dataset.type;
  303. let currentList = [];
  304. if (type == 0) {
  305. currentList = this.data.totalList;
  306. } else if (type == 3) { //会员券
  307. currentList = this.data.totalList.filter(item => {
  308. return item.type == 4
  309. });
  310. } else {
  311. currentList = this.data.totalList.filter(item => {
  312. return item.voucherType == type
  313. });
  314. }
  315. this.setData({
  316. currentType: type,
  317. couponList: currentList
  318. });
  319. },
  320. onTabChange(e) {
  321. let status = e.detail.name;
  322. this.setData({
  323. couponList: [],
  324. status: status,
  325. page: 1,
  326. limit: 20,
  327. count: 0,
  328. scrollTop: 0,
  329. showPage: false
  330. });
  331. this.getCouponList();
  332. },
  333. })