uni-calendar.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 :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(this.cale.selectDate.fullDate)
  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.cale.selectDate.fullDate)
  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.cale.setDate(value)
  174. this.init(value)
  175. },
  176. /**
  177. * 初始化日期显示
  178. * @param {Object} date
  179. */
  180. init(date) {
  181. this.weeks = this.cale.weeks
  182. this.nowDate = this.calendar = this.cale.getInfo(date)
  183. console.log("this.weeks",this.weeks)
  184. },
  185. /**
  186. * 打开日历弹窗
  187. */
  188. open() {
  189. // 弹窗模式并且清理数据
  190. if (this.clearDate && !this.insert) {
  191. this.cale.cleanMultipleStatus()
  192. this.cale.setDate(this.date)
  193. this.init(this.cale.selectDate.fullDate)
  194. }
  195. this.show = true
  196. this.$nextTick(() => {
  197. setTimeout(() => {
  198. this.aniMaskShow = true
  199. }, 50)
  200. })
  201. },
  202. /**
  203. * 关闭日历弹窗
  204. */
  205. close() {
  206. this.aniMaskShow = false
  207. this.$nextTick(() => {
  208. setTimeout(() => {
  209. this.show = false
  210. this.$emit('close')
  211. }, 300)
  212. })
  213. },
  214. /**
  215. * 确认按钮
  216. */
  217. confirm() {
  218. this.setEmit('confirm')
  219. this.close()
  220. },
  221. /**
  222. * 变化触发
  223. */
  224. change() {
  225. if (!this.insert) return
  226. this.setEmit('change')
  227. },
  228. /**
  229. * 选择月份触发
  230. */
  231. monthSwitch() {
  232. let {
  233. year,
  234. month
  235. } = this.nowDate
  236. this.$emit('monthSwitch', {
  237. year,
  238. month: Number(month)
  239. })
  240. },
  241. /**
  242. * 派发事件
  243. * @param {Object} name
  244. */
  245. setEmit(name) {
  246. let {
  247. year,
  248. month,
  249. date,
  250. fullDate,
  251. lunar,
  252. extraInfo
  253. } = this.calendar
  254. this.$emit(name, {
  255. range: this.cale.multipleStatus,
  256. year,
  257. month,
  258. date,
  259. fulldate: fullDate,
  260. lunar,
  261. extraInfo: extraInfo || {}
  262. })
  263. },
  264. /**
  265. * 选择天触发
  266. * @param {Object} weeks
  267. */
  268. choiceDate(weeks) {
  269. console.log(weeks)
  270. if (weeks.disable){
  271. //this.next()
  272. return
  273. }
  274. this.calendar = weeks
  275. // 设置多选
  276. this.cale.setMultiple(this.calendar.fullDate)
  277. this.weeks = this.cale.weeks
  278. this.change()
  279. },
  280. /**
  281. * 回到今天
  282. */
  283. backtoday() {
  284. console.log(this.cale.getDate(new Date()).fullDate);
  285. let date = this.cale.getDate(new Date()).fullDate
  286. this.cale.setDate(date)
  287. this.init(date)
  288. this.change()
  289. },
  290. /**
  291. * 上个月
  292. */
  293. pre() {
  294. const preDate = this.cale.getDate(this.nowDate.fullDate, -1, 'month').fullDate
  295. this.setDate(preDate)
  296. this.monthSwitch()
  297. this.$emit("monthChange",preDate)
  298. },
  299. /**
  300. * 下个月
  301. */
  302. next() {
  303. const nextDate = this.cale.getDate(this.nowDate.fullDate, +1, 'month').fullDate
  304. this.setDate(nextDate)
  305. this.monthSwitch()
  306. this.$emit("monthChange",nextDate)
  307. },
  308. /**
  309. * 设置日期
  310. * @param {Object} date
  311. */
  312. setDate(date) {
  313. this.cale.setDate(date)
  314. this.weeks = this.cale.weeks
  315. this.nowDate = this.cale.getInfo(date)
  316. }
  317. }
  318. }
  319. </script>
  320. <style lang="scss" scoped>
  321. .uni-calendar {
  322. /* #ifndef APP-NVUE */
  323. display: flex;
  324. /* #endif */
  325. flex-direction: column;
  326. }
  327. .uni-calendar__mask {
  328. position: fixed;
  329. bottom: 0;
  330. top: 0;
  331. left: 0;
  332. right: 0;
  333. background-color: $uni-bg-color-mask;
  334. transition-property: opacity;
  335. transition-duration: 0.3s;
  336. opacity: 0;
  337. /* #ifndef APP-NVUE */
  338. z-index: 99;
  339. /* #endif */
  340. }
  341. .uni-calendar--mask-show {
  342. opacity: 1
  343. }
  344. .uni-calendar--fixed {
  345. position: fixed;
  346. bottom: 0;
  347. left: 0;
  348. right: 0;
  349. transition-property: transform;
  350. transition-duration: 0.3s;
  351. transform: translateY(460px);
  352. /* #ifndef APP-NVUE */
  353. z-index: 99;
  354. /* #endif */
  355. }
  356. .uni-calendar--ani-show {
  357. transform: translateY(0);
  358. }
  359. .uni-calendar__content {
  360. background-color: #fff;
  361. }
  362. .uni-calendar__header {
  363. position: relative;
  364. /* #ifndef APP-NVUE */
  365. display: flex;
  366. /* #endif */
  367. flex-direction: row;
  368. justify-content: center;
  369. align-items: center;
  370. height: 50px;
  371. border-bottom-color: $uni-border-color-gray;
  372. border-bottom-style: solid;
  373. border-bottom-width: 1px;
  374. }
  375. .uni-calendar--fixed-top {
  376. /* #ifndef APP-NVUE */
  377. display: flex;
  378. /* #endif */
  379. flex-direction: row;
  380. justify-content: space-between;
  381. border-top-color: $uni-border-color;
  382. border-top-style: solid;
  383. border-top-width: 1px;
  384. }
  385. .uni-calendar--fixed-width {
  386. width: 50px;
  387. // padding: 0 15px;
  388. }
  389. .uni-calendar__backtoday {
  390. position: absolute;
  391. right: 0;
  392. top: 25rpx;
  393. padding: 0 5px;
  394. padding-left: 10px;
  395. height: 25px;
  396. line-height: 25px;
  397. font-size: 12px;
  398. border-top-left-radius: 25px;
  399. border-bottom-left-radius: 25px;
  400. color: $uni-text-color;
  401. background-color: $uni-bg-color-hover;
  402. }
  403. .uni-calendar__header-text {
  404. text-align: center;
  405. width: 100px;
  406. font-size: $uni-font-size-xl;
  407. color: $uni-text-color;
  408. }
  409. .uni-calendar__header-btn-box {
  410. /* #ifndef APP-NVUE */
  411. display: flex;
  412. /* #endif */
  413. flex-direction: row;
  414. align-items: center;
  415. justify-content: center;
  416. width: 50px;
  417. height: 50px;
  418. }
  419. .uni-calendar__header-btn {
  420. width: 10px;
  421. height: 10px;
  422. border-left-color: $uni-text-color-placeholder;
  423. border-left-style: solid;
  424. border-left-width: 2px;
  425. border-top-color: $uni-color-subtitle;
  426. border-top-style: solid;
  427. border-top-width: 2px;
  428. }
  429. .uni-calendar--left {
  430. transform: rotate(-45deg);
  431. }
  432. .uni-calendar--right {
  433. transform: rotate(135deg);
  434. }
  435. .uni-calendar__weeks {
  436. position: relative;
  437. /* #ifndef APP-NVUE */
  438. display: flex;
  439. /* #endif */
  440. flex-direction: row;
  441. }
  442. .uni-calendar__weeks-item {
  443. flex: 1;
  444. }
  445. .uni-calendar__weeks-day {
  446. flex: 1;
  447. /* #ifndef APP-NVUE */
  448. display: flex;
  449. /* #endif */
  450. flex-direction: column;
  451. justify-content: center;
  452. align-items: center;
  453. height: 45px;
  454. border-bottom-color: #F5F5F5;
  455. border-bottom-style: solid;
  456. border-bottom-width: 1px;
  457. }
  458. .uni-calendar__weeks-day-text {
  459. font-size: 14px;
  460. }
  461. .uni-calendar__box {
  462. position: relative;
  463. }
  464. .uni-calendar__box-bg {
  465. /* #ifndef APP-NVUE */
  466. display: flex;
  467. /* #endif */
  468. justify-content: center;
  469. align-items: center;
  470. position: absolute;
  471. top: 0;
  472. left: 0;
  473. right: 0;
  474. bottom: 0;
  475. }
  476. .uni-calendar__box-bg-text {
  477. font-size: 200px;
  478. font-weight: bold;
  479. color: $uni-text-color-grey;
  480. opacity: 0.1;
  481. text-align: center;
  482. /* #ifndef APP-NVUE */
  483. line-height: 1;
  484. /* #endif */
  485. }
  486. </style>