pushangyuqi-calendar.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. <template>
  2. <view class="calendar">
  3. <view class="table">
  4. <view class="chose-head">
  5. <view class="chose-item">
  6. <view class="btn" @click="yearsub">
  7. <image src="../../static/images/arrow_left12.png" mode=""></image>
  8. </view>
  9. <view class="year-td">
  10. <picker mode="selector" :range="years" @change="yearchange" :value="y">
  11. <view class="text">{{years[y]}}年</view>
  12. </picker>
  13. </view>
  14. <view class="btn" @click="yearadd">
  15. <image src="../../static/images/arrow_right12.png" mode=""></image>
  16. </view>
  17. </view>
  18. <view class="chose-item">
  19. <view class="btn" @click="monthsub">
  20. <image src="../../static/images/arrow_left12.png" mode=""></image>
  21. </view>
  22. <view class="month-td">
  23. <picker mode="selector" :range="months" @change="monthchange" :value="m">
  24. <view class="text">{{months[m]}}月</view>
  25. </picker>
  26. </view>
  27. <view class="btn" @click="monthadd">
  28. <image src="../../static/images/arrow_right12.png" mode=""></image>
  29. </view>
  30. </view>
  31. </view>
  32. <view class="week">
  33. <view class="item">日</view>
  34. <view class="item">一</view>
  35. <view class="item">二</view>
  36. <view class="item">三</view>
  37. <view class="item">四</view>
  38. <view class="item">五</view>
  39. <view class="item">六</view>
  40. </view>
  41. <view class="date-tr" v-for="(i,index) in dates" :key="index">
  42. <view v-for="(j,k) in i" :key="k" :style="{color:j[0]}" :class="j[2]=='#FF5C03'?'td active':'td'" @click="itemChose(j,index,k)">{{j[1]}}</view>
  43. </view>
  44. </view>
  45. </view>
  46. </template>
  47. <script>
  48. export default {
  49. data() {
  50. return {
  51. year: 2022,
  52. month: 12,
  53. date: 19,
  54. /* 年月是当天的日期,日是这个月日历的第一天,
  55. 其实就是上个月的最后一个周日,这里是随便定义的,
  56. 后面在mounted里面根据当前日期赋值 */
  57. years: [], //存放1900到2100共200年
  58. months: [], //存放1到12月
  59. y: 0,
  60. m: 0, //picker的迭代器
  61. dates: [
  62. [],
  63. [],
  64. [],
  65. [],
  66. [],
  67. []
  68. ], //外层括号表示一个月,内层括号表示一周,每个元素是一天
  69. activeDateStr: '', // 当前选中的时间
  70. }
  71. },
  72. mounted() {
  73. for (let i = 1900; i <= 2100; i++) {
  74. this.years.push(i);
  75. }
  76. for (let i = 1; i <= 12; i++) {
  77. this.months.push(i);
  78. }
  79. //获取当前时间和月份
  80. var date = new Date();
  81. this.year = date.getFullYear();
  82. this.month = date.getMonth() + 1;
  83. let dayNum = date.getDate();
  84. for (let i = 0; i < 200; i++) {
  85. if (this.years[i] == this.year) {
  86. this.y = i;
  87. break;
  88. }
  89. }
  90. for (let i = 0; i < 12; i++) {
  91. if (this.months[i] == this.month) {
  92. this.m = i;
  93. break;
  94. }
  95. }
  96. //当前月份的天数和上个月的天数
  97. var maxdays = this.maxdformat();
  98. var maxday = maxdays[0];
  99. var premaxday = maxdays[1];
  100. //通过当月一号的星期来计算上个月的最后一个星期日时几号
  101. date.setDate(1);
  102. var weekday = date.getDay();
  103. this.date = premaxday - weekday + 1;
  104. //编排这个月的日历
  105. this.datesformat(maxday, premaxday);
  106. let mothnum = this.months[this.m]
  107. if(mothnum<10) {
  108. mothnum = '0'+mothnum
  109. }
  110. if(dayNum<10) {
  111. dayNum = '0'+dayNum
  112. }
  113. this.activeDateStr = this.years[this.y] + '-' + mothnum + '-' + dayNum
  114. },
  115. methods: {
  116. //编排日历
  117. datesformat(maxday, premaxday) {
  118. //获取当前日期,用来突出显示当前日期
  119. var date = new Date();
  120. var y = date.getFullYear();
  121. var m = date.getMonth() + 1;
  122. var d = date.getDate();
  123. var i = 0,
  124. j = 0;
  125. for (i; i < 3; i++) {
  126. for (j = 0; j < 7; j++) {
  127. let cache = ['#222222', '', '#fff']; //第一个元素用来定义字体颜色,第二个是日期,第三个元素是背景色,用来突出显示当前日期
  128. if (this.date > 21 && this.date <= premaxday) {
  129. cache[0] = '#BBBBBB';
  130. }
  131. /* 上个月的日期显示为灰色,只能判断前半个月,
  132. 因为如果到这个月的21号之后的话造成这个月的日期也变为灰色,
  133. 选21是因为前3周不可能到21号(1号在周日的话会放在第二周),
  134. 并且上个月的最后一个星期日必然是21号之后 */
  135. else if (this.year == y && this.month == m && this.date == d) {
  136. cache[2] = '#FF5C03'; // 当前日期背景颜色
  137. cache[0] = '#fff'; // 当前日期文字颜色
  138. }
  139. if (this.date <= premaxday) {
  140. cache[1] = this.date++;
  141. } else {
  142. this.date = 1;
  143. cache[1] = this.date++; //日期递增,如果超过上个月的最后一天的话重新从1号开始
  144. }
  145. this.dates[i].push(cache);
  146. }
  147. }
  148. for (i; i < 6; i++) {
  149. for (j = 0; j < 7; j++) {
  150. let cache = ['#222222', '', "#fff"]
  151. if (this.date < 14 || this.date == maxday + 1) {
  152. cache[0] = '#BBBBBB'
  153. } else if (this.year == y && this.month == m && this.date == d) {
  154. cache[2] = '#FF5C03'
  155. cache[0] = '#fff'; // 当前日期文字颜色
  156. }
  157. if (this.date <= maxday) {
  158. cache[1] = this.date++;
  159. } else {
  160. this.date = 1;
  161. cache[1] = this.date++;
  162. }
  163. /* 基本与上半月相似,下个月的日期显示为灰色,
  164. 选14是因为第4周的第一天最早是15号(1号在周日并放在第二周的情况),
  165. 并且下个月出现最多日期的情况(2月1号是周一)也不会有14天 */
  166. this.dates[i].push(cache);
  167. }
  168. }
  169. },
  170. //计算当月的天数和上个月的天数
  171. maxdformat() {
  172. var maxday = 30;
  173. var premaxday = 30; //都定义为30天,这样小月就不用判断了
  174. if (this.month == 2) {
  175. if (this.year % 400 == 0 || (this.year % 4 == 0 && this.year % 100 != 0)) { //二月判断平闰年
  176. maxday = 29;
  177. } else {
  178. maxday = 28;
  179. }
  180. } else if (this.month == 1 || this.month == 3 || this.month == 5 || this.month == 7 ||
  181. this.month == 8 || this.month == 10 || this.month == 12) { //给大月赋值
  182. maxday = 31;
  183. }
  184. //计算上个月的天数,月份+1直接用上面的
  185. if (this.month == 3) {
  186. if (this.year % 400 == 0 || (this.year % 4 == 0 && this.year % 100 != 0)) {
  187. premaxday = 29;
  188. } else {
  189. premaxday = 28;
  190. }
  191. } else if (this.month == 2 || this.month == 4 || this.month == 6 || this.month == 8 ||
  192. this.month == 9 || this.month == 11 || this.month == 1) {
  193. premaxday = 31;
  194. }
  195. return [maxday, premaxday];
  196. },
  197. //下一个月
  198. monthadd() {
  199. //月份加一,十二月下一个月是一月
  200. if (this.month == 12) {
  201. this.y++;
  202. this.year++;
  203. this.m = 0;
  204. this.month = 1;
  205. } else {
  206. this.m++;
  207. this.month++;
  208. }
  209. var maxdays = this.maxdformat();
  210. var maxday = maxdays[0];
  211. var premaxday = maxdays[1];
  212. //这个月的最后一个周日,就是下个月日历的第一天
  213. if (this.dates[5][0][1] > 14) {
  214. this.date = this.dates[5][0][1];
  215. } else {
  216. this.date = this.dates[4][0][1];
  217. }
  218. this.dates = [
  219. [],
  220. [],
  221. [],
  222. [],
  223. [],
  224. []
  225. ]; //日历初始化
  226. this.datesformat(maxday, premaxday); //编排下个月的日历
  227. },
  228. //上一个月
  229. monthsub() {
  230. if (this.month == 1) {
  231. this.m = 11;
  232. this.month = 12;
  233. this.y--;
  234. this.year--;
  235. } else {
  236. this.m--;
  237. this.month--;
  238. }
  239. var maxdays = this.maxdformat();
  240. var maxday = maxdays[0];
  241. var premaxday = maxdays[1];
  242. //根据上个月的最后一个周日,计算上个月日历的第一天
  243. if (this.dates[0][0][1] <= 28) {
  244. this.date = premaxday + this.dates[0][0][1] - 28;
  245. } else {
  246. this.date = premaxday + this.dates[0][0][1] - 35;
  247. }
  248. this.dates = [
  249. [],
  250. [],
  251. [],
  252. [],
  253. [],
  254. []
  255. ];
  256. this.datesformat(maxday, premaxday)
  257. },
  258. //下一年,就是把下一个月运行12次
  259. yearadd() {
  260. for (let c = 0; c < 12; c++) {
  261. this.monthadd();
  262. }
  263. },
  264. //上一年,就是把上一个月运行12次
  265. yearsub() {
  266. for (let c = 0; c < 12; c++) {
  267. this.monthsub();
  268. }
  269. },
  270. //picker直接选择年份,计算中间差几年,然后运行几次上一年或下一年
  271. yearchange(e) {
  272. this.y = e.target.value
  273. let d = this.year - this.years[this.y];
  274. if (d > 0) {
  275. for (let i = 0; i < d; i++) {
  276. this.yearsub();
  277. }
  278. } else {
  279. for (let i = 0; i < 0 - d; i++) {
  280. this.yearadd();
  281. }
  282. }
  283. this.y += d;
  284. },
  285. //picker直接选择月份
  286. monthchange(e) {
  287. this.m = e.target.value
  288. let d = this.month - this.months[this.m];
  289. if (d > 0) {
  290. for (let i = 0; i < d; i++) {
  291. this.monthsub();
  292. }
  293. } else {
  294. for (let i = 0; i < 0 - d; i++) {
  295. this.monthadd();
  296. }
  297. }
  298. this.m += d;
  299. },
  300. // 日期选择
  301. itemChose(day,w,k) {
  302. if(day[0] == '#222222'){ // 取消所有选中数据
  303. for(let i=0;i<6;i++) {
  304. for(let j=0;j<7;j++) {
  305. if(this.dates[i][j][0] != '#BBBBBB' && this.dates[i][j][2] == '#FF5C03') {
  306. this.dates[i][j][2] = '#fff'
  307. this.dates[i][j][0] = '#222222'
  308. }
  309. }
  310. }
  311. this.dates[w][k][2] = '#FF5C03'
  312. this.dates[w][k][0] = '#fff'
  313. let newValue = ['#fff', day[1], "#FF5C03"]
  314. this.$set(this.dates[w], k, newValue)
  315. }
  316. let mothnum = this.months[this.m]
  317. if(mothnum<10) {
  318. mothnum = '0'+mothnum
  319. }
  320. let dayNum = day[1]
  321. if(dayNum<10) {
  322. dayNum = '0'+dayNum
  323. }
  324. this.activeDateStr = this.years[this.y] + '-' + mothnum + '-' + dayNum
  325. }
  326. },
  327. }
  328. </script>
  329. <style scoped lang="scss">
  330. .calendar{
  331. display: flex;
  332. flex-direction: column;
  333. justify-content: center;
  334. align-items: center;
  335. .chose-head{
  336. width: 100%;
  337. height: 88upx;
  338. border-bottom: 2upx solid #EEEEEE;
  339. display: flex;
  340. align-items: center;
  341. justify-content: space-between;
  342. padding: 0 97upx 0 67upx;
  343. .chose-item{
  344. display: flex;
  345. align-items: center;
  346. .btn{
  347. width: 40upx;
  348. height: 40upx;
  349. background: #F2F4F5;
  350. border-radius: 6upx;
  351. display: flex;
  352. align-items: center;
  353. justify-content: center;
  354. image{
  355. width: 11upx;
  356. height: 18upx;
  357. }
  358. }
  359. .year-td{
  360. width: 160upx;
  361. display: flex;
  362. align-items: center;
  363. justify-content: center;
  364. .text{
  365. font-size: 30upx;
  366. font-family: PingFang SC;
  367. font-weight: 500;
  368. color: #222222;
  369. }
  370. }
  371. .month-td{
  372. width: 100upx;
  373. display: flex;
  374. align-items: center;
  375. justify-content: center;
  376. .text{
  377. font-size: 30upx;
  378. font-family: PingFang SC;
  379. font-weight: 500;
  380. color: #222222;
  381. }
  382. }
  383. }
  384. }
  385. .table{
  386. width: 100%;
  387. .week{
  388. display: flex;
  389. align-items: center;
  390. justify-content: space-between;
  391. padding: 0 20upx;
  392. .item{
  393. width: 10%;
  394. height: 60upx;
  395. line-height: 60upx;
  396. margin: 12upx 0;
  397. font-size: 26upx;
  398. font-family: PingFang SC;
  399. font-weight: 500;
  400. color: #222222;
  401. text-align: center;
  402. }
  403. }
  404. .date-tr{
  405. display: flex;
  406. align-items: center;
  407. justify-content: space-between;
  408. padding: 0 20upx;
  409. .td{
  410. width: 10%;
  411. height: 60upx;
  412. line-height: 60upx;
  413. margin: 12upx 0;
  414. font-size: 26upx;
  415. text-align: center;
  416. font-family: PingFang SC;
  417. font-weight: 500;
  418. color: #222222;
  419. border-radius: 6upx;
  420. &.active{
  421. background-color: #FF5C03;
  422. }
  423. }
  424. }
  425. }
  426. }
  427. </style>