time-picker.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <template>
  2. <view class="w-picker-view">
  3. <picker-view class="d-picker-view" :indicator-style="itemHeight" :value="pickVal" @change="handlerChange">
  4. <picker-view-column>
  5. <view class="w-picker-item" v-for="(item,index) in range.hours" :key="index">{{item}}时</view>
  6. </picker-view-column>
  7. <picker-view-column>
  8. <view class="w-picker-item" v-for="(item,index) in range.minutes" :key="index">{{item}}分</view>
  9. </picker-view-column>
  10. <picker-view-column v-if="second">
  11. <view class="w-picker-item" v-for="(item,index) in range.seconds" :key="index">{{item}}秒</view>
  12. </picker-view-column>
  13. </picker-view>
  14. </view>
  15. </template>
  16. <script>
  17. let _this=null;
  18. export default {
  19. data() {
  20. return {
  21. pickVal:[],
  22. range:{},
  23. checkObj:{}
  24. };
  25. },
  26. props:{
  27. itemHeight:{
  28. type:String,
  29. default:"44px"
  30. },
  31. value:{
  32. type:[String,Array,Number],
  33. default:""
  34. },
  35. current:{//是否默认选中当前日期
  36. type:Boolean,
  37. default:false
  38. },
  39. second:{
  40. type:Boolean,
  41. default:true
  42. }
  43. },
  44. watch:{
  45. value(val){
  46. this.initData();
  47. }
  48. },
  49. created() {
  50. _this=this;
  51. _this.initData();
  52. },
  53. methods:{
  54. formatNum(n){
  55. return (Number(n)<10?'0'+Number(n):Number(n)+'');
  56. },
  57. checkValue(value){
  58. let strReg=/^\d{2}:\d{2}:\d{2}$/,example="18:00:05";
  59. if(!strReg.test(value)){
  60. console.log(new Error("请传入与mode、fields匹配的value值,例value="+example+""))
  61. }
  62. return strReg.test(value);
  63. },
  64. resetData(year,month,day,hour,minute){
  65. let curDate=_this.getCurrenDate();
  66. let curFlag=_this.current;
  67. let curHour=curDate.curHour;
  68. let curMinute=curDate.curMinute;
  69. let curSecond=curDate.curSecond;
  70. for(let hour=0;hour<24;hour++){
  71. hours.push(_this.formatNum(hour));
  72. }
  73. for(let minute=0;minute<60;minute++){
  74. minutes.push(_this.formatNum(minute));
  75. }
  76. for(let second=0;second<60;second++){
  77. seconds.push(_this.formatNum(second));
  78. }
  79. return{
  80. hours,
  81. minutes,
  82. seconds
  83. }
  84. },
  85. getData(curDate){
  86. //用来处理初始化数据
  87. let hours=[],minutes=[],seconds=[];
  88. let curFlag=_this.current;
  89. let disabledAfter=_this.disabledAfter;
  90. let fields=_this.fields;
  91. let curHour=curDate.curHour;
  92. let curMinute=curDate.curMinute;
  93. let curSecond=curDate.curSecond;
  94. for(let hour=0;hour<24;hour++){
  95. hours.push(_this.formatNum(hour));
  96. }
  97. for(let minute=0;minute<60;minute++){
  98. minutes.push(_this.formatNum(minute));
  99. }
  100. for(let second=0;second<60;second++){
  101. seconds.push(_this.formatNum(second));
  102. }
  103. return _this.second?{
  104. hours,
  105. minutes,
  106. seconds
  107. }:{
  108. hours,
  109. minutes
  110. }
  111. },
  112. getCurrenDate(){
  113. let curDate=new Date();
  114. let curHour=curDate.getHours();
  115. let curMinute=curDate.getMinutes();
  116. let curSecond=curDate.getSeconds();
  117. return _this.second?{
  118. curHour,
  119. curMinute,
  120. curSecond
  121. }:{
  122. curHour,
  123. curMinute,
  124. }
  125. },
  126. getDval(){
  127. let value=_this.value;
  128. let fields=_this.fields;
  129. let dVal=null;
  130. let aDate=new Date();
  131. let hour=_this.formatNum(aDate.getHours());
  132. let minute=_this.formatNum(aDate.getMinutes());
  133. let second=_this.formatNum(aDate.getSeconds());
  134. if(value){
  135. let flag=_this.checkValue(value);
  136. if(!flag){
  137. dVal=[hour,minute,second]
  138. }else{
  139. dVal=value?value.split(":"):[];
  140. }
  141. }else{
  142. dVal=_this.second?[hour,minute,second]:[hour,minute]
  143. }
  144. return dVal;
  145. },
  146. initData(){
  147. let curDate=_this.getCurrenDate();
  148. let dateData=this.getData(curDate);
  149. let pickVal=[],obj={},full="",result="",hour="",minute="",second="";
  150. let dVal=_this.getDval();
  151. let curFlag=_this.current;
  152. let disabledAfter=_this.disabledAfter;
  153. let hours=dateData.hours;
  154. let minutes=dateData.minutes;
  155. let seconds=dateData.seconds;
  156. let defaultArr=_this.second?[
  157. dVal[0]&&hours.indexOf(dVal[0])!=-1?hours.indexOf(dVal[0]):0,
  158. dVal[1]&&minutes.indexOf(dVal[1])!=-1?minutes.indexOf(dVal[1]):0,
  159. dVal[2]&&seconds.indexOf(dVal[2])!=-1?seconds.indexOf(dVal[2]):0
  160. ]:[
  161. dVal[0]&&hours.indexOf(dVal[0])!=-1?hours.indexOf(dVal[0]):0,
  162. dVal[1]&&minutes.indexOf(dVal[1])!=-1?minutes.indexOf(dVal[1]):0
  163. ];
  164. pickVal=disabledAfter?defaultArr:(curFlag?(_this.second?[
  165. hours.indexOf(_this.formatNum(curDate.curHour)),
  166. minutes.indexOf(_this.formatNum(curDate.curMinute)),
  167. seconds.indexOf(_this.formatNum(curDate.curSecond)),
  168. ]:[
  169. hours.indexOf(_this.formatNum(curDate.curHour)),
  170. minutes.indexOf(_this.formatNum(curDate.curMinute))
  171. ]):defaultArr);
  172. _this.range=dateData;
  173. _this.checkObj=obj;
  174. hour=dVal[0]?dVal[0]:hours[0];
  175. minute=dVal[1]?dVal[1]:minutes[0];
  176. second=dVal[2]?dVal[0]:seconds[0];
  177. result=_this.second?`${hour+':'+minute+':'+second}`:`${hour+':'+minute}`;
  178. full=_this.second?`${hour+':'+minute+':'+second}`:`${hour+':'+minute+':00'}`;
  179. _this.$nextTick(()=>{
  180. _this.pickVal=pickVal;
  181. });
  182. _this.$emit("change",{
  183. result:result,
  184. value:full,
  185. obj:obj
  186. })
  187. },
  188. handlerChange(e){
  189. let arr=[...e.detail.value];
  190. let data=_this.range;
  191. let hour="",minute="",second="",result="",full="",obj={};
  192. hour=(arr[0]||arr[0]==0)?data.hours[arr[0]]||data.hours[data.hours.length-1]:"";
  193. minute=(arr[1]||arr[1]==0)?data.minutes[arr[1]]||data.minutes[data.minutes.length-1]:"";
  194. second=(arr[2]||arr[2]==0)?data.seconds[arr[2]]||data.seconds[data.seconds.length-1]:"";
  195. obj=_this.second?{
  196. hour,
  197. minute,
  198. second
  199. }:{
  200. hour,
  201. minute
  202. };
  203. this.checkObj=obj;
  204. result=_this.second?`${hour+':'+minute+':'+second}`:`${hour+':'+minute}`;
  205. full=_this.second?`${hour+':'+minute+':'+second}`:`${hour+':'+minute+':00'}`;
  206. _this.$emit("change",{
  207. result:result,
  208. value:full,
  209. obj:obj
  210. })
  211. }
  212. }
  213. }
  214. </script>
  215. <style lang="scss">
  216. @import "./w-picker.css";
  217. </style>