dateSelect.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <template>
  2. <view class="select-bg_bx">
  3. <view v-if="isShowTit" @click="switchCut" :class="['select-title',dropdownShow?'select_corlor':'']">
  4. <text>{{defaultDateName}}</text>
  5. <text v-if="placeholder" style="color: #ccc;">{{placeholder}}</text>
  6. <i v-if="isShowMore" class="iconfont iconsanjiao_xia"></i>
  7. </view>
  8. <view v-else @click="switchCut" :class="['select-title',dropdownShow?'select_corlor':'']">
  9. <text v-if="placeholder" style="color: #ccc;">{{placeholder}}</text>
  10. </view>
  11. <view class="select-mark-view" v-show="dropdownShow" @click="switchCut"></view>
  12. <view v-if="dropdownShow" :style="{top}" class="select-fixed_bx">
  13. <slot>
  14. <view class="select-item-box">
  15. <view class="title">选日期</view>
  16. <view class="item-child_box" >
  17. <view class="admin-button-com mini-btn default custom" @click="showDatePicker(2)">{{startDate || '选择日期'}}</view>
  18. </view>
  19. </view>
  20. <view class="select-item-box" v-if="isShowMonth">
  21. <view class="title">选月份</view>
  22. <picker mode="date" :value="date" :start="startDate" :end="endDate" fields="month" @change="bindMonthChange" >
  23. <view class="item-child_box">
  24. <view class="admin-button-com mini-btn default custom" >{{showMonth||'选择月份'}}</view>
  25. </view>
  26. </picker>
  27. </view>
  28. <view class="select-item-box">
  29. <view class="title">快捷选项</view>
  30. <view class="item-child_box">
  31. <button
  32. v-for="(item,index) in dateOptions"
  33. :key="index"
  34. @click="changeDateOption(item)"
  35. :class="['admin-button-com','mini-btn',selectItemId === item.id?'blue':'default']">
  36. {{item.name}}
  37. </button>
  38. </view>
  39. </view>
  40. <view class="select-item-box">
  41. <view class="title">选时段</view>
  42. <view class="item-child_box" >
  43. <view class="admin-button-com mini-btn default custom" @click="showDatePicker(0)">{{startDate || '选择开始日期'}}</view>
  44. <view class="admin-button-com mini-btn default custom" @click="showDatePicker(1)">{{endDate || '选择结束日期'}}</view>
  45. <view class="admin-button-com mini-btn blue custom" @click="customDateConfirm()">确认</view>
  46. </view>
  47. </view>
  48. </slot>
  49. </view>
  50. <mx-date-picker :show="showPicker" format="yyyy-mm-dd" type="date" :value="defaultPickerDate" :show-tips="true" @confirm="confirmPicker" @cancel="cancelPicker" />
  51. </view>
  52. </template>
  53. <script>
  54. import DropdownList from "@/components/plugin/dropdown-list";
  55. import MxDatePicker from '@/components/mx-datepicker/mx-datepicker.vue';
  56. import {mapGetters} from "vuex";
  57. export default {
  58. name:'dateSelect',
  59. components:{
  60. DropdownList,MxDatePicker
  61. },
  62. props:{
  63. top:{
  64. type: String,
  65. default: 'auto'
  66. },
  67. selectItemId:{
  68. type:[String,Number],
  69. default:''
  70. },
  71. isShowTit:{
  72. type: Boolean,
  73. default: true
  74. },
  75. isShowMore:{
  76. type: Boolean,
  77. default: true
  78. },
  79. placeholder: {
  80. type: String,
  81. default: ''
  82. },
  83. defaultShowName:{
  84. type: String,
  85. default: '日期'
  86. },
  87. isShowMonth: {
  88. type: Boolean,
  89. default: true
  90. },
  91. customDateOption: {
  92. type: Array,
  93. default: () => []
  94. },
  95. maxDays: {
  96. type: Number,
  97. default: 0
  98. }
  99. },
  100. data(){
  101. return {
  102. defaultPickerDate:'',
  103. showPicker: false,
  104. dropdownShow: false,
  105. selectItem:'',
  106. startDate:'',
  107. endDate:'',
  108. dateIndex:0,
  109. defaultDateName:'今天',
  110. showMonth:null
  111. }
  112. },
  113. watch:{
  114. defaultShowName: {
  115. handler(newVal) {
  116. this.defaultDateName = newVal
  117. },
  118. immediate:true
  119. }
  120. },
  121. computed:{
  122. ...mapGetters(["getDictionariesInfo"]),
  123. dateOptions() {
  124. return this.customDateOption && this.customDateOption.length > 0 ? this.customDateOption : (this.getDictionariesInfo.dateDefaultOption || []);
  125. }
  126. },
  127. mounted(){
  128. let nowDate = new Date();
  129. let currentDate = nowDate.Format("yyyy-MM-dd")
  130. this.defaultPickerDate = currentDate
  131. },
  132. onLoad(){
  133. },
  134. methods:{
  135. bindMonthChange(e){
  136. this.dropdownShow = !this.dropdownShow
  137. this.dropdownShow = false;
  138. let currentMonth = e.target.value
  139. this.defaultDateName = currentMonth
  140. this.showMonth = currentMonth
  141. this.$emit('selectDateFn',{searchTime:'byMonth',startTime:currentMonth})
  142. },
  143. showDatePicker(index){//显示
  144. this.showPicker = true;
  145. this.dateIndex = index
  146. },
  147. confirmPicker(e) {//选择
  148. this.showPicker = false;
  149. if(this.dateIndex == 0){
  150. this.startDate = e.value
  151. }else if(this.dateIndex == 1){
  152. this.endDate = e.value
  153. }else if(this.dateIndex == 2){
  154. this.startDate = e.value
  155. this.endDate = e.value
  156. this.customDateConfirm()
  157. }else{
  158. this.endDate = e.value
  159. }
  160. },
  161. customDateConfirm() {
  162. if(this.$util.isEmpty(this.startDate)){
  163. this.$msg('请选择开始日期')
  164. return false
  165. }
  166. if(this.$util.isEmpty(this.endDate)){
  167. this.$msg('请选择结束日期')
  168. return false
  169. }
  170. let start = new Date(this.startDate.replace(/-/g, '/')).getTime();
  171. let end = new Date(this.endDate.replace(/-/g, '/')).getTime();
  172. if (end < start) {
  173. this.$msg('结束日期不能小于开始日期')
  174. return false
  175. }
  176. if (this.maxDays > 0) {
  177. let days = (end - start) / (1000 * 60 * 60 * 24);
  178. if (days > this.maxDays - 1) {
  179. this.$msg(`最多只能选择${this.maxDays}天`)
  180. return false
  181. }
  182. }
  183. this.dropdownShow = false
  184. this.defaultDateName = this.startDate == this.endDate ? this.startDate.substring(5) : this.startDate.substring(5) + '/' + this.endDate.substring(5)
  185. this.$emit('selectDateFn',{searchTime:'custom',startTime:this.startDate,endTime:this.endDate})
  186. },
  187. cancelPicker(){
  188. this.showPicker = false;
  189. },
  190. switchCut(){
  191. this.dropdownShow = !this.dropdownShow
  192. },
  193. changeDateOption(val){
  194. this.dropdownShow = false;
  195. this.defaultDateName = val.name;
  196. this.$emit('selectDateFn',{searchTime:val.value})
  197. }
  198. }
  199. }
  200. </script>
  201. <style lang="scss" scoped>
  202. .select-bg_bx {
  203. .select-title {
  204. color: #666666;
  205. font-size: 26upx;
  206. & .iconsanjiao_xia {
  207. margin-left: 10upx;
  208. }
  209. &.select_corlor {
  210. color: #3385FF;
  211. & .iconsanjiao_xia {
  212. transform: rotate(180deg);
  213. }
  214. }
  215. }
  216. .select-mark-view {
  217. height: 100vh;
  218. height: 100%;
  219. position: fixed;
  220. width: 100%;
  221. background-color: rgba(0, 0, 0, 0.5);
  222. left: 0;
  223. top: 0upx;
  224. z-index: 20;
  225. }
  226. & .select-fixed_bx {
  227. position: fixed;
  228. width: 100%;
  229. background-color: #FFFFFF;
  230. left: 0;
  231. z-index: 29;
  232. padding:50upx 30upx 40upx;
  233. border-radius: 0upx 0upx 20upx 20upx;
  234. transition: all 0.5s ease-in-out;
  235. & .select-item-box {
  236. & .title {
  237. font-size: 24upx;
  238. color: #333333;
  239. font-weight: 600;
  240. text-align: left;
  241. }
  242. & .item-child_box {
  243. padding: 20upx 0 0;
  244. align-items: center;
  245. & .admin-button-com {
  246. margin-bottom: 20upx;
  247. padding: 25upx 50upx;
  248. margin-right: 20upx;
  249. &.custom {
  250. width: 100%;
  251. }
  252. }
  253. }
  254. }
  255. }
  256. }
  257. </style>