dateTime.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <template>
  2. <view class="tui-datetime-picker">
  3. <view class="tui-mask" :class="{ 'tui-mask-show': isShow }" @touchmove.stop.prevent="stop" catchtouchmove="stop" @tap="hide"></view>
  4. <view class="tui-header" :class="{ 'tui-show': isShow }">
  5. <view class="tui-picker-header" @touchmove.stop.prevent="stop" catchtouchmove="stop">
  6. <view class="tui-btn-picker" :style="{ color: cancelColor }" hover-class="tui-opacity" :hover-stay-time="150"
  7. @click="hide">取消</view>
  8. <view class="tui-btn-picker" :style="{ color: color }" hover-class="tui-opacity" :hover-stay-time="150" @click="btnFix">确定</view>
  9. </view>
  10. <view class="tui-picker-body">
  11. <picker-view :value="value" @change="change" class="tui-picker-view">
  12. <picker-view-column v-if="!reset && type!=4">
  13. <view class="tui-column-item" v-for="(item,index) in years" :key="index">
  14. {{ item }}<text class="tui-text">年</text>
  15. </view>
  16. </picker-view-column>
  17. <picker-view-column v-if="!reset && type!=4">
  18. <view class="tui-column-item" v-for="(item,index) in months" :key="index">
  19. {{ formatNum(item)}}<text class="tui-text">月</text>
  20. </view>
  21. </picker-view-column>
  22. <picker-view-column v-if="!reset && (type==1 || type==2)">
  23. <view class="tui-column-item" v-for="(item,index) in days" :key="index">
  24. {{ formatNum(item) }}<text class="tui-text">日</text>
  25. </view>
  26. </picker-view-column>
  27. <picker-view-column v-if="!reset && (type==1 || type==4)">
  28. <view class="tui-column-item" v-for="(item,index) in hours" :key="index">
  29. {{ formatNum(item) }}<text class="tui-text">时</text>
  30. </view>
  31. </picker-view-column>
  32. <picker-view-column v-if="!reset && (type==1 || type==4)">
  33. <view class="tui-column-item" v-for="(item,index) in minutes" :key="index">
  34. {{ formatNum(item) }}<text class="tui-text">分</text>
  35. </view>
  36. </picker-view-column>
  37. </picker-view>
  38. </view>
  39. </view>
  40. </view>
  41. </template>
  42. <script>
  43. export default {
  44. name: "tuiDatetime",
  45. props: {
  46. //1-日期+时间(年月日+时分) 2-日期(年月日) 3-日期(年月) 4-时间(时分)
  47. type: {
  48. type: Number,
  49. default: 1
  50. },
  51. //年份区间
  52. startYear: {
  53. type: Number,
  54. default: 1980
  55. },
  56. //年份区间
  57. endYear: {
  58. type: Number,
  59. default: 2050
  60. },
  61. //"取消"字体颜色
  62. cancelColor: {
  63. type: String,
  64. default: "#888"
  65. },
  66. //"确定"字体颜色
  67. color: {
  68. type: String,
  69. default: "#C74547"
  70. },
  71. //设置默认显示日期 2019-08-01 || 2019-08-01 17:01 || 2019/08/01
  72. setDateTime: {
  73. type: String,
  74. default: ""
  75. }
  76. },
  77. data() {
  78. return {
  79. isShow: false,
  80. years: [],
  81. months: [],
  82. days: [],
  83. hours: [],
  84. minutes: [],
  85. year: 0,
  86. month: 0,
  87. day: 0,
  88. hour: 0,
  89. minute: 0,
  90. startDate: "",
  91. endDate: "",
  92. value: [0, 0, 0, 0, 0],
  93. reset: false
  94. };
  95. },
  96. mounted() {
  97. this.initData();
  98. },
  99. computed: {
  100. yearOrMonth() {
  101. return `${this.year}-${this.month}`
  102. },
  103. propsChange() {
  104. return `${this.setDateTime}-${this.type}-${this.startYear}-${this.endYear}`
  105. }
  106. },
  107. watch: {
  108. yearOrMonth() {
  109. this.setDays();
  110. },
  111. propsChange() {
  112. this.reset = true
  113. setTimeout(() => {
  114. this.initData()
  115. }, 10);
  116. }
  117. },
  118. methods: {
  119. stop() {},
  120. formatNum: function(num) {
  121. return num < 10 ? "0" + num : num + "";
  122. },
  123. generateArray: function(start, end) {
  124. return Array.from(new Array(end + 1).keys()).slice(start)
  125. },
  126. getIndex: function(arr, val) {
  127. let index = arr.indexOf(val);
  128. return ~index ? index : 0
  129. },
  130. //日期时间处理
  131. initSelectValue() {
  132. let fdate = this.setDateTime.replace(/\-/g, '/');
  133. fdate = fdate && fdate.indexOf("/") == -1 ? `2020/01/01 ${fdate}` : fdate
  134. let time = null;
  135. if (fdate)
  136. time = new Date(fdate);
  137. else
  138. time = new Date();
  139. this.year = time.getFullYear()
  140. this.month = time.getMonth() + 1;
  141. this.day = time.getDate();
  142. this.hour = time.getHours();
  143. this.minute = time.getMinutes();
  144. },
  145. initData() {
  146. this.initSelectValue()
  147. this.reset = false
  148. switch (this.type) {
  149. case 1:
  150. this.value = [0, 0, 0, 0, 0];
  151. this.setYears();
  152. this.setMonths();
  153. this.setDays();
  154. this.setHours();
  155. this.setMinutes();
  156. break;
  157. case 2:
  158. this.value = [0, 0, 0];
  159. this.setYears();
  160. this.setMonths();
  161. this.setDays();
  162. break;
  163. case 3:
  164. this.value = [0, 0];
  165. this.setYears();
  166. this.setMonths();
  167. break;
  168. case 4:
  169. this.value = [0, 0];
  170. this.setHours();
  171. this.setMinutes();
  172. break;
  173. default:
  174. break;
  175. }
  176. },
  177. setYears() {
  178. this.years = this.generateArray(this.startYear, this.endYear);
  179. setTimeout(()=> {
  180. this.$set(this.value, 0, this.getIndex(this.years, this.year));
  181. }, 8);
  182. },
  183. setMonths() {
  184. this.months = this.generateArray(1, 12);
  185. setTimeout(()=> {
  186. this.$set(this.value, 1, this.getIndex(this.months, this.month));
  187. }, 8);
  188. },
  189. setDays() {
  190. if (this.type == 3 || this.type == 4) return;
  191. let totalDays = new Date(this.year, this.month, 0).getDate();
  192. this.days = this.generateArray(1, totalDays);
  193. setTimeout(()=> {
  194. this.$set(this.value, 2, this.getIndex(this.days, this.day));
  195. }, 8);
  196. },
  197. setHours() {
  198. this.hours = this.generateArray(0, 23);
  199. setTimeout(()=> {
  200. this.$set(this.value, this.value.length - 2, this.getIndex(this.hours, this.hour));
  201. }, 8);
  202. },
  203. setMinutes() {
  204. this.minutes = this.generateArray(0, 59);
  205. setTimeout(()=> {
  206. this.$set(this.value, this.value.length - 1, this.getIndex(this.minutes, this.minute));
  207. }, 8);
  208. },
  209. show() {
  210. setTimeout(() => {
  211. this.isShow = true;
  212. }, 50);
  213. },
  214. hide() {
  215. this.isShow = false;
  216. },
  217. change(e) {
  218. this.value = e.detail.value;
  219. switch (this.type) {
  220. case 1:
  221. this.year = this.years[this.value[0]];
  222. this.month = this.months[this.value[1]];
  223. this.day = this.days[this.value[2]];
  224. this.hour = this.hours[this.value[3]];
  225. this.minute = this.minutes[this.value[4]];
  226. break;
  227. case 2:
  228. this.year = this.years[this.value[0]];
  229. this.month = this.months[this.value[1]];
  230. this.day = this.days[this.value[2]];
  231. break;
  232. case 3:
  233. this.year = this.years[this.value[0]];
  234. this.month = this.months[this.value[1]];
  235. break;
  236. case 4:
  237. this.hour = this.hours[this.value[0]];
  238. this.minute = this.minutes[this.value[1]];
  239. break;
  240. default:
  241. break;
  242. }
  243. },
  244. btnFix() {
  245. let result = {};
  246. let year = this.year;
  247. let month = this.formatNum(this.month || 0);
  248. let day = this.formatNum(this.day || 0);
  249. let hour = this.formatNum(this.hour || 0);
  250. let minute = this.formatNum(this.minute || 0);
  251. switch (this.type) {
  252. case 1:
  253. result = {
  254. year: year,
  255. month: month,
  256. day: day,
  257. hour: hour,
  258. minute: minute,
  259. result: `${year}-${month}-${day} ${hour}:${minute}`
  260. }
  261. break;
  262. case 2:
  263. result = {
  264. year: year,
  265. month: month,
  266. day: day,
  267. result: `${year}-${month}-${day}`
  268. }
  269. break;
  270. case 3:
  271. result = {
  272. year: year,
  273. month: month,
  274. result: `${year}-${month}`
  275. }
  276. break;
  277. case 4:
  278. result = {
  279. hour: hour,
  280. minute: minute,
  281. result: `${hour}:${minute}`
  282. }
  283. break;
  284. default:
  285. break;
  286. }
  287. this.$emit("confirm", result);
  288. this.hide();
  289. }
  290. }
  291. };
  292. </script>
  293. <style>
  294. .tui-datetime-picker {
  295. position: relative;
  296. z-index: 999;
  297. }
  298. .tui-picker-view {
  299. height: 100%;
  300. box-sizing: border-box;
  301. }
  302. .tui-mask {
  303. position: fixed;
  304. z-index: 9998;
  305. top: 0;
  306. right: 0;
  307. bottom: 0;
  308. left: 0;
  309. background-color: rgba(0, 0, 0, 0.6);
  310. visibility: hidden;
  311. opacity: 0;
  312. transition: all 0.3s ease-in-out;
  313. }
  314. .tui-mask-show {
  315. visibility: visible !important;
  316. opacity: 1 !important;
  317. }
  318. .tui-header {
  319. z-index: 9999;
  320. position: fixed;
  321. bottom: 0;
  322. left: 0;
  323. width: 100%;
  324. transition: all 0.3s ease-in-out;
  325. transform: translateY(100%);
  326. }
  327. .tui-show {
  328. transform: translateY(0);
  329. }
  330. .tui-picker-header {
  331. width: 100%;
  332. height: 90rpx;
  333. padding: 0 40rpx;
  334. display: flex;
  335. justify-content: space-between;
  336. align-items: center;
  337. box-sizing: border-box;
  338. font-size: 32rpx;
  339. background: #fff;
  340. position: relative;
  341. }
  342. .tui-picker-header::after {
  343. content: '';
  344. position: absolute;
  345. border-bottom: 1rpx solid #eaeef1;
  346. -webkit-transform: scaleY(0.5);
  347. transform: scaleY(0.5);
  348. bottom: 0;
  349. right: 0;
  350. left: 0;
  351. }
  352. .tui-picker-body {
  353. width: 100%;
  354. height: 500rpx;
  355. overflow: hidden;
  356. background-color: #fff;
  357. }
  358. .tui-column-item {
  359. display: flex;
  360. align-items: center;
  361. justify-content: center;
  362. font-size: 36rpx;
  363. color: #333;
  364. }
  365. .tui-text {
  366. font-size: 24rpx;
  367. padding-left: 8rpx;
  368. }
  369. .tui-btn-picker {
  370. padding: 16rpx;
  371. box-sizing: border-box;
  372. text-align: center;
  373. text-decoration: none;
  374. }
  375. .tui-opacity {
  376. opacity: 0.5;
  377. }
  378. </style>