海南旅游项目 前端仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

324 lines
7.5 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <template>
  2. <view class="calendar-box">
  3. <view class="month lf-p-l-10 lf-p-r-10">
  4. <view @click="lastMonth" v-if="havePre">上月</view>
  5. <view>{{year}}{{month}}</view>
  6. <view @click="nextMonth" v-if="haveNext">下月</view>
  7. </view>
  8. <view class="week">
  9. <view v-for="weeks in weekArr">{{weeks}}</view>
  10. </view>
  11. <view class="day">
  12. <view :class="[{'checkday':days.date==''},{'choose':days.flag==true}]"
  13. :style="'background:'+(days.flag==true?bgday:'')+';'" v-for="(days,index) in dayArr" :key="index">
  14. {{days.day}}
  15. <view :class="[{'circle':days.flag},{'repair':days.day<day},{'sign':days.day==day}]"></view>
  16. </view>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. props: {
  23. lang: {
  24. type: String,
  25. default: 'zh'
  26. },
  27. type: {
  28. type: String,
  29. default: 'calendar'
  30. },
  31. checkDate: {
  32. type: Boolean,
  33. default: false
  34. },
  35. bgweek: {
  36. type: String,
  37. default: '#1998FE'
  38. },
  39. bgday: {
  40. type: String,
  41. default: '#1998FE'
  42. },
  43. startTime: {
  44. type: String,
  45. default: ''
  46. },
  47. endTime: {
  48. type: String,
  49. default: ''
  50. }
  51. },
  52. data() {
  53. return {
  54. weeked: '', // 今天周几
  55. dayArr: [], // 当前月每日
  56. localDate: '', // 今天日期
  57. day: new Date().getDate(), // 当前日
  58. year: new Date().getFullYear(), // 当前年
  59. month: new Date().getMonth() + 1, // 当前月
  60. weekArr: ['日', '一', '二', '三', '四', '五', '六'], // 每周
  61. selfDays: 0,
  62. havePre: false,
  63. haveNext: false
  64. }
  65. },
  66. onShow() {
  67. },
  68. mounted() {
  69. let that = this;
  70. that.dateDiff(that.startTime,that.endTime);
  71. // 初始日期
  72. that.initDate();
  73. // 今天日期
  74. that.localDate = that.year + '-' + that.formatNum(that.month) + '-' + that.formatNum(that.day);
  75. // 中英切换
  76. if (that.lang != 'zh') that.weekArr = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
  77. // 今天周几
  78. that.weeked = that.weekArr[new Date().getDay()];
  79. // 签到功能
  80. if (that.type != 'calendar') {
  81. for (let i in that.dayArr) {
  82. let chooseList = []
  83. if(that.selfDays) {
  84. var dataStart = that.startTime.split('-');
  85. var endStart = that.endTime.split('-');
  86. let numberStart = Number(dataStart[2])
  87. let nowMonth = new Date().getMonth() + 1;
  88. let splitStartMonth = dataStart[1];
  89. let splitEndMonth = endStart[1];
  90. console.log('当前月',nowMonth)
  91. console.log('传来的月份',that.month)
  92. if(that.month < nowMonth) {
  93. console.log('小于当前月')
  94. this.havePre = true
  95. }else if(splitEndMonth > nowMonth){
  96. console.log('大于当前月')
  97. this.haveNext = true
  98. }
  99. var totalDays = Number(that.selfDays) + Number(dataStart[2]);
  100. for(let j = numberStart;j < totalDays;j++) {
  101. if(that.dayArr[i].day == j && that.dayArr[i].date) {
  102. that.$set(that.dayArr[i], 'flag', true);
  103. }
  104. }
  105. }
  106. // that.$set(that.dayArr[i], 'flag', false);
  107. }
  108. }
  109. },
  110. methods: {
  111. // 选择年月
  112. changeDate(e) {
  113. let that = this;
  114. that.year = parseInt(e.detail.value.split('-')[0]);
  115. that.month = parseInt(e.detail.value.split('-')[1]);
  116. that.initDate();
  117. },
  118. // 点击签到
  119. signToday(obj, index) {
  120. let that = this;
  121. // 指定签到类型可访问
  122. if (that.type == 'calendar') return;
  123. // 限制本月可进行签到
  124. if ((new Date().getMonth() + 1) != parseInt(obj.date.split('-')[1])) return;
  125. // 禁用非本月点击签到且限制签到日期小于等于本日
  126. if (obj.date != '' && obj.day <= that.day) {
  127. // 开启已签到提醒
  128. if (that.dayArr[index].flag) {
  129. uni.showToast({
  130. title: '已签到',
  131. icon: 'none'
  132. });
  133. } else {
  134. uni.showToast({
  135. title: that.day > obj.day ? '补签成功' : '签到成功',
  136. icon: 'success'
  137. });
  138. that.$set(that.dayArr[index], 'flag', true);
  139. that.$emit('change', obj.date);
  140. }
  141. }
  142. },
  143. dateDiff(sDate1,sDate2) {
  144. var aDate, oDate1, oDate2, iDays;
  145. aDate = sDate1.split('-');
  146. console.log('addate',aDate[1])
  147. if(aDate[1] == '01') {
  148. this.month = 1
  149. }else if (aDate[1] == '02') {
  150. this.month = 2
  151. }else if (aDate[1] == '03') {
  152. this.month = 3
  153. }else if (aDate[1] == '04') {
  154. this.month = 4
  155. }else if (aDate[1] == '05') {
  156. this.month = 5
  157. }else if (aDate[1] == '06') {
  158. this.month = 6
  159. }else if (aDate[1] == '07') {
  160. this.month = 7
  161. }else if (aDate[1] == '08') {
  162. this.month = 8
  163. }else if (aDate[1] == '09') {
  164. this.month = 9
  165. }else{
  166. this.month = Number(aDate[1])
  167. }
  168. if(aDate[1])
  169. oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);
  170. aDate = sDate2.split('-');
  171. oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);
  172. iDays = parseInt(Math.abs(oDate1 - oDate2) /1000/60/60/24);
  173. this.selfDays = iDays+1;
  174. return iDays + 1;
  175. },
  176. // 初始化日期
  177. initDate() {
  178. let that = this;
  179. that.dayArr = [];
  180. // 当前月总天数
  181. let totalDay = new Date(that.year, that.month, 0).getDate();
  182. // 遍历总天数将日期逐个添加至数组
  183. for (let i = 1; i <= totalDay; i++) {
  184. // 得到需补充天数
  185. let value = (new Date(that.year, that.month - 1, i)).getDay();
  186. // 补充前面空白日期
  187. if (i == 1 && value != 0) that.addBefore(value);
  188. // 添加本月日期
  189. let obj = {};
  190. obj.date = that.year + '-' + that.formatNum(that.month) + '-' + that.formatNum(i);
  191. obj.day = i;
  192. that.dayArr.push(obj);
  193. if (i == totalDay && value != 6) that.addAfter(value);
  194. }
  195. },
  196. // 补充前面空白日期
  197. addBefore(value) {
  198. let that = this;
  199. let totalDay = new Date(that.year, that.month - 1, 0).getDate();
  200. for (let i = 0; i < value; i++) {
  201. let obj = {};
  202. obj.date = '';
  203. obj.day = totalDay - (value - i) + 1;
  204. that.dayArr.push(obj);
  205. }
  206. },
  207. // 补充后空白日期
  208. addAfter(value) {
  209. let that = this;
  210. for (let i = 0; i < (6 - value); i++) {
  211. let obj = {};
  212. obj.date = '';
  213. obj.day = i + 1;
  214. that.dayArr.push(obj);
  215. }
  216. },
  217. // 格式化日期位数
  218. formatNum(num) {
  219. return num < 10 ? ('0' + num) : num;
  220. },
  221. // 上一个月
  222. lastMonth() {
  223. let that = this;
  224. if (that.month == 1) {
  225. that.year -= 1;
  226. that.month = 12;
  227. } else {
  228. that.month -= 1;
  229. }
  230. that.initDate();
  231. },
  232. // 下一个月
  233. nextMonth() {
  234. let that = this;
  235. if (that.month == 12) {
  236. that.year += 1;
  237. that.month = 1;
  238. } else {
  239. that.month += 1;
  240. }
  241. that.initDate();
  242. }
  243. }
  244. }
  245. </script>
  246. <style scoped>
  247. .calendar-box {
  248. width: 100%;
  249. flex-direction: column;
  250. justify-content: center;
  251. }
  252. .calendar-box,
  253. .month,
  254. .week,
  255. .day {
  256. display: flex;
  257. align-items: center;
  258. justify-content: space-between;
  259. }
  260. .month,
  261. .week,
  262. .day {
  263. width: 700rpx;
  264. }
  265. .month {
  266. margin: 30rpx 0;
  267. position: relative;
  268. }
  269. .picker {
  270. width: 160rpx;
  271. height: 40rpx;
  272. position: absolute;
  273. top: 20rpx;
  274. left: 50%;
  275. transform: translate(-50%, -50%);
  276. }
  277. .day {
  278. flex-wrap: wrap;
  279. }
  280. .week>view,
  281. .day>view {
  282. width: 100rpx;
  283. height: 100rpx;
  284. text-align: center;
  285. position: relative;
  286. line-height: 100rpx;
  287. }
  288. .checkday {
  289. color: #999;
  290. }
  291. .choose {
  292. color: #FFFFFF;
  293. border-radius: 50%;
  294. }
  295. .circle {
  296. width: 10rpx;
  297. height: 10rpx;
  298. border-radius: 50%;
  299. position: absolute;
  300. bottom: 10%;
  301. left: 50%;
  302. transform: translate(-50%, -50%);
  303. }
  304. .sign {
  305. background-color: #0089fe;
  306. }
  307. .repair {
  308. background-color: #1998FE;
  309. }
  310. </style>