CalendarCard.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <template>
  2. <view class="calendar">
  3. <view class="calendar-grid mt-20 mb-20" style="border: none;background: white;">
  4. <view class="calendar-grid-title">一</view>
  5. <view class="calendar-grid-title">二</view>
  6. <view class="calendar-grid-title">三</view>
  7. <view class="calendar-grid-title">四</view>
  8. <view class="calendar-grid-title">五</view>
  9. <view class="calendar-grid-title" style="color: #F7913D;">六</view>
  10. <view class="calendar-grid-title" style="color: #F7913D;">日</view>
  11. </view>
  12. <swiper class="swiper" circular :style="{height:swiperHeight+'rpx'}" :current="current"
  13. @change="(e)=>changeCurrent(e.detail.current)">
  14. <swiper-item>
  15. <view class="calendar-grid">
  16. <!-- ,item.date==today?'nowday':'',item.date==choose?'chooseday':'' -->
  17. <view :class="[item.mm!=month?'notexist':'']" v-for="(item,index) in dateList" :key="index"
  18. @click="chooseDate(item)">
  19. <!-- <template v-if="!$scopedSlots.default"> -->
  20. <view class="content-item flex-n fac fjb" :class="[item.mm!=month?'notexist-text':'']">
  21. <view class="content-item-text1" :class="[item.date==today?'nowday':'']">{{item.dd}}
  22. </view>
  23. <view class="content-item-text2">{{item.IDayCn}}</view>
  24. </view>
  25. <!-- </template> -->
  26. <view style="min-height: 120rpx;">
  27. <slot :data="item"></slot>
  28. </view>
  29. </view>
  30. </view>
  31. </swiper-item>
  32. <swiper-item>
  33. <view class="calendar-grid">
  34. <!-- ,item.date==today?'nowday':'',item.date==choose?'chooseday':'' -->
  35. <view :class="[item.mm!=month?'notexist':'']" v-for="(item,index) in dateList" :key="index"
  36. @click="chooseDate(item)">
  37. <!-- <template v-if="!$scopedSlots.default"> -->
  38. <view class="content-item flex-n fac fjb" :class="[item.mm!=month?'notexist-text':'']">
  39. <view class="content-item-text1" :class="[item.date==today?'nowday':'']">{{item.dd}}
  40. </view>
  41. <view class="content-item-text2">初一</view>
  42. </view>
  43. <!-- </template> -->
  44. <view style="min-height: 120rpx;">
  45. <slot :data="item"></slot>
  46. </view>
  47. </view>
  48. </view>
  49. </swiper-item>
  50. <swiper-item>
  51. <view class="calendar-grid">
  52. <!-- ,item.date==today?'nowday':'',item.date==choose?'chooseday':'' -->
  53. <view :class="[item.mm!=month?'notexist':'']" v-for="(item,index) in dateList" :key="index"
  54. @click="chooseDate(item)">
  55. <!-- <template v-if="!$scopedSlots.default"> -->
  56. <view class="content-item flex-n fac fjb" :class="[item.mm!=month?'notexist-text':'']">
  57. <view class="content-item-text1" :class="[item.date==today?'nowday':'']">{{item.dd}}
  58. </view>
  59. <view class="content-item-text2">初一</view>
  60. </view>
  61. <!-- </template> -->
  62. <view style="min-height: 120rpx;">
  63. <slot :data="item"></slot>
  64. </view>
  65. </view>
  66. </view>
  67. </swiper-item>
  68. </swiper>
  69. </view>
  70. </template>
  71. <script>
  72. import soloarlunar from '@/common/js/solarlunar.js'
  73. export default {
  74. name: "CalendarCard",
  75. data() {
  76. return {
  77. dateList: [],
  78. month: 1,
  79. current: 0,
  80. swiperHeight: 0,
  81. xPos: 0,
  82. today: '',
  83. choose: '',
  84. firstLoading: true,
  85. };
  86. },
  87. watch: {
  88. choose(newval, oldval) {
  89. this.$emit('getDate', this.getDateInfo(newval))
  90. },
  91. },
  92. mounted() {
  93. this.today = this.getDateInfo().date;
  94. // this.getCanlendar(new Date())
  95. // console.error(this.$scopedSlots)
  96. },
  97. methods: {
  98. //设置日期
  99. setDate(date){
  100. this.getCanlendar(new Date(date))
  101. },
  102. chooseDate(item) {
  103. this.$emit('click',item)
  104. let strList = []; //滑动数
  105. this.choose = item.date;
  106. //和日历中间比看是是这个月 还是其他
  107. let current = this.current;
  108. //只有月才有前后翻
  109. if (this.getDateTime(item.date) < this.getDateTime(this.dateList[15].date) && (item.mm != this
  110. .dateList[15].mm)) {
  111. current = current - 1 == -1 ? 2 : current - 1;
  112. console.error('往前翻------', current)
  113. } else if (this.getDateTime(item.date) > this.getDateTime(this.dateList[15].date) && (item.mm != this
  114. .dateList[15].mm)) {
  115. console.error('往后翻------')
  116. current = current + 1 == 3 ? 0 : current + 1;
  117. }
  118. this.changeCurrent(current)
  119. },
  120. getDateTime(date = '') {
  121. return new Date(date).getTime();
  122. },
  123. //滑动结束切换日历
  124. changeCurrent(current) {
  125. let str = '0,1,2,0'; //滑动数
  126. if (str.indexOf(this.current + ',' + current) > -1) {
  127. console.log('左滑了')
  128. this.getCanlendar(new Date(new Date(this.dateList[this.dateList.length - 1].date).getTime() + 24 * 60 *
  129. 60 * 1000))
  130. } else if (str.split(',').reverse().join(",").indexOf(this.current + ',' + current) > -1) {
  131. console.log('右滑了')
  132. this.getCanlendar(new Date(new Date(this.dateList[0].date).getTime() - 24 * 60 * 60 * 1000))
  133. }
  134. this.current = current;
  135. },
  136. setSwiperHeight() {
  137. this.swiperHeight =180 * Math.ceil(this.dateList.length / 7)
  138. },
  139. getCanlendar(date) {
  140. console.log(date)
  141. let d = this.getDateInfo(date);
  142. this.month = d.mm;
  143. let nowMonthDay = 0, //当前月的天数
  144. dateList = []; //日期列表
  145. /*1,3,5,7,8,10 31天*/
  146. /*4,6,9,11,12 30天*/
  147. /*2月份28,29*/
  148. if (d.mm == 2) {
  149. if (this.isRunYear(d.yy)) {
  150. nowMonthDay = 29;
  151. } else {
  152. nowMonthDay = 28;
  153. }
  154. } else if ([1, 3, 5, 7, 8, 10, 12].indexOf(d.mm) > -1) {
  155. nowMonthDay = 31;
  156. } else {
  157. nowMonthDay = 30;
  158. }
  159. let beforeDateList = [],
  160. afterDateList = [],
  161. lastDateTime = 0,
  162. lastDateXX = 1,
  163. beginDateTime = 0,
  164. beginDateXX = 1;
  165. for (let i = 1; i <= nowMonthDay; i++) {
  166. let dayxx = new Date(d.yy + '-' + d.mm + '-' + i).getDay() == 0 ? 7 : new Date(d.yy + '-' + d.mm +
  167. '-' + i)
  168. .getDay();
  169. if (i == 1) {
  170. beginDateXX = dayxx;
  171. beginDateTime = new Date(d.yy + '-' + d.mm + '-' + i).getTime();
  172. }
  173. if (nowMonthDay == i) {
  174. lastDateXX = dayxx;
  175. lastDateTime = new Date(d.yy + '-' + d.mm + '-' + i).getTime();
  176. }
  177. dateList.push(this.getDateInfo(d.yy + '-' + d.mm + '-' + i))
  178. }
  179. //最后一天不是星期日要补
  180. if (lastDateXX != 7) {
  181. for (let i = 1; i <= 7 - lastDateXX; i++) {
  182. afterDateList.push(this.getDateInfo(lastDateTime + i * 24 * 60 * 60 * 1000))
  183. }
  184. dateList = dateList.concat(afterDateList);
  185. }
  186. //第一天不是星期一要不
  187. if (beginDateXX != 1) {
  188. for (let i = beginDateXX - 1; i > 0; i--) {
  189. beforeDateList.push(this.getDateInfo(beginDateTime - i * 24 * 60 * 60 * 1000))
  190. }
  191. dateList = beforeDateList.concat(dateList);
  192. }
  193. this.dateList = dateList;
  194. this.$emit('nowDateList',dateList)
  195. console.log(this.dateList)
  196. if (this.firstLoading) {
  197. this.choose = this.today;
  198. this.firstLoading = false;
  199. } else {
  200. let haschoose = false;
  201. for (let i in dateList) {
  202. if (this.choose == dateList[i].date && this.choose.substring(0, 7) == dateList[15].date.substring(
  203. 0, 7)) {
  204. haschoose = true;
  205. break;
  206. }
  207. }
  208. if (!haschoose) {
  209. this.choose = this.dateList[15].ym + '-01';
  210. }
  211. }
  212. // this.$emit('getDate',this.getDateInfo(this.choose))
  213. //设置完数据后计算高度
  214. this.$nextTick(() => {
  215. this.setSwiperHeight();
  216. })
  217. },
  218. getDateInfo(date = new Date()) {
  219. let d = new Date(date);
  220. return {
  221. yy: d.getFullYear(),
  222. mm: d.getMonth() + 1,
  223. dd: d.getDate(),
  224. xx: d.getDay() == 0 ? 7 : d.getDay(),
  225. ym: d.getFullYear() + '-' + ((d.getMonth() + 1) > 9 ? (d.getMonth() + 1) : ('0' + (d.getMonth() +
  226. 1))),
  227. IDayCn:soloarlunar.solar2lunar(d.getFullYear(), d.getMonth() + 1,d.getDate()).IDayCn,
  228. date: d.getFullYear() + '-' + ((d.getMonth() + 1) > 9 ? (d.getMonth() + 1) : ('0' + (d.getMonth() +
  229. 1))) + '-' + (d.getDate() > 9 ? d.getDate() : ('0' + d.getDate()))
  230. }
  231. },
  232. // 判断是不是为闰年的函数
  233. isRunYear(year) {
  234. // 如果是闰年我们返回true 否则返回false
  235. var flag = false;
  236. if (year % 4 == 0 && year % 100 !== 0 || year % 400 == 0) {
  237. flag = true;
  238. }
  239. return flag;
  240. }
  241. }
  242. }
  243. </script>
  244. <style scoped lang="scss">
  245. .calendar {
  246. background: white;
  247. .calendar-title {
  248. font-weight: 600;
  249. }
  250. .calendar-grid {
  251. display: grid;
  252. grid-template-columns: repeat(7, 1fr);
  253. grid-gap: 1px;
  254. border-top: 1px solid #E4E4E4;
  255. border-bottom: 1px solid #E4E4E4;
  256. background: #E4E4E4;
  257. >view {
  258. text-align: center;
  259. color: #333;
  260. font-size: 32rpx;
  261. padding:5rpx 0;
  262. background: white;
  263. overflow: hidden;
  264. }
  265. .notexist {
  266. background: #FAFAFA;
  267. }
  268. .content-item {
  269. &.notexist-text {
  270. color: #999;
  271. .content-item-text1 {
  272. color: #999;
  273. }
  274. }
  275. .content-item-text1 {
  276. color: #333;
  277. }
  278. .content-item-text2 {
  279. font-size: 12rpx;
  280. white-space: nowrap;
  281. text-align: right;
  282. color: #BBBBBB;
  283. }
  284. .nowday,
  285. .content-item-text1 {
  286. border-radius: 50%;
  287. width: 38rpx;
  288. height: 38rpx;
  289. min-width: 38rpx;
  290. font-size: 12rpx;
  291. line-height: 38rpx;
  292. }
  293. .nowday {
  294. color: white;
  295. background: #F7913D;
  296. }
  297. }
  298. .calendar-grid-title {
  299. color: #999;
  300. font-size: 28rpx;
  301. padding: 0;
  302. }
  303. }
  304. }
  305. </style>