uni-calendar.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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 { initVueI18n } from '@dcloudio/uni-i18n'
  65. import i18nMessages from './i18n/index.js'
  66. const { t } = initVueI18n(i18nMessages)
  67. /**
  68. * Calendar 日历
  69. * @description 日历组件可以查看日期,选择任意范围内的日期,打点操作。常用场景如:酒店日期预订、火车机票选择购买日期、上下班打卡等
  70. * @tutorial https://ext.dcloud.net.cn/plugin?id=56
  71. * @property {String} date 自定义当前时间,默认为今天
  72. * @property {Boolean} lunar 显示农历
  73. * @property {String} startDate 日期选择范围-开始日期
  74. * @property {String} endDate 日期选择范围-结束日期
  75. * @property {Boolean} range 范围选择
  76. * @property {Boolean} insert = [true|false] 插入模式,默认为false
  77. * @value true 弹窗模式
  78. * @value false 插入模式
  79. * @property {Boolean} clearDate = [true|false] 弹窗模式是否清空上次选择内容
  80. * @property {Array} selected 打点,期待格式[{date: '2019-06-27', info: '签到', data: { custom: '自定义信息', name: '自定义消息头',xxx:xxx... }}]
  81. * @property {Boolean} showMonth 是否选择月份为背景
  82. * @event {Function} change 日期改变,`insert :ture` 时生效
  83. * @event {Function} confirm 确认选择`insert :false` 时生效
  84. * @event {Function} monthSwitch 切换月份时触发
  85. * @example <uni-calendar :insert="true":lunar="true" :start-date="'2019-3-2'":end-date="'2019-5-20'"@change="change" />
  86. */
  87. export default {
  88. components: {
  89. CalendarItem
  90. },
  91. emits:['close','confirm','change','monthSwitch'],
  92. props: {
  93. date: {
  94. type: String,
  95. default: ''
  96. },
  97. selected: {
  98. type: Array,
  99. default () {
  100. return []
  101. }
  102. },
  103. lunar: {
  104. type: Boolean,
  105. default: false
  106. },
  107. startDate: {
  108. type: String,
  109. default: ''
  110. },
  111. endDate: {
  112. type: String,
  113. default: ''
  114. },
  115. range: {
  116. type: Boolean,
  117. default: false
  118. },
  119. insert: {
  120. type: Boolean,
  121. default: true
  122. },
  123. showMonth: {
  124. type: Boolean,
  125. default: true
  126. },
  127. clearDate: {
  128. type: Boolean,
  129. default: true
  130. }
  131. },
  132. data() {
  133. return {
  134. show: false,
  135. weeks: [],
  136. calendar: {},
  137. nowDate: '',
  138. aniMaskShow: false
  139. }
  140. },
  141. computed:{
  142. /**
  143. * for i18n
  144. */
  145. okText() {
  146. return t("uni-calender.ok")
  147. },
  148. cancelText() {
  149. return t("uni-calender.cancel")
  150. },
  151. todayText() {
  152. return t("uni-calender.today")
  153. },
  154. monText() {
  155. return t("uni-calender.MON")
  156. },
  157. TUEText() {
  158. return t("uni-calender.TUE")
  159. },
  160. WEDText() {
  161. return t("uni-calender.WED")
  162. },
  163. THUText() {
  164. return t("uni-calender.THU")
  165. },
  166. FRIText() {
  167. return t("uni-calender.FRI")
  168. },
  169. SATText() {
  170. return t("uni-calender.SAT")
  171. },
  172. SUNText() {
  173. return t("uni-calender.SUN")
  174. },
  175. },
  176. watch: {
  177. date(newVal) {
  178. // this.cale.setDate(newVal)
  179. this.init(newVal)
  180. },
  181. startDate(val){
  182. this.cale.resetSatrtDate(val)
  183. this.cale.setDate(this.nowDate.fullDate)
  184. this.weeks = this.cale.weeks
  185. },
  186. endDate(val){
  187. this.cale.resetEndDate(val)
  188. this.cale.setDate(this.nowDate.fullDate)
  189. this.weeks = this.cale.weeks
  190. },
  191. selected(newVal) {
  192. this.cale.setSelectInfo(this.nowDate.fullDate, newVal)
  193. this.weeks = this.cale.weeks
  194. }
  195. },
  196. created() {
  197. this.cale = new Calendar({
  198. selected: this.selected,
  199. startDate: this.startDate,
  200. endDate: this.endDate,
  201. range: this.range,
  202. })
  203. this.init(this.date)
  204. },
  205. methods: {
  206. // 取消穿透
  207. clean() {},
  208. bindDateChange(e) {
  209. const value = e.detail.value + '-1'
  210. this.setDate(value)
  211. const { year,month } = this.cale.getDate(value)
  212. this.$emit('monthSwitch', {
  213. year,
  214. month
  215. })
  216. },
  217. /**
  218. * 初始化日期显示
  219. * @param {Object} date
  220. */
  221. init(date) {
  222. this.cale.setDate(date)
  223. this.weeks = this.cale.weeks
  224. this.nowDate = this.calendar = this.cale.getInfo(date)
  225. },
  226. /**
  227. * 打开日历弹窗
  228. */
  229. open() {
  230. // 弹窗模式并且清理数据
  231. if (this.clearDate && !this.insert) {
  232. this.cale.cleanMultipleStatus()
  233. // this.cale.setDate(this.date)
  234. this.init(this.date)
  235. }
  236. this.show = true
  237. this.$nextTick(() => {
  238. setTimeout(() => {
  239. this.aniMaskShow = true
  240. }, 50)
  241. })
  242. },
  243. /**
  244. * 关闭日历弹窗
  245. */
  246. close() {
  247. this.aniMaskShow = false
  248. this.$nextTick(() => {
  249. setTimeout(() => {
  250. this.show = false
  251. this.$emit('close')
  252. }, 300)
  253. })
  254. },
  255. /**
  256. * 确认按钮
  257. */
  258. confirm() {
  259. this.setEmit('confirm')
  260. this.close()
  261. },
  262. /**
  263. * 变化触发
  264. */
  265. change() {
  266. if (!this.insert) return
  267. this.setEmit('change')
  268. },
  269. /**
  270. * 选择月份触发
  271. */
  272. monthSwitch() {
  273. let {
  274. year,
  275. month
  276. } = this.nowDate
  277. this.$emit('monthSwitch', {
  278. year,
  279. month: Number(month)
  280. })
  281. },
  282. /**
  283. * 派发事件
  284. * @param {Object} name
  285. */
  286. setEmit(name) {
  287. let {
  288. year,
  289. month,
  290. date,
  291. fullDate,
  292. lunar,
  293. extraInfo
  294. } = this.calendar
  295. this.$emit(name, {
  296. range: this.cale.multipleStatus,
  297. year,
  298. month,
  299. date,
  300. fulldate: fullDate,
  301. lunar,
  302. extraInfo: extraInfo || {}
  303. })
  304. },
  305. /**
  306. * 选择天触发
  307. * @param {Object} weeks
  308. */
  309. choiceDate(weeks) {
  310. if (weeks.disable) return
  311. this.calendar = weeks
  312. // 设置多选
  313. this.cale.setMultiple(this.calendar.fullDate)
  314. this.weeks = this.cale.weeks
  315. this.change()
  316. },
  317. /**
  318. * 回到今天
  319. */
  320. backToday() {
  321. const nowYearMonth = `${this.nowDate.year}-${this.nowDate.month}`
  322. const date = this.cale.getDate(new Date())
  323. const todayYearMonth = `${date.year}-${date.month}`
  324. this.init(date.fullDate)
  325. if(nowYearMonth !== todayYearMonth) {
  326. this.monthSwitch()
  327. }
  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. }
  437. .uni-calendar__backtoday {
  438. position: absolute;
  439. right: 0;
  440. top: 25rpx;
  441. padding: 0 5px;
  442. padding-left: 10px;
  443. height: 25px;
  444. line-height: 25px;
  445. font-size: 12px;
  446. border-top-left-radius: 25px;
  447. border-bottom-left-radius: 25px;
  448. color: $uni-text-color;
  449. background-color: $uni-bg-color-hover;
  450. }
  451. .uni-calendar__header-text {
  452. text-align: center;
  453. width: 100px;
  454. font-size: $uni-font-size-base;
  455. color: $uni-text-color;
  456. }
  457. .uni-calendar__header-btn-box {
  458. /* #ifndef APP-NVUE */
  459. display: flex;
  460. /* #endif */
  461. flex-direction: row;
  462. align-items: center;
  463. justify-content: center;
  464. width: 50px;
  465. height: 50px;
  466. }
  467. .uni-calendar__header-btn {
  468. width: 10px;
  469. height: 10px;
  470. border-left-color: $uni-text-color-placeholder;
  471. border-left-style: solid;
  472. border-left-width: 2px;
  473. border-top-color: $uni-color-subtitle;
  474. border-top-style: solid;
  475. border-top-width: 2px;
  476. }
  477. .uni-calendar--left {
  478. transform: rotate(-45deg);
  479. }
  480. .uni-calendar--right {
  481. transform: rotate(135deg);
  482. }
  483. .uni-calendar__weeks {
  484. position: relative;
  485. /* #ifndef APP-NVUE */
  486. display: flex;
  487. /* #endif */
  488. flex-direction: row;
  489. }
  490. .uni-calendar__weeks-item {
  491. flex: 1;
  492. }
  493. .uni-calendar__weeks-day {
  494. flex: 1;
  495. /* #ifndef APP-NVUE */
  496. display: flex;
  497. /* #endif */
  498. flex-direction: column;
  499. justify-content: center;
  500. align-items: center;
  501. height: 45px;
  502. border-bottom-color: #F5F5F5;
  503. border-bottom-style: solid;
  504. border-bottom-width: 1px;
  505. }
  506. .uni-calendar__weeks-day-text {
  507. font-size: 14px;
  508. }
  509. .uni-calendar__box {
  510. position: relative;
  511. }
  512. .uni-calendar__box-bg {
  513. /* #ifndef APP-NVUE */
  514. display: flex;
  515. /* #endif */
  516. justify-content: center;
  517. align-items: center;
  518. position: absolute;
  519. top: 0;
  520. left: 0;
  521. right: 0;
  522. bottom: 0;
  523. }
  524. .uni-calendar__box-bg-text {
  525. font-size: 200px;
  526. font-weight: bold;
  527. color: $uni-text-color-grey;
  528. opacity: 0.1;
  529. text-align: center;
  530. /* #ifndef APP-NVUE */
  531. line-height: 1;
  532. /* #endif */
  533. }
  534. </style>