CalendarCard.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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">{{item.IDayCn}}</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">{{item.IDayCn}}</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. let d = this.getDateInfo(date);
  150. this.month = d.mm;
  151. let nowMonthDay = 0, //当前月的天数
  152. dateList = []; //日期列表
  153. /*1,3,5,7,8,10 31天*/
  154. /*4,6,9,11,12 30天*/
  155. /*2月份28,29*/
  156. if (d.mm == 2) {
  157. if (this.isRunYear(d.yy)) {
  158. nowMonthDay = 29;
  159. } else {
  160. nowMonthDay = 28;
  161. }
  162. } else if ([1, 3, 5, 7, 8, 10, 12].indexOf(d.mm) > -1) {
  163. nowMonthDay = 31;
  164. } else {
  165. nowMonthDay = 30;
  166. }
  167. let beforeDateList = [],
  168. afterDateList = [],
  169. lastDateTime = 0,
  170. lastDateXX = 1,
  171. beginDateTime = 0,
  172. beginDateXX = 1;
  173. for (let i = 1; i <= nowMonthDay; i++) {
  174. let dayxx = new Date(d.yy + '/' + d.mm + '/' + i).getDay() == 0 ? 7 : new Date(d.yy + '/' + d.mm +
  175. '/' + i)
  176. .getDay();
  177. if (i == 1) {
  178. beginDateXX = dayxx;
  179. beginDateTime = new Date(d.yy + '/' + d.mm + '/' + i).getTime();
  180. }
  181. if (nowMonthDay == i) {
  182. lastDateXX = dayxx;
  183. lastDateTime = new Date(d.yy + '/' + d.mm + '/' + i).getTime();
  184. }
  185. dateList.push(this.getDateInfo(d.yy + '/' + d.mm + '/' + i))
  186. }
  187. //最后一天不是星期日要补
  188. if (lastDateXX != 7) {
  189. for (let i = 1; i <= 7 - lastDateXX; i++) {
  190. afterDateList.push(this.getDateInfo(lastDateTime + i * 24 * 60 * 60 * 1000))
  191. }
  192. dateList = dateList.concat(afterDateList);
  193. }
  194. //第一天不是星期一要不
  195. if (beginDateXX != 1) {
  196. for (let i = beginDateXX - 1; i > 0; i--) {
  197. beforeDateList.push(this.getDateInfo(beginDateTime - i * 24 * 60 * 60 * 1000))
  198. }
  199. dateList = beforeDateList.concat(dateList);
  200. }
  201. this.dateList = dateList;
  202. this.$emit('nowDateList',dateList)
  203. console.log(this.dateList)
  204. if (this.firstLoading) {
  205. this.choose = this.today;
  206. this.firstLoading = false;
  207. } else {
  208. let haschoose = false;
  209. for (let i in dateList) {
  210. if (this.choose == dateList[i].date && this.choose.substring(0, 7) == dateList[15].date.substring(
  211. 0, 7)) {
  212. haschoose = true;
  213. break;
  214. }
  215. }
  216. if (!haschoose) {
  217. this.choose = this.dateList[15].ym + '-01';
  218. }
  219. }
  220. // this.$emit('getDate',this.getDateInfo(this.choose))
  221. //设置完数据后计算高度
  222. this.$nextTick(() => {
  223. this.setSwiperHeight();
  224. })
  225. },
  226. getDateInfo(date = new Date()) {
  227. let d = new Date(date);
  228. return {
  229. yy: d.getFullYear(),
  230. mm: d.getMonth() + 1,
  231. dd: d.getDate(),
  232. xx: d.getDay() == 0 ? 7 : d.getDay(),
  233. ym: d.getFullYear() + '-' + ((d.getMonth() + 1) > 9 ? (d.getMonth() + 1) : ('0' + (d.getMonth() +
  234. 1))),
  235. IDayCn:soloarlunar.solar2lunar(d.getFullYear(), d.getMonth() + 1,d.getDate()).IDayCn,
  236. date: d.getFullYear() + '-' + ((d.getMonth() + 1) > 9 ? (d.getMonth() + 1) : ('0' + (d.getMonth() +
  237. 1))) + '-' + (d.getDate() > 9 ? d.getDate() : ('0' + d.getDate()))
  238. }
  239. },
  240. // 判断是不是为闰年的函数
  241. isRunYear(year) {
  242. // 如果是闰年我们返回true 否则返回false
  243. var flag = false;
  244. if (year % 4 == 0 && year % 100 !== 0 || year % 400 == 0) {
  245. flag = true;
  246. }
  247. return flag;
  248. }
  249. }
  250. }
  251. </script>
  252. <style scoped lang="scss">
  253. .calendar {
  254. background: white;
  255. .calendar-title {
  256. font-weight: 600;
  257. }
  258. .calendar-grid {
  259. display: grid;
  260. grid-template-columns: repeat(7, 1fr);
  261. grid-gap: 1px;
  262. border-top: 1px solid #E4E4E4;
  263. border-bottom: 1px solid #E4E4E4;
  264. background: #E4E4E4;
  265. >view {
  266. text-align: center;
  267. color: #333;
  268. font-size: 32rpx;
  269. padding:5rpx 0;
  270. background: white;
  271. overflow: hidden;
  272. }
  273. .notexist {
  274. background: #FAFAFA;
  275. }
  276. .content-item {
  277. &.notexist-text {
  278. color: #999;
  279. .content-item-text1 {
  280. color: #999;
  281. }
  282. }
  283. .content-item-text1 {
  284. color: #333;
  285. }
  286. .content-item-text2 {
  287. font-size: 12rpx;
  288. white-space: nowrap;
  289. text-align: right;
  290. color: #BBBBBB;
  291. }
  292. .nowday,
  293. .content-item-text1 {
  294. border-radius: 50%;
  295. width: 38rpx;
  296. height: 38rpx;
  297. min-width: 38rpx;
  298. font-size: 12rpx;
  299. line-height: 38rpx;
  300. }
  301. .nowday {
  302. color: white;
  303. background: #F7913D;
  304. }
  305. }
  306. .calendar-grid-title {
  307. color: #999;
  308. font-size: 28rpx;
  309. padding: 0;
  310. }
  311. }
  312. }
  313. </style>