uni-calendar.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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">{{ $t(`取消`) }}</text>
  8. </view>
  9. <view class="uni-calendar__header-btn-box" @click="confirm">
  10. <text class="uni-calendar__header-text uni-calendar--fixed-width">{{ $t(`确定`) }}</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 || '') + $t(`年`) + (nowDate.month || '') + $t(`月`) }}</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">{{ $t(`回到当天`) }}</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">{{ $t(`天`) }}</text>
  30. </view>
  31. <view class="uni-calendar__weeks-day">
  32. <text class="uni-calendar__weeks-day-text">{{ $t(`一`) }}</text>
  33. </view>
  34. <view class="uni-calendar__weeks-day">
  35. <text class="uni-calendar__weeks-day-text">{{ $t(`二`) }}</text>
  36. </view>
  37. <view class="uni-calendar__weeks-day">
  38. <text class="uni-calendar__weeks-day-text">{{ $t(`三`) }}</text>
  39. </view>
  40. <view class="uni-calendar__weeks-day">
  41. <text class="uni-calendar__weeks-day-text">{{ $t(`四`) }}</text>
  42. </view>
  43. <view class="uni-calendar__weeks-day">
  44. <text class="uni-calendar__weeks-day-text">{{ $t(`五`) }}</text>
  45. </view>
  46. <view class="uni-calendar__weeks-day">
  47. <text class="uni-calendar__weeks-day-text">{{ $t(`六`) }}</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="(w, weeksIndex) in item" :key="weeksIndex">
  52. <uni-calendar-item :weeks="w" :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. export default {
  63. components: {
  64. uniCalendarItem
  65. },
  66. props: {
  67. /**
  68. * 当前日期
  69. */
  70. date: {
  71. type: String,
  72. default: ''
  73. },
  74. /**
  75. * 打点日期
  76. */
  77. selected: {
  78. type: Array,
  79. default() {
  80. return [];
  81. }
  82. },
  83. /**
  84. * 是否开启阴历日期
  85. */
  86. lunar: {
  87. type: Boolean,
  88. default: false
  89. },
  90. /**
  91. * 开始时间
  92. */
  93. startDate: {
  94. type: String,
  95. default: ''
  96. },
  97. /**
  98. * 结束时间
  99. */
  100. endDate: {
  101. type: String,
  102. default: ''
  103. },
  104. /**
  105. * 范围
  106. */
  107. range: {
  108. type: Boolean,
  109. default: false
  110. },
  111. /**
  112. * 插入
  113. */
  114. insert: {
  115. type: Boolean,
  116. default: true
  117. },
  118. /**
  119. * 是否显示月份背景
  120. */
  121. showMonth: {
  122. type: Boolean,
  123. default: true
  124. }
  125. },
  126. data() {
  127. return {
  128. show: false,
  129. weeks: [],
  130. calendar: {},
  131. nowDate: '',
  132. aniMaskShow: false
  133. };
  134. },
  135. watch: {
  136. selected(newVal) {
  137. this.cale.setSelectInfo(this.nowDate.fullDate, newVal);
  138. this.weeks = this.cale.weeks;
  139. }
  140. },
  141. created() {
  142. // 获取日历方法实例
  143. this.cale = new Calendar({
  144. date: this.date,
  145. selected: this.selected,
  146. startDate: this.startDate,
  147. endDate: this.endDate,
  148. range: this.range
  149. });
  150. this.init(this.cale.date.fullDate);
  151. },
  152. methods: {
  153. // 取消穿透
  154. clean() {},
  155. init(date) {
  156. this.weeks = this.cale.weeks;
  157. this.nowDate = this.calendar = this.cale.getInfo(date);
  158. },
  159. open() {
  160. this.show = true;
  161. this.$nextTick(() => {
  162. setTimeout(() => {
  163. this.aniMaskShow = true;
  164. }, 50);
  165. });
  166. },
  167. close() {
  168. this.aniMaskShow = false;
  169. this.$nextTick(() => {
  170. setTimeout(() => {
  171. this.show = false;
  172. }, 300);
  173. });
  174. },
  175. confirm() {
  176. this.setEmit('confirm');
  177. this.close();
  178. },
  179. change() {
  180. if (!this.insert) return;
  181. this.setEmit('change');
  182. },
  183. monthSwitch() {
  184. let { year, month } = this.nowDate;
  185. this.$emit('monthSwitch', {
  186. year,
  187. month: Number(month)
  188. });
  189. },
  190. setEmit(name) {
  191. let { year, month, date, fullDate, lunar, extraInfo } = this.calendar;
  192. this.$emit(name, {
  193. range: this.cale.multipleStatus,
  194. year,
  195. month,
  196. date,
  197. fulldate: fullDate,
  198. lunar,
  199. extraInfo: extraInfo || {}
  200. });
  201. },
  202. choiceDate(weeks) {
  203. if (weeks.disable) return;
  204. this.calendar = weeks;
  205. // 设置多选
  206. this.cale.setMultiple(this.calendar.fullDate);
  207. this.weeks = this.cale.weeks;
  208. this.change();
  209. },
  210. backtoday() {
  211. this.cale.multipleStatus.data = [this.nowDate.fullDate, this.nowDate.fullDate];
  212. this.cale.multipleStatus.before = this.nowDate.fullDate;
  213. this.cale.multipleStatus.after = this.nowDate.fullDate;
  214. this.cale.setDate(this.date);
  215. this.nowDate = this.calendar = this.cale.getInfo(this.date);
  216. this.change();
  217. this.$nextTick((e) => {
  218. this.weeks = this.cale.weeks;
  219. });
  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 lang="scss" 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: $uni-bg-color-mask;
  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: $uni-border-color;
  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: $uni-border-color;
  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. .uni-calendar__backtoday {
  309. position: absolute;
  310. right: 0;
  311. top: 25rpx;
  312. padding: 0 5px;
  313. padding-left: 10px;
  314. height: 25px;
  315. line-height: 25px;
  316. font-size: 12px;
  317. border-top-left-radius: 25px;
  318. border-bottom-left-radius: 25px;
  319. color: $uni-text-color;
  320. background-color: $uni-bg-color-hover;
  321. }
  322. .uni-calendar__header-text {
  323. text-align: center;
  324. width: 100px;
  325. font-size: $uni-font-size-base;
  326. color: $uni-text-color;
  327. }
  328. .uni-calendar__header-btn-box {
  329. /* #ifndef APP-NVUE */
  330. display: flex;
  331. /* #endif */
  332. flex-direction: row;
  333. align-items: center;
  334. justify-content: center;
  335. width: 50px;
  336. height: 50px;
  337. }
  338. .uni-calendar__header-btn {
  339. width: 10px;
  340. height: 10px;
  341. border-left-color: $uni-text-color-placeholder;
  342. border-left-style: solid;
  343. border-left-width: 2px;
  344. border-top-color: $uni-color-subtitle;
  345. border-top-style: solid;
  346. border-top-width: 2px;
  347. }
  348. .uni-calendar--left {
  349. transform: rotate(-45deg);
  350. }
  351. .uni-calendar--right {
  352. transform: rotate(135deg);
  353. }
  354. .uni-calendar__weeks {
  355. position: relative;
  356. /* #ifndef APP-NVUE */
  357. display: flex;
  358. /* #endif */
  359. flex-direction: row;
  360. }
  361. .uni-calendar__weeks-item {
  362. flex: 1;
  363. }
  364. .uni-calendar__weeks-day {
  365. flex: 1;
  366. /* #ifndef APP-NVUE */
  367. display: flex;
  368. /* #endif */
  369. flex-direction: column;
  370. justify-content: center;
  371. align-items: center;
  372. height: 45px;
  373. border-bottom-color: #f5f5f5;
  374. border-bottom-style: solid;
  375. border-bottom-width: 1px;
  376. }
  377. .uni-calendar__weeks-day-text {
  378. font-size: 14px;
  379. }
  380. .uni-calendar__box {
  381. position: relative;
  382. }
  383. .uni-calendar__box-bg {
  384. /* #ifndef APP-NVUE */
  385. display: flex;
  386. /* #endif */
  387. justify-content: center;
  388. align-items: center;
  389. position: absolute;
  390. top: 0;
  391. left: 0;
  392. right: 0;
  393. bottom: 0;
  394. }
  395. .uni-calendar__box-bg-text {
  396. font-size: 200px;
  397. font-weight: bold;
  398. color: $uni-text-color-grey;
  399. opacity: 0.1;
  400. text-align: center;
  401. /* #ifndef APP-NVUE */
  402. line-height: 1;
  403. /* #endif */
  404. }
  405. </style>