half-picker.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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.years" :key="index">{{item}}年</view>
  6. </picker-view-column>
  7. <picker-view-column>
  8. <view class="w-picker-item" v-for="(item,index) in range.months" :key="index">{{item}}月</view>
  9. </picker-view-column>
  10. <picker-view-column>
  11. <view class="w-picker-item" v-for="(item,index) in range.days" :key="index">{{item}}日</view>
  12. </picker-view-column>
  13. <picker-view-column>
  14. <view class="w-picker-item" v-for="(item,index) in range.sections" :key="index">{{item}}</view>
  15. </picker-view-column>
  16. </picker-view>
  17. </view>
  18. </template>
  19. <script>
  20. export default {
  21. data() {
  22. return {
  23. pickVal:[],
  24. range:{},
  25. checkObj:{}
  26. };
  27. },
  28. props:{
  29. itemHeight:{
  30. type:String,
  31. default:"44px"
  32. },
  33. startYear:{
  34. type:String,
  35. default:""
  36. },
  37. endYear:{
  38. type:String,
  39. default:""
  40. },
  41. value:{
  42. type:[String,Array,Number],
  43. default:""
  44. },
  45. current:{//是否默认选中当前日期
  46. type:Boolean,
  47. default:false
  48. },
  49. disabledAfter:{//是否禁用当前之后的日期
  50. type:Boolean,
  51. default:false
  52. }
  53. },
  54. watch:{
  55. value(val){
  56. this.initData();
  57. }
  58. },
  59. created() {
  60. this.initData();
  61. },
  62. methods:{
  63. formatNum(n){
  64. return (Number(n)<10?'0'+Number(n):Number(n)+'');
  65. },
  66. checkValue(value){
  67. let strReg=/^\d{4}-\d{2}-\d{2} [\u4e00-\u9fa5]{2}$/,example;
  68. if(!strReg.test(value)){
  69. console.log(new Error("请传入与mode、fields匹配的value值,例value="+example+""))
  70. }
  71. return strReg.test(value);
  72. },
  73. resetData(year,month,day){
  74. let curDate=this.getCurrenDate();
  75. let curFlag=this.current;
  76. let curYear=curDate.curYear;
  77. let curMonth=curDate.curMonth;
  78. let curDay=curDate.curDay;
  79. let curHour=curDate.curHour;
  80. let months=[],days=[],sections=[];
  81. let disabledAfter=this.disabledAfter;
  82. let monthsLen=disabledAfter?(year*1<curYear?12:curMonth):12;
  83. let totalDays=new Date(year,month,0).getDate();//计算当月有几天;
  84. let daysLen=disabledAfter?((year*1<curYear||month*1<curMonth)?totalDays:curDay):totalDays;
  85. let sectionFlag=disabledAfter?((year*1<curYear||month*1<curMonth||day*1<curDay)==true?false:true):(curHour>12==true?true:false);
  86. sections=["上午","下午"];
  87. for(let month=1;month<=monthsLen;month++){
  88. months.push(this.formatNum(month));
  89. };
  90. for(let day=1;day<=daysLen;day++){
  91. days.push(this.formatNum(day));
  92. }
  93. if(sectionFlag){
  94. sections=["上午"];
  95. }
  96. return{
  97. months,
  98. days,
  99. sections
  100. }
  101. },
  102. getData(dVal){
  103. //用来处理初始化数据
  104. let curFlag=this.current;
  105. let disabledAfter=this.disabledAfter;
  106. let curDate=this.getCurrenDate();
  107. let curYear=curDate.curYear;
  108. let curMonthdays=curDate.curMonthdays;
  109. let curMonth=curDate.curMonth;
  110. let curDay=curDate.curDay;
  111. let curHour=curDate.curHour;
  112. let defaultDate=this.getDefaultDate();
  113. let startYear=this.getStartDate().getFullYear();
  114. let endYear=this.getEndDate().getFullYear();
  115. let years=[],months=[],days=[],sections=[];
  116. let year=dVal[0]*1;
  117. let month=dVal[1]*1;
  118. let day=dVal[2]*1;
  119. let monthsLen=disabledAfter?(year<curYear?12:curDate.curMonth):12;
  120. let daysLen=disabledAfter?((year<curYear||month<curMonth)?defaultDate.defaultDays:curDay):(curFlag?curMonthdays:defaultDate.defaultDays);
  121. let sectionFlag=disabledAfter?((year*1<curYear||month*1<curMonth||day*1<curDay)==true?false:true):(curHour>12==true?true:false);
  122. for(let year=startYear;year<=(disabledAfter?curYear:endYear);year++){
  123. years.push(year.toString())
  124. }
  125. for(let month=1;month<=monthsLen;month++){
  126. months.push(this.formatNum(month));
  127. }
  128. for(let day=1;day<=daysLen;day++){
  129. days.push(this.formatNum(day));
  130. }
  131. if(sectionFlag){
  132. sections=["下午"];
  133. }else{
  134. sections=["上午","下午"];
  135. }
  136. sections=["上午","下午"];
  137. return {
  138. years,
  139. months,
  140. days,
  141. sections
  142. }
  143. },
  144. getCurrenDate(){
  145. let curDate=new Date();
  146. let curYear=curDate.getFullYear();
  147. let curMonth=curDate.getMonth()+1;
  148. let curMonthdays=new Date(curYear,curMonth,0).getDate();
  149. let curDay=curDate.getDate();
  150. let curHour=curDate.getHours();
  151. let curSection="上午";
  152. if(curHour>=12){
  153. curSection="下午";
  154. }
  155. return{
  156. curDate,
  157. curYear,
  158. curMonth,
  159. curMonthdays,
  160. curDay,
  161. curHour,
  162. curSection
  163. }
  164. },
  165. getDefaultDate(){
  166. let value=this.value;
  167. let reg=/-/g;
  168. let defaultDate=value?new Date(value.split(" ")[0].replace(reg,"/")):new Date();
  169. let defaultYear=defaultDate.getFullYear();
  170. let defaultMonth=defaultDate.getMonth()+1;
  171. let defaultDay=defaultDate.getDate();
  172. let defaultDays=new Date(defaultYear,defaultMonth,0).getDate()*1;
  173. return{
  174. defaultDate,
  175. defaultYear,
  176. defaultMonth,
  177. defaultDay,
  178. defaultDays
  179. }
  180. },
  181. getStartDate(){
  182. let start=this.startYear;
  183. let startDate="";
  184. let reg=/-/g;
  185. if(start){
  186. startDate=new Date(start+"/01/01");
  187. }else{
  188. startDate=new Date("1970/01/01");
  189. }
  190. return startDate;
  191. },
  192. getEndDate(){
  193. let end=this.endYear;
  194. let reg=/-/g;
  195. let endDate="";
  196. if(end){
  197. endDate=new Date(end+"/12/31");
  198. }else{
  199. endDate=new Date();
  200. }
  201. return endDate;
  202. },
  203. getDval(){
  204. let value=this.value;
  205. let dVal=null;
  206. let aDate=new Date();
  207. let year=this.formatNum(aDate.getFullYear());
  208. let month=this.formatNum(aDate.getMonth()+1);
  209. let day=this.formatNum(aDate.getDate());
  210. let hour=aDate.getHours();
  211. let section="上午";
  212. if(hour>=12)section="下午";
  213. if(value){
  214. let flag=this.checkValue(value);
  215. if(!flag){
  216. dVal=[year,month,day,section]
  217. }else{
  218. let v=value.split(" ");
  219. dVal=[...v[0].split("-"),v[1]];
  220. }
  221. }else{
  222. dVal=[year,month,day,section]
  223. }
  224. return dVal;
  225. },
  226. initData(){
  227. let startDate,endDate,startYear,endYear,startMonth,endMonth,startDay,endDay;
  228. let years=[],months=[],days=[],sections=[];
  229. let dVal=[],pickVal=[];
  230. let value=this.value;
  231. let reg=/-/g;
  232. let range={};
  233. let result="",full="",year,month,day,section,obj={};
  234. let defaultDate=this.getDefaultDate();
  235. let defaultYear=defaultDate.defaultYear;
  236. let defaultMonth=defaultDate.defaultMonth;
  237. let defaultDay=defaultDate.defaultDay;
  238. let defaultDays=defaultDate.defaultDays;
  239. let curFlag=this.current;
  240. let disabledAfter=this.disabledAfter;
  241. let curDate=this.getCurrenDate();
  242. let curYear=curDate.curYear;
  243. let curMonth=curDate.curMonth;
  244. let curMonthdays=curDate.curMonthdays;
  245. let curDay=curDate.curDay;
  246. let curSection=curDate.curSection;
  247. let dateData=[];
  248. dVal=this.getDval();
  249. startDate=this.getStartDate();
  250. endDate=this.getEndDate();
  251. startYear=startDate.getFullYear();
  252. startMonth=startDate.getMonth();
  253. startDay=startDate.getDate();
  254. endYear=endDate.getFullYear();
  255. endMonth=endDate.getMonth();
  256. endDay=endDate.getDate();
  257. dateData=this.getData(dVal);
  258. years=dateData.years;
  259. months=dateData.months;
  260. days=dateData.days;
  261. sections=dateData.sections;
  262. pickVal=disabledAfter?[
  263. dVal[0]&&years.indexOf(dVal[0])!=-1?years.indexOf(dVal[0]):0,
  264. dVal[1]&&months.indexOf(dVal[1])!=-1?months.indexOf(dVal[1]):0,
  265. dVal[2]&&days.indexOf(dVal[2])!=-1?days.indexOf(dVal[2]):0,
  266. dVal[3]&&sections.indexOf(dVal[3])!=-1?sections.indexOf(dVal[3]):0
  267. ]:(curFlag?[
  268. years.indexOf(curYear+''),
  269. months.indexOf(this.formatNum(curMonth)),
  270. days.indexOf(this.formatNum(curDay)),
  271. sections.indexOf(curSection),
  272. ]:[
  273. dVal[0]&&years.indexOf(dVal[0])!=-1?years.indexOf(dVal[0]):0,
  274. dVal[1]&&months.indexOf(dVal[1])!=-1?months.indexOf(dVal[1]):0,
  275. dVal[2]&&days.indexOf(dVal[2])!=-1?days.indexOf(dVal[2]):0,
  276. dVal[3]&&sections.indexOf(dVal[3])!=-1?sections.indexOf(dVal[3]):0
  277. ]);
  278. range={years,months,days,sections};
  279. year=dVal[0]?dVal[0]:years[0];
  280. month=dVal[1]?dVal[1]:months[0];
  281. day=dVal[2]?dVal[2]:days[0];
  282. section=dVal[3]?dVal[3]:sections[0];
  283. result=full=`${year+'-'+month+'-'+day+' '+section}`;
  284. obj={
  285. year,
  286. month,
  287. day,
  288. section
  289. }
  290. this.range=range;
  291. this.checkObj=obj;
  292. this.$nextTick(()=>{
  293. this.pickVal=pickVal;
  294. });
  295. this.$emit("change",{
  296. result:result,
  297. value:full,
  298. obj:obj
  299. })
  300. },
  301. handlerChange(e){
  302. let arr=[...e.detail.value];
  303. let data=this.range;
  304. let year="",month="",day="",section="";
  305. let result="",full="",obj={};
  306. let months=null,days=null,sections=null;
  307. let disabledAfter=this.disabledAfter;
  308. year=(arr[0]||arr[0]==0)?data.years[arr[0]]||data.years[data.years.length-1]:"";
  309. month=(arr[1]||arr[1]==0)?data.months[arr[1]]||data.months[data.months.length-1]:"";
  310. day=(arr[2]||arr[2]==0)?data.days[arr[2]]||data.days[data.days.length-1]:"";
  311. section=(arr[3]||arr[3]==0)?data.sections[arr[3]]||data.sections[data.sections.length-1]:"";
  312. result=full=`${year+'-'+month+'-'+day+' '+section}`;
  313. let resetData=this.resetData(year,month,day);
  314. if(this.disabledAfter){
  315. months=resetData.months;
  316. days=resetData.days;
  317. sections=resetData.sections;
  318. }else{
  319. if(year%4==0||(month!=this.checkObj.month)){
  320. days=resetData.days;
  321. }
  322. }
  323. if(months)this.range.months=months;
  324. if(days)this.range.days=days;
  325. if(sections)this.range.sections=sections;
  326. obj={
  327. year,
  328. month,
  329. day,
  330. section
  331. }
  332. this.checkObj=obj;
  333. this.$emit("change",{
  334. result:result,
  335. value:full,
  336. obj:obj
  337. })
  338. }
  339. }
  340. }
  341. </script>
  342. <style lang="scss">
  343. @import "./w-picker.css";
  344. </style>