couponList.js 9.4 KB

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