uni-calendar.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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-calendar {
  360. /* #ifndef APP-NVUE */
  361. display: flex;
  362. /* #endif */
  363. flex-direction: column;
  364. }
  365. .uni-calendar__mask {
  366. position: fixed;
  367. bottom: 0;
  368. top: 0;
  369. left: 0;
  370. right: 0;
  371. background-color: $uni-bg-color-mask;
  372. transition-property: opacity;
  373. transition-duration: 0.3s;
  374. opacity: 0;
  375. /* #ifndef APP-NVUE */
  376. z-index: 99;
  377. /* #endif */
  378. }
  379. .uni-calendar--mask-show {
  380. opacity: 1
  381. }
  382. .uni-calendar--fixed {
  383. position: fixed;
  384. bottom: calc(var(--window-bottom));
  385. left: 0;
  386. right: 0;
  387. transition-property: transform;
  388. transition-duration: 0.3s;
  389. transform: translateY(460px);
  390. /* #ifndef APP-NVUE */
  391. z-index: 99;
  392. /* #endif */
  393. }
  394. .uni-calendar--ani-show {
  395. transform: translateY(0);
  396. }
  397. .uni-calendar__content {
  398. background-color: #fff;
  399. }
  400. .uni-calendar__header {
  401. position: relative;
  402. /* #ifndef APP-NVUE */
  403. display: flex;
  404. /* #endif */
  405. flex-direction: row;
  406. justify-content: center;
  407. align-items: center;
  408. height: 50px;
  409. border-bottom-color: $uni-border-color;
  410. border-bottom-style: solid;
  411. border-bottom-width: 1px;
  412. }
  413. .uni-calendar--fixed-top {
  414. /* #ifndef APP-NVUE */
  415. display: flex;
  416. /* #endif */
  417. flex-direction: row;
  418. justify-content: space-between;
  419. border-top-color: $uni-border-color;
  420. border-top-style: solid;
  421. border-top-width: 1px;
  422. }
  423. .uni-calendar--fixed-width {
  424. width: 50px;
  425. // padding: 0 15px;
  426. }
  427. .uni-calendar__backtoday {
  428. position: absolute;
  429. right: 0;
  430. top: 25rpx;
  431. padding: 0 5px;
  432. padding-left: 10px;
  433. height: 25px;
  434. line-height: 25px;
  435. font-size: 12px;
  436. border-top-left-radius: 25px;
  437. border-bottom-left-radius: 25px;
  438. color: $uni-text-color;
  439. background-color: $uni-bg-color-hover;
  440. }
  441. .uni-calendar__header-text {
  442. text-align: center;
  443. width: 100px;
  444. font-size: $uni-font-size-base;
  445. color: $uni-text-color;
  446. }
  447. .uni-calendar__header-btn-box {
  448. /* #ifndef APP-NVUE */
  449. display: flex;
  450. /* #endif */
  451. flex-direction: row;
  452. align-items: center;
  453. justify-content: center;
  454. width: 50px;
  455. height: 50px;
  456. }
  457. .uni-calendar__header-btn {
  458. width: 10px;
  459. height: 10px;
  460. border-left-color: $uni-text-color-placeholder;
  461. border-left-style: solid;
  462. border-left-width: 2px;
  463. border-top-color: $uni-color-subtitle;
  464. border-top-style: solid;
  465. border-top-width: 2px;
  466. }
  467. .uni-calendar--left {
  468. transform: rotate(-45deg);
  469. }
  470. .uni-calendar--right {
  471. transform: rotate(135deg);
  472. }
  473. .uni-calendar__weeks {
  474. position: relative;
  475. /* #ifndef APP-NVUE */
  476. display: flex;
  477. /* #endif */
  478. flex-direction: row;
  479. }
  480. .uni-calendar__weeks-item {
  481. flex: 1;
  482. }
  483. .uni-calendar__weeks-day {
  484. flex: 1;
  485. /* #ifndef APP-NVUE */
  486. display: flex;
  487. /* #endif */
  488. flex-direction: column;
  489. justify-content: center;
  490. align-items: center;
  491. height: 45px;
  492. border-bottom-color: #F5F5F5;
  493. border-bottom-style: solid;
  494. border-bottom-width: 1px;
  495. }
  496. .uni-calendar__weeks-day-text {
  497. font-size: 14px;
  498. }
  499. .uni-calendar__box {
  500. position: relative;
  501. }
  502. .uni-calendar__box-bg {
  503. /* #ifndef APP-NVUE */
  504. display: flex;
  505. /* #endif */
  506. justify-content: center;
  507. align-items: center;
  508. position: absolute;
  509. top: 0;
  510. left: 0;
  511. right: 0;
  512. bottom: 0;
  513. }
  514. .uni-calendar__box-bg-text {
  515. font-size: 200px;
  516. font-weight: bold;
  517. color: $uni-text-color-grey;
  518. opacity: 0.1;
  519. text-align: center;
  520. /* #ifndef APP-NVUE */
  521. line-height: 1;
  522. /* #endif */
  523. }
  524. </style>