混凝土运输管理微信小程序、替班
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.5 KiB

1 week ago
  1. <template>
  2. <view>
  3. <picker mode="multiSelector" :range="range" @change="handleChange">
  4. <!-- <view class="picker">
  5. {{range[0][value[0]]}}{{range[1][value[1]]}}{{range[2][value[2]]}}
  6. {{range[3][value[3]]}}{{range[4][value[4]]}}
  7. </view> -->
  8. <slot></slot>
  9. </picker>
  10. </view>
  11. </template>
  12. <script>
  13. export default {
  14. data() {
  15. return {
  16. range: [
  17. // 年
  18. [],
  19. // 月
  20. [],
  21. // 日
  22. [],
  23. // 时
  24. [],
  25. // 分
  26. []
  27. ],
  28. value: [0, 0, 0, 0, 0] // 默认值
  29. };
  30. },
  31. mounted() {
  32. // 初始化年月日时分范围
  33. this.initDateRange();
  34. },
  35. methods: {
  36. initDateRange() {
  37. const now = new Date();
  38. const year = now.getFullYear();
  39. const yearx = now.getFullYear();
  40. const month = now.getMonth() + 1;
  41. const day = now.getDate();
  42. const hour = now.getHours();
  43. const minute = now.getMinutes();
  44. this.value = [yearx, month, day, hour, minute]
  45. const years = [];
  46. for (let i = year; i <= year + 10; i++) {
  47. years.push(i > 9 ? i : '0' + 1);
  48. }
  49. const months = [];
  50. for (let i = 1; i <= 12; i++) {
  51. months.push(i > 9 ? i : '0' + i);
  52. }
  53. const days = [];
  54. const daysInMonth = new Date(year, month, 0).getDate();
  55. for (let i = 1; i <= daysInMonth; i++) {
  56. days.push(i > 9 ? i : '0' + i);
  57. }
  58. const hours = [];
  59. for (let i = 0; i < 24; i++) {
  60. hours.push(i > 9 ? i : '0' + i);
  61. }
  62. const minutes = [];
  63. for (let i = 0; i < 60; i++) {
  64. minutes.push(i > 9 ? i : '0' + i);
  65. }
  66. this.range = [years, months, days, hours, minutes];
  67. //this.value = [
  68. // years.indexOf(year), months.indexOf(month), days.indexOf(day),
  69. // hours.indexOf(hour), minutes.indexOf(minute)
  70. //];
  71. },
  72. handleChange(e) {
  73. this.value = e.detail.value
  74. const str =
  75. `${this.range[0][this.value[0]]}-${this.range[1][this.value[1]]}-${this.range[2][this.value[2]]} ${this.range[3][this.value[3]]}:${this.range[4][this.value[4]]}:00`;
  76. if (!this.isFutureDate(str)) {
  77. uni.showToast({
  78. title: "不可以早于当前时间",
  79. icon: "none"
  80. })
  81. return;
  82. }
  83. this.$emit("selected", str)
  84. },
  85. isFutureDate(data) {
  86. let m = String(data).replace("年", "-").replace("月", "-").replace("日","").replace("时",":").replace("分",":00")
  87. let inputDate = new Date(m);
  88. let currentDate = new Date();
  89. return inputDate > currentDate;
  90. }
  91. }
  92. };
  93. </script>
  94. <style>
  95. .picker {
  96. padding: 15px;
  97. background-color: #f0f0f0;
  98. text-align: center;
  99. }
  100. </style>