calendar.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. console.log(this.currentDate)
  115. // 获取当前月第一天
  116. curMonthStartDay = new Date(this.formatDate(now.getFullYear(), now.getMonth() + 1, 1))
  117. // 当前月第一天是周几
  118. curMonthStartWeek = curMonthStartDay.getDay() // 1,2,3,4,5,6,0
  119. if (curMonthStartWeek == 0) {
  120. curMonthStartWeek = 7
  121. }
  122. // 日历当前页开始日期
  123. curPageStartDay =
  124. curMonthStartDay - (this.fropmsun ? curMonthStartWeek : curMonthStartWeek - 1) * 24 * 60 * 60 * 1000
  125. // 循环获取日历当前页所有日期(7*this.lines \5/6\)
  126. this.days = []
  127. for (let i = 0; i < this.lines * 7; i++) {
  128. let year = new Date(curPageStartDay + i * 24 * 60 * 60 * 1000).getFullYear()
  129. let month = new Date(curPageStartDay + i * 24 * 60 * 60 * 1000).getMonth() + 1
  130. let day = new Date(curPageStartDay + i * 24 * 60 * 60 * 1000).getDate()
  131. let obj = calendar.solar2lunar(year, month, day)
  132. this.days.push(obj)
  133. if (obj['lunarFestival'] == '春节') {
  134. this.days[i - 1]['lunarFestival'] = '除夕'
  135. }
  136. }
  137. for (let i = 0; i < this.days.length; i++) {
  138. // 母亲节 5月的第2个星期日
  139. if (this.days[i]['date'].substr(5, 10) == '5-1' && this.days[i + 14 - this.days[i]['nWeek']]) {
  140. this.days[i + 14 - this.days[i]['nWeek']]['lunarFestival'] = '母亲节'
  141. }
  142. // 父亲节 6月的第3个星期日
  143. if (this.days[i]['date'].substr(5, 10) == '6-1' && this.days[i + 21 - this.days[i]['nWeek']]) {
  144. this.days[i + 21 - this.days[i]['nWeek']]['lunarFestival'] = '父亲节'
  145. }
  146. }
  147. if (!cur) {
  148. this.curDayMsg = calendar.solar2lunar(this.currentYear, this.currentMonth, now.getDate())
  149. }
  150. },
  151. // 上一月
  152. preMonth() {
  153. this.currentMonth--
  154. if (this.currentMonth == 0) {
  155. this.currentMonth = 12
  156. this.currentYear--
  157. }
  158. this.initData(this.formatDate(this.currentYear, this.currentMonth, 1))
  159. },
  160. // 下一月
  161. nextMonth() {
  162. this.currentMonth++
  163. if (this.currentMonth == 13) {
  164. this.currentMonth = 1
  165. this.currentYear++
  166. }
  167. this.initData(this.formatDate(this.currentYear, this.currentMonth, 1))
  168. },
  169. // 点击日期
  170. getClickDay(el) {
  171. this.curDayMsg = el
  172. this.$emit('clickDate', el.date)
  173. },
  174. // 格式化 -> 2020-11-20
  175. formatDate(year, month, day) {
  176. if (month < 10) month = '0' + month
  177. if (day < 10) day = '0' + day
  178. return year + '-' + month + '-' + day
  179. },
  180. },
  181. mounted() {},
  182. }
  183. </script>
  184. <style lang="scss" scoped>
  185. .schedule {
  186. width: 100%;
  187. height: 100%;
  188. }
  189. #calendar {
  190. width: 100%;
  191. margin: 0 auto;
  192. overflow: hidden;
  193. padding: 3%;
  194. }
  195. .header {
  196. padding: 34rpx 44rpx 46rpx;
  197. .year-month {
  198. font-size: 32rpx;
  199. color: #323232;
  200. }
  201. }
  202. .weekdays {
  203. font-size: 28rpx;
  204. display: flex;
  205. color: #969696;
  206. justify-content: space-around;
  207. padding: 0 0 3% 0;
  208. .week-item {
  209. display: inline-block;
  210. width: 14.2%;
  211. .week {
  212. width: 78rpx;
  213. text-align: center;
  214. }
  215. }
  216. }
  217. .days {
  218. margin: 0;
  219. padding: 1% 0;
  220. display: flex;
  221. justify-content: space-around;
  222. flex-wrap: wrap;
  223. .days-item {
  224. display: inline-block;
  225. width: 14.2%;
  226. height: 78rpx;
  227. margin-bottom: 24rpx;
  228. text-align: center;
  229. color: #000;
  230. cursor: pointer;
  231. .day {
  232. position: relative;
  233. width: 78rpx;
  234. height: 78rpx;
  235. .other-month {
  236. color: #cdcdcd;
  237. .festival {
  238. color: #cdcdcd;
  239. }
  240. }
  241. .cday {
  242. font-size: 28rpx;
  243. display: inline-block;
  244. text-align: center;
  245. }
  246. }
  247. }
  248. .curDay {
  249. position: absolute;
  250. top: 0;
  251. width: 12rpx;
  252. height: 12rpx;
  253. background: #ffb83c;
  254. left: 0;
  255. right: 0;
  256. margin: auto;
  257. bottom: -62rpx;
  258. border-radius: 50%;
  259. }
  260. .active {
  261. width: 78rpx;
  262. height: 78rpx;
  263. text-align: center;
  264. border-radius: 50%;
  265. background: #2c92f9 !important;
  266. color: #fff;
  267. .idaycn,
  268. .festival {
  269. color: #fff;
  270. }
  271. }
  272. .vishidden {
  273. visibility: hidden;
  274. }
  275. .idaycn {
  276. color: #000;
  277. font-size: 18rpx;
  278. }
  279. .festival {
  280. color: #f84141;
  281. }
  282. }
  283. </style>