todaydate-picker.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <template>
  2. <div >
  3. <u-popup ref="popup" class="popup" :mask="true" mode="bottom" >
  4. <view >
  5. <view class="popup-title ">
  6. <text >时间筛选</text>
  7. <text @click="$refs.popup.close()">×</text>
  8. </view>
  9. <view style="margin-left: 16px;">
  10. <text class="quick-title">快捷选择</text>
  11. <view style="margin: 15px 0;">
  12. <text style="margin-right: 25px;" :class="typeStatistics == val.value ? 'selected' : 'text-border'" v-for="val in partnerTypeOption"
  13. :key="val.value" @click="getPartnerType(val.value)">{{ val.lable }}</text>
  14. </view>
  15. </view>
  16. <text class="quick-title" style="margin-left: 16px;">自定义时间</text>
  17. <view style="padding: 15px">
  18. <view class="popup-data dis j-s">
  19. <text :class="startShow? 'active' :''" @click="startClick()">{{ interviewStartTime||'开始时间' }}</text>至
  20. <text :class="endShow? 'active' :''" @click="endClick()">{{ interviewEndTime || '结束时间' }}</text>
  21. </view>
  22. <picker-view v-show="visible" :indicator-style="indicatorStyle" :value="value" @change="bindChange"
  23. class="picker-view">
  24. <picker-view-column>
  25. <view class="item" v-for="(item,index) in years" :key="index">{{item}}年</view>
  26. </picker-view-column>
  27. <picker-view-column>
  28. <view class="item" v-for="(item,index) in months" :key="index">{{item}}月</view>
  29. </picker-view-column>
  30. <picker-view-column>
  31. <view class="item" v-for="(item,index) in days" :key="index">{{item}}日</view>
  32. </picker-view-column>
  33. </picker-view>
  34. </view>
  35. </view>
  36. <view class="popup-footer dis j-s">
  37. <text @click.stop="reset" >重置</text>
  38. <text @click.stop="confirm" >确定</text>
  39. </view>
  40. </u-popup>
  41. <u-toast ref="uToast" />
  42. </div>
  43. </template>
  44. <script>
  45. import { log } from 'util';
  46. export default {
  47. data() {
  48. return {
  49. typeStatistics:0,
  50. partnerTypeOption: [
  51. {
  52. lable: '今日',
  53. value: '1'
  54. }, {
  55. lable: '本月',
  56. value: '2'
  57. }, {
  58. lable: '本年',
  59. value: '3'
  60. }, {
  61. lable: '全部',
  62. value: '4'
  63. }
  64. ],
  65. startShow: false,
  66. endShow: false,
  67. interviewStartTime:'',
  68. interviewEndTime:'',
  69. beginStartTime:'',
  70. beginEndTime:'',
  71. years: [],
  72. year: null,
  73. months: [],
  74. month: null,
  75. days: [],
  76. day: null,
  77. hours: [],
  78. hour: null,
  79. minutes: [],
  80. minute: null,
  81. value: [],
  82. indicatorStyle: `height: 50px;color:#2D6DFF `,
  83. visible:false
  84. }
  85. },
  86. props: {
  87. // visible: {
  88. // type: Boolean,
  89. // default: true
  90. // },
  91. // interviewTime: {
  92. // type: String,
  93. // default() {
  94. // return '';
  95. // },
  96. // }
  97. },
  98. mounted() {
  99. this.init();
  100. },
  101. watch: {
  102. // visible: {
  103. // handler(newValue, oldValue) {
  104. // if (newValue) {
  105. // if (this.interviewTime) {
  106. // this.init(this.interviewTime);
  107. // }
  108. // }
  109. // },
  110. // immediate: false,
  111. // deep: true
  112. // }
  113. },
  114. methods: {
  115. open(){
  116. this.typeStatistics=0
  117. this.interviewStartTime=''
  118. this.interviewEndTime =''
  119. this.visible=false
  120. this.startShow = false
  121. this.endShow = false
  122. this.$refs.popup.open()
  123. },
  124. bindChange: function (e) {
  125. const val = e.detail.value
  126. let year = this.years[val[0]]
  127. let isDay = 30, days = [];
  128. if (val[1]+1 == 2) {
  129. if((val[0]%4==0 && year%100!=0)||(year%400==0)){
  130. console.log('闰年')
  131. isDay = 29;
  132. }else {
  133. isDay = 28;
  134. console.log('平年')
  135. }
  136. } else if ([1,3,5,7,8,10,12].includes(val[1]+1)) {
  137. isDay = 31;
  138. } else {
  139. isDay = 30;
  140. }
  141. for (let i = 1; i <= isDay; i++) {
  142. days.push(i)
  143. }
  144. this.days = days;
  145. this.year = this.years[val[0]]
  146. this.month = this.months[val[1]]
  147. this.day = this.days[val[2]]
  148. if(this.startShow){
  149. this.interviewStartTime=this.year+'-'+this.month+'-'+this.day
  150. }
  151. if(this.endShow){
  152. this.interviewEndTime=this.year+'-'+this.month+'-'+this.day
  153. }
  154. },
  155. init() {
  156. const date = new Date();
  157. const years = []
  158. const year = date.getFullYear()
  159. const months = []
  160. const month = date.getMonth() + 1
  161. const days = []
  162. const day = date.getDate()
  163. let isDay = 30;
  164. if (month == 2) {
  165. if((year%4==0 && year%100!=0)||(year%400==0)){
  166. isDay = 29;
  167. }else {
  168. isDay = 28;
  169. }
  170. } else if ([1,3,5,7,8,10,12].includes(month)) {
  171. isDay = 31;
  172. } else {
  173. isDay = 30;
  174. }
  175. for (let i = date.getFullYear(); i >= 1900; i--) {
  176. years.push(i)
  177. }
  178. for (let i = 1; i <= 12; i++) {
  179. months.push(i)
  180. }
  181. for (let i = 1; i <= isDay; i++) {
  182. days.push(i)
  183. }
  184. this.years = years
  185. this.year = year
  186. this.months = months
  187. this.month = month
  188. this.days = days
  189. this.day = day
  190. this.value = [0, month-1, day-1]
  191. },
  192. // 补0
  193. padZeroStr(originStr){
  194. if(+originStr < 10){
  195. return String(originStr).padStart(2,'0')
  196. }
  197. return originStr + ''
  198. },
  199. close(){
  200. this.$emit('update:visible', false);
  201. },
  202. reset(){
  203. this.$emit('update:visible', false);
  204. this.interviewStartTime=''
  205. this.interviewEndTime =''
  206. },
  207. startClick(){
  208. // this.interviewStartTime=''
  209. this.typeStatistics=0
  210. this.interviewStartTime = this.year+'-'+this.month+'-'+this.day
  211. this.visible=true
  212. this.startShow = true
  213. this.endShow = false
  214. },
  215. endClick(){
  216. this.typeStatistics=0
  217. this.interviewEndTime= this.year+'-'+this.month+'-'+this.day
  218. this.visible=true
  219. this.startShow = false
  220. this.endShow = true
  221. },
  222. confirm() {
  223. // let monthStr = this.padZeroStr(this.month)
  224. // let dayStr = this.padZeroStr(this.day)
  225. // let hourStr = this.padZeroStr(this.hour)
  226. // let minuteStr = this.padZeroStr(this.minute)
  227. // this.timeValue = `${this.year}/${monthStr}/${dayStr} ${hourStr}:${minuteStr}:00`;
  228. let timeValue=''
  229. // if(this.interviewStartTime || this.interviewEndTime){
  230. // timeValue=this.interviewStartTime ||this.interviewEndTime
  231. // }
  232. if(this.interviewStartTime && this.interviewEndTime){
  233. // let timeValue=this.interviewStartTime +'~'+this.interviewEndTime
  234. // console.log(this.compareArrays(this.interviewStartTime.split('-'),this.interviewEndTime.split('-')),999)
  235. // if(!this.compareArrays(this.interviewStartTime.split('-'),this.interviewEndTime.split('-'))){
  236. // this.$refs.uToast.show({
  237. // title: '开始时间必须小于结束时间',
  238. // type: 'warning',
  239. // icon:false
  240. // // url: '/pages/user/index'
  241. // })
  242. // return false
  243. // }
  244. // this.compareArrays(this.interviewStartTime.split('-'),this.interviewEndTime.split('-'))
  245. // log(this.compareDates(new Date(this.interviewStartTime),new Date(this.interviewEndTime),222222))
  246. // log(this.compareDates(new Date("2022-01-01"),new Date("2022-01-02"),222222))
  247. if(this.compareDates(new Date(this.interviewStartTime),new Date(this.interviewEndTime))==1){
  248. this.$refs.uToast.show({
  249. title: '开始时间必须小于结束时间',
  250. type: 'warning',
  251. icon:false
  252. // url: '/pages/user/index'
  253. })
  254. return false
  255. }
  256. }
  257. else{
  258. this.$refs.uToast.show({
  259. title: '请选择时间!',
  260. type: 'warning',
  261. icon:false
  262. // url: '/pages/user/index'
  263. })
  264. return false
  265. }
  266. this.$emit('confirmPickDate', this.interviewStartTime,this.interviewEndTime,this.interviewStartTime +'~'+this.interviewEndTime);
  267. this.$refs.popup.close()
  268. },
  269. compareDates(date1, date2) {
  270. var dateString1 = date1.toISOString().slice(0, 10);
  271. var dateString2 = date2.toISOString().slice(0, 10);
  272. if (dateString1 > dateString2) {
  273. return 1;
  274. } else if (dateString1 < dateString2) {
  275. return -1;
  276. } else {
  277. return 0;
  278. }
  279. },
  280. getCurrentMonthFirst() {
  281. // 获取当月第一天数据
  282. let date = new Date()
  283. date.setDate(1)
  284. let month = parseInt(date.getMonth() + 1)
  285. let day = date.getDate()
  286. if (month < 10) month = '0' + month
  287. if (day < 10) day = '0' + day
  288. return date.getFullYear() + '-' + month + '-' + day
  289. },
  290. getCurrentMonthLast() {
  291. // 获取当月最后一天数据
  292. let date = new Date()
  293. let year = date.getFullYear()
  294. let month = date.getMonth() + 1
  295. month = month < 10 ? '0' + month : month
  296. let day = new Date(year, month, 0)
  297. return year + '-' + month + '-' + day.getDate()
  298. },
  299. getPartnerType(type) {
  300. this.typeStatistics=type
  301. let now = new Date();
  302. let year = now.getFullYear();
  303. let month = now.getMonth() + 1; // 月份是从0开始的,所以需要加1
  304. let day = now.getDate();
  305. if(type==1){
  306. let theDay=`${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;
  307. this.$emit('confirmPickDate',theDay,theDay,'今日数据');
  308. this.$refs.popup.close()
  309. }
  310. else if(type==2){
  311. this.$emit('confirmPickDate', this.getCurrentMonthFirst(),this.getCurrentMonthLast(),'本月数据');
  312. this.$refs.popup.close()
  313. }
  314. else if(type==3 ){
  315. this.$emit('confirmPickDate', year+'-01-01',year+ '-12-31','本年数据');
  316. this.$refs.popup.close()
  317. }
  318. else if(type==4 ){
  319. this.$emit('confirmPickDate', '','','全部数据');
  320. this.$refs.popup.close()
  321. }
  322. this.interviewStartTime=''
  323. this.interviewEndTime =''
  324. },
  325. },
  326. }
  327. </script>
  328. <style lang="scss" scoped>
  329. .popup {
  330. border-radius:10px 10px 0 0 !important;
  331. }
  332. .quick-title{
  333. font-weight: 400;
  334. font-size: 16px;
  335. color: #333333;
  336. line-height: 18px;
  337. }
  338. .text-border{
  339. border-radius: 2px 2px 2px 2px;
  340. border: 1px solid #EEEEEE;
  341. color: #666666;
  342. font-size: 14px;
  343. padding: 3px 10px;
  344. }
  345. .selected {
  346. padding: 3px 10px;
  347. color: #2D6DFF;
  348. background: rgba(45,109,255,0.1);
  349. }
  350. .popup-title{
  351. font-size: 15px;
  352. color: #333333;
  353. padding:10px;
  354. margin-bottom: 10px;
  355. position: relative;
  356. text-align: center;
  357. text:first-child {
  358. font-weight: 600;
  359. }
  360. text:last-child {
  361. position: absolute;
  362. right: 10px;
  363. font-size: 21px;
  364. color: #999999;
  365. line-height: 23px;
  366. }
  367. }
  368. .popup-footer{
  369. margin:10px 15px;
  370. text{
  371. font-size: 16px;
  372. padding: 5px 0;
  373. color: #FFFFFF;
  374. display: inline-block;
  375. width: 45%;
  376. text-align: center;
  377. }
  378. text:first-child {
  379. color: #2D6DFF;
  380. background: rgba(45,109,255,0.1);
  381. border-radius: 5px 5px 5px 5px;
  382. }
  383. text:last-child {
  384. background: linear-gradient( 132deg, #2DD9FF 0%, #2D6DFF 100%);
  385. border-radius: 5px 5px 5px 5px;
  386. }
  387. }
  388. .popup-data{
  389. line-height: 25px;
  390. text{
  391. font-size: 14px;
  392. color: #999999 ;
  393. display: inline-block;
  394. width: 45%;
  395. text-align: center;
  396. border: 1px solid #DDDDDD;
  397. border-radius: 2px 2px 2px 2px;
  398. }
  399. .active{
  400. border: 1px solid #2D6DFF;
  401. color: #2D6DFF;
  402. }
  403. }
  404. .picker-view {
  405. width: 100%;
  406. height: 150px;
  407. }
  408. </style>