app-time-screening.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <template>
  2. <view @touchmove.stop.prevent="" class="time-bg cross-center">
  3. <view class="time-dialog">
  4. <view class="dialog-title">筛选时间</view>
  5. <view class="flex-wrap main-between time-area">
  6. <view @click="change(item.id)"
  7. :style="{'color': choose==item.id ? theme.color : '#666','border-color':choose==item.id ? theme.color : '#ddd'}"
  8. v-for="item in timeType" :key="item.id" class="dialog-choose-item time-area-item">{{item.name}}</view>
  9. <view class="dialog-choose-item" style='border: 0'></view>
  10. </view>
  11. <view class="choose-time" v-if="custom">
  12. <view class="time-title">起始时间</view>
  13. <view class="year-1">年</view>
  14. <view class="month-1">月</view>
  15. <view class="day-1">日</view>
  16. <picker-view :value="start" :indicator-style="indicatorStyle" class="picker-view" @change="startChange">
  17. <picker-view-column v-for="(date,i) in startList" :key="i">
  18. <view v-for="(item,idx) in date" :key="item"
  19. :style="{ color: startVal[i] == idx ? theme.color : startVal[i] == idx + 1 || startVal[i] == idx - 1 ? '#999999' : startVal[i] == idx + 2 || startVal[i] == idx - 2 ? '#cdcdcd': '', lineHeight: lineHeight }">{{item}}</view>
  20. </picker-view-column>
  21. </picker-view>
  22. <view class="time-title">结束时间</view>
  23. <view class="year-2">年</view>
  24. <view class="month-2">月</view>
  25. <view class="day-2">日</view>
  26. <picker-view :value="end" :indicator-style="indicatorStyle" class="picker-view" @change="endChange">
  27. <picker-view-column v-for="(date,i) in endList" :key="i">
  28. <view v-for="(item,idx) in date" :key="item"
  29. :style="{ color: endVal[i] == idx ? theme.color : endVal[i] == idx + 1 || endVal[i] == idx - 1 ? '#999999' : endVal[i] == idx + 2 || endVal[i] == idx - 2 ? '#cdcdcd': '', lineHeight: lineHeight }">{{item}}</view>
  30. </picker-view-column>
  31. </picker-view>
  32. </view>
  33. <view class="main-center btn-area">
  34. <view class="submit-btn" @click='cancel' style="color: #666;">取消</view>
  35. <view class="line"></view>
  36. <view class="submit-btn" :style="{'color': theme.color}" @click='click'>确认</view>
  37. </view>
  38. </view>
  39. </view>
  40. </template>
  41. <script>
  42. const date = new Date()
  43. const years = []
  44. const months = [1,2,3,4,5,6,7,8,9,10,11,12]
  45. const bigDays = []
  46. const smallDays = []
  47. const secDays = []
  48. const otherDays = []
  49. for (let i = 2017; i <= date.getFullYear(); i++) {
  50. years.push(i)
  51. }
  52. for (let i = 1; i <= 31; i++) {
  53. bigDays.push(i)
  54. }
  55. for (let i = 1; i <= 30; i++) {
  56. smallDays.push(i)
  57. }
  58. for (let i = 1; i <= 28; i++) {
  59. secDays.push(i)
  60. }
  61. for (let i = 1; i <= 29; i++) {
  62. otherDays.push(i)
  63. }
  64. export default {
  65. name: "app-time-screening",
  66. props: {
  67. startDate: String,
  68. endDate: String,
  69. time: {
  70. type: Number,
  71. default() {
  72. return 0;
  73. }
  74. },
  75. theme: Object,
  76. },
  77. data() {
  78. return {
  79. timeType: [
  80. {id:0, name: '汇总'},
  81. {id:1, name: '今日'},
  82. {id:2, name: '昨日'},
  83. {id:3, name: '7日'},
  84. {id:4, name: '自定义'},
  85. ],
  86. lineHeight: '72rpx',
  87. indicatorStyle: '',
  88. choose: 0,
  89. today: '',
  90. yesterday: '',
  91. weekday: '',
  92. date_end: '',
  93. date_start: '',
  94. years: years,
  95. months: months,
  96. startDays: bigDays,
  97. endDays: bigDays,
  98. bigDays: bigDays,
  99. smallDays: smallDays,
  100. secDays: secDays,
  101. otherDays: otherDays,
  102. start: [],
  103. end: [],
  104. startVal: [],
  105. endVal: [],
  106. custom: false,
  107. startList: [],
  108. endList: [],
  109. }
  110. },
  111. methods: {
  112. click() {
  113. let that = this;
  114. if (that.choose != 4) {
  115. that.date_start = '';
  116. that.date_end = that.today + ' 23:59:59';
  117. switch (that.choose.toString()) {
  118. case '0':
  119. that.date_start = '';
  120. that.date_end = '';
  121. break;
  122. case '1':
  123. that.date_start = that.today;
  124. break;
  125. case '2':
  126. that.date_start = that.yesterday;
  127. that.date_end = that.yesterday + ' 23:59:59';
  128. break;
  129. case '3':
  130. that.date_start = that.weekday;
  131. break;
  132. }
  133. that.$emit('click', {date_start: that.date_start,date_end: that.date_end,choose:that.choose});
  134. } else {
  135. let endTime = that.date_end.substring(0, 10)
  136. let startTime = that.date_start.substring(0, 10)
  137. let end = endTime.split("-");
  138. let start = startTime.split("-");
  139. if (+end[0] < +start[0]) {
  140. uni.showToast({
  141. title: '结束时间不应早于开始时间',
  142. icon: 'none',
  143. duration: 1000
  144. })
  145. } else if (+end[0] == +start[0]) {
  146. if (+end[1] < +start[1]) {
  147. uni.showToast({
  148. title: '结束时间不应早于开始时间',
  149. icon: 'none',
  150. duration: 1000
  151. })
  152. } else if (+end[1] == +start[1]) {
  153. if (+end[2] < +start[2]) {
  154. uni.showToast({
  155. title: '结束时间不应早于开始时间',
  156. icon: 'none',
  157. duration: 1000
  158. })
  159. }else {
  160. that.$emit('click', {date_start: that.date_start,date_end: that.date_end,choose:that.choose});
  161. }
  162. }else {
  163. that.$emit('click', {date_start: that.date_start,date_end: that.date_end,choose:that.choose});
  164. }
  165. }else {
  166. that.$emit('click', {date_start: that.date_start,date_end: that.date_end,choose:that.choose});
  167. }
  168. }
  169. },
  170. cancel() {
  171. this.$emit('cancel');
  172. },
  173. change(e) {
  174. let that = this;
  175. that.choose = e;
  176. that.custom = false;
  177. if (that.choose == 4) {
  178. that.custom = true;
  179. that.date_end = that.today;
  180. that.date_start = that.today + ' 00:00:00';
  181. }
  182. },
  183. startChange: function(e) {
  184. const val = e.detail.value;
  185. this.startVal = e.detail.value;
  186. let years = this.years;
  187. let year = this.years[val[0]];
  188. let month = this.months[val[1]];
  189. if(month == 2) {
  190. if(year%4 == 0 && year %400 != 0) {
  191. this.startDays = this.otherDays
  192. }else {
  193. this.startDays = this.secDays
  194. }
  195. }else if((month < 8 && month%2 == 1) || (month > 7 && month%2 == 0)) {
  196. this.startDays = this.bigDays
  197. }else {
  198. this.startDays = this.smallDays
  199. }
  200. let day = this.startDays[val[2]];
  201. if (month >= 1 && month <= 9) {
  202. month = "0" + month;
  203. }
  204. if (day >= 1 && day <= 9) {
  205. day = "0" + day;
  206. }
  207. this.date_start = year + '-' + month + '-' + day;
  208. this.startList = [this.years,this.months,this.startDays];
  209. },
  210. endChange: function(e) {
  211. const val = e.detail.value;
  212. this.endVal = e.detail.value;
  213. let year = this.years[val[0]];
  214. let month = this.months[val[1]];
  215. if(month == 2) {
  216. if(year%4 == 0 && year %400 != 0) {
  217. this.endDays = this.otherDays
  218. }else {
  219. this.endDays = this.secDays
  220. }
  221. }else if((month < 8 && month%2 == 1) || (month > 7 && month%2 == 0)) {
  222. this.endDays = this.bigDays
  223. }else {
  224. this.endDays = this.smallDays
  225. }
  226. let day = this.endDays[val[2]];
  227. if (month >= 1 && month <= 9) {
  228. month = "0" + month;
  229. }
  230. if (day >= 1 && day <= 9) {
  231. day = "0" + day;
  232. }
  233. this.date_end = year + '-' + month + '-' + day + " 23:59:59";
  234. this.endList = [this.years,this.months,this.endDays];
  235. },
  236. },
  237. created() {
  238. let that = this;
  239. let screenWidth = uni.getSystemInfoSync().windowWidth;
  240. let p = 375 / screenWidth;
  241. this.lineHeight = 72*p + 'rpx'
  242. that.choose = that.time;
  243. if(that.choose == 4) {
  244. that.custom = true
  245. }
  246. that.date_start = that.startDate;
  247. that.date_end = that.endDate;
  248. that.indicatorStyle = 'height: 36px;font-size:14px;';
  249. var myDate = new Date();
  250. // 今天
  251. let year = myDate.getFullYear();
  252. let month = myDate.getMonth() + 1;
  253. if (month >= 1 && month <= 9) {
  254. month = "0" + month;
  255. }
  256. let now = myDate.getDate();
  257. that.today = year + "-" + month + "-" + now;
  258. let timestamp = Date.parse(new Date());
  259. // 昨天
  260. let yesterTime = (timestamp / 1000 - 24 * 60 * 60) * 1000;
  261. let yesterDate = new Date(yesterTime)
  262. let yester_year = yesterDate.getFullYear();
  263. let yester_month = yesterDate.getMonth() + 1;
  264. if (yester_month >= 1 && yester_month <= 9) {
  265. yester_month = "0" + yester_month;
  266. }
  267. let yester_now = yesterDate.getDate();
  268. that.yesterday = yester_year + "-" + yester_month + "-" + yester_now;
  269. // 7天
  270. let weekTime = (timestamp / 1000 - 24 * 60 * 60 * 7) * 1000;
  271. let weekDate = new Date(weekTime)
  272. let week_year = weekDate.getFullYear();
  273. let week_month = weekDate.getMonth() + 1;
  274. if (week_month >= 1 && week_month <= 9) {
  275. week_month = "0" + week_month;
  276. }
  277. let week_now = weekDate.getDate();
  278. that.weekday = week_year + "-" + week_month + "-" + week_now;
  279. that.start = [];
  280. that.end = [];
  281. let startDay = that.date_start ? that.date_start : that.today
  282. let endDay = that.date_end ? that.date_end : that.today;
  283. for(let i in that.years) {
  284. if (startDay.substring(0, 4) == that.years[i]) {
  285. that.start[0] = +i
  286. }
  287. }
  288. for(let i in that.months) {
  289. if (startDay.substring(5, 7) == that.months[i]) {
  290. that.start[1] = +i
  291. }
  292. }
  293. let startMonth = +that.start[1] + 1;
  294. if(startMonth == 2) {
  295. if(year%4 == 0 && year %400 != 0) {
  296. that.startDays = that.otherDays
  297. }else {
  298. that.startDays = that.secDays
  299. }
  300. }else if((startMonth < 8 && startMonth%2 == 1) || (startMonth > 7 && startMonth%2 == 0)) {
  301. that.startDays = that.bigDays
  302. }else {
  303. that.startDays = that.smallDays
  304. }
  305. for(let i in that.startDays) {
  306. if (startDay.substring(8, 10) == that.startDays[i]) {
  307. that.start[2] = +i
  308. }
  309. }
  310. that.startVal = that.start;
  311. if (that.date_end) {
  312. endDay = that.date_end
  313. }
  314. for(let i in that.years) {
  315. if (endDay.substring(0, 4) == that.years[i]) {
  316. that.end[0] = +i
  317. }
  318. }
  319. for(let i in that.months) {
  320. if (endDay.substring(5, 7) == that.months[i]) {
  321. that.end[1] = +i
  322. }
  323. }
  324. let endMonth = +that.end[1] + 1;
  325. if(endMonth == 2) {
  326. if(year%4 == 0 && year%400 != 0) {
  327. that.endDay = that.otherDays
  328. }else {
  329. that.endDay = that.secDays
  330. }
  331. }else if((endMonth < 8 && endMonth%2 == 1) || (endMonth > 7 && endMonth%2 == 0)) {
  332. that.endDays = that.bigDays
  333. }else {
  334. that.endDays = that.smallDays
  335. }
  336. for(let i in that.endDays) {
  337. if (endDay.substring(8, 10) == that.endDays[i]) {
  338. that.end[2] = +i
  339. }
  340. }
  341. that.endVal = that.end;
  342. that.startList = [that.years,that.months,that.startDays];
  343. that.endList = [that.years,that.months,that.endDays];
  344. },
  345. }
  346. </script>
  347. <style scoped lang="scss">
  348. .time-bg {
  349. background-color: rgba(0, 0, 0, 0.3);
  350. position: fixed;
  351. top: 0;
  352. left: 0;
  353. height: 100%;
  354. width: 100%;
  355. z-index: 100;
  356. .time-dialog {
  357. width: #{620rpx};
  358. border-radius: #{16rpx};
  359. margin: 0 auto;
  360. background-color: #fff;
  361. z-index: 20;
  362. .dialog-title {
  363. font-size: #{32rpx};
  364. color: #353535;
  365. width: #{620rpx};
  366. margin: #{32rpx} auto #{40rpx};
  367. text-align: center;
  368. }
  369. .time-area {
  370. margin-bottom: #{24rpx};
  371. padding: 0 #{32rpx};
  372. .dialog-choose-item {
  373. width: #{170rpx};
  374. height: #{68rpx};
  375. line-height: #{68rpx};
  376. text-align: center;
  377. border-radius: #{34rpx};
  378. border: #{2rpx} solid;
  379. font-size: #{28rpx};
  380. margin-bottom: #{16rpx};
  381. &.time-area-item {
  382. border-color: #ddd;
  383. color: #666;
  384. }
  385. }
  386. }
  387. .choose-time {
  388. position: relative;
  389. .time-title {
  390. margin-left: #{32rpx};
  391. color: #666;
  392. font-size: #{28rpx};
  393. margin-bottom: #{20rpx};
  394. }
  395. .year-1 {
  396. position: absolute;
  397. left: #{192rpx};
  398. top: #{146rpx};
  399. }
  400. .month-1 {
  401. position: absolute;
  402. left: #{380rpx};
  403. top: #{146rpx};
  404. }
  405. .day-1 {
  406. position: absolute;
  407. right: #{32rpx};
  408. top: #{146rpx};
  409. }
  410. .year-2 {
  411. position: absolute;
  412. left: #{192rpx};
  413. bottom: #{88rpx};
  414. }
  415. .month-2 {
  416. position: absolute;
  417. left: #{380rpx};
  418. bottom: #{88rpx};
  419. }
  420. .day-2 {
  421. position: absolute;
  422. right: #{32rpx};
  423. bottom: #{88rpx};
  424. }
  425. .picker-view {
  426. width: #{556rpx};
  427. height: #{216rpx};
  428. margin: 0 auto #{20rpx};
  429. text-align: center;
  430. view {
  431. line-height: #{72rpx};
  432. }
  433. }
  434. }
  435. .btn-area {
  436. height: #{88rpx};
  437. position: relative;
  438. border-top: #{2rpx} solid #e2e2e2;
  439. &.other-btn-area {
  440. margin-top: #{10rpx};
  441. }
  442. .line {
  443. height: #{32rpx};
  444. width: #{1rpx};
  445. background-color: #e2e2e2;
  446. position: absolute;
  447. top: #{28rpx};
  448. left: 0;
  449. right: 0;
  450. margin: 0 auto;
  451. }
  452. .submit-btn {
  453. height: #{88rpx};
  454. line-height: #{88rpx};
  455. font-size: #{32rpx};
  456. width: #{310rpx};
  457. text-align: center;
  458. }
  459. }
  460. }
  461. }
  462. </style>