uni-calendar.vue 7.5 KB

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