pushangyuqi-calendar.vue 12 KB

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