index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. const WXAPI = require('apifm-wxapi')
  2. const AUTH = require('../../utils/auth')
  3. var sliderWidth = 96; // 需要设置slider的宽度,用于计算中间位置
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. tabs: ['可领', '已领', '失效', '口令'],
  10. activeIndex: 0,
  11. showPwdPop: false
  12. },
  13. /**
  14. * 生命周期函数--监听页面加载
  15. */
  16. onLoad: function (e) {
  17. },
  18. /**
  19. * 生命周期函数--监听页面初次渲染完成
  20. */
  21. onReady: function () {
  22. },
  23. /**
  24. * 生命周期函数--监听页面显示
  25. */
  26. onShow: function () {
  27. if (this.data.activeIndex == 0) {
  28. this.sysCoupons()
  29. }
  30. AUTH.checkHasLogined().then(isLogined => {
  31. this.setData({
  32. isLogined
  33. })
  34. if (isLogined) {
  35. if (this.data.activeIndex == 1) {
  36. this.getMyCoupons()
  37. }
  38. if (this.data.activeIndex == 2) {
  39. this.invalidCoupons()
  40. }
  41. }
  42. })
  43. },
  44. onReachBottom: function () {
  45. },
  46. tabClick: function (e) {
  47. this.setData({
  48. activeIndex: e.detail.index
  49. });
  50. if (this.data.activeIndex == 0) {
  51. this.sysCoupons()
  52. }
  53. if (this.data.activeIndex == 1) {
  54. this.getMyCoupons()
  55. }
  56. if (this.data.activeIndex == 2) {
  57. this.invalidCoupons()
  58. }
  59. },
  60. sysCoupons() { // 读取可领取券列表
  61. var _this = this;
  62. wx.showLoading({
  63. title: '',
  64. })
  65. WXAPI.coupons().then(function (res) {
  66. wx.hideLoading({
  67. success: (res) => {},
  68. })
  69. if (res.code == 0) {
  70. _this.setData({
  71. coupons: res.data
  72. });
  73. } else {
  74. _this.setData({
  75. coupons: null
  76. });
  77. }
  78. })
  79. },
  80. getCounpon2(){
  81. if (!this.data.couponPwd) {
  82. wx.showToast({
  83. title: '请输入口令',
  84. icon: 'none'
  85. })
  86. return
  87. }
  88. const e = {
  89. kl: true,
  90. currentTarget: {
  91. dataset: {
  92. id: this.data.pwdCounponId
  93. }
  94. }
  95. }
  96. this.getCounpon(e)
  97. },
  98. getCounpon: function (e) {
  99. const that = this
  100. if (e.currentTarget.dataset.pwd) {
  101. this.setData({
  102. pwdCounponId: e.currentTarget.dataset.id,
  103. showPwdPop: true
  104. })
  105. return
  106. } else {
  107. if (!e.kl) {
  108. this.data.couponPwd = ''
  109. }
  110. }
  111. this.setData({
  112. showPwdPop: false
  113. })
  114. WXAPI.fetchCoupons({
  115. id: e.currentTarget.dataset.id,
  116. token: wx.getStorageSync('token'),
  117. pwd: this.data.couponPwd
  118. }).then(function (res) {
  119. if (res.code == 20001 || res.code == 20002) {
  120. wx.showModal({
  121. title: '错误',
  122. content: '来晚了',
  123. showCancel: false
  124. })
  125. return;
  126. }
  127. if (res.code == 20003) {
  128. wx.showModal({
  129. title: '错误',
  130. content: '你领过了,别贪心哦~',
  131. showCancel: false
  132. })
  133. return;
  134. }
  135. if (res.code == 30001) {
  136. wx.showModal({
  137. title: '错误',
  138. content: '您的积分不足',
  139. showCancel: false
  140. })
  141. return;
  142. }
  143. if (res.code == 20004) {
  144. wx.showModal({
  145. title: '错误',
  146. content: '已过期~',
  147. showCancel: false
  148. })
  149. return;
  150. }
  151. if (res.code == 0) {
  152. wx.showToast({
  153. title: '领取成功',
  154. icon: 'success'
  155. })
  156. } else {
  157. wx.showModal({
  158. title: '错误',
  159. content: res.msg,
  160. showCancel: false
  161. })
  162. }
  163. })
  164. },
  165. getMyCoupons: function () {
  166. var _this = this;
  167. wx.showLoading({
  168. title: '',
  169. })
  170. WXAPI.myCoupons({
  171. token: wx.getStorageSync('token'),
  172. status: 0
  173. }).then(function (res) {
  174. wx.hideLoading({
  175. success: (res) => {},
  176. })
  177. if (res.code == 0) {
  178. res.data.forEach(ele => {
  179. if (ele.dateEnd) {
  180. ele.dateEnd = ele.dateEnd.split(" ")[0]
  181. }
  182. })
  183. _this.setData({
  184. coupons: res.data
  185. })
  186. } else {
  187. _this.setData({
  188. coupons: null
  189. })
  190. }
  191. })
  192. },
  193. invalidCoupons: function () {
  194. var _this = this;
  195. wx.showLoading({
  196. title: '',
  197. })
  198. WXAPI.myCoupons({
  199. token: wx.getStorageSync('token'),
  200. status: '1,2,3'
  201. }).then(function (res) {
  202. wx.hideLoading({
  203. success: (res) => {},
  204. })
  205. if (res.code == 0) {
  206. _this.setData({
  207. coupons: res.data
  208. })
  209. } else {
  210. _this.setData({
  211. coupons: null
  212. })
  213. }
  214. })
  215. },
  216. toIndexPage: function () {
  217. wx.switchTab({
  218. url: "/pages/index/index"
  219. });
  220. },
  221. pwdCouponChange(e){
  222. this.setData({
  223. couponPwd: e.detail.value
  224. })
  225. },
  226. onPullDownRefresh() {
  227. if (this.data.activeIndex == 0) {
  228. this.sysCoupons()
  229. }
  230. if (this.data.activeIndex == 1) {
  231. this.getMyCoupons()
  232. }
  233. if (this.data.activeIndex == 2) {
  234. this.invalidCoupons()
  235. }
  236. wx.stopPullDownRefresh()
  237. },
  238. closePwd() {
  239. this.setData({
  240. showPwdPop: false
  241. })
  242. },
  243. processLogin(e) {
  244. if (!e.detail.userInfo) {
  245. wx.showToast({
  246. title: '已取消',
  247. icon: 'none',
  248. })
  249. return;
  250. }
  251. AUTH.register(this);
  252. },
  253. async exchangeCoupons() {
  254. if (!this.data.number) {
  255. wx.showToast({
  256. title: '请输入券号',
  257. icon: 'none'
  258. })
  259. return
  260. }
  261. if (!this.data.pwd) {
  262. wx.showToast({
  263. title: '请输入密码',
  264. icon: 'none'
  265. })
  266. return
  267. }
  268. this.setData({
  269. exchangeCouponsLoading: true
  270. })
  271. wx.showLoading({
  272. title: '',
  273. })
  274. const res = await WXAPI.exchangeCoupons(wx.getStorageSync('token'), this.data.number, this.data.pwd)
  275. wx.hideLoading({
  276. success: (res) => {},
  277. })
  278. this.setData({
  279. exchangeCouponsLoading: false
  280. })
  281. if (res.code != 0) {
  282. wx.showToast({
  283. title: res.msg,
  284. icon: 'none'
  285. })
  286. } else {
  287. wx.showToast({
  288. title: '兑换成功'
  289. })
  290. }
  291. },
  292. })