uni-calendar.vue 13 KB

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