1
0

app-time-screening.vue 20 KB

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