couponList.js 8.2 KB

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