CalendarCard.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. //苹果需要换成/
  101. date=this.toChangeSplit(date)
  102. this.getCanlendar(new Date(date))
  103. },
  104. //苹果需要换成/
  105. toChangeSplit(date){
  106. console.log('------------',date)
  107. return date.replace(/\-/g, '/');
  108. },
  109. chooseDate(item) {
  110. this.$emit('click',item)
  111. let strList = []; //滑动数
  112. this.choose = item.date;
  113. //和日历中间比看是是这个月 还是其他
  114. let current = this.current;
  115. //只有月才有前后翻
  116. if (this.getDateTime(item.date) < this.getDateTime(this.dateList[15].date) && (item.mm != this
  117. .dateList[15].mm)) {
  118. current = current - 1 == -1 ? 2 : current - 1;
  119. console.error('往前翻------', current)
  120. } else if (this.getDateTime(item.date) > this.getDateTime(this.dateList[15].date) && (item.mm != this
  121. .dateList[15].mm)) {
  122. console.error('往后翻------')
  123. current = current + 1 == 3 ? 0 : current + 1;
  124. }
  125. this.changeCurrent(current)
  126. },
  127. //转换毫秒
  128. getDateTime(date = '') {
  129. date=this.toChangeSplit(date)
  130. return new Date(date).getTime();
  131. },
  132. //滑动结束切换日历
  133. changeCurrent(current) {
  134. let str = '0,1,2,0'; //滑动数
  135. if (str.indexOf(this.current + ',' + current) > -1) {
  136. console.log('左滑了')
  137. this.getCanlendar(new Date(this.getDateTime(this.dateList[this.dateList.length - 1].date) + 24 * 60 *
  138. 60 * 1000))
  139. } else if (str.split(',').reverse().join(",").indexOf(this.current + ',' + current) > -1) {
  140. console.log('右滑了')
  141. this.getCanlendar(new Date(this.getDateTime(this.dateList[0].date) - 24 * 60 * 60 * 1000))
  142. }
  143. this.current = current;
  144. },
  145. setSwiperHeight() {
  146. this.swiperHeight =180 * Math.ceil(this.dateList.length / 7)
  147. },
  148. getCanlendar(date) {
  149. console.log(date)
  150. let d = this.getDateInfo(date);
  151. this.month = d.mm;
  152. let nowMonthDay = 0, //当前月的天数
  153. dateList = []; //日期列表
  154. /*1,3,5,7,8,10 31天*/
  155. /*4,6,9,11,12 30天*/
  156. /*2月份28,29*/
  157. if (d.mm == 2) {
  158. if (this.isRunYear(d.yy)) {
  159. nowMonthDay = 29;
  160. } else {
  161. nowMonthDay = 28;
  162. }
  163. } else if ([1, 3, 5, 7, 8, 10, 12].indexOf(d.mm) > -1) {
  164. nowMonthDay = 31;
  165. } else {
  166. nowMonthDay = 30;
  167. }
  168. let beforeDateList = [],
  169. afterDateList = [],
  170. lastDateTime = 0,
  171. lastDateXX = 1,
  172. beginDateTime = 0,
  173. beginDateXX = 1;
  174. for (let i = 1; i <= nowMonthDay; i++) {
  175. let dayxx = new Date(d.yy + '/' + d.mm + '/' + i).getDay() == 0 ? 7 : new Date(d.yy + '/' + d.mm +
  176. '/' + i)
  177. .getDay();
  178. if (i == 1) {
  179. beginDateXX = dayxx;
  180. beginDateTime = new Date(d.yy + '/' + d.mm + '/' + i).getTime();
  181. }
  182. if (nowMonthDay == i) {
  183. lastDateXX = dayxx;
  184. lastDateTime = new Date(d.yy + '/' + d.mm + '/' + i).getTime();
  185. }
  186. dateList.push(this.getDateInfo(d.yy + '/' + d.mm + '/' + i))
  187. }
  188. //最后一天不是星期日要补
  189. if (lastDateXX != 7) {
  190. for (let i = 1; i <= 7 - lastDateXX; i++) {
  191. afterDateList.push(this.getDateInfo(lastDateTime + i * 24 * 60 * 60 * 1000))
  192. }
  193. dateList = dateList.concat(afterDateList);
  194. }
  195. //第一天不是星期一要不
  196. if (beginDateXX != 1) {
  197. for (let i = beginDateXX - 1; i > 0; i--) {
  198. beforeDateList.push(this.getDateInfo(beginDateTime - i * 24 * 60 * 60 * 1000))
  199. }
  200. dateList = beforeDateList.concat(dateList);
  201. }
  202. this.dateList = dateList;
  203. this.$emit('nowDateList',dateList)
  204. console.log(this.dateList)
  205. if (this.firstLoading) {
  206. this.choose = this.today;
  207. this.firstLoading = false;
  208. } else {
  209. let haschoose = false;
  210. for (let i in dateList) {
  211. if (this.choose == dateList[i].date && this.choose.substring(0, 7) == dateList[15].date.substring(
  212. 0, 7)) {
  213. haschoose = true;
  214. break;
  215. }
  216. }
  217. if (!haschoose) {
  218. this.choose = this.dateList[15].ym + '-01';
  219. }
  220. }
  221. // this.$emit('getDate',this.getDateInfo(this.choose))
  222. //设置完数据后计算高度
  223. this.$nextTick(() => {
  224. this.setSwiperHeight();
  225. })
  226. },
  227. getDateInfo(date = new Date()) {
  228. let d = new Date(date);
  229. return {
  230. yy: d.getFullYear(),
  231. mm: d.getMonth() + 1,
  232. dd: d.getDate(),
  233. xx: d.getDay() == 0 ? 7 : d.getDay(),
  234. ym: d.getFullYear() + '-' + ((d.getMonth() + 1) > 9 ? (d.getMonth() + 1) : ('0' + (d.getMonth() +
  235. 1))),
  236. IDayCn:soloarlunar.solar2lunar(d.getFullYear(), d.getMonth() + 1,d.getDate()).IDayCn,
  237. date: d.getFullYear() + '-' + ((d.getMonth() + 1) > 9 ? (d.getMonth() + 1) : ('0' + (d.getMonth() +
  238. 1))) + '-' + (d.getDate() > 9 ? d.getDate() : ('0' + d.getDate()))
  239. }
  240. },
  241. // 判断是不是为闰年的函数
  242. isRunYear(year) {
  243. // 如果是闰年我们返回true 否则返回false
  244. var flag = false;
  245. if (year % 4 == 0 && year % 100 !== 0 || year % 400 == 0) {
  246. flag = true;
  247. }
  248. return flag;
  249. }
  250. }
  251. }
  252. </script>
  253. <style scoped lang="scss">
  254. .calendar {
  255. background: white;
  256. .calendar-title {
  257. font-weight: 600;
  258. }
  259. .calendar-grid {
  260. display: grid;
  261. grid-template-columns: repeat(7, 1fr);
  262. grid-gap: 1px;
  263. border-top: 1px solid #E4E4E4;
  264. border-bottom: 1px solid #E4E4E4;
  265. background: #E4E4E4;
  266. >view {
  267. text-align: center;
  268. color: #333;
  269. font-size: 32rpx;
  270. padding:5rpx 0;
  271. background: white;
  272. overflow: hidden;
  273. }
  274. .notexist {
  275. background: #FAFAFA;
  276. }
  277. .content-item {
  278. &.notexist-text {
  279. color: #999;
  280. .content-item-text1 {
  281. color: #999;
  282. }
  283. }
  284. .content-item-text1 {
  285. color: #333;
  286. }
  287. .content-item-text2 {
  288. font-size: 12rpx;
  289. white-space: nowrap;
  290. text-align: right;
  291. color: #BBBBBB;
  292. }
  293. .nowday,
  294. .content-item-text1 {
  295. border-radius: 50%;
  296. width: 38rpx;
  297. height: 38rpx;
  298. min-width: 38rpx;
  299. font-size: 12rpx;
  300. line-height: 38rpx;
  301. }
  302. .nowday {
  303. color: white;
  304. background: #F7913D;
  305. }
  306. }
  307. .calendar-grid-title {
  308. color: #999;
  309. font-size: 28rpx;
  310. padding: 0;
  311. }
  312. }
  313. }
  314. </style>