index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. let minutes = [];
  2. for (let i = 0; i <= 59; i++) {
  3. if (i < 10) {
  4. i = '0' + i;
  5. }
  6. minutes.push(i);
  7. }
  8. Component({
  9. /**
  10. * 组件的属性列表
  11. */
  12. properties: {
  13. show: {
  14. type: Boolean,
  15. value: false
  16. },
  17. beginTime: {
  18. type: String,
  19. value: ""
  20. },
  21. isOne: {
  22. type: Boolean,
  23. value: false
  24. },
  25. endTime: {
  26. type: String,
  27. default: ""
  28. },
  29. remark: {
  30. type: [String, Object],
  31. default: ""
  32. }
  33. },
  34. /**
  35. * 组件的初始数据
  36. */
  37. data: {
  38. value: '', //默认结束开始时间
  39. hoursList: ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23'],
  40. minutes,
  41. second: minutes
  42. },
  43. attached() {
  44. let { beginTime, endTime } = this.data;
  45. let value = "";
  46. if(beginTime||endTime) {
  47. let beginArr = beginTime.split(':');
  48. let endArr = endTime.split(':');
  49. value = [
  50. beginArr[0]*1,
  51. beginArr[1]*1,
  52. beginArr[2]*1,
  53. 0,
  54. endArr[0]*1,
  55. endArr[1]*1,
  56. endArr[2]*1,
  57. ];
  58. } else {
  59. let timestamp = Date.parse(new Date());
  60. let myDate = new Date();
  61. let nextDate = new Date(timestamp+1000);
  62. value = [
  63. myDate.getHours(),
  64. myDate.getMinutes(),
  65. myDate.getSeconds(),
  66. 0,
  67. nextDate.getHours(),
  68. nextDate.getMinutes(),
  69. nextDate.getSeconds()
  70. ];
  71. }
  72. this.setData({ value })
  73. },
  74. /**
  75. * 组件的方法列表
  76. */
  77. methods: {
  78. confirm() {
  79. let { value, hoursList } = this.data;
  80. let time = hoursList[value[0]*1] + ':' + minutes[value[1]*1] + ':' + minutes[value[2]*1] + '~' + hoursList[value[4]*1] + ':' + minutes[value[5]*1] + ':' + minutes[value[6]*1];
  81. if(this.data.isOne) {
  82. time = hoursList[value[0]*1] + ':' + minutes[value[1]*1] + ':' + minutes[value[2]*1];
  83. }
  84. this.triggerEvent('confrim', { time, remark: this.data.remark });
  85. },
  86. cancel() {
  87. let { value, hoursList } = this.data;
  88. let time = hoursList[value[0]*1] + ':' + minutes[value[1]*1] + ':' + minutes[value[2]*1] + '~' + hoursList[value[4]*1] + ':' + minutes[value[5]*1] + ':' + minutes[value[6]*1];
  89. if(this.data.isOne) {
  90. time = hoursList[value[0]*1] + ':' + minutes[value[1]*1] + ':' + minutes[value[2]*1];
  91. }
  92. this.triggerEvent('cancel', { time, remark: this.data.remark });
  93. },
  94. getime(e) {
  95. let val = e.detail.value;
  96. let sh = this.data.hoursList[val[0]];
  97. let sm = this.data.minutes[val[1]];
  98. let ss = this.data.minutes[val[2]];
  99. let eh = this.data.hoursList[val[4]];
  100. let em = this.data.minutes[val[5]];
  101. let es = this.data.minutes[val[6]];
  102. // 比较前后时间
  103. if(!this.data.isOne) {
  104. if (sh * 1 > eh * 1) {
  105. sh = eh;
  106. sm = 0;
  107. ss = 0;
  108. es = 1;
  109. } else if (sh * 1 == eh * 1) {
  110. if (sm * 1 > em * 1) {
  111. sm = em;
  112. } else if (sm * 1 == em * 1) {
  113. if (ss * 1 >= es * 1) {
  114. if (es > 0) {
  115. ss = es - 1;
  116. } else {
  117. if (sm > 0) {
  118. sm = sm * 1 - 1;
  119. ss = 0;
  120. } else {
  121. if (sh > 0) {
  122. sh = sh * 1 - 1;
  123. sm = 0;
  124. ss = 0;
  125. } else {
  126. sh = 0;
  127. sm = 0;
  128. ss = 0;
  129. es = 1;
  130. }
  131. }
  132. }
  133. }
  134. }
  135. }
  136. }
  137. this.setData({
  138. value: [sh, sm, ss, '0', eh, em, es]
  139. })
  140. }
  141. }
  142. })