graceDateTime.nvue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <template>
  2. <view>
  3. <view @tap.stop="open"><slot></slot></view>
  4. <view class="graceDateTime" @touchmove.stop.prevent="" @tap.self="close" :style="{backgroundColor:background, width:show ? '750rpx' : '0px'}"></view>
  5. <view class="graceDateTime-body" :style="{bottom : show ? '0px' : '-1000px', paddingBottom:paddingBottom}">
  6. <view class="graceDateTime-header grace-space-between" v-if="isHeaderBar">
  7. <text class="graceDateTime-header-btn" :style="{color:cancelTColor}" @tap="close">{{cancelText}}</text>
  8. <text class="graceDateTime-header-btn" :style="{textAlign:'right', color:confirmColor}" @tap="confirm">{{confirmText}}</text>
  9. </view>
  10. <picker-view :indicator-style="indicatorStyle" class="graceDateTime-main"
  11. :value="defaultVal" @change="change" :style="{height:height}">
  12. <picker-view-column>
  13. <text class="graceDateTime-item" v-for="(item, index) in sDate[0]" :key="index">{{item}}{{units[0]}}</text>
  14. </picker-view-column>
  15. <picker-view-column>
  16. <text class="graceDateTime-item" v-for="(item, index) in sDate[1]" :key="index">{{item}}{{units[1]}}</text>
  17. </picker-view-column>
  18. <picker-view-column>
  19. <text class="graceDateTime-item" v-for="(item, index) in sDate[2]" :key="index">{{item}}{{units[2]}}</text>
  20. </picker-view-column>
  21. <picker-view-column v-if="isTime">
  22. <text class="graceDateTime-item" v-for="(item, index) in sDate[3]" :key="index">{{item}}{{units[3]}}</text>
  23. </picker-view-column>
  24. <picker-view-column v-if="isTime">
  25. <text class="graceDateTime-item" v-for="(item, index) in sDate[4]" :key="index">{{item}}{{units[4]}}</text>
  26. </picker-view-column>
  27. <picker-view-column v-if="isTime && isSecond">
  28. <text class="graceDateTime-item" v-for="(item, index) in sDate[5]" :key="index">{{item}}{{units[5]}}</text>
  29. </picker-view-column>
  30. </picker-view>
  31. </view>
  32. </view>
  33. </template>
  34. <script>
  35. export default {
  36. props: {
  37. background:{ type : String, default : 'rgba(0, 0, 0, 0.5)' },
  38. cancelText : { type : String, default : '取消' },
  39. cancelTColor : { type : String, default : '#888888' },
  40. confirmText : { type : String, default : '确定' },
  41. confirmColor : { type : String, default : '#3688FF' },
  42. value : { type : String , default:''},
  43. isTime : {type : Boolean, default : true},
  44. isSecond : {type : Boolean, default : true},
  45. startYear : {type : Number, default : 1980},
  46. endYear : {type : Number, default : 2050},
  47. units : {type : Array , default:function(){return new Array('年','月','日','时','分','秒')}},
  48. height:{ type : String, default : '300rpx' },
  49. isHeaderBar : {type : Boolean, default : true},
  50. paddingBottom :{ type : String, default : '0rpx' }
  51. },
  52. data() {
  53. return {
  54. show:false,
  55. indicatorStyle : 'height:35px',
  56. defaultVal : [0,0,0,0,0,0],
  57. sDate:[[],[],[],[],[],[]],
  58. timer:null
  59. }
  60. },
  61. created() {
  62. this.init();
  63. },
  64. methods: {
  65. now : function () {
  66. var date = new Date();
  67. var y = date.getFullYear();
  68. var m = date.getMonth() + 1;
  69. m = m < 10 ? ('0' + m) : m;
  70. var d = date.getDate();
  71. d = d < 10 ? ('0' + d) : d;
  72. var h = date.getHours();
  73. h = h < 10 ? ('0' + h) : h;
  74. var minute = date.getMinutes();
  75. var second = date.getSeconds();
  76. minute = minute < 10 ? ('0' + minute) : minute;
  77. second = second < 10 ? ('0' + second) : second;
  78. return y + '-' + m + '-' + d + ' '+ h +':' + minute + ':' + second;
  79. },
  80. arrayIndexOf : function(arr, needFind){
  81. var index = -1;
  82. for(let i = 0; i < arr.length; i++){if(arr[i] == needFind){index = i; return i;}}
  83. return index;
  84. },
  85. setValue : function (val) {
  86. if(val == ''){val = this.now();}
  87. var reg = /^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})$/;
  88. var res = val.match(reg);
  89. if(res == null){
  90. reg = /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/;
  91. res = val.match(reg);
  92. if(res == null){
  93. this.setValue(this.now());
  94. return ;
  95. }
  96. res[4] = '00';
  97. res[5] = '00';
  98. res[6] = '00';
  99. }
  100. this.setDefaults([res[1],res[2],res[3],res[4],res[5],res[6]]);
  101. console.log('ok');
  102. },
  103. setDefaults : function (res) {
  104. for(let i = 0; i < res.length; i++){
  105. var index = this.arrayIndexOf(this.sDate[i], res[i]);
  106. if(index == -1){index = 0;}
  107. this.defaultVal.splice(i, 1, index);
  108. }
  109. this.changeBase(this.defaultVal);
  110. },
  111. // 初始化组件
  112. init:function(){
  113. if(this.endYear < this.startYear){this.endYear = this.startYear + 10;}
  114. var years = new Array();
  115. for(let i = this.startYear; i <= this.endYear; i++){years.push(i);}
  116. var months = new Array();
  117. for(let i = 1; i <= 12; i++){if(i < 10){months.push('0'+i);}else{months.push(i);}}
  118. var days = new Array();
  119. for(let i = 1; i <= 31; i++){if(i < 10){days.push('0'+i);}else{days.push(i);}}
  120. var hours = new Array();
  121. for(let i = 0; i < 24; i++){if(i < 10){hours.push('0'+i);}else{hours.push(i);}}
  122. var minutes = new Array();
  123. var seconds = new Array();
  124. for(let i = 0; i < 60; i++){
  125. if(i < 10){minutes.push('0'+i); seconds.push('0'+i);}else{minutes.push(i); seconds.push(i);}
  126. }
  127. this.sDate = [years, months, days, hours, minutes, seconds];
  128. this.$nextTick(()=>{setTimeout(()=>{ this.setValue(this.value);}, 500);});
  129. },
  130. change : function (res) {
  131. if(this.timer != null){clearTimeout(this.timer);}
  132. this.timer = setTimeout(()=>{this.changeBase(res.detail.value);},500);
  133. },
  134. changeBase:function(res){
  135. var date = new Date(this.sDate[0][res[0]], this.sDate[1][res[1]], 0);
  136. var days = date.getDate();
  137. var daysOut = new Array();
  138. for(let i = 1; i <= days; i++){if(i < 10){daysOut.push('0'+i);}else{daysOut.push(i);}}
  139. this.sDate.splice(2, 1, daysOut);
  140. if(res[2] + 1 > days){res[2] = days - 1;}
  141. this.defaultVal = res;
  142. if(this.isTime){
  143. var resdata = new Array(this.sDate[0][this.defaultVal[0]],
  144. this.sDate[1][this.defaultVal[1]],
  145. this.sDate[2][this.defaultVal[2]],
  146. this.sDate[3][this.defaultVal[3]],
  147. this.sDate[4][this.defaultVal[4]],
  148. this.sDate[5][this.defaultVal[5]]);
  149. }else{
  150. var resdata = new Array(
  151. this.sDate[0][this.defaultVal[0]],
  152. this.sDate[1][this.defaultVal[1]],
  153. this.sDate[2][this.defaultVal[2]]
  154. )
  155. }
  156. this.$emit('change', resdata);
  157. },
  158. confirm:function () {
  159. if(this.isTime){
  160. var res = new Array(this.sDate[0][this.defaultVal[0]],
  161. this.sDate[1][this.defaultVal[1]],
  162. this.sDate[2][this.defaultVal[2]],
  163. this.sDate[3][this.defaultVal[3]],
  164. this.sDate[4][this.defaultVal[4]],
  165. this.sDate[5][this.defaultVal[5]]);
  166. }else{
  167. var res = new Array(this.sDate[0][this.defaultVal[0]],
  168. this.sDate[1][this.defaultVal[1]],
  169. this.sDate[2][this.defaultVal[2]])
  170. }
  171. this.$emit('confirm', res);
  172. this.show = false;
  173. },
  174. open : function () {
  175. this.show = true;
  176. },
  177. close : function () {
  178. this.show = false;
  179. }
  180. }
  181. }
  182. </script>
  183. <style scoped>
  184. .graceDateTime{position:fixed; top:0; left:0; bottom:0; width:0rpx; flex:1;}
  185. .graceDateTime-body{background-color:#FFFFFF; position:fixed; bottom:-1000px; left:0; width:750rpx;}
  186. .graceDateTime-header{padding:25rpx;}
  187. .graceDateTime-header-btn{width:200rpx; line-height:38rpx; height:38rpx; font-size:28rpx;}
  188. .graceDateTime-main{width:750rpx;}
  189. .graceDateTime-item{height:35px; font-size:28rpx; line-height:35px; overflow:hidden; text-align:center;}
  190. </style>