ss-calendar.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <template>
  2. <view class="calendar__wrap">
  3. <view class="header">
  4. <view class="current-date">{{ currentDate }}</view>
  5. </view>
  6. <view class="body">
  7. <view class="weeks">
  8. <view class="week__item" v-for="week in weeks" :key="week">{{ week }}</view>
  9. </view>
  10. <view class="day__list">
  11. <view class="day__item" v-for="(item, index) in dateData" :key="index">
  12. <view class="checked-box" :class="[checksClass]" v-if="item === 'checked'">
  13. <text class="checked iconfavor" v-if="!checksIcon"></text>
  14. <image v-else :src="checksIcon" mode="aspectFit"></image>
  15. <view class="check_text" v-if="checkTextShow">{{ dayLoad(index) }}</view>
  16. </view>
  17. <text class="current-box" v-else>{{ item }}</text>
  18. </view>
  19. </view>
  20. </view>
  21. <view class="header" v-if="nextMounth">
  22. <view class="current-date">{{ currentDate1 }}</view>
  23. </view>
  24. <view class="body" v-if="nextMounth">
  25. <view class="day__list">
  26. <view class="day__item" v-for="(item, index) in dateData1" :key="index">
  27. <view class="checked-box" :class="[checksClass]" v-if="item === 'checked'">
  28. <text class="checked iconfavor" v-if="!checksIcon"></text>
  29. <image v-else :src="checksIcon" mode="aspectFit"></image>
  30. <view class="check_text" v-if="checkTextShow">{{ dayLoad(index) }}</view>
  31. </view>
  32. <text class="current-box" v-else>{{ item }}</text>
  33. </view>
  34. </view>
  35. </view>
  36. </view>
  37. </template>
  38. <script>
  39. export default {
  40. props: {
  41. //选中的日期数据
  42. checks: {
  43. type: Array,
  44. default() {
  45. // 例子[1,2,3,4],表示本月1,2,3,4号4天被选中
  46. return [];
  47. }
  48. },
  49. nextMounth: {
  50. type: Boolean,
  51. default: false
  52. },
  53. // 选中物品的样式类
  54. checksClass: {
  55. type: String,
  56. default: ''
  57. },
  58. // 选中时图标
  59. checksIcon: {
  60. type: String
  61. },
  62. // 选中时是否显示文字
  63. checkTextShow: {
  64. type: Boolean,
  65. default: false
  66. },
  67. // 表示当前日期的样式
  68. actionClass: {
  69. type: String,
  70. default: ''
  71. }
  72. },
  73. data() {
  74. const { year, month, day } = this.getDate();
  75. const dateData = this.getDateData(year, month);
  76. const dateData1 = this.getDateData(year, month + 1);
  77. const noDay = new Date(year, month - 1, 1).getDay();
  78. const noDay1 = new Date(year, month, 1).getDay();
  79. return {
  80. year,
  81. month,
  82. day,
  83. dateData,
  84. dateData1,
  85. noDay, //本月1号是星期几
  86. noDay1, //下个月1号是星期几
  87. weeks: ['日', '一', '二', '三', '四', '五', '六'],
  88. };
  89. },
  90. computed: {
  91. // 获取当前日期
  92. currentDate() {
  93. return `${this.year}-${this.format(this.month)}`;
  94. },
  95. // 获取下个月日期
  96. currentDate1() {
  97. return `${this.year}-${this.format(this.month + 1)}`;
  98. }
  99. },
  100. watch: {
  101. checks(val) {
  102. console.log(val,"123456");
  103. const { year, month } = this.getDate();
  104. const dateData = this.getDateData(year, month);
  105. const dateData1 = this.getDateData1(year, month + 1);
  106. // 保存当前月份信息
  107. this.dateData = dateData;
  108. this.dateData1 = dateData1;
  109. }
  110. },
  111. methods: {
  112. // 重新计算时间
  113. dayLoad: function(value) {
  114. if(this.nextMounth) {
  115. return value + 1 - this.noDay1;
  116. }else {
  117. return value + 1 - this.noDay;
  118. }
  119. },
  120. // 获取当前日期
  121. getDate(current) {
  122. const date = current ? new Date(current) : new Date();
  123. const year = date.getFullYear();
  124. // 月份值默认从0开始
  125. const month = date.getMonth() + 1;
  126. const day = date.getDate();
  127. return {
  128. year,
  129. month,
  130. day
  131. };
  132. },
  133. // 日期处理
  134. getDateData(year, month) {
  135. // 新增月份时需要减少1个月
  136. const date = new Date(year, month - 1, 1);
  137. const firstDayWeek = date.getDay();
  138. let data
  139. if(this.nextMounth) {
  140. data = [...this.getEmptys(firstDayWeek), ...this.getDays1(firstDayWeek)];
  141. }else {
  142. data = [...this.getEmptys(firstDayWeek), ...this.getDays(firstDayWeek)];
  143. }
  144. const day = new Date().getDate();
  145. if(this.nextMounth) {
  146. const cd = data.indexOf(day);
  147. let del = (cd - cd%7);
  148. for(let i=0;i<del;i++) {
  149. data.shift()
  150. }
  151. }
  152. console.log(data);
  153. return data;
  154. },
  155. // 下个月日期处理
  156. getDateData1(year, month) {
  157. // 新增月份时需要减少1个月
  158. const date = new Date(year, month - 1, 1);
  159. const firstDayWeek = date.getDay();
  160. let data
  161. if(this.nextMounth) {
  162. data = [...this.getEmptys(firstDayWeek), ...this.getDays(firstDayWeek)];
  163. }else {
  164. data = [...this.getEmptys(firstDayWeek), ...this.getDays1(firstDayWeek)];
  165. }
  166. data.length = 14
  167. return data;
  168. },
  169. // 查询日期列表有几个空格
  170. getEmptys(count) {
  171. let arr = [];
  172. if (count) {
  173. for (let i = 0; i < count; i++) {
  174. arr.push('');
  175. }
  176. }
  177. return arr;
  178. },
  179. getLastDay() {
  180. let { year, month } = this.getDate();
  181. month += 1;
  182. if (month > 11) {
  183. year += 1;
  184. month = 1;
  185. }
  186. let firstDayTimeStamp = new Date(`${year}/${month}/1`).getTime();
  187. let oneDayTimeStamp = 24 * 60 * 60 * 1000;
  188. let lastDay = new Date(firstDayTimeStamp - oneDayTimeStamp).getDate();
  189. return lastDay;
  190. },
  191. getDays() {
  192. const lastDay = this.getLastDay();
  193. const days = [];
  194. for (let i = 1; i <= lastDay; i++) {
  195. days.push(this.checks.includes(i) ? 'checked' : i);
  196. }
  197. return days;
  198. },
  199. getDays1() {
  200. const lastDay = this.getLastDay();
  201. const days = [];
  202. for (let i = 1; i <= lastDay; i++) {
  203. days.push(this.checks.includes(i) ? 'checked' : i);
  204. }
  205. return days;
  206. },
  207. format(num) {
  208. return num < 10 ? `0${num}` : num;
  209. }
  210. }
  211. };
  212. </script>
  213. <style lang="scss" scoped>
  214. .calendar__wrap {
  215. background-color: #fff;
  216. color: $uni-text-color;
  217. .header {
  218. padding: 0 24rpx;
  219. .current-date {
  220. text-align: center;
  221. font-size: $font-lg + 2rpx;
  222. // border-bottom: 2rpx solid #eee;
  223. padding: 32rpx 0;
  224. }
  225. }
  226. .body {
  227. .weeks {
  228. display: flex;
  229. font-size: $font-lg;
  230. padding: 10rpx 0;
  231. background-color: #f4f7ff;
  232. .week__item {
  233. flex: 1;
  234. text-align: center;
  235. }
  236. }
  237. .day__list {
  238. display: flex;
  239. flex-wrap: wrap;
  240. .day__item {
  241. display: flex;
  242. justify-content: center;
  243. width: 14.285%;
  244. text-align: center;
  245. padding: 30rpx 0;
  246. font-size: 34rpx;
  247. color: $font-color-light;
  248. .checked-box,
  249. .current-box {
  250. display: flex;
  251. align-items: center;
  252. justify-content: center;
  253. border-radius: 100%;
  254. box-sizing: border-box;
  255. position: relative;
  256. }
  257. image,
  258. .checked-box,
  259. .current-box,
  260. .check_text {
  261. width: 56rpx;
  262. height: 56rpx;
  263. line-height: 56rpx;
  264. }
  265. image,
  266. .check_text {
  267. position: absolute;
  268. top: 0;
  269. left: 0;
  270. }
  271. .checked {
  272. width: 56rpx;
  273. height: 56rpx;
  274. border-radius: 6rpx;
  275. font-size: 40rpx;
  276. background-color: #fbed66;
  277. color: #fff;
  278. }
  279. .current {
  280. padding: 18rpx;
  281. background-color: #fbed66;
  282. color: #fff;
  283. font-size: 28rpx;
  284. }
  285. }
  286. }
  287. }
  288. }
  289. </style>