index.js 9.7 KB

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