oa-calendar.vue 8.2 KB

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