discount-view.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. var sysT = require('../../../tool/sys-tool.js')
  2. var businessT = require('../../../tool/business-tool.js')
  3. var url = require('../../../constant/url.js')
  4. import Net from '../../../tool/net.js'
  5. var net = new Net()
  6. var app = getApp()
  7. var p = require('../../../page/home/public/model/public.js')
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. type: 'choose',
  14. fee: '0.00',
  15. status: 0,//0有效的,1无效的
  16. usedPage:0,
  17. unusedPage: 0,
  18. isLoading: true,
  19. usedNoData: false,
  20. unusedNoData: false,
  21. adapter: {
  22. suffix: ''
  23. },
  24. chose: [],
  25. usedData:[],
  26. unusedData:[],
  27. tips: ['', '', ''],
  28. tab: {
  29. id: '1',
  30. animation: {}
  31. },
  32. hadLeft: false,//判断左边是否有数据
  33. hadRight: false,//判断右边边是否有数据
  34. },
  35. tabChoose: businessT.action(function (sender) {
  36. if (sender.currentTarget.id == this.data.tab.id) {
  37. return
  38. }
  39. // this.data.bgScrollView.scroll = true;
  40. // this.setData({
  41. // bgScrollView: this.data.bgScrollView
  42. // })
  43. var animation = wx.createAnimation({
  44. duration: 300,
  45. timingFunction: 'ease',
  46. })
  47. var bgAnimation = wx.createAnimation({
  48. duration: 300,
  49. timingFunction: 'ease',
  50. })
  51. var w = sysT.sysInfo().windowWidth
  52. businessT.debugLog(w)
  53. var s = sender.currentTarget.id - 1
  54. // if (sender.currentTarget.id == "1") {
  55. // animation.translateX(0).step()
  56. // this.data.bgScrollView.left = 0
  57. // } else {
  58. // animation.translateX(w / 2).step()
  59. // this.data.bgScrollView.left = sysT.sysInfo().windowWidth
  60. // }
  61. animation.translateX(s * w / 2).step()
  62. bgAnimation.translateX(-w * s).step()
  63. this.data.tab.id = sender.currentTarget.id
  64. this.data.tab.animation = animation.export()
  65. // this.data.bgScrollView.scroll = false;
  66. this.setData({
  67. tab: this.data.tab,
  68. bgAnimation: bgAnimation.export()
  69. // bgScrollView: this.data.bgScrollView
  70. })
  71. businessT.debugLog(sender.currentTarget.id)
  72. if (this.data.tab.id == "1") {
  73. this.data.status = 0
  74. if (!this.data.hadLeft) {
  75. this.getCashCouponList()
  76. }
  77. } else {
  78. this.data.status = 1
  79. if (!this.data.hadRight) {
  80. this.getCashCouponList()
  81. }
  82. }
  83. }),
  84. chooseAction: businessT.action(function (sender) {
  85. if (this.data.type != 'choose') {
  86. if (this.data.status == 0) {
  87. wx.switchTab({
  88. url: '../../../page/home/index',
  89. })
  90. }
  91. return
  92. }
  93. businessT.debugLog(this.data.chose)
  94. businessT.debugLog(sender.currentTarget.id)
  95. var c = Number(sender.currentTarget.id)
  96. var s = this.data.chose.indexOf(c)
  97. businessT.debugLog(s)
  98. if (s >= 0) {
  99. this.data.chose.splice(s, 1)
  100. } else {
  101. var order = app.purchase.order
  102. businessT.debugLog(order.timeInterval)
  103. if (order.timeInterval <= this.data.chose.length) {
  104. wx.showToast({
  105. title: '本次购买只可选择' + order.timeInterval + '张抵用券',
  106. icon: 'none'
  107. })
  108. return
  109. }
  110. this.data.chose.push(c)
  111. }
  112. var fee = 0
  113. for (var i in this.data.chose) {
  114. var index = this.data.chose[i]
  115. fee += this.data.unusedData[index].coupon_price
  116. }
  117. this.data.fee = fee.toFixed(2)
  118. this.setData({
  119. fee: this.data.fee,
  120. chose: this.data.chose,
  121. tips: this.getTips()
  122. })
  123. }, 100),
  124. checkUnuseAction: businessT.action(function (sender) {
  125. this.getCashCouponList()
  126. }),
  127. confirmAction: businessT.action(function (sender) {
  128. var page = getCurrentPages()
  129. if (page[page.length - 2].route == 'page/home/pages/pay-order/pay-order') {
  130. var fee = 0
  131. var ids = ''
  132. for (var i in this.data.chose) {
  133. var item = this.data.unusedData[this.data.chose[i]]
  134. fee += item.coupon_price
  135. if (i == 0) {
  136. ids = ids + item.id
  137. } else {
  138. ids = ids + ',' + item.id
  139. }
  140. }
  141. var dis = {
  142. fee: fee,
  143. ids: ids
  144. }
  145. page[page.length-2].refreshDiscount(dis)
  146. }
  147. wx.navigateBack({
  148. })
  149. }),
  150. getCashCouponList: function (flag) {
  151. if (flag) {
  152. wx.showLoading({
  153. title: '加载中',
  154. mask: true
  155. })
  156. }
  157. this.setData({
  158. isLoading: true
  159. })
  160. var page = this.data.status == 0 ? this.data.unusedPage + 1 : this.data.usedPage + 1
  161. net.connectNeedLogin({
  162. url: url.app_host + url.getCashCoupon,
  163. data: {
  164. phone: app.globalData.userInfo.phone,
  165. page: page,
  166. expired: this.data.status == 0 ? 0 : 1
  167. },
  168. success: (err, res) => {
  169. this.getCashCouponListSuccess(res)
  170. this.data.isLoading = false
  171. if (this.data.status == 0) {
  172. this.setData({
  173. isLoading: this.data.isLoading,
  174. unusedData: this.data.unusedData
  175. })
  176. } else {
  177. this.setData({
  178. isLoading: this.data.isLoading,
  179. usedData: this.data.usedData
  180. })
  181. }
  182. wx.hideLoading()
  183. },
  184. fail: (err, res) => {
  185. this.data.isLoading = false
  186. this.setData({
  187. isLoading: this.data.isLoading
  188. })
  189. wx.hideLoading()
  190. businessT.showFailTips(err, '加载失败')
  191. }
  192. }, true)
  193. },
  194. getCashCouponListSuccess: function (res) {
  195. var data = res.data.list
  196. if (data.length == 0) {
  197. return
  198. }
  199. if (this.data.status == 0) {
  200. this.data.unusedPage = res.currentPage
  201. } else {
  202. this.data.usedPage = res.currentPage
  203. }
  204. for (var index in data) {
  205. var item = data[index]
  206. if (item.status == 1 && item.expired == 0) {
  207. // item.end_time = '2019-03-17 00:00:01'
  208. var now = new Date().format('yyyy-MM-dd h:m:s')
  209. var nowDate = now.slice(0, 10)
  210. var endDate = item.end_time.slice(0, 10)
  211. if (nowDate == endDate) {
  212. item.p = '今天过期'
  213. } else {
  214. var tempNowDate = nowDate + ' 00:00:00'
  215. var tempEndDate = endDate + ' 00:00:00'
  216. var d = businessT.dateDiff('d', tempNowDate, tempEndDate)
  217. if (tempEndDate == item.end_time) {
  218. d -= 1
  219. }
  220. if (d == 0) {
  221. item.p = '今天过期'
  222. } else {
  223. item.p = '还有' + d + '天过期'
  224. }
  225. }
  226. } else {
  227. if (item.status == 1) {
  228. item.p = '已过期'
  229. } else {
  230. item.p = '已使用'
  231. }
  232. }
  233. item.end = item.end_time.slice(0, 10)
  234. }
  235. var noData = false
  236. if (res.data.list.length < 10) {
  237. noData = true
  238. }
  239. if (this.data.status == 0) {
  240. this.data.unusedData = this.data.unusedData.concat(res.data.list)
  241. this.data.unusedNoData = noData
  242. } else {
  243. this.data.usedData = this.data.usedData.concat(res.data.list)
  244. this.data.usedNoData = noData
  245. }
  246. },
  247. uploadView: businessT.action(function (sender) {
  248. if (this.data.isLoading) {
  249. return
  250. }
  251. if (this.data.status == 0 && this.data.unusedNoData) {
  252. return
  253. }
  254. if (this.data.status == 1 && this.data.usedNoData) {
  255. return
  256. }
  257. this.getCashCouponList()
  258. businessT.debugLog('abc')
  259. }),
  260. getTips: function () {
  261. if (this.data.type != 'choose') {
  262. return []
  263. }
  264. if (this.data.chose.length > 0) {
  265. return ['您已选中优惠券 ' + this.data.chose.length + ' 张,共可抵用', '¥' + this.data.fee, '']
  266. }
  267. var order = app.purchase.order
  268. return ['您当前下了 ' + order.timeInterval + ' 个月订单,可同时选 ', order.timeInterval, ' 张优惠券']
  269. },
  270. /**
  271. * 生命周期函数--监听页面加载
  272. */
  273. onLoad: function (options) {
  274. if (options.type) {
  275. this.data.type = options.type
  276. }
  277. this.data.adapter = {
  278. suffix: sysT.suffixOfClass(),
  279. }
  280. this.setData({
  281. adapter: this.data.adapter,
  282. type: this.data.type,
  283. tips: this.getTips()
  284. })
  285. this.getCashCouponList(true)
  286. },
  287. /**
  288. * 生命周期函数--监听页面初次渲染完成
  289. */
  290. onReady: function () {
  291. },
  292. /**
  293. * 生命周期函数--监听页面显示
  294. */
  295. onShow: function () {
  296. },
  297. /**
  298. * 生命周期函数--监听页面隐藏
  299. */
  300. onHide: function () {
  301. },
  302. /**
  303. * 生命周期函数--监听页面卸载
  304. */
  305. onUnload: function () {
  306. },
  307. /**
  308. * 页面相关事件处理函数--监听用户下拉动作
  309. */
  310. onPullDownRefresh: function () {
  311. },
  312. /**
  313. * 页面上拉触底事件的处理函数
  314. */
  315. onReachBottom: function () {
  316. },
  317. /**
  318. * 用户点击右上角分享
  319. */
  320. onShareAppMessage: function () {
  321. return {
  322. path: '/page/home/index',
  323. imageUrl: '../../../icon/public/share_img.png',
  324. title: '自助缴社保公积金,覆盖200多个城市,专业团队操作'
  325. }
  326. }
  327. })