coupon.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // coupon.js - 优惠劵
  2. // useState : 使用状态 0-未使用 1-使用 2-过期
  3. var app = getApp() ;
  4. var couponList = [
  5. ];
  6. Page({
  7. data: {
  8. couponList: couponList,
  9. isSelect: false,
  10. usey : true,
  11. total2 : 0,
  12. hiddenmodal: true, //弹窗
  13. modalCont: ''
  14. },
  15. onLoad: function (options){
  16. var that = this;
  17. var host = getApp().globalData.servsers;
  18. var un_id = getApp().globalData.un_id;
  19. console.log(un_id);
  20. un_id = 'oovpNwvQGE0uUqpsQrbiVigaEFM0';
  21. if (un_id != undefined && un_id != '' && un_id != null){
  22. //增加登陆优惠券(后台自动判断是否是新用户)
  23. wx.request({
  24. url: host + "mycouponapi/insertadd",
  25. data: {
  26. user_id: un_id,
  27. coupon_id: '1',
  28. batch_no: '0'
  29. },
  30. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  31. header: {
  32. 'Accept': 'application/json'
  33. },
  34. success: function (res) {
  35. if (res == null || res.data == null) {
  36. console.error('网络请求失败');
  37. return;
  38. }
  39. }
  40. })
  41. wx.request({//请求回当前用户下的所有优惠劵
  42. url: host + "couponapi/couponall",
  43. data: {
  44. user_id: un_id,
  45. coupon_id: '1',
  46. },
  47. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  48. header: {
  49. 'Accept': 'application/json'
  50. },
  51. success: function (res) {
  52. that.setData({
  53. couponList: res.data.rows
  54. });
  55. console.log(res.data.rows)
  56. if (res == null || res.data == null) {
  57. console.error('网络请求失败');
  58. return;
  59. }
  60. }
  61. });
  62. wx.request({//将当前优惠劵活动对应的优惠劵插入相应的用户中
  63. url: host + "mycouponapi/insertadd",
  64. data: {
  65. user_id: un_id,
  66. coupon_id: '2,3,4',
  67. batch_no: '1'
  68. },
  69. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  70. header: {
  71. 'Accept': 'application/json'
  72. },
  73. success: function (res) {
  74. wx.request({//请求回当前用户下的所有优惠劵
  75. url: host + "couponapi/couponall",
  76. data: {
  77. user_id: un_id,
  78. coupon_id: '1',
  79. },
  80. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  81. header: {
  82. 'Accept': 'application/json'
  83. },
  84. success: function (res) {
  85. that.setData({
  86. couponList: res.data.rows
  87. });
  88. if (res == null || res.data == null) {
  89. console.error('网络请求失败');
  90. return;
  91. }
  92. }
  93. })
  94. if (res == null || res.data == null) {
  95. console.error('网络请求失败');
  96. return;
  97. }
  98. }
  99. })
  100. //获取当前日期
  101. var date = new Date();
  102. var Y = date.getFullYear();
  103. var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  104. var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  105. var nowTime = Y + '/' + M + '/' + D;
  106. nowTime = nowTime.replace(/-/g, '/');
  107. nowTime = Date.parse(nowTime);
  108. var couponList = that.data.couponList;
  109. var listLen = couponList.length;
  110. for (var i = 0; i < listLen; i++) {
  111. var beiginTime = that.data.couponList[i].coupon_start;
  112. var beginTimestamp = beiginTime.replace(/-/g, '/');
  113. beginTimestamp = Date.parse(beginTimestamp);
  114. var endTime = that.data.couponList[i].coupon_stop;
  115. var endTimestamp = endTime.replace(/-/g, '/');
  116. endTimestamp = Date.parse(endTimestamp);
  117. if (parseInt(nowTime) > parseInt(endTimestamp)) {//已过期
  118. if (that.data.couponList[i].coupon_state == 0) {
  119. that.data.couponList[i].coupon_state = 4;
  120. }
  121. }
  122. }
  123. that.data.total2 = options.total2;
  124. var pages = getCurrentPages();
  125. var currPage = pages[pages.length - 1]; //当前页面
  126. var prevPage = pages[pages.length - 2]; //上一个页面
  127. if (prevPage.route == 'pages/mine/index') {//从"我的"进入,则没有使用状态
  128. that.setData({
  129. usey: false
  130. })
  131. }
  132. }
  133. },
  134. //选用优惠劵
  135. usrCoupon: function (e) {
  136. var that = this;
  137. var pages = getCurrentPages();
  138. var currPage = pages[pages.length - 1]; //当前页面
  139. var prevPage = pages[pages.length - 2]; //上一个页面
  140. var id = e.currentTarget.dataset.id;
  141. var youhuiLimit = that.data.couponList[id].coupon_full_money;
  142. //获取当前日期
  143. var date = new Date();
  144. var Y = date.getFullYear();
  145. var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  146. var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  147. var nowTime = Y + '/' + M + '/' + D;
  148. nowTime = nowTime.replace(/-/g, '/');
  149. nowTime = Date.parse(nowTime);
  150. var beiginTime = that.data.couponList[id].coupon_start;
  151. var endTime = that.data.couponList[id].coupon_stop;
  152. if (beiginTime == '' || beiginTime == null || beiginTime == undefined){
  153. beginTimestamp = parseInt(nowTime) + 1;
  154. }else{
  155. var beginTimestamp = beiginTime.replace(/-/g, '/');
  156. beginTimestamp = Date.parse(beginTimestamp);
  157. }
  158. if (endTime == '' || endTime == null || endTime  ==  undefined) {
  159. endTimestamp = parseInt(nowTime) + 1;
  160. } else {
  161. var endTimestamp = endTime.replace(/-/g, '/');
  162. endTimestamp = Date.parse(endTimestamp);
  163. }
  164. // console.log("1:" + (parseInt(nowTime) >= parseInt(beginTimestamp)));
  165. // console.log("2:" + (parseInt(nowTime) <= parseInt(endTimestamp)));
  166. // console.log("nowTime:" + nowTime);
  167. // console.log("beginTimestamp:" + beginTimestamp);
  168. // console.log("endTimestamp:" + endTimestamp);
  169. // console.log("that.data.total2:" + that.data.total2);
  170. // console.log("youhuiLimit:" + youhuiLimit);
  171. if (parseInt(nowTime) >= parseInt(beginTimestamp) && parseInt(nowTime) <= parseInt(endTimestamp) || (beiginTime == null && endTime == null)) {//限定使用时间之内
  172. if (parseFloat(that.data.total2) > parseFloat(youhuiLimit) || parseFloat(that.data.total2) == parseFloat(youhuiLimit)) {
  173. var couponName = that.data.couponList[id].coupon_rule;
  174. var minNum = that.data.couponList[id].coupon_cou_money;
  175. var total2 = parseFloat(that.data.total2);
  176. var newTotal = 0;
  177. if (total2 > minNum){
  178. minNum = minNum;
  179. newTotal = this.data.total2 - parseFloat(minNum);
  180. } else{
  181. minNum = total2 - 0.01;
  182. newTotal = 0.01;
  183. }
  184. var coupon_id = that.data.couponList[id].id;
  185. prevPage.setData({
  186. minNum: minNum,//优惠金额
  187. couponName: couponName,
  188. total2: newTotal,
  189. coupon_id: coupon_id
  190. });
  191. wx.navigateBack({});
  192. }else{
  193. that.setData({
  194. hiddenmodal: false,
  195. modalCont: '不满足使用条件!'
  196. })
  197. setTimeout(function () {
  198. that.setData({
  199. hiddenmodal: true
  200. })
  201. }, 1500)
  202. var newTotal = this.data.total2;
  203. var couponName = '';
  204. var minNum = 0;
  205. }
  206. }else{
  207. console.log("dd")
  208. that.setData({
  209. hiddenmodal: false,
  210. modalCont: '不满足使用条件!'
  211. })
  212. setTimeout(function () {
  213. that.setData({
  214. hiddenmodal: true
  215. })
  216. }, 1500)
  217. var newTotal = this.data.total2 ;
  218. var couponName = '';
  219. var minNum = 0;
  220. }
  221. }
  222. })