uni-calendar.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. // +----------------------------------------------------------------------
  61. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  62. // +----------------------------------------------------------------------
  63. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  64. // +----------------------------------------------------------------------
  65. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  66. // +----------------------------------------------------------------------
  67. // | Author: CRMEB Team <admin@crmeb.com>
  68. // +----------------------------------------------------------------------
  69. import Calendar from './util.js';
  70. import uniCalendarItem from './uni-calendar-item.vue'
  71. export default {
  72. components: {
  73. uniCalendarItem
  74. },
  75. props: {
  76. /**
  77. * 当前日期
  78. */
  79. date: {
  80. type: String,
  81. default: ''
  82. },
  83. /**
  84. * 打点日期
  85. */
  86. selected: {
  87. type: Array,
  88. default () {
  89. return []
  90. }
  91. },
  92. /**
  93. * 是否开启阴历日期
  94. */
  95. lunar: {
  96. type: Boolean,
  97. default: false
  98. },
  99. /**
  100. * 开始时间
  101. */
  102. startDate: {
  103. type: String,
  104. default: ''
  105. },
  106. /**
  107. * 结束时间
  108. */
  109. endDate: {
  110. type: String,
  111. default: ''
  112. },
  113. /**
  114. * 范围
  115. */
  116. range: {
  117. type: Boolean,
  118. default: false
  119. },
  120. /**
  121. * 插入
  122. */
  123. insert: {
  124. type: Boolean,
  125. default: true
  126. },
  127. /**
  128. * 是否显示月份背景
  129. */
  130. showMonth: {
  131. type: Boolean,
  132. default: true
  133. }
  134. },
  135. data() {
  136. return {
  137. show: false,
  138. weeks: [],
  139. calendar: {},
  140. nowDate: '',
  141. aniMaskShow: false
  142. }
  143. },
  144. watch: {
  145. selected(newVal) {
  146. this.cale.setSelectInfo(this.nowDate.fullDate, newVal)
  147. this.weeks = this.cale.weeks
  148. }
  149. },
  150. created() {
  151. // 获取日历方法实例
  152. this.cale = new Calendar({
  153. date: this.date,
  154. selected: this.selected,
  155. startDate: this.startDate,
  156. endDate: this.endDate,
  157. range: this.range,
  158. })
  159. this.init(this.cale.date.fullDate)
  160. },
  161. methods: {
  162. // 取消穿透
  163. clean() {},
  164. init(date) {
  165. this.weeks = this.cale.weeks
  166. this.nowDate = this.calendar = this.cale.getInfo(date)
  167. },
  168. open() {
  169. this.show = true
  170. this.$nextTick(() => {
  171. setTimeout(()=>{
  172. this.aniMaskShow = true
  173. },50)
  174. })
  175. },
  176. close() {
  177. this.aniMaskShow = false
  178. this.$nextTick(() => {
  179. setTimeout(() => {
  180. this.show = false
  181. this.$emit('close')
  182. }, 300)
  183. })
  184. },
  185. confirm() {
  186. this.setEmit('confirm')
  187. this.close()
  188. },
  189. change() {
  190. if (!this.insert) return
  191. this.setEmit('change')
  192. },
  193. monthSwitch() {
  194. let {
  195. year,
  196. month
  197. } = this.nowDate
  198. this.$emit('monthSwitch', {
  199. year,
  200. month: Number(month)
  201. })
  202. },
  203. setEmit(name) {
  204. let {
  205. year,
  206. month,
  207. date,
  208. fullDate,
  209. lunar,
  210. extraInfo
  211. } = this.calendar
  212. this.$emit(name, {
  213. range: this.cale.multipleStatus,
  214. year,
  215. month,
  216. date,
  217. fulldate: fullDate,
  218. lunar,
  219. extraInfo: extraInfo || {}
  220. })
  221. },
  222. choiceDate(weeks) {
  223. if (weeks.disable) return
  224. this.calendar = weeks
  225. // 设置多选
  226. this.cale.setMultiple(this.calendar.fullDate)
  227. this.weeks = this.cale.weeks
  228. this.change()
  229. },
  230. backtoday() {
  231. this.cale.setMultiple('')
  232. this.cale.setDate(this.date)
  233. this.weeks = this.cale.weeks
  234. this.nowDate = this.calendar = this.cale.getInfo(this.date)
  235. this.change()
  236. },
  237. pre() {
  238. const preDate = this.cale.getDate(this.nowDate.fullDate, -1, 'month').fullDate
  239. this.setDate(preDate)
  240. this.monthSwitch()
  241. },
  242. next() {
  243. const nextDate = this.cale.getDate(this.nowDate.fullDate, +1, 'month').fullDate
  244. this.setDate(nextDate)
  245. this.monthSwitch()
  246. },
  247. setDate(date) {
  248. this.cale.setDate(date)
  249. this.weeks = this.cale.weeks
  250. this.nowDate = this.cale.getInfo(date)
  251. }
  252. }
  253. }
  254. </script>
  255. <style lang="scss" scoped>
  256. .uni-calendar {
  257. /* #ifndef APP-NVUE */
  258. display: flex;
  259. /* #endif */
  260. flex-direction: column;
  261. }
  262. .uni-calendar__mask {
  263. position: fixed;
  264. bottom: 0;
  265. top: 0;
  266. left: 0;
  267. right: 0;
  268. background-color: $uni-bg-color-mask;
  269. transition-property: opacity;
  270. transition-duration: 0.3s;
  271. opacity: 0;
  272. /* #ifndef APP-NVUE */
  273. z-index: 99;
  274. /* #endif */
  275. }
  276. .uni-calendar--mask-show {
  277. opacity: 1
  278. }
  279. .uni-calendar--fixed {
  280. position: fixed;
  281. bottom: 0;
  282. left: 0;
  283. right: 0;
  284. transition-property: transform;
  285. transition-duration: 0.3s;
  286. transform: translateY(460px);
  287. /* #ifndef APP-NVUE */
  288. z-index: 99;
  289. /* #endif */
  290. }
  291. .uni-calendar--ani-show {
  292. transform: translateY(0);
  293. }
  294. .uni-calendar__content {
  295. background-color: #fff;
  296. }
  297. .uni-calendar__header {
  298. position: relative;
  299. /* #ifndef APP-NVUE */
  300. display: flex;
  301. /* #endif */
  302. flex-direction: row;
  303. justify-content: center;
  304. align-items: center;
  305. height: 50px;
  306. border-bottom-color: $uni-border-color;
  307. border-bottom-style: solid;
  308. border-bottom-width: 1px;
  309. }
  310. .uni-calendar--fixed-top {
  311. /* #ifndef APP-NVUE */
  312. display: flex;
  313. /* #endif */
  314. flex-direction: row;
  315. justify-content: space-between;
  316. border-top-color: $uni-border-color;
  317. border-top-style: solid;
  318. border-top-width: 1px;
  319. }
  320. .uni-calendar--fixed-width {
  321. width: 50px;
  322. // padding: 0 15px;
  323. }
  324. .uni-calendar__backtoday {
  325. position: absolute;
  326. right: 0;
  327. top: 25rpx;
  328. padding: 0 5px;
  329. padding-left: 10px;
  330. height: 25px;
  331. line-height: 25px;
  332. font-size: 12px;
  333. border-top-left-radius: 25px;
  334. border-bottom-left-radius: 25px;
  335. color: $uni-text-color;
  336. background-color: $uni-bg-color-hover;
  337. }
  338. .uni-calendar__header-text {
  339. text-align: center;
  340. width: 100px;
  341. font-size: $uni-font-size-base;
  342. color: $uni-text-color;
  343. }
  344. .uni-calendar__header-btn-box {
  345. /* #ifndef APP-NVUE */
  346. display: flex;
  347. /* #endif */
  348. flex-direction: row;
  349. align-items: center;
  350. justify-content: center;
  351. width: 50px;
  352. height: 50px;
  353. }
  354. .uni-calendar__header-btn {
  355. width: 10px;
  356. height: 10px;
  357. border-left-color: $uni-text-color-placeholder;
  358. border-left-style: solid;
  359. border-left-width: 2px;
  360. border-top-color: $uni-color-subtitle;
  361. border-top-style: solid;
  362. border-top-width: 2px;
  363. }
  364. .uni-calendar--left {
  365. transform: rotate(-45deg);
  366. }
  367. .uni-calendar--right {
  368. transform: rotate(135deg);
  369. }
  370. .uni-calendar__weeks {
  371. position: relative;
  372. /* #ifndef APP-NVUE */
  373. display: flex;
  374. /* #endif */
  375. flex-direction: row;
  376. }
  377. .uni-calendar__weeks-item {
  378. flex: 1;
  379. }
  380. .uni-calendar__weeks-day {
  381. flex: 1;
  382. /* #ifndef APP-NVUE */
  383. display: flex;
  384. /* #endif */
  385. flex-direction: column;
  386. justify-content: center;
  387. align-items: center;
  388. height: 45px;
  389. border-bottom-color: #F5F5F5;
  390. border-bottom-style: solid;
  391. border-bottom-width: 1px;
  392. }
  393. .uni-calendar__weeks-day-text {
  394. font-size: 14px;
  395. }
  396. .uni-calendar__box {
  397. position: relative;
  398. }
  399. .uni-calendar__box-bg {
  400. /* #ifndef APP-NVUE */
  401. display: flex;
  402. /* #endif */
  403. justify-content: center;
  404. align-items: center;
  405. position: absolute;
  406. top: 0;
  407. left: 0;
  408. right: 0;
  409. bottom: 0;
  410. }
  411. .uni-calendar__box-bg-text {
  412. font-size: 200px;
  413. font-weight: bold;
  414. color: $uni-text-color-grey;
  415. opacity: 0.1;
  416. text-align: center;
  417. /* #ifndef APP-NVUE */
  418. line-height: 1;
  419. /* #endif */
  420. }
  421. </style>