index.js 9.8 KB

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