pushangyuqi-calendar.vue 12 KB

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