index.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. import DateTimePicker from '../dateTimePicker/index.vue';
  2. import DateUtil from '../dateTimePicker/dateUtil';
  3. import { DATE_TYPES } from '../dateTimePicker/constant';
  4. export default {
  5. components: {
  6. DateTimePicker
  7. },
  8. data() {
  9. return {
  10. showStartDatePicker: false,
  11. showEndDatePicker: false,
  12. startDate: '',
  13. endDate: '',
  14. activeDate: 'startDate' ,// 正在处理哪一个日期值,startDate/endDate
  15. dateList:[{
  16. name:'当日',
  17. type:'day'
  18. },
  19. {
  20. name:'本月',
  21. type:'month'
  22. },
  23. {
  24. name:'本年',
  25. type:'year'
  26. }],
  27. currentIndex:null,
  28. isYear:false
  29. };
  30. },
  31. props: {
  32. // 日期筛选模式,1:年月日(默认),2:年月,3:年,4:年月日时分秒,5:时分秒,6:时分
  33. mode: {
  34. type: Number,
  35. default: DATE_TYPES.YMD
  36. },
  37. isYear:{
  38. type:Boolean,
  39. default:false
  40. },
  41. // 默认开始日期
  42. defaultStartDate: {
  43. type: String,
  44. default: ''
  45. },
  46. // 默认结束日期
  47. defaultEndDate: {
  48. type: String,
  49. default: ''
  50. },
  51. // 可选的最小日期
  52. minDate: {
  53. type: String,
  54. default: ''
  55. },
  56. // 可选的最大日期
  57. maxDate: {
  58. type: String,
  59. default: ''
  60. }
  61. },
  62. watch: {
  63. mode() {
  64. // 筛选模式更换时清空一下数据
  65. this.resetData();
  66. },
  67. startDate() {
  68. this.$emit('onChange', {
  69. startDate: this.startDate,
  70. endDate: this.endDate
  71. });
  72. },
  73. endDate() {
  74. this.$emit('onChange', {
  75. startDate: this.startDate,
  76. endDate: this.endDate
  77. });
  78. },
  79. defaultStartDate: {
  80. handler(defaultStartDate) {
  81. if (!defaultStartDate) {
  82. return;
  83. }
  84. if (this.mode == DATE_TYPES.HMS || this.mode == DATE_TYPES.HM) {
  85. console.error('时分秒/时分模式不支持设置默认开始时间');
  86. return;
  87. }
  88. if (DateUtil.isBefore(defaultStartDate, this.minDate)) {
  89. console.warn(
  90. `默认开始日期不可小于最小可选日期,已把默认开始日期设为最小可选日期。默认开始日期:${defaultStartDate},最小可选日期:${this.minDate}`
  91. );
  92. this.startDate = this.getModeFormatDateString(this.minDate);
  93. } else {
  94. this.startDate = this.getModeFormatDateString(defaultStartDate);
  95. }
  96. },
  97. immediate: true
  98. },
  99. defaultEndDate: {
  100. handler(defaultEndDate) {
  101. if (!defaultEndDate) {
  102. return;
  103. }
  104. if (this.mode == DATE_TYPES.HMS || this.mode == DATE_TYPES.HM) {
  105. console.error('时分秒/时分模式不支持设置默认结束时间');
  106. return;
  107. }
  108. if (DateUtil.isAfter(defaultEndDate, this.maxDate)) {
  109. console.warn(
  110. `默认结束日期不可大于最大可选日期,已把默认结束日期设为最大可选日期。默认结束日期:${defaultEndDate},最大可选日期:${this.maxDate}`
  111. );
  112. this.endDate = this.getModeFormatDateString(this.maxDate);
  113. } else {
  114. this.endDate = this.getModeFormatDateString(defaultEndDate);
  115. }
  116. },
  117. immediate: true
  118. },
  119. minDate(val) {
  120. if ((val && this.mode == DATE_TYPES.HMS) || this.mode == DATE_TYPES.HM) {
  121. console.error('时分秒/时分模式不支持设置最小可选时间');
  122. return;
  123. }
  124. },
  125. maxDate(val) {
  126. if ((val && this.mode == DATE_TYPES.HMS) || this.mode == DATE_TYPES.HM) {
  127. console.error('时分秒/时分模式不支持设置最大可选时间');
  128. return;
  129. }
  130. }
  131. },
  132. methods: {
  133. selectYear(item,index){
  134. this.currentIndex = index
  135. //今日
  136. if(item.type == 'day'){
  137. // 获取当前时间
  138. const now = new Date();
  139. // 获取今日的起始时间(00:00:00)
  140. const startOfDay = new Date(now);
  141. startOfDay.setHours(0, 0, 0, 0); // 设置为 00:00:00.000
  142. // 获取今日的结束时间(23:59:59)
  143. const endOfDay = new Date(now);
  144. endOfDay.setHours(23, 59, 59, 999); // 设置为 23:59:59.999
  145. // 格式化输出
  146. const formatDate = (date) => {
  147. const year = date.getFullYear();
  148. const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从 0 开始,需要 +1
  149. const day = String(date.getDate()).padStart(2, '0');
  150. const hours = String(date.getHours()).padStart(2, '0');
  151. const minutes = String(date.getMinutes()).padStart(2, '0');
  152. const seconds = String(date.getSeconds()).padStart(2, '0');
  153. return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  154. };
  155. console.log("今日起始时间:", formatDate(startOfDay)); // 例如:2023-10-05 00:00:00
  156. console.log("今日结束时间:", formatDate(endOfDay)); // 例如:2023-10-05 23:59:59
  157. this.startDate = formatDate(startOfDay)
  158. this.endDate = formatDate(endOfDay)
  159. }
  160. //本月
  161. else if(item.type == 'month'){
  162. // 获取当前时间
  163. const now = new Date();
  164. // 获取本月的第一天(00:00:00)
  165. const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1, 0, 0, 0, 0);
  166. // 获取本月的最后一天(23:59:59)
  167. const endOfMonth = new Date(now.getFullYear(), now.getMonth() + 1, 0, 23, 59, 59, 999);
  168. // 格式化日期为 YYYY-MM-DD HH:mm:ss
  169. const formatDate = (date) => {
  170. const year = date.getFullYear();
  171. const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从 0 开始,需要 +1
  172. const day = String(date.getDate()).padStart(2, '0');
  173. const hours = String(date.getHours()).padStart(2, '0');
  174. const minutes = String(date.getMinutes()).padStart(2, '0');
  175. const seconds = String(date.getSeconds()).padStart(2, '0');
  176. return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  177. };
  178. console.log("本月起始时间:", formatDate(startOfMonth)); // 例如:2023-10-01 00:00:00
  179. console.log("本月结束时间:", formatDate(endOfMonth)); // 例如:2023-10-31 23:59:59
  180. this.startDate = formatDate(startOfMonth)
  181. this.endDate = formatDate(endOfMonth)
  182. }
  183. //今年
  184. else if(item.type == 'year'){
  185. // 获取当前时间
  186. const now = new Date();
  187. // 获取今年的第一天(00:00:00)
  188. const startOfYear = new Date(now.getFullYear(), 0, 1, 0, 0, 0, 0); // 月份从 0 开始,0 表示 1 月
  189. // 获取今年的最后一天(23:59:59)
  190. const endOfYear = new Date(now.getFullYear(), 11, 31, 23, 59, 59, 999); // 11 表示 12 月
  191. // 格式化日期为 YYYY-MM-DD HH:mm:ss
  192. const formatDate = (date) => {
  193. const year = date.getFullYear();
  194. const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从 0 开始,需要 +1
  195. const day = String(date.getDate()).padStart(2, '0');
  196. const hours = String(date.getHours()).padStart(2, '0');
  197. const minutes = String(date.getMinutes()).padStart(2, '0');
  198. const seconds = String(date.getSeconds()).padStart(2, '0');
  199. return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  200. };
  201. console.log("今年起始时间:", formatDate(startOfYear)); // 例如:2023-01-01 00:00:00
  202. console.log("今年结束时间:", formatDate(endOfYear)); // 例如:2023-12-31 23:59:59
  203. this.startDate = formatDate(startOfYear)
  204. this.endDate = formatDate(endOfYear)
  205. }
  206. },
  207. open(){
  208. this.$refs.popup.open()
  209. },
  210. onTapStartDate() {
  211. this.showEndDatePicker = false;
  212. if (!this.startDate) {
  213. this.startDate = this.getModeFormatDateString(new Date());
  214. }
  215. this.activeDate = 'startDate';
  216. this.showStartDatePicker = true;
  217. },
  218. onTapEndDate() {
  219. this.showStartDatePicker = false;
  220. if (!this.endDate) {
  221. this.endDate = this.startDate;
  222. }
  223. this.activeDate = 'endDate';
  224. this.showEndDatePicker = true;
  225. },
  226. onChangeStartDate(date) {
  227. this.startDate = date;
  228. },
  229. onChangeEndDate(date) {
  230. this.endDate = date;
  231. },
  232. validateInput() {
  233. if (!this.startDate) {
  234. uni.showToast({
  235. title: '请选择开始时间',
  236. icon: 'none'
  237. });
  238. return false;
  239. } else if (!this.endDate) {
  240. uni.showToast({
  241. title: '请选择结束时间',
  242. icon: 'none'
  243. });
  244. return false;
  245. } else if (DateUtil.isAfter(this.startDate, this.endDate)) {
  246. uni.showToast({
  247. title: '结束时间不能小于开始时间',
  248. icon: 'none'
  249. });
  250. return false;
  251. }
  252. return true;
  253. },
  254. onCancel() {
  255. this.resetData();
  256. },
  257. onConfirm() {
  258. if (this.validateInput()) {
  259. this.$emit('onSubmit', {
  260. startDate: this.startDate,
  261. endDate: this.endDate
  262. });
  263. this.showStartDatePicker = false;
  264. this.showEndDatePicker = false;
  265. this.$refs.popup.close()
  266. }
  267. },
  268. resetData() {
  269. this.startDate = '';
  270. this.endDate = '';
  271. this.activeDate = 'startDate';
  272. this.showStartDatePicker = false;
  273. this.$refs.popup.close()
  274. this.showEndDatePicker = false;
  275. this.$refs.popup.close()
  276. },
  277. // 返回对应日期模式的时间字符串
  278. getModeFormatDateString(date) {
  279. let fmt = 'YYYY-MM-DD';
  280. switch (this.mode) {
  281. case DATE_TYPES.YM:
  282. fmt = 'YYYY-MM';
  283. break;
  284. case DATE_TYPES.Y:
  285. fmt = 'YYYY';
  286. break;
  287. case DATE_TYPES['YMD-HMS']:
  288. fmt = 'YYYY-MM-DD HH:mm:ss';
  289. break;
  290. case DATE_TYPES.HMS:
  291. fmt = 'HH:mm:ss';
  292. break;
  293. case DATE_TYPES.HM:
  294. fmt = 'HH:mm';
  295. break;
  296. default:
  297. break;
  298. }
  299. return DateUtil.formatDate(date, fmt);
  300. }
  301. }
  302. };