gui-datetime-bt-base.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <template>
  2. <picker-view :style="{height:height}"
  3. :indicator-style="indicatorStyle"
  4. class="graceDateTime-main"
  5. :value="defaultVal" @change="change">
  6. <picker-view-column>
  7. <text class="graceDateTime-item gui-block-text"
  8. v-for="(item, index) in sDate[0]" :key="index">{{item}}{{units[0]}}</text>
  9. </picker-view-column>
  10. <picker-view-column>
  11. <text class="graceDateTime-item gui-block-text"
  12. v-for="(item, index) in sDate[1]" :key="index">{{item}}{{units[1]}}</text>
  13. </picker-view-column>
  14. <picker-view-column v-if="isDay">
  15. <text class="graceDateTime-item gui-block-text"
  16. v-for="(item, index) in sDate[2]" :key="index">{{item}}{{units[2]}}</text>
  17. </picker-view-column>
  18. <picker-view-column v-if="isTime">
  19. <text class="graceDateTime-item gui-block-text"
  20. v-for="(item, index) in sDate[3]" :key="index">{{item}}{{units[3]}}</text>
  21. </picker-view-column>
  22. <picker-view-column v-if="isTime && isMinute">
  23. <text class="graceDateTime-item gui-block-text"
  24. v-for="(item, index) in sDate[4]" :key="index">{{item}}{{units[4]}}</text>
  25. </picker-view-column>
  26. <picker-view-column v-if="isTime && isMinute && isSecond">
  27. <text class="graceDateTime-item gui-block-text"
  28. v-for="(item, index) in sDate[5]" :key="index">{{item}}{{units[5]}}</text>
  29. </picker-view-column>
  30. </picker-view>
  31. </template>
  32. <script>
  33. export default {
  34. name : "gui-datetime-bt-base",
  35. props : {
  36. value : { type : String , default:''},
  37. isMinute : { type : Boolean, default : true},
  38. isTime : { type : Boolean, default : true},
  39. isSecond : { type : Boolean, default : true},
  40. isDay : { type : Boolean, default : true },
  41. startYear : { type : Number, default : 1980},
  42. endYear : { type : Number, default : 2050},
  43. units : { type : Array , default:function(){return new Array('年','月','日','时','分','秒')}},
  44. height : { type : String, default : '300rpx' }
  45. },
  46. data() {
  47. return {
  48. indicatorStyle : 'height:38px',
  49. defaultVal : [0,0,0,0,0,0],
  50. sDate:[[],[],[],[],[],[]]
  51. }
  52. },
  53. created() {this.init();},
  54. methods: {
  55. now : function () {
  56. var date = new Date();
  57. var y = date.getFullYear();
  58. var m = date.getMonth() + 1;
  59. m = m < 10 ? ('0' + m) : m;
  60. var d = date.getDate();
  61. d = d < 10 ? ('0' + d) : d;
  62. var h = date.getHours();
  63. h = h < 10 ? ('0' + h) : h;
  64. var minute = date.getMinutes();
  65. var second = date.getSeconds();
  66. minute = minute < 10 ? ('0' + minute) : minute;
  67. second = second < 10 ? ('0' + second) : second;
  68. return y + '-' + m + '-' + d + ' '+ h +':' + minute + ':' + second;
  69. },
  70. arrayIndexOf : function(arr, needFind){
  71. var index = -1;
  72. for(let i = 0; i < arr.length; i++){if(arr[i] == needFind){index = i; return i;}}
  73. return index;
  74. },
  75. setValue : function (val) {
  76. if(val == ''){val = this.now();}
  77. var reg = /^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})$/;
  78. var res = val.match(reg);
  79. if(res == null){
  80. reg = /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/;
  81. res = val.match(reg);
  82. if(res == null){
  83. this.setValue(this.now());
  84. return ;
  85. }
  86. res[4] = '00';
  87. res[5] = '00';
  88. res[6] = '00';
  89. }
  90. this.setDefaults([res[1],res[2],res[3],res[4],res[5],res[6]]);
  91. },
  92. setDefaults : function (res) {
  93. for(let i = 0; i < res.length; i++){
  94. var index = this.arrayIndexOf(this.sDate[i], res[i]);
  95. if(index == -1){index = 0;}
  96. this.defaultVal.splice(i, 1, index);
  97. }
  98. this.changeBase(this.defaultVal);
  99. },
  100. // 初始化组件
  101. init:function(){
  102. if(this.endYear < this.startYear){this.endYear = this.startYear + 10;}
  103. var years = new Array();
  104. for(let i = this.startYear; i <= this.endYear; i++){years.push(i);}
  105. var months = new Array();
  106. for(let i = 1; i <= 12; i++){if(i < 10){months.push('0'+i);}else{months.push(i);}}
  107. var days = new Array();
  108. for(let i = 1; i <= 31; i++){if(i < 10){days.push('0'+i);}else{days.push(i);}}
  109. var hours = new Array();
  110. for(let i = 0; i < 24; i++){if(i < 10){hours.push('0'+i);}else{hours.push(i);}}
  111. var minutes = new Array();
  112. var seconds = new Array();
  113. for(let i = 0; i < 60; i++){
  114. if(i < 10){minutes.push('0'+i); seconds.push('0'+i);}else{minutes.push(i); seconds.push(i);}
  115. }
  116. this.sDate = [years, months, days, hours, minutes, seconds];
  117. this.$nextTick(()=>{setTimeout(()=>{ this.setValue(this.value);},800);});
  118. },
  119. change : function (res) {
  120. this.changeBase(res.detail.value);
  121. },
  122. changeBase:function(res){
  123. var date = new Date(this.sDate[0][res[0]], this.sDate[1][res[1]], 0);
  124. var days = date.getDate();
  125. var daysOut = new Array();
  126. for(let i = 1; i <= days; i++){if(i < 10){daysOut.push('0'+i);}else{daysOut.push(i);}}
  127. this.sDate.splice(2, 1, daysOut);
  128. if(res[2] + 1 > days){res[2] = days - 1;}
  129. this.defaultVal = res;
  130. if(this.isTime){
  131. var resdata = new Array(this.sDate[0][this.defaultVal[0]],
  132. this.sDate[1][this.defaultVal[1]],
  133. this.sDate[2][this.defaultVal[2]],
  134. this.sDate[3][this.defaultVal[3]],
  135. this.sDate[4][this.defaultVal[4]],
  136. this.sDate[5][this.defaultVal[5]]);
  137. }else{
  138. var resdata = new Array(this.sDate[0][this.defaultVal[0]],
  139. this.sDate[1][this.defaultVal[1]],
  140. this.sDate[2][this.defaultVal[2]])
  141. }
  142. this.$emit('change', resdata);
  143. },
  144. confirm:function () {
  145. if(this.isTime){
  146. var res = new Array(this.sDate[0][this.defaultVal[0]],
  147. this.sDate[1][this.defaultVal[1]],
  148. this.sDate[2][this.defaultVal[2]],
  149. this.sDate[3][this.defaultVal[3]],
  150. this.sDate[4][this.defaultVal[4]],
  151. this.sDate[5][this.defaultVal[5]]);
  152. }else{
  153. var res = new Array(this.sDate[0][this.defaultVal[0]],
  154. this.sDate[1][this.defaultVal[1]],
  155. this.sDate[2][this.defaultVal[2]])
  156. }
  157. this.$emit('confirm', res);
  158. }
  159. }
  160. }
  161. </script>
  162. <style scoped>
  163. .graceDateTime-main{width:750rpx; height:300rpx; color:#323232;}
  164. .graceDateTime-item{font-size:14px; line-height:38px; text-align:center;}
  165. /* #ifndef APP-NVUE */
  166. .graceDateTime-item{display:block;}
  167. /* #endif */
  168. </style>