dataTimePicke.vue 11 KB

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