DateTimePicker.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <template>
  2. <view class="date-time-picker" v-if="visible">
  3. <view class="date-time-mask" @click.stop="hide"></view>
  4. <view class="date-time-container" @click.stop="handleEvent">
  5. <view class="time-picker-tool" v-if='isShowToolBar'>
  6. <view :class="[cancelButtonClass]" @click.stop="cancel">
  7. <text>{{cancelButtonText}}</text>
  8. </view>
  9. <view :class="[toolBarTitleClass]">
  10. <text>{{toolBarTitle}}</text>
  11. </view>
  12. <view :class="[confirmButtonClass]" @click.stop="confirm">
  13. <text>{{confirmButtonText}}</text>
  14. </view>
  15. </view>
  16. <picker-view class="picker-view" :indicator-style="indicatorStyleString" :value="dateTime" @change="dateTimePickerChange">
  17. <picker-view-column data-id='year' v-if='isShowYear'>
  18. <view class="item" v-for="(item,index) in years" :key="index">{{item}}年</view>
  19. </picker-view-column>
  20. <picker-view-column data-id='month' v-if='isShowMonth'>
  21. <view class="item" v-for="(item,index) in months" :key="index">{{item}}月</view>
  22. </picker-view-column>
  23. <picker-view-column data-id='day' v-if='isShowDay'>
  24. <view class="item" v-for="(item,index) in days" :key="index">{{item}}日</view>
  25. </picker-view-column>
  26. <picker-view-column data-id='hour' v-if='isShowHour'>
  27. <view class="item" v-for="(item,index) in hours" :key="index">{{item}}时</view>
  28. </picker-view-column>
  29. <picker-view-column data-id='minute' v-if='isShowMinute'>
  30. <view class="item" v-for="(item,index) in minutes" :key="index">{{item}}分</view>
  31. </picker-view-column>
  32. <picker-view-column data-id='second' v-if='isShowSecond'>
  33. <view class="item" v-for="(item,index) in seconds" :key="index">{{item}}秒</view>
  34. </picker-view-column>
  35. </picker-view>
  36. </view>
  37. </view>
  38. </template>
  39. <script>
  40. import {
  41. getOneMonthDays,
  42. getTimeArray,
  43. addZero,
  44. getIndexOfArray
  45. } from './uitls/util.js'
  46. export default {
  47. name: 'DateTimePicker',
  48. props: {
  49. startYear: {
  50. type: Number,
  51. default: 1900
  52. },
  53. endYear: {
  54. type: Number,
  55. default: new Date().getFullYear()
  56. },
  57. isShowToolBar: {
  58. type: Boolean,
  59. default: true
  60. },
  61. cancelButtonText: {
  62. type: String,
  63. default: '取消'
  64. },
  65. cancelButtonClass: {
  66. type: String,
  67. default: 'cancel-btn'
  68. },
  69. toolBarTitle: {
  70. type: String,
  71. default: '请选择'
  72. },
  73. toolBarTitleClass: {
  74. type: String,
  75. default: 'tool-title'
  76. },
  77. confirmButtonText: {
  78. type: String,
  79. default: '确定'
  80. },
  81. confirmButtonClass: {
  82. type: String,
  83. default: 'confirm-btn'
  84. },
  85. datestring: {
  86. type: String,
  87. default: ''
  88. },
  89. type: {
  90. /**
  91. * date 年月日
  92. * year-month 年月
  93. * year 年
  94. * datetime 年月日 时分
  95. * datetime-all 年月日 时分秒
  96. * time 时分秒
  97. * hour-minute 时分
  98. */
  99. type: String,
  100. default: 'date'
  101. },
  102. indicatorStyle: {
  103. type: Object,
  104. default: null
  105. }
  106. },
  107. data() {
  108. return {
  109. visible: false,
  110. dateTime: [],
  111. days: [],
  112. indicatorStyleString: ''
  113. }
  114. },
  115. watch: {
  116. indicatorStyle(val){
  117. this.getIndicatorStyle();
  118. },
  119. type() {
  120. this.initDateTime()
  121. },
  122. datestring(){
  123. this.initDateTime()
  124. }
  125. },
  126. computed: {
  127. years() {
  128. return this.initTimeData(this.endYear, this.startYear);
  129. },
  130. isShowYear() {
  131. return this.type !== 'time' && this.type !== 'hour-minute';
  132. },
  133. months() {
  134. return this.initTimeData(12, 1);
  135. },
  136. isShowMonth() {
  137. return this.type !== 'year' && this.type !== 'time' && this.type !== 'hour-minute';
  138. },
  139. isShowDay() {
  140. return this.type === 'date' || this.type === 'datetime' || this.type === 'datetime-all';
  141. },
  142. hours() {
  143. return this.initTimeData(23, 0);
  144. },
  145. isShowHour() {
  146. return this.type !== 'date' && this.type !== 'year-month' && this.type !== 'year';
  147. },
  148. minutes() {
  149. return this.initTimeData(59, 0);
  150. },
  151. isShowMinute() {
  152. return this.type !== 'date' && this.type !== 'year-month' && this.type !== 'year';
  153. },
  154. seconds() {
  155. return this.initTimeData(59, 0);
  156. },
  157. isShowSecond() {
  158. return this.type === 'datetime-all' || this.type === 'time';
  159. }
  160. },
  161. methods: {
  162. getIndicatorStyle(){
  163. if(this.indicatorStyle){
  164. for(let key in this.indicatorStyle){
  165. this.indicatorStyleString += `${key}:${this.indicatorStyle[key]};`
  166. }
  167. }
  168. },
  169. handleEvent() {
  170. return;
  171. },
  172. cancel() {
  173. this.hide();
  174. },
  175. confirm() {
  176. this.formatDate();
  177. this.hide();
  178. },
  179. show() {
  180. this.visible = true;
  181. },
  182. hide() {
  183. this.visible = false;
  184. },
  185. initDateTime() {
  186. let value;
  187. if (this.datestring.length > 0) {
  188. if (this.type === 'year') {
  189. value = new Date(this.datestring, 0);
  190. } else if (this.type === 'time' || this.type === 'hour-minute') {
  191. let date = new Date();
  192. let ary = this.datestring.split(':');
  193. ary.forEach((item, index) => {
  194. if (index == 0) {
  195. date.setHours(item)
  196. } else if (index == 1) {
  197. date.setMinutes(item)
  198. } else if (index == 2) {
  199. date.setSeconds(item)
  200. }
  201. })
  202. value = date;
  203. } else {
  204. value = new Date(this.datestring.replace(/-/g, '/'));
  205. }
  206. } else {
  207. value = new Date();
  208. }
  209. let len, timeArray, index;
  210. let array = getTimeArray(value);
  211. let [year, month, day, hour, minute, second] = array;
  212. this.days = this.initTimeData(getOneMonthDays(year, month), 1);
  213. let names = ['year', 'month', 'day', 'hour', 'minute', 'second'];
  214. switch (this.type) {
  215. case "date":
  216. len = 3;
  217. break;
  218. case "year-month":
  219. len = 2;
  220. break;
  221. case "year":
  222. len = 1;
  223. break;
  224. case "datetime":
  225. len = 5;
  226. break;
  227. case "datetime-all":
  228. len = 6;
  229. break;
  230. case "time":
  231. len = 3;
  232. break;
  233. case "hour-minute":
  234. len = 2;
  235. break;
  236. }
  237. timeArray = new Array(len).fill(0);
  238. if (this.type === 'time' || this.type === 'hour-minute') {
  239. names = names.slice(3);
  240. array = array.slice(3);
  241. }
  242. timeArray = timeArray.map((item, index) => {
  243. const name = names[index];
  244. return getIndexOfArray(array[index], this[name + 's'])
  245. })
  246. this.dateTime = timeArray;
  247. },
  248. initTimeData(end, start) {
  249. let timeArray = [];
  250. while (start <= end) {
  251. timeArray.push(start);
  252. start++;
  253. }
  254. return timeArray;
  255. },
  256. formatDate() {
  257. let names = ['year', 'month', 'day', 'hour', 'minute', 'second'];
  258. let dateString, formatDateArray = [];
  259. if (this.type === 'date' || this.type === 'year-month' || this.type === 'year') {
  260. formatDateArray = this.dateTime.map((item, index) => {
  261. return this[names[index] + 's'][item] < 10 ? addZero(this[names[index] + 's'][item]) : this[names[index] + 's'][item];
  262. })
  263. dateString = formatDateArray.join('-');
  264. } else if (this.type === 'time' || this.type === 'hour-minute') {
  265. names = names.splice(3);
  266. formatDateArray = this.dateTime.map((item, index) => {
  267. return this[names[index] + 's'][item] < 10 ? addZero(this[names[index] + 's'][item]) : this[names[index] + 's'][item];
  268. })
  269. dateString = formatDateArray.join(':');
  270. } else {
  271. let name1 = names.splice(0, 3);
  272. formatDateArray = this.dateTime.map((item, index) => {
  273. if (index > 2) {
  274. return this[names[index - 3] + 's'][item] < 10 ? addZero(this[names[index - 3] + 's'][item]) : this[names[index - 3] + 's'][item];
  275. } else {
  276. return this[name1[index] + 's'][item] < 10 ? addZero(this[name1[index] + 's'][item]) : this[name1[index] + 's'][item];
  277. }
  278. })
  279. dateString = formatDateArray.splice(0, 3).join('-') + ' ' + formatDateArray.join(':');
  280. }
  281. this.$emit('change', dateString)
  282. },
  283. dateTimePickerChange(e) {
  284. let columns = e.target.value;
  285. if (this.type === 'date' || this.type === 'datetime' || this.type === 'datetime-all') {
  286. this.dateTime.splice(0, 1, columns[0]);
  287. if (columns[0] != this.dateTime[0]) {
  288. this.days = this.initTimeData(getOneMonthDays(this.years[this.dateTime[0]], this.months[this.dateTime[1]]), 1);
  289. if (this.dateTime[1] == 1) {
  290. if (this.dateTime[2] === this.days.length - 1) {
  291. if (getOneMonthDays(this.years[columns[0]], this.dateTime[1]) < getOneMonthDays(this.years[this.dateTime[0]],this.dateTime[1])) {
  292. this.dateTime.splice(2, 1, this.days.length - 1)
  293. }
  294. }
  295. }
  296. } else {
  297. this.dateTime.splice(1, 1, columns[1]);
  298. this.days = this.initTimeData(getOneMonthDays(this.years[this.dateTime[0]], this.dateTime[1]), 1);
  299. if (columns[1] != this.dateTime[1]) {
  300. if (this.dateTime[1] == 1) {
  301. if (this.dateTime[2] === this.days.length - 1) {
  302. if (getOneMonthDays(this.years[columns[0]], this.dateTime[1]) < getOneMonthDays(this.years[this.dateTime[0]],
  303. this.dateTime[1])) {
  304. this.dateTime.splice(2, 1, this.days.length - 1)
  305. }
  306. }
  307. } else {
  308. if (this.dateTime[2] > this.days.length - 1) {
  309. this.dateTime.splice(2, 1, this.days.length - 1)
  310. } else {
  311. this.dateTime.splice(2, 1, columns[2])
  312. }
  313. }
  314. } else {
  315. this.dateTime.splice(2, 1, columns[2])
  316. }
  317. }
  318. if (columns.length > 2) {
  319. columns.splice(3).forEach((column, index) => {
  320. this.dateTime.splice(index + 3, 1, column);
  321. })
  322. }
  323. } else {
  324. columns.forEach((column, index) => {
  325. this.dateTime.splice(index, 1, column);
  326. })
  327. }
  328. if (!this.isShowToolBar) {
  329. this.formatDate();
  330. }
  331. },
  332. },
  333. mounted() {
  334. this.getIndicatorStyle();
  335. this.initDateTime();
  336. }
  337. }
  338. </script>
  339. <style lang='scss' scoped>
  340. .date-time-picker {
  341. .date-time-mask {
  342. position: fixed;
  343. top: 0;
  344. bottom: 0;
  345. left: 0;
  346. right: 0;
  347. background-color: rgba($color: #000000, $alpha: .5);
  348. z-index: 998;
  349. }
  350. .date-time-container {
  351. position: fixed;
  352. height: 50%;
  353. bottom: 0;
  354. right: 0;
  355. left: 0;
  356. background-color: #f6f6f6;
  357. z-index: 1000;
  358. display: flex;
  359. flex-direction: column;
  360. .time-picker-tool {
  361. height: 100rpx;
  362. display: flex;
  363. align-items: center;
  364. justify-content: space-between;
  365. font-size: 28rpx;
  366. .cancel-btn {
  367. padding: 0 28rpx;
  368. box-sizing: border-box;
  369. color: #969799;
  370. }
  371. .tool-title {
  372. font-weight: 500;
  373. font-size: 16px;
  374. max-width: 50%;
  375. overflow: hidden;
  376. white-space: nowrap;
  377. text-overflow: ellipsis;
  378. }
  379. .confirm-btn {
  380. padding: 0 28rpx;
  381. box-sizing: border-box;
  382. color: #E62129;
  383. }
  384. }
  385. .picker-view {
  386. width: 100%;
  387. flex: 1;
  388. .item {
  389. font-size: 34rpx;
  390. display: flex;
  391. align-items: center;
  392. justify-content: center;
  393. }
  394. }
  395. }
  396. }
  397. </style>