uni-calendar.vue 13 KB

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