calendar.nvue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 title="弹出模式" type="line"></uni-section>
  10. <view class="example-body">
  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" :endDate="info.endDate" :range="info.range" @confirm="confirm" @close="close" />
  14. </view>
  15. </template>
  16. <script>
  17. /**
  18. * 获取任意时间
  19. */
  20. function getDate(date, AddDayCount = 0) {
  21. if (!date) {
  22. date = new Date()
  23. }
  24. if (typeof date !== 'object') {
  25. date = date.replace(/-/g, '/')
  26. }
  27. const dd = new Date(date)
  28. dd.setDate(dd.getDate() + AddDayCount) // 获取AddDayCount天后的日期
  29. const y = dd.getFullYear()
  30. const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获取当前月份的日期,不足10补0
  31. const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号,不足10补0
  32. return {
  33. fullDate: y + '-' + m + '-' + d,
  34. year: y,
  35. month: m,
  36. date: d,
  37. day: dd.getDay()
  38. }
  39. }
  40. export default {
  41. components: {},
  42. data() {
  43. return {
  44. showCalendar: false,
  45. info: {
  46. lunar: true,
  47. range: true,
  48. insert: false,
  49. selected: []
  50. }
  51. }
  52. },
  53. onReady() {
  54. this.$nextTick(() => {
  55. this.showCalendar = true
  56. })
  57. // TODO 模拟请求异步同步数据
  58. setTimeout(() => {
  59. this.info.date = getDate(new Date(), -30).fullDate
  60. this.info.startDate = getDate(new Date(), -60).fullDate
  61. this.info.endDate = getDate(new Date(), 30).fullDate
  62. this.info.selected = [{
  63. date: getDate(new Date(), -3).fullDate,
  64. info: '打卡'
  65. },
  66. {
  67. date: getDate(new Date(), -2).fullDate,
  68. info: '签到',
  69. data: {
  70. custom: '自定义信息',
  71. name: '自定义消息头'
  72. }
  73. },
  74. {
  75. date: getDate(new Date(), -1).fullDate,
  76. info: '已打卡'
  77. }
  78. ]
  79. }, 2000)
  80. },
  81. methods: {
  82. open() {
  83. this.$refs.calendar.open()
  84. },
  85. close() {
  86. console.log('弹窗关闭');
  87. },
  88. change(e) {
  89. console.log('change 返回:', e)
  90. // 模拟动态打卡
  91. if (this.info.selected.length > 5) return
  92. this.info.selected.push({
  93. date: e.fulldate,
  94. info: '打卡'
  95. })
  96. },
  97. confirm(e) {
  98. console.log('confirm 返回:', e)
  99. },
  100. monthSwitch(e) {
  101. console.log('monthSwitchs 返回:', e)
  102. }
  103. }
  104. }
  105. </script>
  106. <style>
  107. @charset "UTF-8";
  108. /* 头条小程序组件内不能引入字体 */
  109. /* #ifdef MP-TOUTIAO */
  110. @font-face {
  111. font-family: uniicons;
  112. font-weight: normal;
  113. font-style: normal;
  114. src: url("~@/static/uni.ttf") format("truetype");
  115. }
  116. /* #endif */
  117. /* #ifndef APP-NVUE */
  118. page {
  119. display: flex;
  120. flex-direction: column;
  121. box-sizing: border-box;
  122. background-color: #efeff4;
  123. min-height: 100%;
  124. height: auto;
  125. }
  126. view {
  127. font-size: 14px;
  128. line-height: inherit;
  129. }
  130. .example {
  131. padding: 0 15px 15px;
  132. }
  133. .example-info {
  134. padding: 15px;
  135. color: #3b4144;
  136. background: #ffffff;
  137. }
  138. .example-body {
  139. /* #ifndef APP-NVUE */
  140. display: flex;
  141. /* #endif */
  142. flex-direction: row;
  143. flex-wrap: wrap;
  144. justify-content: center;
  145. padding: 0;
  146. font-size: 14px;
  147. background-color: #ffffff;
  148. }
  149. /* #endif */
  150. .example {
  151. padding: 0 15px;
  152. }
  153. .example-info {
  154. /* #ifndef APP-NVUE */
  155. display: block;
  156. /* #endif */
  157. padding: 15px;
  158. color: #3b4144;
  159. background-color: #ffffff;
  160. font-size: 14px;
  161. line-height: 20px;
  162. }
  163. .example-info-text {
  164. font-size: 14px;
  165. line-height: 20px;
  166. color: #3b4144;
  167. }
  168. .example-body {
  169. flex-direction: column;
  170. padding: 15px;
  171. background-color: #ffffff;
  172. }
  173. .word-btn-white {
  174. font-size: 18px;
  175. color: #FFFFFF;
  176. }
  177. .word-btn {
  178. /* #ifndef APP-NVUE */
  179. display: flex;
  180. /* #endif */
  181. flex-direction: row;
  182. align-items: center;
  183. justify-content: center;
  184. border-radius: 6px;
  185. height: 48px;
  186. margin: 15px;
  187. background-color: #007AFF;
  188. }
  189. .word-btn--hover {
  190. background-color: #4ca2ff;
  191. }
  192. .example-body {
  193. /* #ifndef APP-NVUE */
  194. display: flex;
  195. /* #endif */
  196. flex-direction: row;
  197. }
  198. .calendar-button {
  199. flex: 1;
  200. font-weight: bold;
  201. font-size: 32rpx;
  202. }
  203. </style>