gui-datetime-bt-base.vue 6.0 KB

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