range-picker.vue 9.8 KB

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