You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

570 lines
13 KiB

3 weeks ago
  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: {
  192. handler(newVal) {
  193. this.cale.setSelectInfo(this.nowDate.fullDate, newVal);
  194. this.weeks = this.cale.weeks;
  195. },
  196. deep: true,
  197. },
  198. },
  199. created() {
  200. this.cale = new Calendar({
  201. selected: this.selected,
  202. startDate: this.startDate,
  203. endDate: this.endDate,
  204. range: this.range,
  205. })
  206. this.init(this.date)
  207. },
  208. methods: {
  209. // 取消穿透
  210. clean() {},
  211. bindDateChange(e) {
  212. const value = e.detail.value + '-1'
  213. this.setDate(value)
  214. let detail = this.cale.getDate(value)
  215. this.$emit('monthSwitch', {
  216. year: detail.year,
  217. month: Number(detail.month)
  218. })
  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. const nowYearMonth = `${this.nowDate.year}-${this.nowDate.month}`
  325. const date = this.cale.getDate(new Date())
  326. const todayYearMonth = `${date.year}-${date.month}`
  327. this.init(date.fullDate)
  328. if(nowYearMonth !== todayYearMonth) {
  329. this.monthSwitch()
  330. }
  331. this.change()
  332. },
  333. /**
  334. * 上个月
  335. */
  336. pre() {
  337. const preDate = this.cale.getDate(this.nowDate.fullDate, -1, 'month').fullDate
  338. this.setDate(preDate)
  339. this.monthSwitch()
  340. },
  341. /**
  342. * 下个月
  343. */
  344. next() {
  345. const nextDate = this.cale.getDate(this.nowDate.fullDate, +1, 'month').fullDate
  346. this.setDate(nextDate)
  347. this.monthSwitch()
  348. },
  349. /**
  350. * 设置日期
  351. * @param {Object} date
  352. */
  353. setDate(date) {
  354. this.cale.setDate(date)
  355. this.weeks = this.cale.weeks
  356. this.nowDate = this.cale.getInfo(date)
  357. }
  358. }
  359. }
  360. </script>
  361. <style lang="scss" scoped>
  362. $uni-bg-color-mask: rgba($color: #000000, $alpha: 0.4);
  363. $uni-border-color: #EDEDED;
  364. $uni-text-color: #333;
  365. $uni-bg-color-hover:#f1f1f1;
  366. $uni-font-size-base:14px;
  367. $uni-text-color-placeholder: #808080;
  368. $uni-color-subtitle: #555555;
  369. $uni-text-color-grey:#999;
  370. .uni-calendar {
  371. /* #ifndef APP-NVUE */
  372. display: flex;
  373. /* #endif */
  374. flex-direction: column;
  375. }
  376. .uni-calendar__mask {
  377. position: fixed;
  378. bottom: 0;
  379. top: 0;
  380. left: 0;
  381. right: 0;
  382. background-color: $uni-bg-color-mask;
  383. transition-property: opacity;
  384. transition-duration: 0.3s;
  385. opacity: 0;
  386. /* #ifndef APP-NVUE */
  387. z-index: 99;
  388. /* #endif */
  389. }
  390. .uni-calendar--mask-show {
  391. opacity: 1
  392. }
  393. .uni-calendar--fixed {
  394. position: fixed;
  395. /* #ifdef APP-NVUE */
  396. bottom: 0;
  397. /* #endif */
  398. left: 0;
  399. right: 0;
  400. transition-property: transform;
  401. transition-duration: 0.3s;
  402. transform: translateY(460px);
  403. /* #ifndef APP-NVUE */
  404. bottom: calc(var(--window-bottom));
  405. z-index: 99;
  406. /* #endif */
  407. }
  408. .uni-calendar--ani-show {
  409. transform: translateY(0);
  410. }
  411. .uni-calendar__content {
  412. background-color: #fff;
  413. }
  414. .uni-calendar__header {
  415. position: relative;
  416. /* #ifndef APP-NVUE */
  417. display: flex;
  418. /* #endif */
  419. flex-direction: row;
  420. justify-content: center;
  421. align-items: center;
  422. height: 50px;
  423. border-bottom-color: $uni-border-color;
  424. border-bottom-style: solid;
  425. border-bottom-width: 1px;
  426. }
  427. .uni-calendar--fixed-top {
  428. /* #ifndef APP-NVUE */
  429. display: flex;
  430. /* #endif */
  431. flex-direction: row;
  432. justify-content: space-between;
  433. border-top-color: $uni-border-color;
  434. border-top-style: solid;
  435. border-top-width: 1px;
  436. }
  437. .uni-calendar--fixed-width {
  438. width: 50px;
  439. }
  440. .uni-calendar__backtoday {
  441. position: absolute;
  442. right: 0;
  443. top: 25rpx;
  444. padding: 0 5px;
  445. padding-left: 10px;
  446. height: 25px;
  447. line-height: 25px;
  448. font-size: 12px;
  449. border-top-left-radius: 25px;
  450. border-bottom-left-radius: 25px;
  451. color: $uni-text-color;
  452. background-color: $uni-bg-color-hover;
  453. }
  454. .uni-calendar__header-text {
  455. text-align: center;
  456. width: 100px;
  457. font-size: $uni-font-size-base;
  458. color: $uni-text-color;
  459. }
  460. .uni-calendar__header-btn-box {
  461. /* #ifndef APP-NVUE */
  462. display: flex;
  463. /* #endif */
  464. flex-direction: row;
  465. align-items: center;
  466. justify-content: center;
  467. width: 50px;
  468. height: 50px;
  469. }
  470. .uni-calendar__header-btn {
  471. width: 10px;
  472. height: 10px;
  473. border-left-color: $uni-text-color-placeholder;
  474. border-left-style: solid;
  475. border-left-width: 2px;
  476. border-top-color: $uni-color-subtitle;
  477. border-top-style: solid;
  478. border-top-width: 2px;
  479. }
  480. .uni-calendar--left {
  481. transform: rotate(-45deg);
  482. }
  483. .uni-calendar--right {
  484. transform: rotate(135deg);
  485. }
  486. .uni-calendar__weeks {
  487. position: relative;
  488. /* #ifndef APP-NVUE */
  489. display: flex;
  490. /* #endif */
  491. flex-direction: row;
  492. }
  493. .uni-calendar__weeks-item {
  494. flex: 1;
  495. }
  496. .uni-calendar__weeks-day {
  497. flex: 1;
  498. /* #ifndef APP-NVUE */
  499. display: flex;
  500. /* #endif */
  501. flex-direction: column;
  502. justify-content: center;
  503. align-items: center;
  504. height: 45px;
  505. border-bottom-color: #F5F5F5;
  506. border-bottom-style: solid;
  507. border-bottom-width: 1px;
  508. }
  509. .uni-calendar__weeks-day-text {
  510. font-size: 14px;
  511. }
  512. .uni-calendar__box {
  513. position: relative;
  514. }
  515. .uni-calendar__box-bg {
  516. /* #ifndef APP-NVUE */
  517. display: flex;
  518. /* #endif */
  519. justify-content: center;
  520. align-items: center;
  521. position: absolute;
  522. top: 0;
  523. left: 0;
  524. right: 0;
  525. bottom: 0;
  526. }
  527. .uni-calendar__box-bg-text {
  528. font-size: 200px;
  529. font-weight: bold;
  530. color: $uni-text-color-grey;
  531. opacity: 0.1;
  532. text-align: center;
  533. /* #ifndef APP-NVUE */
  534. line-height: 1;
  535. /* #endif */
  536. }
  537. </style>