timePicker.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. Component({
  2. /**
  3. * 组件的属性列表
  4. */
  5. properties: {
  6. pickerShow: {
  7. type: Boolean,
  8. observer:function(val){ //弹出动画
  9. // console.log(this.data);
  10. if(val){
  11. let animation = wx.createAnimation({
  12. duration: 500,
  13. timingFunction: "ease"
  14. });
  15. let animationOpacity = wx.createAnimation({
  16. duration: 500,
  17. timingFunction: "ease"
  18. });
  19. setTimeout(() => {
  20. animation.bottom(0).step();
  21. animationOpacity.opacity(0.7).step();
  22. this.setData({
  23. animationOpacity: animationOpacity.export(),
  24. animationData: animation.export()
  25. })
  26. }, 0);
  27. }else{
  28. let animation = wx.createAnimation({
  29. duration: 100,
  30. timingFunction: "ease"
  31. });
  32. let animationOpacity = wx.createAnimation({
  33. duration: 500,
  34. timingFunction: "ease"
  35. });
  36. animation.bottom(-320).step();
  37. animationOpacity.opacity(0).step();
  38. this.setData({
  39. animationOpacity: animationOpacity.export(),
  40. animationData: animation.export()
  41. });
  42. }
  43. // 在picker滚动未停止前点确定,会使startValue数组各项归零,发生错误,这里判断并重新初始化
  44. // 微信新增了picker滚动的回调函数,已进行兼容
  45. if(this.data.startValue&&this.data.endValue){
  46. let s = 0, e = 0;
  47. let conf = this.data.config;
  48. this.data.startValue.map(val => {
  49. if (val == 0) {
  50. s++
  51. }
  52. })
  53. this.data.endValue.map(val => {
  54. if (val == 0) {
  55. e++;
  56. }
  57. });
  58. let tmp={
  59. hour:4,
  60. minute:5,
  61. second:6
  62. }
  63. let n = tmp[conf.column];
  64. if (s>=n || e>=n) {
  65. this.initPick(this.data.config);
  66. this.setData({
  67. startValue: this.data.startValue,
  68. endValue: this.data.endValue,
  69. });
  70. }
  71. }
  72. }
  73. },
  74. config: Object
  75. },
  76. /**
  77. * 组件的初始数据
  78. */
  79. data: {
  80. // pickerShow:true
  81. // limitStartTime: new Date().getTime()-1000*60*60*24*30,
  82. // limitEndTime: new Date().getTime(),
  83. // yearStart:2000,
  84. // yearEnd:2100
  85. },
  86. detached: function() {
  87. console.log("dele");
  88. },
  89. attached: function() {},
  90. ready: function() {
  91. this.readConfig();
  92. this.initPick(this.data.config || null);
  93. this.setData({
  94. startValue: this.data.startValue,
  95. endValue: this.data.endValue,
  96. });
  97. },
  98. /**
  99. * 组件的方法列表
  100. */
  101. methods: {
  102. //阻止滑动事件
  103. onCatchTouchMove(e) {
  104. },
  105. //读取配置项
  106. readConfig() {
  107. let limitEndTime = new Date().getTime();
  108. let limitStartTime = new Date().getTime() - 1000 * 60 * 60 * 24 * 30;
  109. if (this.data.config) {
  110. let conf = this.data.config;
  111. if (typeof conf.dateLimit == "number") {
  112. limitStartTime =
  113. new Date().getTime() - 1000 * 60 * 60 * 24 * conf.dateLimit;
  114. }
  115. if(conf.limitStartTime){
  116. limitStartTime = new Date(conf.limitStartTime.replace(/-/g,'/')).getTime();
  117. }
  118. if (conf.limitEndTime) {
  119. limitEndTime = new Date(conf.limitEndTime.replace(/-/g, '/')).getTime();
  120. }
  121. this.setData({
  122. yearStart: conf.yearStart || 2000,
  123. yearEnd: conf.yearEnd || 2100,
  124. endDate: conf.endDate || false,
  125. dateLimit: conf.dateLimit || false,
  126. hourColumn:
  127. conf.column == "hour" ||
  128. conf.column == "minute" ||
  129. conf.column == "second",
  130. minColumn: conf.column == "minute" || conf.column == "second",
  131. secColumn: conf.column == "second"
  132. });
  133. }
  134. let limitStartTimeArr = formatTime(limitStartTime);
  135. let limitEndTimeArr = formatTime(limitEndTime);
  136. this.setData({
  137. limitStartTime,
  138. limitStartTimeArr,
  139. limitEndTime,
  140. limitEndTimeArr
  141. });
  142. },
  143. //滚动开始
  144. handlePickStart:function(e){
  145. this.setData({
  146. isPicking:true
  147. })
  148. },
  149. //滚动结束
  150. handlePickEnd:function(e){
  151. this.setData({
  152. isPicking:false
  153. })
  154. },
  155. onConfirm: function() {
  156. //滚动未结束时不能确认
  157. if(this.data.isPicking){return}
  158. let startTime = new Date(this.data.startPickTime.replace(/-/g, "/"));
  159. let endTime = new Date(this.data.endPickTime.replace(/-/g, "/"));
  160. if (startTime <= endTime || !this.data.endDate) {
  161. this.setData({
  162. startTime,
  163. endTime
  164. });
  165. let startArr = formatTime(startTime).arr;
  166. let endArr = formatTime(endTime).arr;
  167. let format0 = function(num){
  168. return num<10?'0'+num:num
  169. }
  170. let startTimeBack =
  171. startArr[0] +
  172. "-" +
  173. format0(startArr[1]) +
  174. "-" +
  175. format0(startArr[2]) +
  176. " " +
  177. (this.data.hourColumn ? format0(startArr[3]) : "00") +
  178. ":" +
  179. (this.data.minColumn ? format0(startArr[4]) : "00") +
  180. ":" +
  181. (this.data.secColumn ? format0(startArr[5]) : "00");
  182. let endTimeBack =
  183. endArr[0] +
  184. "-" +
  185. format0(endArr[1]) +
  186. "-" +
  187. format0(endArr[2]) +
  188. " " +
  189. (this.data.hourColumn ? format0(endArr[3]) : "00") +
  190. ":" +
  191. (this.data.minColumn ? format0(endArr[4]) : "00") +
  192. ":" +
  193. (this.data.secColumn ? format0(endArr[5]) : "00");
  194. let time = {
  195. startTime: startTimeBack,
  196. endTime: endTimeBack
  197. };
  198. //触发自定义事件
  199. this.triggerEvent("setPickerTime", time);
  200. this.triggerEvent("hidePicker", {});
  201. } else {
  202. wx.showToast({
  203. icon: "none",
  204. title: "时间不合理"
  205. });
  206. }
  207. },
  208. hideModal: function() {
  209. this.triggerEvent("hidePicker", {});
  210. },
  211. changeStartDateTime: function(e) {
  212. let val = e.detail.value;
  213. this.compareTime(val, "start");
  214. },
  215. changeEndDateTime: function(e) {
  216. let val = e.detail.value;
  217. this.compareTime(val, "end");
  218. },
  219. //比较时间是否在范围内
  220. compareTime(val, type) {
  221. let h = val[3] ? this.data.HourList[val[3]] : "00";
  222. let m = val[4] ? this.data.MinuteList[val[4]] : "00";
  223. let s = val[5] ? this.data.SecondList[val[5]] : "00";
  224. let time =
  225. this.data.YearList[val[0]] +
  226. "-" +
  227. this.data.MonthList[val[1]] +
  228. "-" +
  229. this.data.DayList[val[2]] +
  230. " " +
  231. h +
  232. ":" +
  233. m +
  234. ":" +
  235. s;
  236. let start = this.data.limitStartTime;
  237. let end = this.data.limitEndTime;
  238. let timeNum = new Date(time.replace(/-/g, '/')).getTime();
  239. let year, month, day, hour, min, sec, limitDate;
  240. let tempArr = []
  241. if (!this.data.dateLimit){
  242. limitDate = [
  243. this.data.YearList[val[0]],
  244. this.data.MonthList[val[1]],
  245. this.data.DayList[val[2]],
  246. this.data.HourList[val[3]],
  247. this.data.MinuteList[val[4]],
  248. this.data.SecondList[val[5]]]
  249. } else if (type == "start" && timeNum > new Date(this.data.endPickTime.replace(/-/g, '/')) && this.data.config.endDate) {
  250. limitDate = formatTime(this.data.endPickTime).arr;
  251. } else if (type == "end" && timeNum < new Date(this.data.startPickTime.replace(/-/g, '/'))) {
  252. limitDate = formatTime(this.data.startPickTime).arr;
  253. } else if (timeNum < start) {
  254. limitDate = this.data.limitStartTimeArr.arr;
  255. } else if (timeNum > end) {
  256. limitDate = this.data.limitEndTimeArr.arr;
  257. } else {
  258. limitDate = [
  259. this.data.YearList[val[0]],
  260. this.data.MonthList[val[1]],
  261. this.data.DayList[val[2]],
  262. this.data.HourList[val[3]],
  263. this.data.MinuteList[val[4]],
  264. this.data.SecondList[val[5]]
  265. ]
  266. }
  267. year = limitDate[0];
  268. month = limitDate[1];
  269. day = limitDate[2];
  270. hour = limitDate[3];
  271. min = limitDate[4];
  272. sec = limitDate[5];
  273. if (type == "start") {
  274. this.setStartDate(year, month, day, hour, min, sec);
  275. } else if (type == "end") {
  276. this.setEndDate(year, month, day, hour, min, sec);
  277. }
  278. },
  279. getDays: function(year, month) {
  280. let daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  281. if (month === 2) {
  282. return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0
  283. ? 29
  284. : 28;
  285. } else {
  286. return daysInMonth[month - 1];
  287. }
  288. },
  289. initPick: function(initData) {
  290. const date = initData.initStartTime ? new Date(initData.initStartTime.replace(/-/g, '/')): new Date();
  291. const endDate = initData.initEndTime ? new Date(initData.initEndTime.replace(/-/g, '/')) : new Date();
  292. // const startDate = new Date(date.getTime() - 1000 * 60 * 60 * 24);
  293. const startDate = date;
  294. const startYear = date.getFullYear();
  295. const startMonth = date.getMonth() + 1;
  296. const startDay = date.getDate();
  297. const startHour = date.getHours();
  298. const startMinute = date.getMinutes();
  299. const startSecond = date.getSeconds();
  300. const endYear = endDate.getFullYear();
  301. const endMonth = endDate.getMonth() + 1;
  302. const endDay = endDate.getDate();
  303. const endHour = endDate.getHours();
  304. const endMinute = endDate.getMinutes();
  305. const endSecond = endDate.getSeconds();
  306. let YearList = [];
  307. let MonthList = [];
  308. let DayList = [];
  309. let HourList = [];
  310. let MinuteList = [];
  311. let SecondList = [];
  312. //设置年份列表
  313. for (let i = this.data.yearStart; i <= this.data.yearEnd; i++) {
  314. YearList.push(i);
  315. }
  316. // 设置月份列表
  317. for (let i = 1; i <= 12; i++) {
  318. MonthList.push(i);
  319. }
  320. // 设置日期列表
  321. for (let i = 1; i <= 31; i++) {
  322. DayList.push(i);
  323. }
  324. // 设置时列表
  325. for (let i = 0; i <= 23; i++) {
  326. if (0 <= i && i < 10) {
  327. i = "0" + i;
  328. }
  329. HourList.push(i);
  330. }
  331. // 分|秒
  332. for (let i = 0; i <= 59; i++) {
  333. if (0 <= i && i < 10) {
  334. i = "0" + i;
  335. }
  336. MinuteList.push(i);
  337. SecondList.push(i);
  338. }
  339. this.setData({
  340. YearList,
  341. MonthList,
  342. DayList,
  343. HourList,
  344. MinuteList,
  345. SecondList
  346. });
  347. this.setStartDate(startYear, startMonth, startDay, startHour, startMinute, startSecond);
  348. this.setEndDate(endYear, endMonth, endDay, endHour, endMinute, endSecond);
  349. //!!!
  350. // setTimeout(() => {
  351. // this.setStartDate(nowYear, nowMonth, nowDay, nowHour, nowMinute)
  352. // this.setEndDate(nowYear, nowMonth, nowDay, nowHour, nowMinute)
  353. // }, 0);
  354. },
  355. setPickerDateArr(type, year, month, day, hour, minute, second) {
  356. let yearIdx = 0;
  357. let monthIdx = 0;
  358. let dayIdx = 0;
  359. let hourIdx = 0;
  360. let minuteIdx = 0;
  361. let secondIdx = 0;
  362. this.data.YearList.map((v, idx) => {
  363. if (parseInt(v) === year) {
  364. yearIdx = idx;
  365. }
  366. });
  367. this.data.MonthList.map((v, idx) => {
  368. if (parseInt(v) === month) {
  369. monthIdx = idx;
  370. }
  371. });
  372. // 重新设置日期列表
  373. let DayList = [];
  374. for (let i = 1; i <= this.getDays(year, month); i++) {
  375. DayList.push(i);
  376. }
  377. DayList.map((v, idx) => {
  378. if (parseInt(v) === day) {
  379. dayIdx = idx;
  380. }
  381. });
  382. if (type == "start") {
  383. this.setData({ startDayList: DayList });
  384. } else if (type == "end") {
  385. this.setData({ endDayList: DayList });
  386. }
  387. this.data.HourList.map((v, idx) => {
  388. if (parseInt(v) === parseInt(hour)) {
  389. hourIdx = idx;
  390. }
  391. });
  392. this.data.MinuteList.map((v, idx) => {
  393. if (parseInt(v) === parseInt(minute)) {
  394. minuteIdx = idx;
  395. }
  396. });
  397. this.data.SecondList.map((v, idx) => {
  398. if (parseInt(v) === parseInt(second)) {
  399. secondIdx = idx;
  400. }
  401. });
  402. return {
  403. yearIdx,
  404. monthIdx,
  405. dayIdx,
  406. hourIdx,
  407. minuteIdx,
  408. secondIdx
  409. };
  410. },
  411. setStartDate: function(year, month, day, hour, minute, second) {
  412. let pickerDateArr = this.setPickerDateArr(
  413. "start",
  414. year,
  415. month,
  416. day,
  417. hour,
  418. minute,
  419. second
  420. );
  421. this.setData({
  422. startYearList: this.data.YearList,
  423. startMonthList: this.data.MonthList,
  424. // startDayList: this.data.DayList,
  425. startHourList: this.data.HourList,
  426. startMinuteList: this.data.MinuteList,
  427. startSecondList: this.data.SecondList,
  428. startValue: [
  429. pickerDateArr.yearIdx,
  430. pickerDateArr.monthIdx,
  431. pickerDateArr.dayIdx,
  432. pickerDateArr.hourIdx,
  433. pickerDateArr.minuteIdx,
  434. pickerDateArr.secondIdx
  435. ],
  436. startPickTime:
  437. this.data.YearList[pickerDateArr.yearIdx] +
  438. "-" +
  439. this.data.MonthList[pickerDateArr.monthIdx] +
  440. "-" +
  441. this.data.DayList[pickerDateArr.dayIdx] +
  442. " " +
  443. this.data.HourList[pickerDateArr.hourIdx] +
  444. ":" +
  445. this.data.MinuteList[pickerDateArr.minuteIdx] +
  446. ":" +
  447. this.data.SecondList[pickerDateArr.secondIdx]
  448. });
  449. },
  450. setEndDate: function(year, month, day, hour, minute, second) {
  451. let pickerDateArr = this.setPickerDateArr(
  452. "end",
  453. year,
  454. month,
  455. day,
  456. hour,
  457. minute,
  458. second
  459. );
  460. this.setData({
  461. endYearList: this.data.YearList,
  462. endMonthList: this.data.MonthList,
  463. // endDayList: this.data.DayList,
  464. endHourList: this.data.HourList,
  465. endMinuteList: this.data.MinuteList,
  466. endSecondList: this.data.SecondList,
  467. endValue: [
  468. pickerDateArr.yearIdx,
  469. pickerDateArr.monthIdx,
  470. pickerDateArr.dayIdx,
  471. pickerDateArr.hourIdx,
  472. pickerDateArr.minuteIdx,
  473. pickerDateArr.secondIdx
  474. ],
  475. endPickTime:
  476. this.data.YearList[pickerDateArr.yearIdx] +
  477. "-" +
  478. this.data.MonthList[pickerDateArr.monthIdx] +
  479. "-" +
  480. this.data.DayList[pickerDateArr.dayIdx] +
  481. " " +
  482. this.data.HourList[pickerDateArr.hourIdx] +
  483. ":" +
  484. this.data.MinuteList[pickerDateArr.minuteIdx] +
  485. ":" +
  486. this.data.SecondList[pickerDateArr.secondIdx]
  487. });
  488. },
  489. }
  490. });
  491. function formatTime(date) {
  492. if (typeof date == 'string' || 'number') {
  493. try {
  494. date = date.replace(/-/g, '/')//兼容ios
  495. } catch (error) {
  496. }
  497. date = new Date(date)
  498. }
  499. const year = date.getFullYear()
  500. const month = date.getMonth() + 1
  501. const day = date.getDate()
  502. const hour = date.getHours()
  503. const minute = date.getMinutes()
  504. const second = date.getSeconds()
  505. return {
  506. str: [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':'),
  507. arr: [year, month, day, hour, minute, second]
  508. }
  509. }
  510. function formatNumber(n) {
  511. n = n.toString()
  512. return n[1] ? n : '0' + n
  513. }