datepick.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. // component/datepick/datepick.js
  2. var Moment = require("moment.js");
  3. var DATE_LIST = [];
  4. var DATE_YEAR = new Date().getFullYear();
  5. var DATE_MONTH = new Date().getMonth() + 1;
  6. var DATE_DAY = new Date().getDate();
  7. Component({
  8. /**
  9. * 组件的属性列表
  10. */
  11. properties: {
  12. phbname: String
  13. },
  14. /**
  15. * 组件的初始数据
  16. */
  17. data: {
  18. indicatordots: false,
  19. pickindex: 1,
  20. maxMonth: 5,
  21. days: [],
  22. years: [],
  23. months: [
  24. [1, 2, 3, 4, 5, 6],
  25. [7, 8, 9, 10, 11, 12]
  26. ],
  27. phbs: [{
  28. name: '供应商排行榜',
  29. key: 1
  30. }, {
  31. name: '商品排行榜',
  32. key: 2
  33. }],
  34. phbindex: 0,
  35. weekStr: ['日', '一', '二', '三', '四', '五', '六'],
  36. stime: '',
  37. etime: '',
  38. isshow4: false,
  39. scrollinto: 'idaaa',
  40. dateList: [],
  41. firsttime: true
  42. },
  43. ready() {
  44. var _this = this;
  45. var si = wx.getStorageSync('systemInfo')
  46. if (si) {
  47. this.setData({
  48. systemInfo: si
  49. });
  50. } else {
  51. wx.getSystemInfo({
  52. success: function(res) {
  53. _this.setData({
  54. systemInfo: res
  55. });
  56. wx.setStorageSync('systemInfo', res)
  57. }
  58. })
  59. }
  60. this.inityear()
  61. this.initmonth()
  62. //console.log(this.properties.phbname)
  63. this.setData({
  64. phbname: this.properties.phbname
  65. })
  66. this.createDateListData()
  67. },
  68. /**
  69. * 组件的方法列表
  70. */
  71. methods: {
  72. siscroll(e) {
  73. this.createDateListData()
  74. },
  75. phbchange(e) {
  76. console.log(e.detail.value)
  77. this.setData({
  78. phbindex: e.detail.value
  79. })
  80. this.triggerEvent('typechange', {
  81. type: parseInt(e.detail.value) + 1
  82. }, {
  83. bubbles: false, //事件是否冒泡
  84. composed: false, //事件是否可以穿越组件边界
  85. capturePhase: false //事件是否拥有捕获阶段
  86. })
  87. },
  88. databack() {
  89. this.triggerEvent('click', {
  90. type: this.data.pickindex,
  91. day: this.data.nowday.toString().padStart(2, '0'),
  92. month: this.data.nowmonth.toString().padStart(2, '0'),
  93. year: this.data.nowyear,
  94. stime: this.data.stime,
  95. etime: this.data.etime
  96. }, {
  97. bubbles: false, //事件是否冒泡
  98. composed: false, //事件是否可以穿越组件边界
  99. capturePhase: false //事件是否拥有捕获阶段
  100. })
  101. },
  102. daysel(e) {
  103. this.setData({
  104. nowday: e.currentTarget.dataset.day
  105. })
  106. this.databack()
  107. },
  108. monthsel(e) {
  109. this.setData({
  110. nowmonth: e.currentTarget.dataset.month
  111. })
  112. this.initdays(1)
  113. this.databack()
  114. },
  115. yearsel(e) {
  116. this.setData({
  117. nowyear: e.currentTarget.dataset.year
  118. })
  119. this.databack()
  120. },
  121. initdays(kind) {
  122. var daynum = new Date(this.data.nowyear, this.data.nowmonth, 0).getDate()
  123. var nowday = new Date().getDate()
  124. var dayindex = 0
  125. var days = []
  126. for (var i = 1; i <= daynum; i++) {
  127. var x = parseInt(i / 7)
  128. if (!days[x]) {
  129. days[x] = []
  130. }
  131. if (i === nowday) {
  132. dayindex = x
  133. }
  134. days[x].push(i)
  135. }
  136. console.log(days)
  137. this.setData({
  138. days: days,
  139. nowday: nowday,
  140. dayindex: dayindex
  141. })
  142. if (!kind) {
  143. this.databack()
  144. }
  145. },
  146. initmonth() {
  147. this.setData({
  148. nowmonth: new Date().getMonth() + 1,
  149. monthindex: parseInt((new Date().getMonth() + 1) / 7)
  150. })
  151. this.initdays()
  152. },
  153. yearchange(e) {
  154. if (e.detail.current === 0) {
  155. this.addyears()
  156. }
  157. },
  158. addyears() {
  159. var years = this.data.years
  160. var firstyear = years.length > 0 ? years[1][0] : this.data.nowyear
  161. for (var i = 0; i < 4; i++) {
  162. var y = []
  163. for (var j = 0; j < 5; j++) {
  164. y.unshift(firstyear - (i * 5 + j))
  165. }
  166. years.unshift(y)
  167. }
  168. this.setData({
  169. years: years,
  170. yearindex: this.data.yearindex ? this.data.yearindex : years.length - 1
  171. })
  172. },
  173. inityear() {
  174. this.setData({
  175. nowyear: new Date().getFullYear()
  176. })
  177. this.addyears()
  178. },
  179. getTotalDayByMonth: function(year, month) {
  180. month = parseInt(month, 10);
  181. var d = new Date(year, month, 0);
  182. return d.getDate();
  183. },
  184. /*
  185. * 获取月的第一天是星期几
  186. */
  187. getWeek: function(year, month, day) {
  188. var d = new Date(year, month - 1, day);
  189. return d.getDay();
  190. },
  191. setindex(e) {
  192. var index = parseInt(e.currentTarget.dataset.index)
  193. this.setData({
  194. pickindex: index
  195. })
  196. if (index === 1 && this.data.nowday) {
  197. this.databack()
  198. }
  199. if (index === 2 && this.data.nowmonth) {
  200. this.databack()
  201. }
  202. if (index === 3 && this.data.nowyear) {
  203. this.databack()
  204. }
  205. if (index === 4 && this.data.stime && this.data.etime) {
  206. this.databack()
  207. }
  208. },
  209. hidetime() {
  210. this.setData({
  211. isshow4: false,
  212. })
  213. },
  214. setime() {
  215. this.setData({
  216. isshow4: true,
  217. })
  218. this.freshPressStyle()
  219. if (this.data.firsttime) {
  220. var now = new Date();
  221. this.setData({
  222. scrollinto: 'idaaa',
  223. firsttime: false
  224. })
  225. }
  226. if (this.data.stime) {
  227. var st = this.data.stime.split('-')
  228. this.renderPressStyle(st[0], parseInt(st[1]), parseInt(st[2]), true)
  229. }
  230. if (this.data.etime) {
  231. var st = this.data.etime.split('-')
  232. this.renderPressStyle(st[0], parseInt(st[1]), parseInt(st[2]), false)
  233. }
  234. },
  235. onPressDate: function(e) {
  236. var {
  237. year,
  238. month,
  239. day
  240. } = e.currentTarget.dataset;
  241. //当前选择的日期为同一个月并小于今天,或者点击了空白处(即day<0),不执行
  242. // if ((day < DATE_DAY && month == DATE_MONTH) || day <= 0) return;
  243. this.freshPressStyle()
  244. var tempMonth = month;
  245. var tempDay = day;
  246. if (month < 10) tempMonth = '0' + month
  247. if (day < 10) tempDay = '0' + day
  248. var date = year + '-' + tempMonth + '-' + tempDay;
  249. if (this.data.markcheckInDate) {
  250. if (date < this.data.checkInDate) {
  251. wx.showToast({
  252. title: '返程日期不能早于出发日期',
  253. icon: 'none',
  254. duration: 2000
  255. })
  256. return
  257. }
  258. this.setData({
  259. checkOutDate: date,
  260. markcheckInDate: false,
  261. dateList: DATE_LIST.concat()
  262. })
  263. } else {
  264. this.setData({
  265. checkInDate: date,
  266. markcheckInDate: true,
  267. dateList: DATE_LIST.concat()
  268. });
  269. }
  270. //设缓存,返回页面时,可在onShow时获取缓存起来的日期
  271. wx.setStorage({
  272. key: 'ROOM_SOURCE_DATE',
  273. data: {
  274. checkInDate: this.data.checkInDate,
  275. checkOutDate: this.data.checkOutDate
  276. }
  277. });
  278. // wx.navigateBack({
  279. // delta: 1, // 回退前 delta(默认为1) 页面
  280. // });
  281. this.renderPressStyle(year, month, day, this.data.markcheckInDate);
  282. if (!this.data.markcheckInDate) {
  283. this.setData({
  284. stime: this.data.checkInDate,
  285. etime: this.data.checkOutDate,
  286. isshow4: false,
  287. pickindex: 4
  288. })
  289. this.databack()
  290. }
  291. },
  292. freshPressStyle() {
  293. var dateList = this.data.dateList;
  294. //渲染点击样式
  295. for (var i = 0; i < dateList.length; i++) {
  296. var dateItem = dateList[i];
  297. var days = dateItem.days;
  298. for (var j = 0; j < days.length; j++) {
  299. days[j].class = days[j].class.split(' ')[0]
  300. delete days[j].inday
  301. delete days[j].outday
  302. }
  303. }
  304. this.setData({
  305. dateList: dateList
  306. });
  307. },
  308. renderPressStyle: function(year, month, day, inorout) {
  309. var dateList = this.data.dateList;
  310. //渲染点击样式
  311. for (var i = 0; i < dateList.length; i++) {
  312. var dateItem = dateList[i];
  313. var id = dateItem.id;
  314. if (id === year + '-' + month) {
  315. var days = dateItem.days;
  316. for (var j = 0; j < days.length; j++) {
  317. // if (days[j].day >= day) {
  318. // days[j].class = days[j].class + ' bgitem';
  319. // } else {
  320. // days[j].class = ''
  321. // }
  322. var tempDay = days[j].day;
  323. if (tempDay == day) {
  324. days[j].class = days[j].class + ' active';
  325. if (inorout) {
  326. days[j].inday = true;
  327. } else {
  328. days[j].outday = true;
  329. }
  330. break;
  331. }
  332. }
  333. break;
  334. }
  335. }
  336. this.setData({
  337. dateList: dateList
  338. });
  339. },
  340. createDateListData: function() {
  341. wx.showLoading({
  342. title: '',
  343. })
  344. var dateList = this.data.dateList;
  345. var now
  346. var scrollid = ''
  347. /*
  348. 设置日期为 年-月-01,否则可能会出现跨月的问题
  349. 比如:2017-01-31为now ,月份直接+1(now.setMonth(now.getMonth()+1)),则会直接跳到跳到2017-03-03月份.
  350. 原因是由于2月份没有31号,顺推下去变成了了03-03
  351. */
  352. if (dateList.length == 0) {
  353. now = new Date();
  354. now = new Date(now.getFullYear(), now.getMonth(), 1);
  355. } else {
  356. now = new Date(dateList[0].year, dateList[0].month, 1);
  357. scrollid = 'id' + dateList[0].year + 'a' + dateList[0].month
  358. now.setMonth(now.getMonth() - 2)
  359. }
  360. for (var i = 0; i >= 0 - this.data.maxMonth; i--) {
  361. var momentDate = Moment(now).add(this.data.maxMonth - (this.data.maxMonth - i), 'month').date;
  362. var year = momentDate.getFullYear();
  363. var month = momentDate.getMonth() + 1;
  364. var days = [];
  365. var totalDay = this.getTotalDayByMonth(year, month);
  366. var week = this.getWeek(year, month, 1);
  367. //-week是为了使当月第一天的日期可以正确的显示到对应的周几位置上,比如星期三(week = 2),
  368. //则当月的1号是从列的第三个位置开始渲染的,前面会占用-2,-1,0的位置,从1开正常渲染
  369. for (var j = -week + 1; j <= totalDay; j++) {
  370. var tempWeek = -1;
  371. if (j > 0)
  372. tempWeek = this.getWeek(year, month, j);
  373. var clazz = '';
  374. if (tempWeek == 0 || tempWeek == 6)
  375. clazz = 'week'
  376. /** if (j < DATE_DAY && year == DATE_YEAR && month == DATE_MONTH)
  377. //当天之前的日期不可用
  378. clazz = 'unavailable ' + clazz;
  379. else
  380. clazz = '' + clazz
  381. **/
  382. days.push({
  383. day: j,
  384. class: clazz
  385. })
  386. }
  387. var dateItem = {
  388. id: year + '-' + month,
  389. year: year,
  390. month: month,
  391. days: days
  392. }
  393. dateList.unshift(dateItem);
  394. }
  395. // var sFtv = this.data.sFtv;
  396. // for (let i = 0; i < dateList.length; i++) { //加入公历节日
  397. // for (let k = 0; k < sFtv.length; k++) {
  398. // if (dateList[i].month == sFtv[k].month) {
  399. // let days = dateList[i].days;
  400. // for (let j = 0; j < days.length; j++) {
  401. // if (days[j].day == sFtv[k].day) {
  402. // days[j].daytext = sFtv[k].name
  403. // }
  404. // }
  405. // }
  406. // }
  407. // }
  408. this.setData({
  409. dateList: dateList,
  410. //+ dateList[0].year + 'a' + dateList[0].month
  411. }, function() {
  412. this.setData({
  413. scrollinto: scrollid
  414. })
  415. });
  416. DATE_LIST = dateList;
  417. wx.hideLoading()
  418. }
  419. }
  420. })