graceDateTime.nvue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <view>
  3. <view @tap.stop="open"><slot></slot></view>
  4. <view class="graceDateTime" @touchmove.stop="" @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. }
  59. },
  60. created() {
  61. this.init();
  62. },
  63. methods: {
  64. now : function () {
  65. var date = new Date();
  66. var y = date.getFullYear();
  67. var m = date.getMonth() + 1;
  68. m = m < 10 ? ('0' + m) : m;
  69. var d = date.getDate();
  70. d = d < 10 ? ('0' + d) : d;
  71. var h = date.getHours();
  72. h = h < 10 ? ('0' + h) : h;
  73. var minute = date.getMinutes();
  74. var second = date.getSeconds();
  75. minute = minute < 10 ? ('0' + minute) : minute;
  76. second = second < 10 ? ('0' + second) : second;
  77. return y + '-' + m + '-' + d + ' '+ h +':' + minute + ':' + second;
  78. },
  79. arrayIndexOf : function(arr, needFind){
  80. var index = -1;
  81. for(let i = 0; i < arr.length; i++){if(arr[i] == needFind){index = i; return i;}}
  82. return index;
  83. },
  84. setValue : function (val) {
  85. if(val == ''){val = this.now();}
  86. var reg = /^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})$/;
  87. var res = val.match(reg);
  88. if(res == null){
  89. reg = /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/;
  90. res = val.match(reg);
  91. if(res == null){
  92. this.setValue(this.now());
  93. return ;
  94. }
  95. res[4] = '00';
  96. res[5] = '00';
  97. res[6] = '00';
  98. }
  99. this.setDefaults([res[1],res[2],res[3],res[4],res[5],res[6]]);
  100. },
  101. setDefaults : function (res) {
  102. for(let i = 0; i < res.length; i++){
  103. var index = this.arrayIndexOf(this.sDate[i], res[i]);
  104. if(index == -1){index = 0;}
  105. this.defaultVal.splice(i, 1, index);
  106. }
  107. this.changeBase(this.defaultVal);
  108. },
  109. // 初始化组件
  110. init:function(){
  111. if(this.startYear < 1970){this.startYear = 1970;}
  112. if(this.endYear < this.startYear){this.endYear = this.startYear + 10;}
  113. var years = new Array();
  114. for(let i = this.startYear; i <= this.endYear; i++){years.push(i);}
  115. var months = new Array();
  116. for(let i = 1; i <= 12; i++){if(i < 10){months.push('0'+i);}else{months.push(i);}}
  117. var days = new Array();
  118. for(let i = 1; i <= 31; i++){if(i < 10){days.push('0'+i);}else{days.push(i);}}
  119. var hours = new Array();
  120. for(let i = 0; i < 24; i++){if(i < 10){hours.push('0'+i);}else{hours.push(i);}}
  121. var minutes = new Array();
  122. var seconds = new Array();
  123. for(let i = 0; i < 60; i++){
  124. if(i < 10){minutes.push('0'+i); seconds.push('0'+i);}else{minutes.push(i); seconds.push(i);}
  125. }
  126. this.sDate = [years, months, days, hours, minutes, seconds];
  127. this.$nextTick(()=>{setTimeout(()=>{ this.setValue(this.value);},800);});
  128. },
  129. change : function (res) {
  130. this.changeBase(res.detail.value);
  131. },
  132. changeBase:function(res){
  133. var date = new Date(this.sDate[0][res[0]], this.sDate[1][res[1]], 0);
  134. var days = date.getDate();
  135. var daysOut = new Array();
  136. for(let i = 1; i <= days; i++){if(i < 10){daysOut.push('0'+i);}else{daysOut.push(i);}}
  137. this.sDate.splice(2, 1, daysOut);
  138. this.defaultVal = res;
  139. if(this.isTime){
  140. var resdata = new Array(this.sDate[0][this.defaultVal[0]],
  141. this.sDate[1][this.defaultVal[1]],
  142. this.sDate[2][this.defaultVal[2]],
  143. this.sDate[3][this.defaultVal[3]],
  144. this.sDate[4][this.defaultVal[4]],
  145. this.sDate[5][this.defaultVal[5]]);
  146. }else{
  147. var resdata = new Array(this.sDate[0][this.defaultVal[0]],
  148. this.sDate[1][this.defaultVal[1]],
  149. this.sDate[2][this.defaultVal[2]])
  150. }
  151. this.$emit('change', resdata);
  152. },
  153. confirm:function () {
  154. if(this.isTime){
  155. var res = new Array(this.sDate[0][this.defaultVal[0]],
  156. this.sDate[1][this.defaultVal[1]],
  157. this.sDate[2][this.defaultVal[2]],
  158. this.sDate[3][this.defaultVal[3]],
  159. this.sDate[4][this.defaultVal[4]],
  160. this.sDate[5][this.defaultVal[5]]);
  161. }else{
  162. var res = new Array(this.sDate[0][this.defaultVal[0]],
  163. this.sDate[1][this.defaultVal[1]],
  164. this.sDate[2][this.defaultVal[2]])
  165. }
  166. this.$emit('confirm', res);
  167. this.show = false;
  168. },
  169. open : function () {
  170. this.show = true;
  171. },
  172. close : function () {
  173. this.show = false;
  174. }
  175. }
  176. }
  177. </script>
  178. <style scoped>
  179. .graceDateTime{position:fixed; top:0; left:0; bottom:0; width:0rpx; flex:1;}
  180. .graceDateTime-body{background-color:#FFFFFF; position:fixed; bottom:-1000px; left:0; width:750rpx;}
  181. .graceDateTime-header{padding:25rpx;}
  182. .graceDateTime-header-btn{width:200rpx; line-height:38rpx; height:38rpx; font-size:28rpx;}
  183. .graceDateTime-main{width:750rpx;}
  184. .graceDateTime-item{height:35px; font-size:28rpx; line-height:35px; overflow:hidden; text-align:center;}
  185. </style>