timeChose.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <template>
  2. <view>
  3. <uni-popup ref="popcash" type="bottom" @change="popChange">
  4. <view class="pop_phone pop-container">
  5. <view class="popup-title">
  6. <view class="u-popup-cancel-btn" @click="closepop">取消</view>
  7. <view class="u-title">选择时间</view>
  8. <view class="u-popup-sure-btn" @click="handleSelectSure">确定</view>
  9. </view>
  10. <view class="m-select-time">
  11. <view @click="timeChose(0)" class="u-time-label" :style="{ color: timeIndex == 0 ? '#f00' : '#666' }">{{ startTimeDisplay }}</view>
  12. <view @click="timeChose(1)" class="u-time-label" :style="{ color: timeIndex == 1 ? '#f00' : '#666' }">{{ endTimeDisplay }}</view>
  13. </view>
  14. <view class="picker-height">
  15. <picker-view v-if="visible" class="mpvue-picker-view" :indicator-style="indicatorStyle" :value.async="curData" @change="bindChange">
  16. <picker-view-column>
  17. <view class="item" v-for="(item, index) in years" :key="index">{{ item }}年</view>
  18. </picker-view-column>
  19. <picker-view-column>
  20. <view class="item" v-for="(item, index) in months" :key="index">{{ item }}月</view>
  21. </picker-view-column>
  22. <picker-view-column>
  23. <view class="item" v-for="(item, index) in days" :key="index">{{ item }}日</view>
  24. </picker-view-column>
  25. </picker-view>
  26. </view>
  27. </view>
  28. </uni-popup>
  29. </view>
  30. </template>
  31. <script>
  32. //import uniPopup from './uni-popup/uni-popup.vue';
  33. export default {
  34. /* components: {
  35. uniPopup
  36. }, */
  37. props: {
  38. isShow: {
  39. type: Boolean,
  40. default: true
  41. }
  42. },
  43. data() {
  44. const currentDate = this.getDate({
  45. format: true
  46. });
  47. const date = new Date();
  48. const years = [];
  49. const year = date.getFullYear();
  50. const months = [];
  51. const month = date.getMonth() + 1;
  52. const days = [];
  53. const day = date.getDate();
  54. for (let i = 1990; i <= date.getFullYear(); i++) {
  55. years.push(i);
  56. }
  57. for (let i = 1; i <= 12; i++) {
  58. months.push(i);
  59. }
  60. for (let i = 1; i <= 31; i++) {
  61. days.push(i);
  62. }
  63. return {
  64. startTime: '',
  65. endTime: '',
  66. startTimeDisplay: '开始时间',
  67. endTimeDisplay: '结束时间',
  68. timeSelectActive: 1,
  69. currentDate: '',
  70. date: currentDate,
  71. timeIndex: 0,
  72. years,
  73. year,
  74. months,
  75. month,
  76. days,
  77. day,
  78. curData: [9999, month - 1, day - 1],
  79. visible: false,
  80. indicatorStyle: `height: ${Math.round(uni.getSystemInfoSync().screenWidth / (750 / 100))}px;`
  81. };
  82. },
  83. watch: {
  84. isShow(val) {
  85. console.log('---val-----', val);
  86. this.$refs.popcash.open();
  87. }
  88. },
  89. mounted() {
  90. this.startTimeDisplay = this.getTodayTime();
  91. this.endTimeDisplay = this.getTodayTime();
  92. // this.startDate = this.getDate('start');
  93. // this.endDate = this.getDate('end');
  94. this.$refs.popcash.open();
  95. this.visible = true;
  96. this.timeChose(0);
  97. },
  98. computed: {
  99. },
  100. methods: {
  101. //今天的时间
  102. getTodayTime() {
  103. var day2 = new Date();
  104. day2.setTime(day2.getTime());
  105. var time = day2.getFullYear() + '-' + this.check0((day2.getMonth() + 1)) + '-' + this.check0(day2.getDate());
  106. return time;
  107. },
  108. check0(num){
  109. num = +num;
  110. return num > 9 ? num : ('0'+num);
  111. },
  112. bindChange(e) {
  113. const val = e.detail.value;
  114. this.curData = val;
  115. this.year = this.years[val[0]];
  116. this.month = this.months[val[1]];
  117. this.day = this.days[val[2]];
  118. //将选择的年月日变为number形式,便于比较之用
  119. var y = parseInt(this.year);
  120. var m = parseInt(this.month);
  121. var d = parseInt(this.day);
  122. //选择不同月份显示的天数不同
  123. if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
  124. if (this.days.length != 31) {
  125. this.days = [];
  126. for (let i = 1; i <= 31; i++) {
  127. this.days.push(i);
  128. }
  129. }
  130. } else if (m == 4 || m == 6 || m == 9 || m == 11) {
  131. if (this.days.length != 30) {
  132. this.days = [];
  133. for (let i = 1; i <= 30; i++) {
  134. this.days.push(i);
  135. }
  136. }
  137. } else if (m == 2) {
  138. if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) {
  139. //闰年
  140. if (this.days.length != 29) {
  141. this.days = [];
  142. for (let i = 1; i <= 29; i++) {
  143. this.days.push(i);
  144. }
  145. }
  146. } else {
  147. //平年
  148. if (this.days.length != 28) {
  149. this.days = [];
  150. for (let i = 1; i <= 28; i++) {
  151. this.days.push(i);
  152. }
  153. }
  154. }
  155. }
  156. //处理选择今年的情况
  157. if (y == this.currentYear) {
  158. //最多显示到当前月份
  159. if (this.months.length != this.currentMonth) {
  160. this.months = [];
  161. for (let i = 1; i <= this.currentMonth; i++) {
  162. this.months.push(i);
  163. }
  164. }
  165. //如果选择的是当前月份,那么日最多显示到今天
  166. if (m == this.currentMonth) {
  167. if (this.days.length != this.currentDay) {
  168. this.days = [];
  169. for (let i = 1; i <= this.currentDay; i++) {
  170. this.days.push(i);
  171. }
  172. }
  173. }
  174. } else {
  175. this.months = [];
  176. for (let i = 1; i <= 12; i++) {
  177. this.months.push(i);
  178. }
  179. }
  180. //我的业务中是选择的孩子日期,根据选择的日期可以计算出孩子几岁了😄
  181. if (y >= this.currentYear) {
  182. this.babyAge = 0;
  183. } else {
  184. //选择的月份大于当前月份
  185. if (m > this.currentMonth) {
  186. this.babyAge = this.currentYear - y - 1;
  187. } else if (m == this.currentMonth) {
  188. if (d > this.currentDay) {
  189. this.babyAge = this.currentYear - y - 1;
  190. } else {
  191. this.babyAge = this.currentYear - y;
  192. }
  193. } else {
  194. this.babyAge = this.currentYear - y;
  195. }
  196. }
  197. this.month = this.check0(this.month);
  198. this.day = this.check0(this.day);
  199. if (this.timeIndex == 0) {
  200. this.startTimeDisplay = this.year.toString() + '-' + this.month.toString() + '-' + this.day.toString();
  201. } else {
  202. this.endTimeDisplay = this.year.toString() + '-' + this.month.toString() + '-' + this.day.toString()
  203. }
  204. console.log('当前选中' + this.year.toString() + '-' + this.month.toString() + '-' + this.day.toString());
  205. },
  206. timeChose(index) {
  207. var that = this;
  208. this.timeIndex = index;
  209. // this.curData = [0,0,0];
  210. var date = index === 0 ? this.startTimeDisplay : this.endTimeDisplay;
  211. that.curData = that.getCurIndex(date);
  212. console.log(this.curData)
  213. },
  214. getCurIndex(date) {
  215. var years = +date.split('-')[0];
  216. var months = +date.split('-')[1];
  217. var days = +date.split('-')[2];
  218. return [this.years.indexOf(years), this.months.indexOf(months), this.days.indexOf(days)]
  219. },
  220. popChange(e) {
  221. console.log('----popChange---', e);
  222. /* if (!e.show) {
  223. var obj = {
  224. isclose: true
  225. };
  226. this.$emit('returnDate', obj);
  227. } */
  228. },
  229. closepop() {
  230. this.$refs.popcash.close();
  231. },
  232. dateMinus(date1, date2) {
  233. var sdate = new Date(date1.replace(/-/g, "/"));
  234. var now = new Date(date2.replace(/-/g, "/"));
  235. var days = now.getTime() - sdate.getTime();
  236. var day = parseInt(days / (1000 * 60 * 60 * 24));
  237. return day;
  238. },
  239. startDateChange: function(e) {
  240. this.timeIndex = 0;
  241. this.startTimeDisplay = e.target.value;
  242. },
  243. endDateChange: function(e) {
  244. this.timeIndex = 1;
  245. this.endTimeDisplay = e.target.value;
  246. },
  247. getDate(type) {
  248. const date = new Date();
  249. let year = date.getFullYear();
  250. let month = date.getMonth() + 1;
  251. let day = date.getDate();
  252. if (type === 'start') {
  253. year = year - 50;
  254. } else if (type === 'end') {
  255. year = year + 2;
  256. }
  257. month = month > 9 ? month : '0' + month;
  258. day = day > 9 ? day : '0' + day;
  259. return `${year}-${month}-${day}`;
  260. },
  261. handleSetActive(active) {
  262. this.timeSelectActive = active;
  263. let time;
  264. if (active === 1) {
  265. time = this.startTimeDisplay.split('-');
  266. } else {
  267. time = this.endTimeDisplay.split('-');
  268. }
  269. this.currentDate = new Date(time[0], +time[1] - 1, time[2]);
  270. },
  271. timeSelectInput(evt) {
  272. if (this.timeSelectActive == 1) {
  273. this.startTimeDisplay = evt.getValues().join('-');
  274. } else if (this.timeSelectActive == 2) {
  275. this.endTimeDisplay = evt.getValues().join('-');
  276. }
  277. },
  278. handleSelectSure() {
  279. console.log("选择时间")
  280. if (this.startTimeDisplay == '开始时间') {
  281. uni.showToast({
  282. title: '请选择开始时间',
  283. icon: 'none'
  284. });
  285. return;
  286. }
  287. if (this.endTimeDisplay == '结束时间') {
  288. uni.showToast({
  289. title: '请选择结束时间',
  290. icon: 'none'
  291. });
  292. return;
  293. }
  294. var start = this.startTimeDisplay.split('-');
  295. var end = this.endTimeDisplay.split('-');
  296. var totalDay = 0;
  297. if (new Date(start[0], +start[1] - 1, start[2]) > new Date(end[0], +end[1] - 1, end[2])) {
  298. this.startTime = this.endTimeDisplay;
  299. this.endTime = this.startTimeDisplay;
  300. totalDay = this.dateMinus(this.endTimeDisplay, this.startTimeDisplay);
  301. } else {
  302. this.startTime = this.startTimeDisplay;
  303. this.endTime = this.endTimeDisplay;
  304. totalDay = this.dateMinus(this.startTimeDisplay, this.endTimeDisplay);
  305. }
  306. console.log(this.startTime, this.endTime)
  307. // if (+totalDay > 31) {
  308. // uni.showToast({
  309. // title: '最多可查询31天内的数据',
  310. // icon: 'none'
  311. // });
  312. // return;
  313. // }
  314. var obj = {
  315. startTime: this.startTime,
  316. endTime: this.endTime,
  317. isclose: false
  318. };
  319. this.$emit('returnDate', obj);
  320. this.$refs.popcash.close();
  321. }
  322. }
  323. };
  324. </script>
  325. <style scoped lang="scss">
  326. .pop-container {
  327. height: 700upx;
  328. background: #fff;
  329. }
  330. .picker-height {
  331. height: 400upx;
  332. }
  333. .popup-title {
  334. height: 90rpx;
  335. display: flex;
  336. justify-content: space-between;
  337. align-items: center;
  338. }
  339. .u-title {
  340. font-size: 30rpx;
  341. }
  342. .u-popup-cancel-btn {
  343. color: #999;
  344. padding: 0 30rpx;
  345. height: 90rpx;
  346. line-height: 90rpx;
  347. }
  348. .u-popup-sure-btn {
  349. color: #007aff;
  350. padding: 0 30rpx;
  351. height: 90rpx;
  352. line-height: 90rpx;
  353. }
  354. .m-select-time {
  355. height: 160rpx;
  356. display: flex;
  357. justify-content: center;
  358. align-items: center;
  359. font-size: 34rpx;
  360. }
  361. .u-time-label {
  362. color: #ccc;
  363. width: 220rpx;
  364. height: 60rpx;
  365. line-height: 60rpx;
  366. border-bottom: 1px solid #ccc;
  367. text-align: center;
  368. margin: 0 40rpx;
  369. }
  370. .mpvue-picker-view {
  371. width: 100%;
  372. height: 100%;
  373. // height: 280upx;
  374. background-color: rgba(255, 255, 255, 1);
  375. .item {
  376. text-align: center;
  377. width: 100%;
  378. height: 100upx;
  379. line-height: 100upx;
  380. text-overflow: ellipsis;
  381. white-space: nowrap;
  382. font-size: 30upx;
  383. }
  384. }
  385. </style>