uni-calendar.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 le" @click="pre">
  15. <view class="iconfont icon-xiangzuohuabeifen"></view>
  16. </view>
  17. <text class="uni-calendar__header-text">{{ (nowDate.year||'') +'年'+( nowDate.month||'') +'月'}}</text>
  18. <view class="uni-calendar__header-btn-box ri" @click="next">
  19. <view class="iconfont icon-xiangyouhuabeifen"></view>
  20. </view>
  21. <!-- <text class="uni-calendar__backtoday" @click="backtoday">回到今天</text> -->
  22. </view>
  23. <!-- <view class="uni-calendar__box">
  24. <view class="uni-calendar__box-bg">
  25. <text class="uni-calendar__box-bg-text">{{nowDate.month}}</text>
  26. </view>
  27. <view class="uni-calendar__weeks" v-for="(item,weekIndex) in weeks" :key="weekIndex">
  28. <view class="uni-calendar__weeks-item" v-for="(weeks,weeksIndex) in item" :key="weeksIndex">
  29. <uni-calendar-item :weeks="weeks" :calendar="calendar" :selected="selected" :lunar="lunar" @change="choiceDate"></uni-calendar-item>
  30. </view>
  31. </view>
  32. </view> -->
  33. </view>
  34. </view>
  35. </template>
  36. <script>
  37. import Calendar from './util.js';
  38. import uniCalendarItem from './uni-calendar-item.vue'
  39. export default {
  40. components: {
  41. uniCalendarItem
  42. },
  43. props: {
  44. /**
  45. * 当前日期
  46. */
  47. date: {
  48. type: String,
  49. default: ''
  50. },
  51. /**
  52. * 打点日期
  53. */
  54. selected: {
  55. type: Array,
  56. default () {
  57. return []
  58. }
  59. },
  60. /**
  61. * 是否开启阴历日期
  62. */
  63. lunar: {
  64. type: Boolean,
  65. default: false
  66. },
  67. /**
  68. * 开始时间
  69. */
  70. startDate: {
  71. type: String,
  72. default: ''
  73. },
  74. /**
  75. * 结束时间
  76. */
  77. endDate: {
  78. type: String,
  79. default: ''
  80. },
  81. /**
  82. * 范围
  83. */
  84. range: {
  85. type: Boolean,
  86. default: false
  87. },
  88. /**
  89. * 插入
  90. */
  91. insert: {
  92. type: Boolean,
  93. default: true
  94. }
  95. },
  96. data() {
  97. return {
  98. show: false,
  99. weeks: [],
  100. calendar: {},
  101. nowDate: '',
  102. aniMaskShow: false
  103. }
  104. },
  105. watch: {
  106. selected(newVal) {
  107. this.cale.setSelectInfo(this.nowDate.fullDate, newVal)
  108. this.weeks = this.cale.weeks
  109. }
  110. },
  111. created() {
  112. // 获取日历方法实例
  113. this.cale = new Calendar({
  114. date: this.date,
  115. selected: this.selected,
  116. startDate: this.startDate,
  117. endDate: this.endDate,
  118. range: this.range,
  119. })
  120. this.init(this.cale.date.fullDate)
  121. },
  122. methods: {
  123. // 取消穿透
  124. clean() {},
  125. init(date) {
  126. this.weeks = this.cale.weeks
  127. this.nowDate = this.calendar = this.cale.getInfo(date)
  128. },
  129. open() {
  130. this.show = true
  131. this.$nextTick(() => {
  132. this.aniMaskShow = true
  133. })
  134. },
  135. close() {
  136. this.aniMaskShow = false
  137. this.$nextTick(() => {
  138. setTimeout(() => {
  139. this.show = false
  140. }, 300)
  141. })
  142. },
  143. confirm() {
  144. this.setEmit('confirm')
  145. this.close()
  146. },
  147. change() {
  148. if (!this.insert) return
  149. this.setEmit('change')
  150. },
  151. monthSwitch() {
  152. let {
  153. year,
  154. month
  155. } = this.nowDate
  156. this.$emit('monthSwitch', {
  157. year,
  158. month:Number(month)
  159. })
  160. },
  161. setEmit(name) {
  162. let {
  163. year,
  164. month,
  165. date,
  166. fullDate,
  167. lunar,
  168. extraInfo
  169. } = this.calendar
  170. this.$emit(name, {
  171. range: this.cale.multipleStatus,
  172. year,
  173. month,
  174. date,
  175. fulldate: fullDate,
  176. lunar,
  177. extraInfo: extraInfo || {}
  178. })
  179. },
  180. choiceDate(weeks) {
  181. if (weeks.disable) return
  182. this.calendar = weeks
  183. // 设置多选
  184. this.cale.setMultiple(this.calendar.fullDate)
  185. this.weeks = this.cale.weeks
  186. this.change()
  187. },
  188. backtoday() {
  189. this.cale.setDate(this.date)
  190. this.weeks = this.cale.weeks
  191. this.nowDate = this.calendar = this.cale.getInfo(this.date)
  192. this.change()
  193. },
  194. pre() {
  195. const preDate = this.cale.getDate(this.nowDate.fullDate, -1, 'month').fullDate
  196. this.setDate(preDate)
  197. this.monthSwitch()
  198. },
  199. next() {
  200. const nextDate = this.cale.getDate(this.nowDate.fullDate, +1, 'month').fullDate
  201. this.setDate(nextDate)
  202. this.monthSwitch()
  203. },
  204. setDate(date) {
  205. this.cale.setDate(date)
  206. this.weeks = this.cale.weeks
  207. this.nowDate = this.cale.getInfo(date)
  208. }
  209. }
  210. }
  211. </script>
  212. <style lang="scss" scoped>
  213. .uni-calendar {
  214. /* #ifndef APP-NVUE */
  215. display: flex;
  216. /* #endif */
  217. flex-direction: column;
  218. width: 702rpx;
  219. padding: 0 24rpx;
  220. background: #FFFFFF;
  221. }
  222. .uni-calendar__mask {
  223. position: fixed;
  224. bottom: 0;
  225. top: 0;
  226. left: 0;
  227. right: 0;
  228. background-color: $uni-bg-color-mask;
  229. transition-property: opacity;
  230. transition-duration: 0.3s;
  231. opacity: 0;
  232. /* #ifndef APP-NVUE */
  233. z-index: 99;
  234. /* #endif */
  235. }
  236. .uni-calendar--mask-show {
  237. opacity: 1
  238. }
  239. .uni-calendar--fixed {
  240. position: fixed;
  241. bottom: 0;
  242. left: 0;
  243. right: 0;
  244. transition-property: transform;
  245. transition-duration: 0.3s;
  246. transform: translateY(460px);
  247. /* #ifndef APP-NVUE */
  248. z-index: 99;
  249. /* #endif */
  250. }
  251. .uni-calendar--ani-show {
  252. transform: translateY(0);
  253. }
  254. .uni-calendar__content {
  255. background-color: #fff;
  256. }
  257. .uni-calendar__header {
  258. position: relative;
  259. /* #ifndef APP-NVUE */
  260. display: flex;
  261. /* #endif */
  262. flex-direction: row;
  263. justify-content: center;
  264. align-items: center;
  265. height: 50px;
  266. border-bottom-color: #EBEBEB;
  267. border-bottom-style: solid;
  268. border-bottom-width: 1px;
  269. }
  270. .uni-calendar--fixed-top {
  271. /* #ifndef APP-NVUE */
  272. display: flex;
  273. /* #endif */
  274. flex-direction: row;
  275. justify-content: space-between;
  276. border-top-color: $uni-border-color;
  277. border-top-style: solid;
  278. border-top-width: 1px;
  279. }
  280. .uni-calendar--fixed-width {
  281. width: 50px;
  282. // padding: 0 15px;
  283. }
  284. .uni-calendar__backtoday {
  285. position: absolute;
  286. right: 0;
  287. top: 25rpx;
  288. padding: 0 5px;
  289. padding-left: 10px;
  290. height: 25px;
  291. line-height: 25px;
  292. font-size: 12px;
  293. border-top-left-radius: 25px;
  294. border-bottom-left-radius: 25px;
  295. color: $uni-text-color;
  296. background-color: $uni-bg-color-hover;
  297. }
  298. .uni-calendar__header-text {
  299. text-align: center;
  300. width: 100px;
  301. font-size: $uni-font-size-base;
  302. color: $uni-text-color;
  303. }
  304. .uni-calendar__header-btn-box {
  305. /* #ifndef APP-NVUE */
  306. display: flex;
  307. /* #endif */
  308. flex-direction: row;
  309. align-items: center;
  310. justify-content: center;
  311. width: 50px;
  312. height: 50px;
  313. }
  314. .uni-calendar__header-btn-box.le{
  315. position: absolute;
  316. left:0;
  317. top:0;
  318. .iconfont{
  319. font-size: 36rpx;
  320. color: #E15616;
  321. }
  322. }
  323. .uni-calendar__header-btn-box.ri{
  324. position: absolute;
  325. right:0;
  326. top:0;
  327. .iconfont{
  328. font-size: 36rpx;
  329. color: #E15616;
  330. }
  331. }
  332. .uni-calendar__header-btn {
  333. width: 10px;
  334. height: 10px;
  335. border-left-color: $uni-text-color-placeholder;
  336. border-left-style: solid;
  337. border-left-width: 2px;
  338. border-top-color: $uni-color-subtitle;
  339. border-top-style: solid;
  340. border-top-width: 2px;
  341. }
  342. .uni-calendar--left {
  343. transform: rotate(-45deg);
  344. }
  345. .uni-calendar--right {
  346. transform: rotate(135deg);
  347. }
  348. .uni-calendar__weeks {
  349. position: relative;
  350. /* #ifndef APP-NVUE */
  351. display: flex;
  352. /* #endif */
  353. flex-direction: row;
  354. }
  355. .uni-calendar__weeks-item {
  356. flex: 1;
  357. }
  358. .uni-calendar__box {
  359. position: relative;
  360. }
  361. .uni-calendar__box-bg {
  362. /* #ifndef APP-NVUE */
  363. display: flex;
  364. /* #endif */
  365. justify-content: center;
  366. align-items: center;
  367. position: absolute;
  368. top: 0;
  369. left: 0;
  370. right: 0;
  371. bottom: 0;
  372. }
  373. .uni-calendar__box-bg-text {
  374. font-size: 200px;
  375. font-weight: bold;
  376. color: $uni-text-color-grey;
  377. opacity: 0.1;
  378. text-align: center;
  379. line-height: 1;
  380. }
  381. </style>