calendar.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <view class="calendar-content" v-if="showCalendar">
  3. <text class="example-info">日历组件可以查看日期,选择任意范围内的日期,打点操作。常用场景如:酒店日期预订、火车机票选择购买日期、上下班打卡等。</text>
  4. <uni-section title="插入模式" type="line"></uni-section>
  5. <view>
  6. <!-- 插入模式 -->
  7. <uni-calendar class="uni-calendar--hook" :selected="info.selected" :showMonth="false" @change="change" @monthSwitch="monthSwitch" />
  8. </view>
  9. <uni-section class="hideOnPc" title="弹出模式" type="line"></uni-section>
  10. <view class="example-body hideOnPc">
  11. <button class="calendar-button" type="button" @click="open">打开日历</button>
  12. </view>
  13. <uni-calendar ref="calendar" class="uni-calendar--hook" :clear-date="true" :date="info.date" :insert="info.insert" :lunar="info.lunar" :startDate="info.startDate"
  14. :endDate="info.endDate" :range="info.range" @confirm="confirm" @close="close"/>
  15. </view>
  16. </template>
  17. <script>
  18. /**
  19. * 获取任意时间
  20. */
  21. function getDate(date, AddDayCount = 0) {
  22. if (!date) {
  23. date = new Date()
  24. }
  25. if (typeof date !== 'object') {
  26. date = date.replace(/-/g, '/')
  27. }
  28. const dd = new Date(date)
  29. dd.setDate(dd.getDate() + AddDayCount) // 获取AddDayCount天后的日期
  30. const y = dd.getFullYear()
  31. const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获取当前月份的日期,不足10补0
  32. const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号,不足10补0
  33. return {
  34. fullDate: y + '-' + m + '-' + d,
  35. year: y,
  36. month: m,
  37. date: d,
  38. day: dd.getDay()
  39. }
  40. }
  41. export default {
  42. components: {},
  43. data() {
  44. return {
  45. showCalendar: false,
  46. info: {
  47. lunar: true,
  48. range: true,
  49. insert: false,
  50. selected: []
  51. }
  52. }
  53. },
  54. onReady() {
  55. this.$nextTick(() => {
  56. this.showCalendar = true
  57. })
  58. // TODO 模拟请求异步同步数据
  59. setTimeout(() => {
  60. this.info.date = getDate(new Date(),-30).fullDate
  61. this.info.startDate = getDate(new Date(),-60).fullDate
  62. this.info.endDate = getDate(new Date(),30).fullDate
  63. this.info.selected = [{
  64. date: getDate(new Date(),-3).fullDate,
  65. info: '打卡'
  66. },
  67. {
  68. date: getDate(new Date(),-2).fullDate,
  69. info: '签到',
  70. data: {
  71. custom: '自定义信息',
  72. name: '自定义消息头'
  73. }
  74. },
  75. {
  76. date: getDate(new Date(),-1).fullDate,
  77. info: '已打卡'
  78. }
  79. ]
  80. }, 2000)
  81. },
  82. methods: {
  83. open() {
  84. this.$refs.calendar.open()
  85. },
  86. close(){
  87. console.log('弹窗关闭');
  88. },
  89. change(e) {
  90. console.log('change 返回:', e)
  91. // 模拟动态打卡
  92. if (this.info.selected.length > 5) return
  93. this.info.selected.push({
  94. date: e.fulldate,
  95. info: '打卡'
  96. })
  97. },
  98. confirm(e) {
  99. console.log('confirm 返回:', e)
  100. },
  101. monthSwitch(e) {
  102. console.log('monthSwitchs 返回:', e)
  103. }
  104. }
  105. }
  106. </script>
  107. <style lang="scss">
  108. .example-body {
  109. /* #ifndef APP-NVUE */
  110. display: flex;
  111. /* #endif */
  112. flex-direction: row;
  113. }
  114. .calendar-button {
  115. flex: 1;
  116. font-weight: bold;
  117. font-size: 32rpx;
  118. }
  119. </style>