calendar.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <!--
  2. * @Author: liuzhenlin 461480418@qq.ocm
  3. * @Date: 2023-01-13 14:29:53
  4. * @LastEditors: liuzhenlin
  5. * @LastEditTime: 2023-01-13 17:19:02
  6. * @Description: file content
  7. * @FilePath: \opms\components\calendar.vue
  8. -->
  9. <template>
  10. <view class="schedule">
  11. <view id="calendar">
  12. <!-- 年份 月份 -->
  13. <view class="header">
  14. <u-row>
  15. <u-col span="4">
  16. <view class="calendar-btn" @click="preMonth">
  17. <u-icon name="arrow-left" color="#969696" size="18"></u-icon>
  18. </view>
  19. </u-col>
  20. <u-col span="4">
  21. <view class="year-month text-center">{{ currentYear }}年{{ currentMonth }}月</view>
  22. </u-col>
  23. <u-col span="4">
  24. <view class="calendar-btn right" @click="nextMonth">
  25. <u-icon name="arrow-right" color="#969696" size="18"></u-icon>
  26. </view>
  27. </u-col>
  28. </u-row>
  29. </view>
  30. <!-- 星期 -->
  31. <view class="weekdays">
  32. <view class="week-item" v-for="item in fropmsun ? weekDaysFropmsun : weekDays" :key="item">
  33. <view class="week">{{ item }}</view>
  34. </view>
  35. </view>
  36. <!-- 日期 -->
  37. <view class="days" id="daybox">
  38. <view v-for="(dayobject, i) in days" :key="i" @click="getClickDay(dayobject)" class="days-item">
  39. <view class="day" :class="{ active: curDayMsg.date == dayobject.date }">
  40. <view v-if="currentDate == dayobject.date" class="curDay"></view>
  41. <view class="cday" :class="{ 'other-month': dayobject.cMonth != currentMonth }">{{ dayobject.cDay }}</view>
  42. <!-- 优先展示节日,其次,如果农历初一,展示当前农历月份,否则展示农历日期 -->
  43. <view v-if="showlunar" class="idaycn" :class="{ 'other-month': dayobject.cMonth != currentMonth }">
  44. <text class="festival" v-if="dayobject.Term">
  45. {{ dayobject.Term }}
  46. </text>
  47. <text class="festival" v-else-if="dayobject.lunarFestival">
  48. {{ dayobject.lunarFestival }}
  49. </text>
  50. <text class="festival" v-else-if="dayobject.festival">
  51. {{ dayobject.festival }}
  52. </text>
  53. <text v-else>
  54. {{ dayobject.IDayCn }}
  55. </text>
  56. </view>
  57. </view>
  58. </view>
  59. </view>
  60. </view>
  61. </view>
  62. </template>
  63. <script>
  64. import calendar from '../utils/date'
  65. export default {
  66. name: 'zpCalendar',
  67. data() {
  68. return {
  69. currentDate: '',
  70. currentDay: 0,
  71. currentMonth: 0,
  72. currentYear: 0,
  73. currentWeek: 0,
  74. days: [],
  75. curDayMsg: [],
  76. weekDays: ['一', '二', '三', '四', '五', '六', '日'],
  77. weekDaysFropmsun: ['日', '一', '二', '三', '四', '五', '六'],
  78. }
  79. },
  80. props: {
  81. showlunar: {
  82. type: Boolean,
  83. default: true,
  84. },
  85. lines: {
  86. type: Number,
  87. default: 6, //一页几行
  88. },
  89. fropmsun: {
  90. type: Boolean,
  91. default: false,
  92. },
  93. },
  94. created() {
  95. this.initData()
  96. },
  97. methods: {
  98. // 初始化
  99. initData(cur) {
  100. let now, curMonthStartDay, curMonthStartWeek, curPageStartDay
  101. if (cur) {
  102. now = new Date(cur)
  103. } else {
  104. now = new Date()
  105. }
  106. this.currentYear = now.getFullYear()
  107. this.currentMonth = now.getMonth() + 1
  108. this.currentDay = now.getDay()
  109. let d = new Date()
  110. let year = d.getFullYear()
  111. let month = d.getMonth() + 1
  112. let date = d.getDate()
  113. this.currentDate = `${year}-${month}-${date}`
  114. // 获取当前月第一天
  115. curMonthStartDay = new Date(this.formatDate(now.getFullYear(), now.getMonth() + 1, 1))
  116. // 当前月第一天是周几
  117. curMonthStartWeek = curMonthStartDay.getDay() // 1,2,3,4,5,6,0
  118. if (curMonthStartWeek == 0) {
  119. curMonthStartWeek = 7
  120. }
  121. // 日历当前页开始日期
  122. curPageStartDay =
  123. curMonthStartDay - (this.fropmsun ? curMonthStartWeek : curMonthStartWeek - 1) * 24 * 60 * 60 * 1000
  124. // 循环获取日历当前页所有日期(7*this.lines \5/6\)
  125. this.days = []
  126. for (let i = 0; i < this.lines * 7; i++) {
  127. let year = new Date(curPageStartDay + i * 24 * 60 * 60 * 1000).getFullYear()
  128. let month = new Date(curPageStartDay + i * 24 * 60 * 60 * 1000).getMonth() + 1
  129. let day = new Date(curPageStartDay + i * 24 * 60 * 60 * 1000).getDate()
  130. let obj = calendar.solar2lunar(year, month, day)
  131. this.days.push(obj)
  132. if (obj['lunarFestival'] == '春节') {
  133. this.days[i - 1]['lunarFestival'] = '除夕'
  134. }
  135. }
  136. for (let i = 0; i < this.days.length; i++) {
  137. // 母亲节 5月的第2个星期日
  138. if (this.days[i]['date'].substr(5, 10) == '5-1' && this.days[i + 14 - this.days[i]['nWeek']]) {
  139. this.days[i + 14 - this.days[i]['nWeek']]['lunarFestival'] = '母亲节'
  140. }
  141. // 父亲节 6月的第3个星期日
  142. if (this.days[i]['date'].substr(5, 10) == '6-1' && this.days[i + 21 - this.days[i]['nWeek']]) {
  143. this.days[i + 21 - this.days[i]['nWeek']]['lunarFestival'] = '父亲节'
  144. }
  145. }
  146. if (!cur) {
  147. this.curDayMsg = calendar.solar2lunar(this.currentYear, this.currentMonth, now.getDate())
  148. }
  149. },
  150. // 上一月
  151. preMonth() {
  152. this.currentMonth--
  153. if (this.currentMonth == 0) {
  154. this.currentMonth = 12
  155. this.currentYear--
  156. }
  157. this.initData(this.formatDate(this.currentYear, this.currentMonth, 1))
  158. },
  159. // 下一月
  160. nextMonth() {
  161. this.currentMonth++
  162. if (this.currentMonth == 13) {
  163. this.currentMonth = 1
  164. this.currentYear++
  165. }
  166. this.initData(this.formatDate(this.currentYear, this.currentMonth, 1))
  167. },
  168. // 点击日期
  169. getClickDay(el) {
  170. this.curDayMsg = el
  171. this.$emit('clickDate', el.date)
  172. },
  173. // 格式化 -> 2020-11-20
  174. formatDate(year, month, day) {
  175. if (month < 10) month = '0' + month
  176. if (day < 10) day = '0' + day
  177. return year + '-' + month + '-' + day
  178. },
  179. },
  180. mounted() {},
  181. }
  182. </script>
  183. <style lang="scss" scoped>
  184. .schedule {
  185. width: 100%;
  186. height: 100%;
  187. }
  188. #calendar {
  189. width: 100%;
  190. margin: 0 auto;
  191. overflow: hidden;
  192. padding: 3%;
  193. }
  194. .header {
  195. padding: 34rpx 44rpx 46rpx;
  196. .year-month {
  197. font-size: 32rpx;
  198. color: #323232;
  199. }
  200. }
  201. .weekdays {
  202. font-size: 28rpx;
  203. display: flex;
  204. color: #969696;
  205. justify-content: space-around;
  206. padding: 0 0 3% 0;
  207. .week-item {
  208. display: inline-block;
  209. width: 14.2%;
  210. .week {
  211. width: 78rpx;
  212. text-align: center;
  213. }
  214. }
  215. }
  216. .days {
  217. margin: 0;
  218. padding: 1% 0;
  219. display: flex;
  220. justify-content: space-around;
  221. flex-wrap: wrap;
  222. .days-item {
  223. display: inline-block;
  224. width: 14.2%;
  225. height: 78rpx;
  226. margin-bottom: 24rpx;
  227. text-align: center;
  228. color: #000;
  229. cursor: pointer;
  230. .day {
  231. position: relative;
  232. width: 78rpx;
  233. height: 78rpx;
  234. .other-month {
  235. color: #cdcdcd;
  236. .festival {
  237. color: #cdcdcd;
  238. }
  239. }
  240. .cday {
  241. font-size: 28rpx;
  242. display: inline-block;
  243. text-align: center;
  244. }
  245. }
  246. }
  247. .curDay {
  248. position: absolute;
  249. top: 0;
  250. width: 12rpx;
  251. height: 12rpx;
  252. background: #ffb83c;
  253. left: 0;
  254. right: 0;
  255. margin: auto;
  256. bottom: -62rpx;
  257. border-radius: 50%;
  258. }
  259. .active {
  260. width: 78rpx;
  261. height: 78rpx;
  262. text-align: center;
  263. border-radius: 50%;
  264. background: #2c92f9 !important;
  265. color: #fff;
  266. .idaycn,
  267. .festival {
  268. color: #fff;
  269. }
  270. }
  271. .vishidden {
  272. visibility: hidden;
  273. }
  274. .idaycn {
  275. color: #000;
  276. font-size: 18rpx;
  277. }
  278. .festival {
  279. color: #f84141;
  280. }
  281. }
  282. </style>