uni-calendar.vue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <template>
  2. <view class="uni-calendar" @touchmove.stop.prevent="clean">
  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">取消</text>
  8. </view>
  9. <view class="uni-calendar__header-btn-box" @click="confirm">
  10. <text class="uni-calendar__header-text uni-calendar--fixed-width">确定</text>
  11. </view>
  12. </view>
  13. <view class="uni-calendar__header">
  14. <view class="uni-calendar__header-btn-box" @click="pre">
  15. <view class="uni-calendar__header-btn uni-calendar--left"></view>
  16. </view>
  17. <text class="uni-calendar__header-text">{{ (nowDate.year||'') +'年'+( nowDate.month||'') +'月'}}</text>
  18. <view class="uni-calendar__header-btn-box" @click="next">
  19. <view class="uni-calendar__header-btn uni-calendar--right"></view>
  20. </view>
  21. <text class="uni-calendar__backtoday" @click="backtoday">回到今天</text>
  22. </view>
  23. <view class="uni-calendar__box">
  24. <view v-if="showMonth" class="uni-calendar__box-bg">
  25. <text class="uni-calendar__box-bg-text">{{nowDate.month}}</text>
  26. </view>
  27. <view class="uni-calendar__weeks">
  28. <view class="uni-calendar__weeks-day">
  29. <text class="uni-calendar__weeks-day-text">日</text>
  30. </view>
  31. <view class="uni-calendar__weeks-day">
  32. <text class="uni-calendar__weeks-day-text">一</text>
  33. </view>
  34. <view class="uni-calendar__weeks-day">
  35. <text class="uni-calendar__weeks-day-text">二</text>
  36. </view>
  37. <view class="uni-calendar__weeks-day">
  38. <text class="uni-calendar__weeks-day-text">三</text>
  39. </view>
  40. <view class="uni-calendar__weeks-day">
  41. <text class="uni-calendar__weeks-day-text">四</text>
  42. </view>
  43. <view class="uni-calendar__weeks-day">
  44. <text class="uni-calendar__weeks-day-text">五</text>
  45. </view>
  46. <view class="uni-calendar__weeks-day">
  47. <text class="uni-calendar__weeks-day-text">六</text>
  48. </view>
  49. </view>
  50. <view class="uni-calendar__weeks" v-for="(item,weekIndex) in weeks" :key="weekIndex">
  51. <view class="uni-calendar__weeks-item" v-for="(weeks,weeksIndex) in item" :key="weeksIndex">
  52. <uni-calendar-item :weeks="weeks" :calendar="calendar" :selected="selected" :lunar="lunar" @change="choiceDate"></uni-calendar-item>
  53. </view>
  54. </view>
  55. </view>
  56. </view>
  57. </view>
  58. </template>
  59. <script>
  60. import Calendar from './util.js';
  61. import uniCalendarItem from './uni-calendar-item.vue'
  62. /**
  63. * Calendar 日历
  64. * @description 日历组件可以查看日期,选择任意范围内的日期,打点操作。常用场景如:酒店日期预订、火车机票选择购买日期、上下班打卡等
  65. * @tutorial https://ext.dcloud.net.cn/plugin?id=56
  66. * @property {String} date 自定义当前时间,默认为今天
  67. * @property {Boolean} lunar 显示农历
  68. * @property {String} startDate 日期选择范围-开始日期
  69. * @property {String} endDate 日期选择范围-结束日期
  70. * @property {Boolean} range 范围选择
  71. * @property {Boolean} insert = [true|false] 插入模式,默认为false
  72. * @value true 弹窗模式
  73. * @value false 插入模式
  74. * @property {Array} selected 打点,期待格式[{date: '2019-06-27', info: '签到', data: { custom: '自定义信息', name: '自定义消息头',xxx:xxx... }}]
  75. * @property {Boolean} showMonth 是否选择月份为背景
  76. * @event {Function} change 日期改变,`insert :ture` 时生效
  77. * @event {Function} confirm 确认选择`insert :false` 时生效
  78. * @event {Function} monthSwitch 切换月份时触发
  79. * @example <uni-calendar :insert="true":lunar="true" :start-date="'2019-3-2'":end-date="'2019-5-20'"@change="change" />
  80. */
  81. export default {
  82. components: {
  83. uniCalendarItem
  84. },
  85. props: {
  86. date: {
  87. type: String,
  88. default: ''
  89. },
  90. selected: {
  91. type: Array,
  92. default () {
  93. return []
  94. }
  95. },
  96. lunar: {
  97. type: Boolean,
  98. default: false
  99. },
  100. startDate: {
  101. type: String,
  102. default: ''
  103. },
  104. endDate: {
  105. type: String,
  106. default: ''
  107. },
  108. range: {
  109. type: Boolean,
  110. default: false
  111. },
  112. insert: {
  113. type: Boolean,
  114. default: true
  115. },
  116. showMonth: {
  117. type: Boolean,
  118. default: true
  119. }
  120. },
  121. data() {
  122. return {
  123. show: false,
  124. weeks: [],
  125. calendar: {},
  126. nowDate: '',
  127. aniMaskShow: false
  128. }
  129. },
  130. watch: {
  131. selected(newVal) {
  132. this.cale.setSelectInfo(this.nowDate.fullDate, newVal)
  133. this.weeks = this.cale.weeks
  134. }
  135. },
  136. created() {
  137. // 获取日历方法实例
  138. this.cale = new Calendar({
  139. date: this.date,
  140. selected: this.selected,
  141. startDate: this.startDate,
  142. endDate: this.endDate,
  143. range: this.range,
  144. })
  145. this.init(this.cale.date.fullDate)
  146. },
  147. methods: {
  148. // 取消穿透
  149. clean() {},
  150. init(date) {
  151. this.weeks = this.cale.weeks
  152. this.nowDate = this.calendar = this.cale.getInfo(date)
  153. },
  154. open() {
  155. this.show = true
  156. this.$nextTick(() => {
  157. setTimeout(() => {
  158. this.aniMaskShow = true
  159. }, 50)
  160. })
  161. },
  162. close() {
  163. this.aniMaskShow = false
  164. this.$nextTick(() => {
  165. setTimeout(() => {
  166. this.show = false
  167. }, 300)
  168. })
  169. },
  170. confirm() {
  171. this.setEmit('confirm')
  172. this.close()
  173. },
  174. change() {
  175. if (!this.insert) return
  176. this.setEmit('change')
  177. },
  178. monthSwitch() {
  179. let {
  180. year,
  181. month
  182. } = this.nowDate
  183. this.$emit('monthSwitch', {
  184. year,
  185. month: Number(month)
  186. })
  187. },
  188. setEmit(name) {
  189. let {
  190. year,
  191. month,
  192. date,
  193. fullDate,
  194. lunar,
  195. extraInfo
  196. } = this.calendar
  197. this.$emit(name, {
  198. range: this.cale.multipleStatus,
  199. year,
  200. month,
  201. date,
  202. fulldate: fullDate,
  203. lunar,
  204. extraInfo: extraInfo || {}
  205. })
  206. },
  207. choiceDate(weeks) {
  208. if (weeks.disable) return
  209. this.calendar = weeks
  210. // 设置多选
  211. this.cale.setMultiple(this.calendar.fullDate)
  212. this.weeks = this.cale.weeks
  213. this.change()
  214. },
  215. backtoday() {
  216. this.cale.setDate(this.date)
  217. this.weeks = this.cale.weeks
  218. this.nowDate = this.calendar = this.cale.getInfo(this.date)
  219. this.change()
  220. },
  221. pre() {
  222. const preDate = this.cale.getDate(this.nowDate.fullDate, -1, 'month').fullDate
  223. this.setDate(preDate)
  224. this.monthSwitch()
  225. },
  226. next() {
  227. const nextDate = this.cale.getDate(this.nowDate.fullDate, +1, 'month').fullDate
  228. this.setDate(nextDate)
  229. this.monthSwitch()
  230. },
  231. setDate(date) {
  232. this.cale.setDate(date)
  233. this.weeks = this.cale.weeks
  234. this.nowDate = this.cale.getInfo(date)
  235. }
  236. }
  237. }
  238. </script>
  239. <style scoped>
  240. .uni-calendar {
  241. /* #ifndef APP-NVUE */
  242. display: flex;
  243. /* #endif */
  244. flex-direction: column;
  245. }
  246. .uni-calendar__mask {
  247. position: fixed;
  248. bottom: 0;
  249. top: 0;
  250. left: 0;
  251. right: 0;
  252. background-color: rgba(0, 0, 0, 0.4);
  253. transition-property: opacity;
  254. transition-duration: 0.3s;
  255. opacity: 0;
  256. /* #ifndef APP-NVUE */
  257. z-index: 99;
  258. /* #endif */
  259. }
  260. .uni-calendar--mask-show {
  261. opacity: 1
  262. }
  263. .uni-calendar--fixed {
  264. position: fixed;
  265. bottom: 0;
  266. left: 0;
  267. right: 0;
  268. transition-property: transform;
  269. transition-duration: 0.3s;
  270. transform: translateY(460px);
  271. /* #ifndef APP-NVUE */
  272. z-index: 99;
  273. /* #endif */
  274. }
  275. .uni-calendar--ani-show {
  276. transform: translateY(0);
  277. }
  278. .uni-calendar__content {
  279. background-color: #fff;
  280. }
  281. .uni-calendar__header {
  282. position: relative;
  283. /* #ifndef APP-NVUE */
  284. display: flex;
  285. /* #endif */
  286. flex-direction: row;
  287. justify-content: center;
  288. align-items: center;
  289. height: 50px;
  290. border-bottom-color: #e5e5e5;
  291. border-bottom-style: solid;
  292. border-bottom-width: 1px;
  293. }
  294. .uni-calendar--fixed-top {
  295. /* #ifndef APP-NVUE */
  296. display: flex;
  297. /* #endif */
  298. flex-direction: row;
  299. justify-content: space-between;
  300. border-top-color: #e5e5e5;
  301. border-top-style: solid;
  302. border-top-width: 1px;
  303. }
  304. .uni-calendar--fixed-width {
  305. width: 50px;
  306. /* padding: 0 15px;
  307. */
  308. }
  309. .uni-calendar__backtoday {
  310. position: absolute;
  311. right: 0;
  312. top: 25rpx;
  313. padding: 0 5px;
  314. padding-left: 10px;
  315. height: 25px;
  316. line-height: 25px;
  317. font-size: 12px;
  318. border-top-left-radius: 25px;
  319. border-bottom-left-radius: 25px;
  320. color: #333;
  321. background-color: #f1f1f1;
  322. }
  323. .uni-calendar__header-text {
  324. text-align: center;
  325. width: 100px;
  326. font-size: 14px;
  327. color: #333;
  328. }
  329. .uni-calendar__header-btn-box {
  330. /* #ifndef APP-NVUE */
  331. display: flex;
  332. /* #endif */
  333. flex-direction: row;
  334. align-items: center;
  335. justify-content: center;
  336. width: 50px;
  337. height: 50px;
  338. }
  339. .uni-calendar__header-btn {
  340. width: 10px;
  341. height: 10px;
  342. border-left-color: #808080;
  343. border-left-style: solid;
  344. border-left-width: 2px;
  345. border-top-color: #555555;
  346. border-top-style: solid;
  347. border-top-width: 2px;
  348. }
  349. .uni-calendar--left {
  350. transform: rotate(-45deg);
  351. }
  352. .uni-calendar--right {
  353. transform: rotate(135deg);
  354. }
  355. .uni-calendar__weeks {
  356. position: relative;
  357. /* #ifndef APP-NVUE */
  358. display: flex;
  359. /* #endif */
  360. flex-direction: row;
  361. }
  362. .uni-calendar__weeks-item {
  363. flex: 1;
  364. }
  365. .uni-calendar__weeks-day {
  366. flex: 1;
  367. /* #ifndef APP-NVUE */
  368. display: flex;
  369. /* #endif */
  370. flex-direction: column;
  371. justify-content: center;
  372. align-items: center;
  373. height: 45px;
  374. border-bottom-color: #F5F5F5;
  375. border-bottom-style: solid;
  376. border-bottom-width: 1px;
  377. }
  378. .uni-calendar__weeks-day-text {
  379. font-size: 14px;
  380. }
  381. .uni-calendar__box {
  382. position: relative;
  383. }
  384. .uni-calendar__box-bg {
  385. /* #ifndef APP-NVUE */
  386. display: flex;
  387. /* #endif */
  388. justify-content: center;
  389. align-items: center;
  390. position: absolute;
  391. top: 0;
  392. left: 0;
  393. right: 0;
  394. bottom: 0;
  395. }
  396. .uni-calendar__box-bg-text {
  397. font-size: 200px;
  398. font-weight: bold;
  399. color: #999;
  400. opacity: 0.1;
  401. text-align: center;
  402. /* #ifndef APP-NVUE */
  403. line-height: 1;
  404. /* #endif */
  405. }
  406. </style>