gui-datetime.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <template>
  2. <view>
  3. <view
  4. @tap.stop="open">
  5. <slot></slot>
  6. </view>
  7. <view
  8. class="gui-dateBT-shade gui-flex gui-columns gui-justify-content-end"
  9. v-if="show"
  10. :style="{zIndex:zIndex, width:width}">
  11. <view
  12. class="graceDateTime-header gui-bg-gray gui-dark-bg-level-1 gui-flex gui-row gui-space-between">
  13. <text
  14. class="graceDateTime-header-btn gui-color-gray"
  15. @tap="close">{{cancelText}}</text>
  16. <text
  17. class="graceDateTime-header-btn gui-primary-text"
  18. style="text-align:right;"
  19. @tap="confirm">{{confirmText}}</text>
  20. </view>
  21. <view
  22. class="gui-bg-white gui-dark-bg-level-3">
  23. <picker-view
  24. :indicator-style="indicatorStyle"
  25. class="graceDateTime-main"
  26. :value="defaultVal"
  27. @change="change"
  28. :style="{height:height, width:width}">
  29. <picker-view-column>
  30. <view
  31. class="gui-picker-item"
  32. :style="indicatorStyle"
  33. v-for="(item, index) in sDate[0]"
  34. :key="index">
  35. <text
  36. class="gui-picker-item gui-block"
  37. :style="indicatorStyle">{{item}}{{units[0]}}</text>
  38. </view>
  39. </picker-view-column>
  40. <picker-view-column>
  41. <view
  42. class="gui-picker-item"
  43. :style="indicatorStyle"
  44. v-for="(item, index) in sDate[1]"
  45. :key="index">
  46. <text
  47. class="gui-picker-item gui-block"
  48. :style="indicatorStyle">{{item}}{{units[1]}}</text>
  49. </view>
  50. </picker-view-column>
  51. <picker-view-column>
  52. <view
  53. class="gui-picker-item"
  54. :style="indicatorStyle"
  55. v-for="(item, index) in sDate[2]"
  56. :key="index">
  57. <text
  58. class="gui-picker-item gui-block"
  59. :style="indicatorStyle">{{item}}{{units[2]}}</text>
  60. </view>
  61. </picker-view-column>
  62. <picker-view-column
  63. v-if="isTime">
  64. <view
  65. class="gui-picker-item"
  66. :style="indicatorStyle"
  67. v-for="(item, index) in sDate[3]"
  68. :key="index">
  69. <text
  70. class="gui-picker-item gui-block"
  71. :style="indicatorStyle">{{item}}{{units[3]}}</text>
  72. </view>
  73. </picker-view-column>
  74. <picker-view-column
  75. v-if="isTime && isMinute">
  76. <view
  77. class="gui-picker-item"
  78. :style="indicatorStyle"
  79. v-for="(item, index) in sDate[4]"
  80. :key="index">
  81. <text
  82. class="gui-picker-item gui-block"
  83. :style="indicatorStyle">{{item}}{{units[4]}}</text>
  84. </view>
  85. </picker-view-column>
  86. <picker-view-column
  87. v-if="isTime && isMinute && isSecond">
  88. <view
  89. class="gui-picker-item"
  90. :style="indicatorStyle"
  91. v-for="(item, index) in sDate[5]"
  92. :key="index">
  93. <text
  94. class="gui-picker-item gui-block"
  95. :style="indicatorStyle">{{item}}{{units[5]}}</text>
  96. </view>
  97. </picker-view-column>
  98. </picker-view>
  99. </view>
  100. </view>
  101. </view>
  102. </template>
  103. <script>
  104. export default {
  105. name : "gui-datetime",
  106. props : {
  107. cancelText : { type : String, default : '取消' },
  108. confirmText : { type : String, default : '确定' },
  109. value : { type : String , default : '请选择日期'},
  110. isTime : { type : Boolean, default : true},
  111. isMinute : { type : Boolean, default : true},
  112. isSecond : { type : Boolean, default : true},
  113. startYear : { type : Number, default : 1980},
  114. endYear : { type : Number, default : 2050},
  115. units : { type : Array , default : function(){
  116. return new Array('年','月','日','时','分','秒')
  117. }},
  118. height : { type : String, default : '390rpx' },
  119. zIndex : { type : Number, default : 90},
  120. width : { type : String, default : '750rpx' },
  121. indicatorStyle : { type : String, default : 'height:36px; line-height:36px;'}
  122. },
  123. data() {
  124. return {
  125. show:false,
  126. defaultVal : [0,0,0,0,0,0],
  127. sDate:[[],[],[],[],[],[]],
  128. timer:null
  129. }
  130. },
  131. created() {
  132. this.init();
  133. },
  134. methods: {
  135. stopfun:function(e){e.stopPropagation(); return ;},
  136. now : function () {
  137. var date = new Date();
  138. var y = date.getFullYear();
  139. var m = date.getMonth() + 1;
  140. m = m < 10 ? ('0' + m) : m;
  141. var d = date.getDate();
  142. d = d < 10 ? ('0' + d) : d;
  143. var h = date.getHours();
  144. h = h < 10 ? ('0' + h) : h;
  145. var minute = date.getMinutes();
  146. var second = date.getSeconds();
  147. minute = minute < 10 ? ('0' + minute) : minute;
  148. second = second < 10 ? ('0' + second) : second;
  149. return y + '-' + m + '-' + d + ' '+ h +':' + minute + ':' + second;
  150. },
  151. arrayIndexOf : function(arr, needFind){
  152. var index = -1;
  153. for(let i = 0; i < arr.length; i++){if(arr[i] == needFind){index = i; return i;}}
  154. return index;
  155. },
  156. setValue : function (val) {
  157. if(val == ''){val = this.now();}
  158. var reg = /^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})$/;
  159. var res = val.match(reg);
  160. if(res == null){
  161. reg = /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/;
  162. res = val.match(reg);
  163. if(res == null){
  164. this.setValue(this.now());
  165. return ;
  166. }
  167. res[4] = '00';
  168. res[5] = '00';
  169. res[6] = '00';
  170. }
  171. this.setDefaults([res[1],res[2],res[3],res[4],res[5],res[6]]);
  172. },
  173. setDefaults : function (res) {
  174. for(let i = 0; i < res.length; i++){
  175. var index = this.arrayIndexOf(this.sDate[i], res[i]);
  176. if(index == -1){index = 0;}
  177. this.defaultVal.splice(i, 1, index);
  178. }
  179. this.changeBase(this.defaultVal);
  180. },
  181. // 初始化组件
  182. init:function(){
  183. if(this.endYear < this.startYear){this.endYear = this.startYear + 10;}
  184. var years = new Array();
  185. for(let i = this.startYear; i <= this.endYear; i++){years.push(i);}
  186. var months = new Array();
  187. for(let i = 1; i <= 12; i++){if(i < 10){months.push('0'+i);}else{months.push(i);}}
  188. var days = new Array();
  189. for(let i = 1; i <= 31; i++){if(i < 10){days.push('0'+i);}else{days.push(i);}}
  190. var hours = new Array();
  191. for(let i = 0; i < 24; i++){if(i < 10){hours.push('0'+i);}else{hours.push(i);}}
  192. var minutes = new Array();
  193. var seconds = new Array();
  194. for(let i = 0; i < 60; i++){
  195. if(i < 10){minutes.push('0'+i); seconds.push('0'+i);}else{minutes.push(i); seconds.push(i);}
  196. }
  197. this.sDate = [years, months, days, hours, minutes, seconds];
  198. this.$nextTick(()=>{setTimeout(()=>{ this.setValue(this.value);}, 500);});
  199. },
  200. change : function (res) {
  201. if(this.timer != null){clearTimeout(this.timer);}
  202. this.timer = setTimeout(()=>{this.changeBase(res.detail.value);},500);
  203. },
  204. changeBase:function(res){
  205. var date = new Date(this.sDate[0][res[0]], this.sDate[1][res[1]], 0);
  206. var days = date.getDate();
  207. var daysOut = new Array();
  208. for(let i = 1; i <= days; i++){if(i < 10){daysOut.push('0'+i);}else{daysOut.push(i);}}
  209. this.sDate.splice(2, 1, daysOut);
  210. if(res[2] + 1 > days){res[2] = days - 1;}
  211. this.defaultVal = res;
  212. if(this.isTime){
  213. var resdata = new Array(this.sDate[0][this.defaultVal[0]],
  214. this.sDate[1][this.defaultVal[1]],
  215. this.sDate[2][this.defaultVal[2]],
  216. this.sDate[3][this.defaultVal[3]],
  217. this.sDate[4][this.defaultVal[4]],
  218. this.sDate[5][this.defaultVal[5]]);
  219. }else{
  220. var resdata = new Array(
  221. this.sDate[0][this.defaultVal[0]],
  222. this.sDate[1][this.defaultVal[1]],
  223. this.sDate[2][this.defaultVal[2]]
  224. )
  225. }
  226. this.$emit('change', resdata);
  227. },
  228. confirm:function () {
  229. if(this.isTime){
  230. var res = new Array(this.sDate[0][this.defaultVal[0]],
  231. this.sDate[1][this.defaultVal[1]],
  232. this.sDate[2][this.defaultVal[2]],
  233. this.sDate[3][this.defaultVal[3]],
  234. this.sDate[4][this.defaultVal[4]],
  235. this.sDate[5][this.defaultVal[5]]);
  236. }else{
  237. var res = new Array(this.sDate[0][this.defaultVal[0]],
  238. this.sDate[1][this.defaultVal[1]],
  239. this.sDate[2][this.defaultVal[2]])
  240. }
  241. this.$emit('confirm', res);
  242. this.close();
  243. },
  244. open : function () {
  245. this.show = true;
  246. },
  247. close : function () {
  248. this.show = false;
  249. }
  250. },
  251. emits : ['change', 'confirm']
  252. }
  253. </script>
  254. <style scoped>
  255. .gui-dateBT-shade{width:750rpx; position:fixed; background-color:rgba(0,0,0,0.7); z-index:99; left:0; top:0; bottom:0; flex:1; overflow:hidden;}
  256. .graceDateTime-header{padding:25rpx;}
  257. .graceDateTime-header-btn{width:200rpx; line-height:38rpx; height:38rpx; font-size:28rpx;}
  258. .graceDateTime-main{width:750rpx;}
  259. </style>