uni-calendar.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. <template>
  2. <view class="uni-calendar">
  3. <view v-if="!insert&&show" class="uni-calendar__mask" :class="{'uni-calendar--mask-show':aniMaskShow}" @click="clean"></view>
  4. <view v-if="insert || show" class="uni-calendar__content" :class="{'uni-calendar--fixed':!insert,'uni-calendar--ani-show':aniMaskShow}">
  5. <view v-if="!insert" class="uni-calendar__header uni-calendar--fixed-top">
  6. <view class="uni-calendar__header-btn-box" @click="close">
  7. <text class="uni-calendar__header-text uni-calendar--fixed-width">取消</text>
  8. </view>
  9. <view class="uni-calendar__header-btn-box" @click="confirm">
  10. <text class="uni-calendar__header-text uni-calendar--fixed-width">确定</text>
  11. </view>
  12. </view>
  13. <view class="uni-calendar__header">
  14. <view class="uni-calendar__header-btn-box" @click.stop="pre">
  15. <view class="uni-calendar__header-btn uni-calendar--left"></view>
  16. </view>
  17. <picker mode="date" :value="date" fields="month" @change="bindDateChange">
  18. <text class="uni-calendar__header-text">{{ (nowDate.year||'') +'年'+( nowDate.month||'') +'月'}}</text>
  19. </picker>
  20. <view class="uni-calendar__header-btn-box" @click.stop="next">
  21. <view class="uni-calendar__header-btn uni-calendar--right"></view>
  22. </view>
  23. <text class="uni-calendar__backtoday" @click="backtoday">回到今天</text>
  24. </view>
  25. <view class="uni-calendar__box">
  26. <view v-if="showMonth" class="uni-calendar__box-bg">
  27. <text class="uni-calendar__box-bg-text">{{nowDate.month}}</text>
  28. </view>
  29. <view class="uni-calendar__weeks">
  30. <view class="uni-calendar__weeks-day">
  31. <text class="uni-calendar__weeks-day-text">日</text>
  32. </view>
  33. <view class="uni-calendar__weeks-day">
  34. <text class="uni-calendar__weeks-day-text">一</text>
  35. </view>
  36. <view class="uni-calendar__weeks-day">
  37. <text class="uni-calendar__weeks-day-text">二</text>
  38. </view>
  39. <view class="uni-calendar__weeks-day">
  40. <text class="uni-calendar__weeks-day-text">三</text>
  41. </view>
  42. <view class="uni-calendar__weeks-day">
  43. <text class="uni-calendar__weeks-day-text">四</text>
  44. </view>
  45. <view class="uni-calendar__weeks-day">
  46. <text class="uni-calendar__weeks-day-text">五</text>
  47. </view>
  48. <view class="uni-calendar__weeks-day">
  49. <text class="uni-calendar__weeks-day-text">六</text>
  50. </view>
  51. </view>
  52. <view class="uni-calendar__weeks" v-for="(item,weekIndex) in weeks" :key="weekIndex">
  53. <view class="uni-calendar__weeks-item" v-for="(weeks,weeksIndex) in item" :key="weeksIndex">
  54. <calendar-item class="uni-calendar-item--hook" :weeks="weeks" :calendar="calendar" :selected="selected" :lunar="lunar" @change="choiceDate"></calendar-item>
  55. </view>
  56. </view>
  57. </view>
  58. </view>
  59. </view>
  60. </template>
  61. <script>
  62. import Calendar from './util.js';
  63. import calendarItem from './uni-calendar-item.vue'
  64. /**
  65. * Calendar 日历
  66. * @description 日历组件可以查看日期,选择任意范围内的日期,打点操作。常用场景如:酒店日期预订、火车机票选择购买日期、上下班打卡等
  67. * @tutorial https://ext.dcloud.net.cn/plugin?id=56
  68. * @property {String} date 自定义当前时间,默认为今天
  69. * @property {Boolean} lunar 显示农历
  70. * @property {String} startDate 日期选择范围-开始日期
  71. * @property {String} endDate 日期选择范围-结束日期
  72. * @property {Boolean} range 范围选择
  73. * @property {Boolean} insert = [true|false] 插入模式,默认为false
  74. * @value true 弹窗模式
  75. * @value false 插入模式
  76. * @property {Boolean} clearDate = [true|false] 弹窗模式是否清空上次选择内容
  77. * @property {Array} selected 打点,期待格式[{date: '2019-06-27', info: '签到', data: { custom: '自定义信息', name: '自定义消息头',xxx:xxx... }}]
  78. * @property {Boolean} showMonth 是否选择月份为背景
  79. * @event {Function} change 日期改变,`insert :ture` 时生效
  80. * @event {Function} confirm 确认选择`insert :false` 时生效
  81. * @event {Function} monthSwitch 切换月份时触发
  82. * @example <uni-calendar :insert="true":lunar="true" :start-date="'2019-3-2'":end-date="'2019-5-20'"@change="change" />
  83. */
  84. export default {
  85. components: {
  86. calendarItem
  87. },
  88. props: {
  89. date: {
  90. type: String,
  91. default: ''
  92. },
  93. selected: {
  94. type: Array,
  95. default () {
  96. return []
  97. }
  98. },
  99. lunar: {
  100. type: Boolean,
  101. default: false
  102. },
  103. startDate: {
  104. type: String,
  105. default: ''
  106. },
  107. endDate: {
  108. type: String,
  109. default: ''
  110. },
  111. range: {
  112. type: Boolean,
  113. default: false
  114. },
  115. insert: {
  116. type: Boolean,
  117. default: true
  118. },
  119. showMonth: {
  120. type: Boolean,
  121. default: true
  122. },
  123. clearDate: {
  124. type: Boolean,
  125. default: true
  126. }
  127. },
  128. data() {
  129. return {
  130. show: false,
  131. weeks: [],
  132. calendar: {},
  133. nowDate: '',
  134. aniMaskShow: false
  135. }
  136. },
  137. watch: {
  138. date(newVal) {
  139. // this.cale.setDate(newVal)
  140. this.init(newVal)
  141. },
  142. startDate(val){
  143. this.cale.resetSatrtDate(val)
  144. },
  145. endDate(val){
  146. this.cale.resetEndDate(val)
  147. },
  148. selected(newVal) {
  149. this.cale.setSelectInfo(this.nowDate.fullDate, newVal)
  150. this.weeks = this.cale.weeks
  151. }
  152. },
  153. created() {
  154. // 获取日历方法实例
  155. this.cale = new Calendar({
  156. // date: new Date(),
  157. selected: this.selected,
  158. startDate: this.startDate,
  159. endDate: this.endDate,
  160. range: this.range,
  161. })
  162. // 选中某一天
  163. // this.cale.setDate(this.date)
  164. this.init(this.date)
  165. // this.setDay
  166. },
  167. methods: {
  168. // 取消穿透
  169. clean() {},
  170. bindDateChange(e) {
  171. const value = e.detail.value + '-1'
  172. console.log(this.cale.getDate(value));
  173. this.init(value)
  174. },
  175. /**
  176. * 初始化日期显示
  177. * @param {Object} date
  178. */
  179. init(date) {
  180. this.cale.setDate(date)
  181. this.weeks = this.cale.weeks
  182. this.nowDate = this.calendar = this.cale.getInfo(date)
  183. },
  184. /**
  185. * 打开日历弹窗
  186. */
  187. open() {
  188. // 弹窗模式并且清理数据
  189. if (this.clearDate && !this.insert) {
  190. this.cale.cleanMultipleStatus()
  191. // this.cale.setDate(this.date)
  192. this.init(this.date)
  193. }
  194. this.show = true
  195. this.$nextTick(() => {
  196. setTimeout(() => {
  197. this.aniMaskShow = true
  198. }, 50)
  199. })
  200. },
  201. /**
  202. * 关闭日历弹窗
  203. */
  204. close() {
  205. this.aniMaskShow = false
  206. this.$nextTick(() => {
  207. setTimeout(() => {
  208. this.show = false
  209. this.$emit('close')
  210. }, 300)
  211. })
  212. },
  213. /**
  214. * 确认按钮
  215. */
  216. confirm() {
  217. this.setEmit('confirm')
  218. this.close()
  219. },
  220. /**
  221. * 变化触发
  222. */
  223. change() {
  224. if (!this.insert) return
  225. this.setEmit('change')
  226. },
  227. /**
  228. * 选择月份触发
  229. */
  230. monthSwitch() {
  231. let {
  232. year,
  233. month
  234. } = this.nowDate
  235. this.$emit('monthSwitch', {
  236. year,
  237. month: Number(month)
  238. })
  239. },
  240. /**
  241. * 派发事件
  242. * @param {Object} name
  243. */
  244. setEmit(name) {
  245. let {
  246. year,
  247. month,
  248. date,
  249. fullDate,
  250. lunar,
  251. extraInfo
  252. } = this.calendar
  253. this.$emit(name, {
  254. range: this.cale.multipleStatus,
  255. year,
  256. month,
  257. date,
  258. fulldate: fullDate,
  259. lunar,
  260. extraInfo: extraInfo || {}
  261. })
  262. },
  263. /**
  264. * 选择天触发
  265. * @param {Object} weeks
  266. */
  267. choiceDate(weeks) {
  268. if (weeks.disable) return
  269. this.calendar = weeks
  270. // 设置多选
  271. this.cale.setMultiple(this.calendar.fullDate)
  272. this.weeks = this.cale.weeks
  273. this.change()
  274. },
  275. /**
  276. * 回到今天
  277. */
  278. backtoday() {
  279. console.log(this.cale.getDate(new Date()).fullDate);
  280. let date = this.cale.getDate(new Date()).fullDate
  281. // this.cale.setDate(date)
  282. this.init(date)
  283. this.change()
  284. },
  285. /**
  286. * 上个月
  287. */
  288. pre() {
  289. const preDate = this.cale.getDate(this.nowDate.fullDate, -1, 'month').fullDate
  290. this.setDate(preDate)
  291. this.monthSwitch()
  292. },
  293. /**
  294. * 下个月
  295. */
  296. next() {
  297. const nextDate = this.cale.getDate(this.nowDate.fullDate, +1, 'month').fullDate
  298. this.setDate(nextDate)
  299. this.monthSwitch()
  300. },
  301. /**
  302. * 设置日期
  303. * @param {Object} date
  304. */
  305. setDate(date) {
  306. this.cale.setDate(date)
  307. this.weeks = this.cale.weeks
  308. this.nowDate = this.cale.getInfo(date)
  309. }
  310. }
  311. }
  312. </script>
  313. <style lang="scss" scoped>
  314. .uni-calendar {
  315. /* #ifndef APP-NVUE */
  316. display: flex;
  317. /* #endif */
  318. flex-direction: column;
  319. }
  320. .uni-calendar__mask {
  321. position: fixed;
  322. bottom: 0;
  323. top: 0;
  324. left: 0;
  325. right: 0;
  326. background-color: $uni-bg-color-mask;
  327. transition-property: opacity;
  328. transition-duration: 0.3s;
  329. opacity: 0;
  330. /* #ifndef APP-NVUE */
  331. z-index: 99;
  332. /* #endif */
  333. }
  334. .uni-calendar--mask-show {
  335. opacity: 1
  336. }
  337. .uni-calendar--fixed {
  338. position: fixed;
  339. bottom: 0;
  340. left: 0;
  341. right: 0;
  342. transition-property: transform;
  343. transition-duration: 0.3s;
  344. transform: translateY(460px);
  345. /* #ifndef APP-NVUE */
  346. z-index: 99;
  347. /* #endif */
  348. }
  349. .uni-calendar--ani-show {
  350. transform: translateY(0);
  351. }
  352. .uni-calendar__content {
  353. background-color: #fff;
  354. }
  355. .uni-calendar__header {
  356. position: relative;
  357. /* #ifndef APP-NVUE */
  358. display: flex;
  359. /* #endif */
  360. flex-direction: row;
  361. justify-content: center;
  362. align-items: center;
  363. height: 50px;
  364. border-bottom-color: $uni-border-color;
  365. border-bottom-style: solid;
  366. border-bottom-width: 1px;
  367. }
  368. .uni-calendar--fixed-top {
  369. /* #ifndef APP-NVUE */
  370. display: flex;
  371. /* #endif */
  372. flex-direction: row;
  373. justify-content: space-between;
  374. border-top-color: $uni-border-color;
  375. border-top-style: solid;
  376. border-top-width: 1px;
  377. }
  378. .uni-calendar--fixed-width {
  379. width: 50px;
  380. // padding: 0 15px;
  381. }
  382. .uni-calendar__backtoday {
  383. position: absolute;
  384. right: 0;
  385. top: 25rpx;
  386. padding: 0 5px;
  387. padding-left: 10px;
  388. height: 25px;
  389. line-height: 25px;
  390. font-size: 12px;
  391. border-top-left-radius: 25px;
  392. border-bottom-left-radius: 25px;
  393. color: $uni-text-color;
  394. background-color: $uni-bg-color-hover;
  395. }
  396. .uni-calendar__header-text {
  397. text-align: center;
  398. width: 100px;
  399. font-size: $uni-font-size-base;
  400. color: $uni-text-color;
  401. }
  402. .uni-calendar__header-btn-box {
  403. /* #ifndef APP-NVUE */
  404. display: flex;
  405. /* #endif */
  406. flex-direction: row;
  407. align-items: center;
  408. justify-content: center;
  409. width: 50px;
  410. height: 50px;
  411. }
  412. .uni-calendar__header-btn {
  413. width: 10px;
  414. height: 10px;
  415. border-left-color: $uni-text-color-placeholder;
  416. border-left-style: solid;
  417. border-left-width: 2px;
  418. border-top-color: $uni-color-subtitle;
  419. border-top-style: solid;
  420. border-top-width: 2px;
  421. }
  422. .uni-calendar--left {
  423. transform: rotate(-45deg);
  424. }
  425. .uni-calendar--right {
  426. transform: rotate(135deg);
  427. }
  428. .uni-calendar__weeks {
  429. position: relative;
  430. /* #ifndef APP-NVUE */
  431. display: flex;
  432. /* #endif */
  433. flex-direction: row;
  434. }
  435. .uni-calendar__weeks-item {
  436. flex: 1;
  437. }
  438. .uni-calendar__weeks-day {
  439. flex: 1;
  440. /* #ifndef APP-NVUE */
  441. display: flex;
  442. /* #endif */
  443. flex-direction: column;
  444. justify-content: center;
  445. align-items: center;
  446. height: 45px;
  447. border-bottom-color: #F5F5F5;
  448. border-bottom-style: solid;
  449. border-bottom-width: 1px;
  450. }
  451. .uni-calendar__weeks-day-text {
  452. font-size: 14px;
  453. }
  454. .uni-calendar__box {
  455. position: relative;
  456. }
  457. .uni-calendar__box-bg {
  458. /* #ifndef APP-NVUE */
  459. display: flex;
  460. /* #endif */
  461. justify-content: center;
  462. align-items: center;
  463. position: absolute;
  464. top: 0;
  465. left: 0;
  466. right: 0;
  467. bottom: 0;
  468. }
  469. .uni-calendar__box-bg-text {
  470. font-size: 200px;
  471. font-weight: bold;
  472. color: $uni-text-color-grey;
  473. opacity: 0.1;
  474. text-align: center;
  475. /* #ifndef APP-NVUE */
  476. line-height: 1;
  477. /* #endif */
  478. }
  479. </style>