choose-date.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. var sysT = require('../../../../tool/sys-tool.js')
  2. var businessT = require('../../../../tool/business-tool.js')
  3. var purchaseO = require('../../public/model/purchaseOrder.js')
  4. var p = require('../../public/model/public.js')
  5. var businessType = require('../../../../public/business-type.js')
  6. var app = getApp()
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. type: 'double',
  13. chooseAnimation: {},
  14. tips: {
  15. s: false,
  16. t: ['开始时间仅支持近半年内的月份,最多可购买12个月']
  17. },
  18. calendar: [],
  19. status: 0,//0未选择,1选择开始月份,2选择结束月份
  20. data: {
  21. firstYear: 2018,
  22. beginMonth: 1,
  23. },
  24. result: {
  25. beginMonth: 0,
  26. endMonth: 0
  27. },
  28. lastChose: 0
  29. },
  30. chooseMonth: businessT.action(function (sender) {
  31. if (this.data.status == 2) {
  32. return
  33. }
  34. businessT.debugLog(sender.currentTarget)
  35. if (this.data.type == 'single') {
  36. this.data.status = 2
  37. this.data.result.beginMonth = Number(sender.currentTarget.id)
  38. var chooseYear = (this.data.result.beginMonth / 100) | 0
  39. var chooseMonth = this.data.result.beginMonth % 100
  40. this.data.calendar[chooseYear - this.data.data.firstYear].m[chooseMonth - 1].s = true
  41. this.setData({
  42. calendar: this.data.calendar
  43. })
  44. console.log(this.data.calendar)
  45. var that = this
  46. setTimeout(function () {
  47. var pages = getCurrentPages()
  48. pages[pages.length - 2].finishChooseDate(that.data.result)
  49. wx.navigateBack({
  50. })
  51. }.bind(this), 1000)
  52. return
  53. }
  54. if (this.data.status == 0) {
  55. this.data.status = 1
  56. this.data.lastChose = this.data.result.beginMonth
  57. this.data.result.beginMonth = Number(sender.currentTarget.id)
  58. this.reloadView()
  59. } else {
  60. if (Number(sender.currentTarget.id) < this.data.result.beginMonth) {
  61. this.data.lastChose = this.data.result.beginMonth
  62. this.data.result.beginMonth = Number(sender.currentTarget.id)
  63. this.reloadView()
  64. } else {
  65. if (this.data.type == 'single') {
  66. var order = app.purchase.order
  67. } else {
  68. var order = p.getOrder()
  69. }
  70. if (!order.isSupplementaryPay) {
  71. var purchase = app.purchase.purchaseData
  72. if (Number(sender.currentTarget.id) - this.data.result.beginMonth + 1 < purchase.nums) {
  73. wx.showToast({
  74. title: '你选择的城市至少需要购买' + purchase.nums + '个月',
  75. icon: 'none'
  76. })
  77. return
  78. }
  79. }
  80. this.data.status = 2
  81. this.data.result.endMonth = Number(sender.currentTarget.id)
  82. var chooseYear = (this.data.result.endMonth / 100) | 0
  83. var chooseMonth = this.data.result.endMonth % 100
  84. this.data.calendar[chooseYear - this.data.data.firstYear].m[chooseMonth - 1].e = true
  85. this.setData({
  86. result: this.data.result,
  87. status: this.data.status,
  88. calendar: this.data.calendar
  89. })
  90. var that = this
  91. setTimeout(function () {
  92. var pages = getCurrentPages()
  93. pages[pages.length - 2].finishChooseDate(that.data.result)
  94. wx.navigateBack({
  95. })
  96. }.bind(this), 1000)
  97. }
  98. }
  99. }),
  100. reloadView: function () {
  101. var chooseYear = (this.data.result.beginMonth / 100) | 0
  102. var chooseMonth = this.data.result.beginMonth % 100
  103. if (this.data.type == 'single') {
  104. var order = app.purchase.order
  105. } else {
  106. var order = p.getOrder()
  107. }
  108. if (!order.isSupplementaryPay) {
  109. if (chooseYear == this.data.data.firstYear) {
  110. var secondR = {
  111. o: 0,
  112. l: chooseMonth - 1
  113. }
  114. if (this.data.data.beginMonth < 7) {
  115. var firstR = {
  116. o: this.data.data.beginMonth - 1,
  117. l: 12 - this.data.data.beginMonth + 1
  118. }
  119. this.data.calendar[0].m = this.createMonthsData(firstR, String(this.data.data.firstYear))
  120. }
  121. var thirdR = {
  122. o: 0,
  123. l: 0
  124. }
  125. this.data.calendar[2].m = this.createMonthsData(thirdR, String(this.data.data.firstYear + 2))
  126. } else {
  127. var secondR = {
  128. o: 0,
  129. l: 12,
  130. }
  131. var thirdR = {
  132. o: 0,
  133. l: chooseMonth - 1
  134. }
  135. this.data.calendar[2].m = this.createMonthsData(thirdR, String(this.data.data.firstYear + 2))
  136. }
  137. this.data.calendar[1].m = this.createMonthsData(secondR, String(this.data.data.firstYear + 1))
  138. }
  139. businessT.debugLog(this.data.calendar)
  140. if (this.data.lastChose != 0) {
  141. var lastChooseYear = ((this.data.lastChose / 100) | 0) - this.data.data.firstYear
  142. var lastChooseMonth = this.data.lastChose % 100
  143. this.data.calendar[lastChooseYear].m[lastChooseMonth - 1].s = false
  144. }
  145. this.data.calendar[chooseYear - this.data.data.firstYear].m[chooseMonth - 1].s = true
  146. var animation = wx.createAnimation({
  147. duration: 300,
  148. timingFunction: 'ease',
  149. })
  150. var t = sysT.rpxToPx() * 390
  151. animation.translateX(t).step()
  152. this.setData({
  153. result: this.data.result,
  154. chooseAnimation: animation.export(),
  155. status: this.data.status,
  156. calendar: this.data.calendar
  157. })
  158. },
  159. createMonthsData: function (choose, year) {
  160. var months = []
  161. for (var i = 0; i < 12; i++) {
  162. if (i < choose.o || i >= choose.o + choose.l) {
  163. months.push({
  164. m: i + 1,
  165. h: '-u'
  166. })
  167. } else {
  168. var id = (i + 1) < 10 ? year + '0' + String(i + 1) : year + String(i + 1)
  169. months.push({
  170. m: i + 1,
  171. a: 'chooseMonth',
  172. i: id
  173. })
  174. }
  175. }
  176. return months
  177. },
  178. /**
  179. * 生命周期函数--监听页面加载
  180. */
  181. onLoad: function (options) {
  182. if (options.type) {
  183. this.data.type = options.type
  184. }
  185. if (this.data.type == 'single') {
  186. var order = app.purchase.order
  187. } else {
  188. var order = p.getOrder()
  189. }
  190. if (order.isSupplementaryPay) {
  191. this.data.tips.s = false
  192. } else {
  193. this.data.tips.s = true
  194. }
  195. var purchase = app.purchase.purchaseData
  196. if (this.data.type == 'single') {
  197. var beginTime = options.beginTime
  198. } else {
  199. var beginTime = purchase.month
  200. }
  201. var year = Number(beginTime.slice(0, 4))
  202. var month = Number(beginTime.slice(5, 7))
  203. if (order.isSupplementaryPay) {
  204. if (month == 1) {
  205. this.data.data.firstYear = year - 2
  206. this.data.data.beginMonth = 12
  207. } else {
  208. this.data.data.firstYear = year - 1
  209. this.data.data.beginMonth = month - 1
  210. }
  211. businessT.debugLog(month)
  212. var canChoose = order.purchaseType == businessType.PurchaseType.socialInsurance ? purchase.addeds_num : purchase.addedf_num
  213. if (canChoose > 12) {
  214. canChoose = 12
  215. }
  216. if (this.data.data.beginMonth - canChoose < 1) {
  217. var secondR = {
  218. o: 0,
  219. l: this.data.data.beginMonth
  220. }
  221. var begin = canChoose - this.data.data.beginMonth >= 12 ? 0 : 12 - (canChoose - this.data.data.beginMonth)
  222. var firstR = {
  223. o: begin,
  224. l: canChoose - this.data.data.beginMonth
  225. }
  226. } else {
  227. var secondR = {
  228. o: this.data.data.beginMonth - canChoose,
  229. l: canChoose
  230. }
  231. var firstR = {
  232. o: 0,
  233. l: 0
  234. }
  235. }
  236. var chooseR = [firstR, secondR]
  237. } else {
  238. this.data.data.firstYear = year
  239. this.data.data.beginMonth = month
  240. if (12 - this.data.data.beginMonth + 1 < 6) {
  241. var firstR = {
  242. o: this.data.data.beginMonth -1,
  243. l: 12 - this.data.data.beginMonth + 1
  244. }
  245. var secondR = {
  246. o: 0,
  247. l: 6 - (12 - this.data.data.beginMonth + 1)
  248. }
  249. var thirdR = {
  250. o: 0,
  251. l: 0
  252. }
  253. } else {
  254. var firstR = {
  255. o: this.data.data.beginMonth - 1,
  256. l: 6
  257. }
  258. var secondR = {
  259. o: 0,
  260. l: 0
  261. }
  262. var thirdR = {
  263. o: 0,
  264. l: 0
  265. }
  266. }
  267. var chooseR = [firstR, secondR, thirdR]
  268. }
  269. businessT.debugLog(chooseR)
  270. var calendar = []
  271. for (var index in chooseR) {
  272. var choose = chooseR[index]
  273. var months = this.createMonthsData(choose, String(this.data.data.firstYear + Number(index)))
  274. calendar.push({
  275. y: this.data.data.firstYear + Number(index),
  276. m: months
  277. })
  278. }
  279. this.setData({
  280. tips: this.data.tips,
  281. type: this.data.type,
  282. calendar: calendar
  283. })
  284. },
  285. /**
  286. * 生命周期函数--监听页面初次渲染完成
  287. */
  288. onReady: function () {
  289. },
  290. /**
  291. * 生命周期函数--监听页面显示
  292. */
  293. onShow: function () {
  294. },
  295. /**
  296. * 生命周期函数--监听页面隐藏
  297. */
  298. onHide: function () {
  299. },
  300. /**
  301. * 生命周期函数--监听页面卸载
  302. */
  303. onUnload: function () {
  304. },
  305. /**
  306. * 页面相关事件处理函数--监听用户下拉动作
  307. */
  308. onPullDownRefresh: function () {
  309. },
  310. /**
  311. * 页面上拉触底事件的处理函数
  312. */
  313. onReachBottom: function () {
  314. },
  315. /**
  316. * 用户点击右上角分享
  317. */
  318. // onShareAppMessage: function () {
  319. // }
  320. })