SelDatePicker.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <template>
  2. <a-range-picker
  3. v-model="momentDate"
  4. :size="'large'"
  5. :locale="locale"
  6. :show-time="{
  7. hideDisabledOptions: true,
  8. defaultValue: [moment('00:00:00', 'HH:mm:ss'), moment('23:59:59', 'HH:mm:ss')],
  9. }"
  10. format="YYYY-MM-DD"
  11. @ok="confirmDate"
  12. @change="changeRange"
  13. >
  14. <template slot="dateRender" slot-scope="current">
  15. <div :id="current.format('YYYY.MM.DD')" class="ant-calendar-date">
  16. <div
  17. class="more-select"
  18. :class="[
  19. nowDates.find(item=>item === current.format('YYYY.MM.DD'))?'is-rang-picker-active':'',
  20. ]"
  21. @click.stop="setSelect(current)"
  22. >
  23. <span><i v-if="nowDates.find(item=>item === current.format('YYYY.MM.DD'))" class="el-icon-check" /></span>
  24. </div>
  25. <template v-if="moment().format('YYYY.MM.DD') !== current.format('YYYY.MM.DD')">{{ current.date() }}</template>
  26. <template v-else><div class="rang-today">今天</div></template>
  27. <div v-if="holiday.find(item=>item === current.format('YYYY.MM.DD'))" class="is-rang-picker-holiday">休</div>
  28. </div>
  29. </template>
  30. </a-range-picker>
  31. </template>
  32. <script>
  33. const _ = require('lodash')
  34. import moment from 'moment'
  35. import 'moment/locale/zh-hk'
  36. moment.locale('zh-hk')
  37. import locale from 'ant-design-vue/es/date-picker/locale/zh_CN'
  38. import { getHolidayDayInfo } from '@/api/projectViewDetails'
  39. export default {
  40. props: {
  41. startEnd: {
  42. type: Array,
  43. default: () => [],
  44. required: false
  45. },
  46. detailDayList: {
  47. type: Array,
  48. default: () => [],
  49. required: false
  50. }
  51. },
  52. data() {
  53. return {
  54. moment,
  55. locale,
  56. nowDates: [],
  57. momentDate: [],
  58. holiday: []
  59. }
  60. },
  61. watch: {
  62. startEnd: {
  63. handler(newName, oldName) {
  64. newName.length > 0 ? this.momentDate = [moment(newName[0]), moment(newName[1])] : this.momentDate = []
  65. },
  66. immediate: true,
  67. deep: true
  68. },
  69. detailDayList: {
  70. handler(newV, oldV) {
  71. this.nowDates = newV
  72. },
  73. immediate: true,
  74. deep: true
  75. }
  76. },
  77. created() {
  78. const startTime = moment().startOf('year').format('YYYY.MM.DD')
  79. const endTime = moment().endOf('year').format('YYYY.MM.DD')
  80. this.getHolidayDayInfo(startTime, endTime)
  81. },
  82. methods: {
  83. async getHolidayDayInfo(startTime, endTime) { // 获取节假日时间
  84. const res = await getHolidayDayInfo({ startTime: startTime, endTime: endTime })
  85. if (res.code === 200) {
  86. this.holiday = res.data
  87. }
  88. },
  89. changeRange(dates) { // 获取开始结束日期
  90. this.nowDates = []
  91. const [start, end] = dates
  92. this.nowDates = this.setHoliday('holiday', start, end)
  93. },
  94. setHoliday(type, start, end) {
  95. const NewArr = []
  96. let nextDate = _.cloneDeep(start)
  97. while (nextDate && nextDate.isBefore(end)) {
  98. if (type === 'holiday') {
  99. const isEx = this.holiday.find(item => item === nextDate.format('YYYY.MM.DD'))
  100. if (!isEx) {
  101. NewArr.push(nextDate.format('YYYY.MM.DD'))
  102. }
  103. } else {
  104. if (nextDate.weekday() < 6 && nextDate.weekday() > 0) { // 默认周六,末不在已选数组中
  105. NewArr.push(nextDate.format('YYYY.MM.DD'))
  106. }
  107. }
  108. nextDate = nextDate.add(1, 'day')
  109. }
  110. return NewArr
  111. },
  112. setSelect(current) { // 选择详细日期
  113. const isExist = this.nowDates.find(item => item === current.format('YYYY.MM.DD'))
  114. if (isExist) {
  115. this.nowDates = this.nowDates.filter(item => item !== isExist)
  116. } else {
  117. this.nowDates.push(current.format('YYYY.MM.DD'))
  118. }
  119. },
  120. confirmDate(e) {
  121. const min = moment.min(this.nowDates.map(item => {
  122. return moment(item)
  123. }))
  124. const max = moment.max(this.nowDates.map(item => {
  125. return moment(item)
  126. }))
  127. this.$emit('update:startEnd', [min.format('YYYY.MM.DD'), max.format('YYYY.MM.DD')])
  128. this.$emit('getDetailDay', this.nowDates)
  129. }
  130. }
  131. }
  132. </script>
  133. <style lang="scss">
  134. .ant-calendar-picker-input {
  135. input {
  136. cursor: pointer;
  137. }
  138. }
  139. .ant-calendar-time-picker-btn {
  140. display: none !important;
  141. }
  142. .ant-calendar-picker-container {
  143. z-index: 9999;
  144. }
  145. .ant-calendar-cell {
  146. height: 40px;
  147. .ant-calendar-date {
  148. position: relative;
  149. }
  150. .is-rang-picker-holiday {
  151. color: #409EFF;
  152. position: absolute;
  153. bottom: -15px;
  154. font-size: 10px;
  155. transform: translate(-50%,0);
  156. left: 5px;
  157. transform: scale(0.8);
  158. }
  159. .rang-today {
  160. word-break: keep-all;
  161. font-size: 10px;
  162. color: #409EFF;
  163. position: absolute;
  164. left: 50%;
  165. top: 50%;
  166. transform: translate(-50%,-50%);
  167. }
  168. }
  169. .ant-calendar-today .ant-calendar-date {
  170. border: none
  171. }
  172. .ant-calendar-range .ant-calendar-in-range-cell::before{
  173. top: 2px;
  174. bottom: 2px;
  175. }
  176. .ant-calendar-selected-start-date,.ant-calendar-selected-end-date{
  177. position: relative;
  178. }
  179. .ant-calendar-selected-start-date::before,.ant-calendar-selected-end-date::before{
  180. position: absolute;
  181. top: 2px;
  182. right: 0;
  183. bottom: 2px;
  184. left: 0;
  185. display: block;
  186. background: #e6f7ff;
  187. border: 0;
  188. border-radius: 0;
  189. content: '';
  190. }
  191. .more-select {
  192. display: none;
  193. }
  194. // .ant-calendar-range .ant-calendar-selected-start-date .ant-calendar-date, .ant-calendar-range .ant-calendar-selected-end-date .ant-calendar-date{
  195. // position: relative;
  196. // background-color: #e6f7ff;
  197. // color: rgba(0, 0, 0, 0.65);
  198. // }
  199. .ant-calendar-in-range-cell,.ant-calendar-selected-start-date,.ant-calendar-selected-end-date{
  200. .ant-calendar-date {
  201. position: relative;
  202. background-color: #e6f7ff;
  203. color: rgba(0, 0, 0, 0.65);
  204. .more-select {
  205. display: block;
  206. position: absolute;
  207. top: -5px;
  208. right: -5px;
  209. height: 10px;
  210. width: 10px;
  211. border: 1px solid #BFBFBF;
  212. border-radius: 2px;
  213. }
  214. }
  215. }
  216. .is-rang-picker-active {
  217. span {
  218. display: block;
  219. height: 100%;
  220. width: 100%;
  221. background-color: #409EFF;
  222. font-size: 10px;
  223. position: relative;
  224. color: #FFFFFF;
  225. }
  226. i {
  227. position: absolute;
  228. left: -2px;
  229. top: 50%;
  230. transform: translateY(-50%);
  231. }
  232. }
  233. </style>