date-picker.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <template>
  2. <div>
  3. <u-popup ref="popup" class="popup" :mask="true" mode="bottom" border-radius="20">
  4. <view>
  5. <view class="popup-title ">
  6. <text>时间筛选</text>
  7. <text @click="$refs.popup.close()">×</text>
  8. </view>
  9. <view class="quickOptions dis f-c ">
  10. <text class="title">快捷选择</text>
  11. <view class=" dis a-c j-start">
  12. <view class="tab" :class="{tabactive:OptionsTabIndex==index}"
  13. v-for="(item,index) in quickOptions" :key="index" @click="OptionsTabSelect(index)">
  14. <text>{{item}}</text>
  15. </view>
  16. </view>
  17. </view>
  18. <text style="margin-left: 42rpx;font-size: 30rpx;color: #333;">自定义时间</text>
  19. <view style="padding:20rpx 42rpx">
  20. <view class="popup-data dis j-s">
  21. <text :class="startShow? 'active' :''"
  22. @click="startClick()">{{ interviewStartTime||'开始时间' }}</text>至
  23. <text :class="endShow? 'active' :''" @click="endClick()">{{ interviewEndTime || '结束时间' }}</text>
  24. </view>
  25. <picker-view v-show="visible" :indicator-style="indicatorStyle" :value="value" @change="bindChange"
  26. class="picker-view">
  27. <picker-view-column>
  28. <view class="item dis a-c j-c " v-for="(item,index) in years" :key="index">{{item}}年</view>
  29. </picker-view-column>
  30. <picker-view-column>
  31. <view class="item dis a-c j-c " v-for="(item,index) in months" :key="index">{{item}}月</view>
  32. </picker-view-column>
  33. <picker-view-column>
  34. <view class="item dis a-c j-c " v-for="(item,index) in days" :key="index">{{item}}日</view>
  35. </picker-view-column>
  36. </picker-view>
  37. </view>
  38. </view>
  39. <view class="popup-footer dis j-s">
  40. <text @click.stop="reset">重置</text>
  41. <text @click.stop="confirm">确定</text>
  42. </view>
  43. </u-popup>
  44. </div>
  45. </template>
  46. <script>
  47. export default {
  48. data() {
  49. return {
  50. quickOptions: ["今日", "本月", "本年"], //快捷选项
  51. OptionsTabIndex: null, //下标
  52. startShow: false,
  53. endShow: false,
  54. interviewStartTime: '',
  55. interviewEndTime: '',
  56. years: [],
  57. year: null,
  58. months: [],
  59. month: null,
  60. days: [],
  61. day: null,
  62. hours: [],
  63. hour: null,
  64. minutes: [],
  65. minute: null,
  66. value: [],
  67. indicatorStyle: `height: 50px;color:#2D6DFF `,
  68. visible: false
  69. }
  70. },
  71. props: {
  72. // visible: {
  73. // type: Boolean,
  74. // default: true
  75. // },
  76. // interviewTime: {
  77. // type: String,
  78. // default() {
  79. // return '';
  80. // },
  81. // }
  82. },
  83. mounted() {
  84. this.init();
  85. },
  86. watch: {
  87. // visible: {
  88. // handler(newValue, oldValue) {
  89. // if (newValue) {
  90. // if (this.interviewTime) {
  91. // this.init(this.interviewTime);
  92. // }
  93. // }
  94. // },
  95. // immediate: false,
  96. // deep: true
  97. // }
  98. },
  99. methods: {
  100. //快捷选择
  101. OptionsTabSelect(index) {
  102. this.OptionsTabIndex = index;
  103. },
  104. open() {
  105. this.$refs.popup.open()
  106. },
  107. bindChange: function(e) {
  108. const val = e.detail.value
  109. let year = this.years[val[0]]
  110. let isDay = 30,
  111. days = [];
  112. if (val[1] + 1 == 2) {
  113. if ((val[0] % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
  114. console.log('闰年')
  115. isDay = 29;
  116. } else {
  117. isDay = 28;
  118. console.log('平年')
  119. }
  120. } else if ([1, 3, 5, 7, 8, 10, 12].includes(val[1] + 1)) {
  121. isDay = 31;
  122. } else {
  123. isDay = 30;
  124. }
  125. for (let i = 1; i <= isDay; i++) {
  126. days.push(i)
  127. }
  128. this.days = days;
  129. this.year = this.years[val[0]]
  130. this.month = this.months[val[1]]
  131. this.day = this.days[val[2]]
  132. if (this.startShow) {
  133. this.interviewStartTime = this.year + '-' + this.month + '-' + this.day
  134. }
  135. if (this.endShow) {
  136. this.interviewEndTime = this.year + '-' + this.month + '-' + this.day
  137. }
  138. },
  139. init() {
  140. const date = new Date();
  141. const years = []
  142. const year = date.getFullYear()
  143. const months = []
  144. const month = date.getMonth() + 1
  145. const days = []
  146. const day = date.getDate()
  147. let isDay = 30;
  148. if (month == 2) {
  149. if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
  150. isDay = 29;
  151. } else {
  152. isDay = 28;
  153. }
  154. } else if ([1, 3, 5, 7, 8, 10, 12].includes(month)) {
  155. isDay = 31;
  156. } else {
  157. isDay = 30;
  158. }
  159. for (let i = date.getFullYear(); i >= 1900; i--) {
  160. years.push(i)
  161. }
  162. for (let i = 1; i <= 12; i++) {
  163. months.push(i)
  164. }
  165. for (let i = 1; i <= isDay; i++) {
  166. days.push(i)
  167. }
  168. this.years = years
  169. this.year = year
  170. this.months = months
  171. this.month = month
  172. this.days = days
  173. this.day = day
  174. this.value = [0, month - 1, day - 1]
  175. },
  176. // 补0
  177. padZeroStr(originStr) {
  178. if (+originStr < 10) {
  179. return String(originStr).padStart(2, '0')
  180. }
  181. return originStr + ''
  182. },
  183. close() {
  184. this.$emit('update:visible', false);
  185. },
  186. reset() {
  187. this.$emit('update:visible', false);
  188. this.interviewStartTime = ''
  189. this.interviewEndTime = ''
  190. },
  191. startClick() {
  192. // this.interviewStartTime=''
  193. this.interviewStartTime = this.year + '-' + this.month + '-' + this.day
  194. this.visible = true
  195. this.startShow = true
  196. this.endShow = false
  197. },
  198. endClick() {
  199. this.interviewEndTime = this.year + '-' + this.month + '-' + this.day
  200. this.visible = true
  201. this.startShow = false
  202. this.endShow = true
  203. },
  204. confirm() {
  205. // let monthStr = this.padZeroStr(this.month)
  206. // let dayStr = this.padZeroStr(this.day)
  207. // let hourStr = this.padZeroStr(this.hour)
  208. // let minuteStr = this.padZeroStr(this.minute)
  209. // this.timeValue = `${this.year}/${monthStr}/${dayStr} ${hourStr}:${minuteStr}:00`;
  210. this.$emit('confirmPickDate', this.interviewStartTime, this.interviewEndTime);
  211. this.$refs.popup.close()
  212. },
  213. },
  214. }
  215. </script>
  216. <style lang="scss" scoped>
  217. .popup {
  218. border-radius: 10px 10px 0 0 !important;
  219. }
  220. .popup-title {
  221. font-size: 15px;
  222. color: #333333;
  223. padding: 10px;
  224. margin-bottom: 10px;
  225. position: relative;
  226. text-align: center;
  227. border-bottom: 1rpx solid #eee;
  228. text:first-child {
  229. font-weight: 600;
  230. }
  231. text:last-child {
  232. position: absolute;
  233. right: 10px;
  234. font-size: 21px;
  235. color: #999999;
  236. line-height: 23px;
  237. }
  238. }
  239. .quickOptions {
  240. padding: 0 42rpx;
  241. box-sizing: border-box;
  242. margin-bottom: 37rpx;
  243. .title {
  244. font-size: 30rpx;
  245. color: #333;
  246. margin-bottom: 20rpx;
  247. }
  248. .tab {
  249. padding: 7rpx 20rpx;
  250. box-sizing: border-box;
  251. font-size: 26rpx;
  252. color: #666;
  253. border-radius: 4rpx 4rpx 4rpx 4rpx;
  254. border: 1rpx solid #EEEEEE;
  255. margin-right: 30rpx;
  256. }
  257. .tabactive {
  258. background: rgba(45, 109, 255, 0.1);
  259. color: #2D6DFF;
  260. border: 1rpx solid #fff;
  261. }
  262. }
  263. .popup-footer {
  264. margin: 10px 15px;
  265. text {
  266. font-size: 16px;
  267. padding: 5px 0;
  268. color: #FFFFFF;
  269. display: inline-block;
  270. width: 45%;
  271. text-align: center;
  272. }
  273. text:first-child {
  274. color: #2D6DFF;
  275. background: rgba(45, 109, 255, 0.1);
  276. border-radius: 5px 5px 5px 5px;
  277. }
  278. text:last-child {
  279. background: linear-gradient(132deg, #2DD9FF 0%, #2D6DFF 100%);
  280. border-radius: 5px 5px 5px 5px;
  281. }
  282. }
  283. .popup-data {
  284. line-height: 25px;
  285. text {
  286. font-size: 14px;
  287. color: #999999;
  288. display: inline-block;
  289. width: 45%;
  290. text-align: center;
  291. border: 1px solid #DDDDDD;
  292. border-radius: 2px 2px 2px 2px;
  293. }
  294. .active {
  295. border: 1px solid #2D6DFF;
  296. color: #2D6DFF;
  297. }
  298. }
  299. .picker-view {
  300. width: 100%;
  301. height: 150px;
  302. }
  303. </style>