pushangyuqi-calendar.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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: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. console.log(this.dates,'array')
  185. },
  186. //计算当月的天数和上个月的天数
  187. maxdformat() {
  188. var maxday = 30;
  189. var premaxday = 30; //都定义为30天,这样小月就不用判断了
  190. if (this.month == 2) {
  191. if (this.year % 400 == 0 || (this.year % 4 == 0 && this.year % 100 != 0)) { //二月判断平闰年
  192. maxday = 29;
  193. } else {
  194. maxday = 28;
  195. }
  196. } else if (this.month == 1 || this.month == 3 || this.month == 5 || this.month == 7 ||
  197. this.month == 8 || this.month == 10 || this.month == 12) { //给大月赋值
  198. maxday = 31;
  199. }
  200. //计算上个月的天数,月份+1直接用上面的
  201. if (this.month == 3) {
  202. if (this.year % 400 == 0 || (this.year % 4 == 0 && this.year % 100 != 0)) {
  203. premaxday = 29;
  204. } else {
  205. premaxday = 28;
  206. }
  207. } else if (this.month == 2 || this.month == 4 || this.month == 6 || this.month == 8 ||
  208. this.month == 9 || this.month == 11 || this.month == 1) {
  209. premaxday = 31;
  210. }
  211. return [maxday, premaxday];
  212. },
  213. //下一个月
  214. monthadd() {
  215. //月份加一,十二月下一个月是一月
  216. if (this.month == 12) {
  217. this.y++;
  218. this.year++;
  219. this.m = 0;
  220. this.month = 1;
  221. } else {
  222. this.m++;
  223. this.month++;
  224. }
  225. var maxdays = this.maxdformat();
  226. var maxday = maxdays[0];
  227. var premaxday = maxdays[1];
  228. //这个月的最后一个周日,就是下个月日历的第一天
  229. if (this.dates[5][0][1] > 14) {
  230. this.date = this.dates[5][0][1];
  231. } else {
  232. this.date = this.dates[4][0][1];
  233. }
  234. this.dates = [
  235. [],
  236. [],
  237. [],
  238. [],
  239. [],
  240. []
  241. ]; //日历初始化
  242. this.datesformat(maxday, premaxday); //编排下个月的日历
  243. },
  244. //上一个月
  245. monthsub() {
  246. if (this.month == 1) {
  247. this.m = 11;
  248. this.month = 12;
  249. this.y--;
  250. this.year--;
  251. } else {
  252. this.m--;
  253. this.month--;
  254. }
  255. var maxdays = this.maxdformat();
  256. var maxday = maxdays[0];
  257. var premaxday = maxdays[1];
  258. //根据上个月的最后一个周日,计算上个月日历的第一天
  259. if (this.dates[0][0][1] <= 28) {
  260. this.date = premaxday + this.dates[0][0][1] - 28;
  261. } else {
  262. this.date = premaxday + this.dates[0][0][1] - 35;
  263. }
  264. this.dates = [
  265. [],
  266. [],
  267. [],
  268. [],
  269. [],
  270. []
  271. ];
  272. this.datesformat(maxday, premaxday)
  273. },
  274. //下一年,就是把下一个月运行12次
  275. yearadd() {
  276. for (let c = 0; c < 12; c++) {
  277. this.monthadd();
  278. }
  279. },
  280. //上一年,就是把上一个月运行12次
  281. yearsub() {
  282. for (let c = 0; c < 12; c++) {
  283. this.monthsub();
  284. }
  285. },
  286. //picker直接选择年份,计算中间差几年,然后运行几次上一年或下一年
  287. yearchange(e) {
  288. this.y = e.target.value
  289. let d = this.year - this.years[this.y];
  290. if (d > 0) {
  291. for (let i = 0; i < d; i++) {
  292. this.yearsub();
  293. }
  294. } else {
  295. for (let i = 0; i < 0 - d; i++) {
  296. this.yearadd();
  297. }
  298. }
  299. this.y += d;
  300. },
  301. //picker直接选择月份
  302. monthchange(e) {
  303. this.m = e.target.value
  304. let d = this.month - this.months[this.m];
  305. if (d > 0) {
  306. for (let i = 0; i < d; i++) {
  307. this.monthsub();
  308. }
  309. } else {
  310. for (let i = 0; i < 0 - d; i++) {
  311. this.monthadd();
  312. }
  313. }
  314. this.m += d;
  315. },
  316. // 日期选择
  317. itemChose(day,w,k) {
  318. if(day[0] == '#222222'){ // 取消所有选中数据
  319. for(let i=0;i<6;i++) {
  320. for(let j=0;j<7;j++) {
  321. if(this.dates[i][j][0] != '#BBBBBB' && this.dates[i][j][2] == '#008FD3') {
  322. this.dates[i][j][2] = '#fff'
  323. this.dates[i][j][0] = '#222222'
  324. }
  325. }
  326. }
  327. this.dates[w][k][2] = '#008FD3'
  328. this.dates[w][k][0] = '#fff'
  329. let newValue = ['#fff', day[1], "#008FD3"]
  330. this.$set(this.dates[w], k, newValue)
  331. }
  332. let mothnum = this.months[this.m]
  333. if(mothnum<10) {
  334. mothnum = '0'+mothnum
  335. }
  336. let dayNum = day[1]
  337. if(dayNum<10) {
  338. dayNum = '0'+dayNum
  339. }
  340. this.activeDateStr = this.years[this.y] + '-' + mothnum + '-' + dayNum
  341. },
  342. submit(){
  343. console.log(this.activeDateStr,'===')
  344. this.$emit('onDayClick', this.activeDateStr);
  345. }
  346. },
  347. }
  348. </script>
  349. <style scoped lang="scss">
  350. .calendar{
  351. display: flex;
  352. flex-direction: column;
  353. justify-content: center;
  354. align-items: center;
  355. .chose-head{
  356. width: 100%;
  357. height: 88upx;
  358. border-bottom: 2upx solid #ECECEC;
  359. display: flex;
  360. align-items: center;
  361. justify-content: space-between;
  362. // padding: 0 97upx 0 67upx;
  363. .chose-item{
  364. display: flex;
  365. align-items: center;
  366. .btn{
  367. width: 48upx;
  368. height: 48upx;
  369. background: #EFF3F7;
  370. border-radius: 8upx;
  371. display: flex;
  372. align-items: center;
  373. justify-content: center;
  374. image{
  375. width: 11upx;
  376. height: 18upx;
  377. }
  378. }
  379. .year-td{
  380. width: 160upx;
  381. display: flex;
  382. align-items: center;
  383. justify-content: center;
  384. .text{
  385. font-size: 28upx;
  386. font-family: PingFang SC;
  387. font-weight: 500;
  388. color: #222222;
  389. }
  390. }
  391. .month-td{
  392. width: 100upx;
  393. display: flex;
  394. align-items: center;
  395. justify-content: center;
  396. .text{
  397. font-size: 30upx;
  398. font-family: PingFang SC;
  399. font-weight: 500;
  400. color: #222222;
  401. }
  402. }
  403. }
  404. }
  405. .table{
  406. width: 100%;
  407. .week{
  408. display: flex;
  409. align-items: center;
  410. justify-content: space-between;
  411. padding: 0 20upx;
  412. border-bottom: 2upx solid #ECECEC;
  413. .item{
  414. width: 10%;
  415. height: 60upx;
  416. line-height: 60upx;
  417. margin: 12upx 0;
  418. font-size: 24upx;
  419. font-family: PingFang SC;
  420. font-weight: 400;
  421. color: #222222;
  422. text-align: center;
  423. // &:first-child{
  424. // color: #FF7700;
  425. // }
  426. // &:last-child{
  427. // color: #FF7700;
  428. // }
  429. }
  430. }
  431. .date-tr{
  432. display: flex;
  433. align-items: center;
  434. justify-content: space-between;
  435. padding: 0 20upx;
  436. .td{
  437. width: 10%;
  438. height: 56upx;
  439. line-height: 56upx;
  440. margin: 12upx 0;
  441. font-size: 28upx;
  442. text-align: center;
  443. font-family: PingFang SC;
  444. font-weight: 600;
  445. color: #222222;
  446. border-radius: 8upx;
  447. &.active{
  448. background-color: #008FD3;
  449. }
  450. }
  451. }
  452. .btn-box {
  453. // height: 120upx;
  454. padding-top: 30rpx;
  455. display: flex;
  456. align-items: center;
  457. justify-content: center;
  458. .sub-btn {
  459. width: 480upx;
  460. height: 88upx;
  461. line-height: 88upx;
  462. text-align: center;
  463. font-size: 32upx;
  464. font-family: PingFang SC;
  465. font-weight: bold;
  466. color: #FFFFFF;
  467. background: #008FD3 ;
  468. border-radius: 44upx;
  469. // margin-bottom: 40upx;
  470. display: flex;
  471. align-items: center;
  472. justify-content: center;
  473. }
  474. }
  475. }
  476. }
  477. </style>