half-picker.vue 9.6 KB

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