checkIn.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. <template>
  2. <view class="attendance-page">
  3. <view class="header">
  4. <text class="title">我的考勤记录</text>
  5. <picker mode="selector" :range="timeRanges" @change="onTimeRangeChange">
  6. <view class="picker">
  7. {{ selectedTimeRange }}/切换
  8. </view>
  9. </picker>
  10. </view>
  11. <!-- 数据统计区域 -->
  12. <view class="statistics" :class="{ 'show': chartShow }">
  13. <view class="statistic-card">
  14. <view class="statistic-title">{{ selectedTimeRange }}考勤统计</view>
  15. <view class="statistic-item">
  16. <view>
  17. <text class="ygoa-icon icon-attendance"></text>
  18. <text class="label">出勤:</text>
  19. </view>
  20. <text class="value">{{ attendanceCount }}</text>
  21. </view>
  22. <view class="statistic-item">
  23. <view>
  24. <text class="ygoa-icon icon-goOut"></text>
  25. <text class="label">外出:</text>
  26. </view>
  27. <text class="value">{{ goOutCount }}</text>
  28. </view>
  29. <view class="statistic-item">
  30. <view>
  31. <text class="ygoa-icon icon-late"></text>
  32. <text class="label">迟到:</text>
  33. </view>
  34. <text class="value">{{ lateCount }}</text>
  35. </view>
  36. <view class="statistic-item">
  37. <view>
  38. <text class="ygoa-icon icon-leaveEarly"></text>
  39. <text class="label">早退:</text>
  40. </view>
  41. <text class="value">{{ leaveEarlyCount }}</text>
  42. </view>
  43. <view class="statistic-item">
  44. <view>
  45. <text class="ygoa-icon icon-absenteeism"></text>
  46. <text class="label">缺勤:</text>
  47. </view>
  48. <text class="value">{{ absenteeismCount }}</text>
  49. </view>
  50. </view>
  51. </view>
  52. <!-- 今日签到信息区域 -->
  53. <view class="todayCheckIn" :class="{ 'show': shouldShow }">
  54. <view class="check-in-container">
  55. <text class="title2">今日签到信息</text>
  56. <view class="info-row">
  57. <view>
  58. <text class="ygoa-icon icon-date"></text>
  59. <text class="value">日期:</text>
  60. </view>
  61. <text class="label">{{ todayData.day }}</text>
  62. </view>
  63. <view class="info-row">
  64. <view>
  65. <text class="ygoa-icon icon-day"></text>
  66. <text class="value">晨签时间:</text>
  67. </view>
  68. <text class="label">{{ todayData.startTime || '未打卡' }} </text>
  69. </view>
  70. <view class="info-row">
  71. <view>
  72. <text class="ygoa-icon icon-night"></text>
  73. <text class="value">晚签时间:</text>
  74. </view>
  75. <text class="label">{{ todayData.endTime || '未打卡' }} </text>
  76. </view>
  77. <view class="info-row">
  78. <view>
  79. <text class="ygoa-icon icon-checkInStatus"></text>
  80. <text class="value">状态:</text>
  81. </view>
  82. <text class="label">{{ todayData.status }}</text>
  83. </view>
  84. </view>
  85. </view>
  86. <!-- 图表区域 -->
  87. <view class="chart-container" :class="{ 'show': chartShow }">
  88. <view class="">
  89. <text class="chart-title">考勤趋势图</text>
  90. <view class="charts-box">
  91. <qiun-data-charts :type="chartsType" :opts="opts" :chartData="chartData" />
  92. </view>
  93. </view>
  94. </view>
  95. </view>
  96. </template>
  97. <script setup>
  98. import { ref, onMounted, reactive } from 'vue';
  99. import { checkAttendance, getMyTotalCount } from '@/api/mine.js'
  100. import { useUserStore } from '@/store/user.js';
  101. const userStore = useUserStore();
  102. // 创建一个响应式对象记录今天的考勤数据
  103. const todayData = reactive({
  104. day: '2024-11-11', // 当前日期
  105. startTime: '', // 晨签到时间
  106. endTime: '', // 晚签到时间
  107. status: '未打卡' // 签到状态
  108. })
  109. // 控制今日签到信息和图表显示的状态
  110. const shouldShow = ref(true) // 今日签到信息是否显示
  111. const chartShow = ref(false) // 图表是否显示
  112. // 图表类型的响应式引用
  113. const chartsType = ref('')
  114. // 图表要填充的数据
  115. const chartData = ref({})
  116. // 图表配置选项
  117. const opts = ref({
  118. color: ["#1890FF", "#91CB74", "#FAC858", "#EE6666", "#73C0DE", "#3CA272", "#FC8452", "#9A60B4",
  119. "#ea7ccc"
  120. ], // 设定图表颜色
  121. padding: [15, 15, 0, 5],
  122. enableScroll: false, // 禁用滚动
  123. legend: {}, // 图例设置
  124. xAxis: {
  125. disableGrid: true // 禁用网格线
  126. },
  127. yAxis: {
  128. data: [{
  129. min: 0 // y轴最小值
  130. }]
  131. },
  132. extra: {
  133. column: {
  134. type: "group",
  135. width: 30,
  136. activeBgColor: "#000000",
  137. activeBgOpacity: 0.08
  138. }
  139. }
  140. });
  141. // 时间范围选择数组
  142. const timeRanges = ref(['日', '周', '月']);
  143. const selectedTimeRange = ref('日'); // 初始化选择的时间范围为“日”
  144. // 考勤相关统计数据
  145. const attendanceCount = ref(0); // 出勤次数
  146. const goOutCount = ref(0); // 外出次数
  147. const lateCount = ref(0); // 迟到次数
  148. const leaveEarlyCount = ref(0); // 早退次数
  149. const absenteeismCount = ref(0); // 缺勤次数
  150. //用户id
  151. const userId = ref('');
  152. //当前周的周一日期
  153. const thisMondayDate = ref('');
  154. onMounted(() => {
  155. userId.value = userStore.user.useId;//拿到用户id
  156. initAttData(); //初始化日考勤数据
  157. });
  158. //初始化考勤数据
  159. function initAttData() {
  160. const nowDate = new Date();
  161. todayData.day = formatDate(nowDate);
  162. getDayAttData();
  163. getthisMondayDate();
  164. }
  165. //格式化日期
  166. function formatDate(date) {
  167. const year = date.getFullYear(); // 获取年份
  168. const month = date.getMonth() + 1; // 获取月份
  169. const day = date.getDate(); // 获取日期
  170. // 格式化月份和日期为两位数
  171. const formattedMonth = month < 10 ? '0' + month : month;
  172. const formattedDay = day < 10 ? '0' + day : day;
  173. return `${year}-${formattedMonth}-${formattedDay}`;
  174. }
  175. //获取日考勤数据
  176. function getDayAttData() {
  177. const params = {
  178. universalid: userId.value,
  179. rizi: todayData.day
  180. }
  181. checkAttendance(params).then(res => {
  182. if ("success" == res.returnMsg) {
  183. const attList = res.returnParams.list;
  184. const time1 = attList.find(item => item.att_type_id === '1');
  185. const time2 = attList.filter(item => item.att_type_id === '2').pop();
  186. if (time1 !== undefined) {
  187. todayData.startTime = time1.att_time.split(' ')[1];
  188. todayData.status = '早签完成'
  189. }
  190. if (time2 !== undefined) {
  191. todayData.endTime = time2.att_time.split(' ')[1];
  192. todayData.status = '晚签完成'
  193. }
  194. }
  195. })
  196. }
  197. // 时间范围切换事件
  198. function onTimeRangeChange(event) {
  199. const selectedIndex = event.detail.value;
  200. selectedTimeRange.value = timeRanges.value[selectedIndex]; // 设置选择的时间范围(日周月)
  201. // fetchAttendanceData(); // 获取考勤数据
  202. // 根据选择的时间范围更新展示内容
  203. switch (selectedTimeRange.value) {
  204. case '日':
  205. getDayAttData();
  206. showDay(); // 显示今日考勤
  207. break;
  208. case '周':
  209. chartsType.value = 'pie'; // 设置图表类型为饼图
  210. fetchWeekAttData().then(message => {
  211. console.log(message);
  212. showWeekAndMonth();
  213. updateChart(message);
  214. })
  215. .catch(error => {
  216. console.error(error); // 如果失败,打印错误消息
  217. });
  218. break;
  219. case '月':
  220. chartsType.value = 'column'; // 设置图表类型为柱状图
  221. fetchMonthAttData().then(message => {
  222. console.log('message', message);
  223. showWeekAndMonth();
  224. updateChart(message);
  225. })
  226. .catch(error => {
  227. console.error(error); // 如果失败,打印错误消息
  228. });
  229. break;
  230. }
  231. };
  232. // 显示今日考勤数据
  233. function showDay() {
  234. shouldShow.value = true; // 显示今日签到信息
  235. chartShow.value = false; // 隐藏图表
  236. }
  237. // 显示周或者月考勤
  238. function showWeekAndMonth() {
  239. shouldShow.value = false; // 隐藏今日签到信息
  240. chartShow.value = true; // 显示图表
  241. }
  242. //计算本周周一日期
  243. function getthisMondayDate() {
  244. const today = new Date();
  245. const dayOfWeek = today.getDay(); // 0 (周日) 到 6 (周六)
  246. const dayOfMonth = today.getDate();
  247. // 计算本周周一的日期
  248. const offset = dayOfWeek === 0 ? -6 : 1; // 如果今天是周日,则偏移量为-6,否则为1
  249. const thisMonday = new Date(today);
  250. thisMonday.setDate(dayOfMonth - dayOfWeek + offset);
  251. thisMondayDate.value = formatDate(thisMonday);
  252. }
  253. //获取周考勤记录
  254. function fetchWeekAttData() {
  255. return new Promise((resolve, reject) => {
  256. const params = {
  257. staffId: userId.value,
  258. start_date: thisMondayDate.value,
  259. end_date: todayData.day
  260. }
  261. getMyTotalCount(params).then(res => {
  262. const myWeekAttData = res.returnParams;
  263. const mockData = {
  264. series: [{
  265. data: [{
  266. "name": "出勤",
  267. "value": myWeekAttData.type0,
  268. "labelText": "出勤:" + myWeekAttData.type0 + "次"
  269. },
  270. {
  271. "name": "外出",
  272. "value": myWeekAttData.type1
  273. },
  274. {
  275. "name": "公休",
  276. "value": myWeekAttData.type2
  277. },
  278. {
  279. "name": "未排班",
  280. "value": myWeekAttData.type3,
  281. },
  282. {
  283. "name": "迟到",
  284. "value": myWeekAttData.type4
  285. },
  286. {
  287. "name": "早退",
  288. "value": myWeekAttData.type5
  289. },
  290. {
  291. "name": "缺勤",
  292. "value": myWeekAttData.type6
  293. }
  294. ]
  295. }],
  296. };
  297. const weekData = mockData.series[0].data;
  298. attendanceCount.value = weekData[0].value;
  299. goOutCount.value = weekData[2].value;
  300. lateCount.value = weekData[4].value;
  301. leaveEarlyCount.value = weekData[5].value;
  302. absenteeismCount.value = weekData[6].value;
  303. resolve(mockData)
  304. }).catch(error => {
  305. reject(error)
  306. })
  307. })
  308. }
  309. // 获取月考勤记录
  310. function fetchMonthAttData() {
  311. return new Promise((resolve, reject) => {
  312. const weekNodeList = [];
  313. weekNodeList.unshift(thisMondayDate.value, todayData.day);
  314. while (weekNodeList.length !== 8) {
  315. const weekNode = getOneDaysAgo(weekNodeList[0]);
  316. weekNodeList.unshift(weekNode);
  317. weekNodeList.unshift(getSixDaysAgo(weekNode));
  318. }
  319. const data1 = [];
  320. const data2 = [];
  321. const data3 = [];
  322. const dataFetchPromises = [];// 存储所有数据请求的promise
  323. for (let i = 0; i < weekNodeList.length - 1; i += 2) {
  324. const params = {
  325. staffId: userId.value,
  326. start_date: weekNodeList[i],
  327. end_date: weekNodeList[i + 1]
  328. };
  329. const fetchPromise = getMyTotalCount(params).then(res => {
  330. // for (const type in res.returnParams) {
  331. // console.log('type', type);
  332. // }
  333. console.log('res.returnParams', res.returnParams);
  334. data1.push(res.returnParams.type0);
  335. data2.push(res.returnParams.type1);
  336. data3.push(res.returnParams.type4);
  337. });
  338. dataFetchPromises.push(fetchPromise);
  339. }
  340. // 等待所有数据请求完成
  341. Promise.all(dataFetchPromises).then(() => {
  342. const params = {
  343. staffId: userId.value,
  344. start_date: weekNodeList[0],
  345. end_date: weekNodeList[weekNodeList.length - 1]
  346. };
  347. return getMyTotalCount(params);
  348. }).then(res => {
  349. const myMonthAttData = res.returnParams;
  350. attendanceCount.value = myMonthAttData.type0;
  351. goOutCount.value = myMonthAttData.type2;
  352. lateCount.value = myMonthAttData.type4;
  353. leaveEarlyCount.value = myMonthAttData.type5;
  354. absenteeismCount.value = myMonthAttData.type6;
  355. const mockData = {
  356. categories: ["第1周", "第2周", "第3周", "第4周"],
  357. series: [{
  358. name: "出勤",
  359. data: data1
  360. }, {
  361. name: "出差",
  362. data: data2
  363. }, {
  364. name: "迟到",
  365. data: data3
  366. }]
  367. };
  368. // console.log(data1); // 打印data1检查内容
  369. resolve(mockData);
  370. }).catch(error => {
  371. reject(error);
  372. });
  373. });
  374. }
  375. //获取六天前的日期
  376. function getSixDaysAgo(dateStr) {
  377. const date = new Date(dateStr);
  378. date.setDate(date.getDate() - 6);
  379. return date.toISOString().split('T')[0]; // 返回格式为'YYYY-MM-DD'的字符串
  380. }
  381. //获取一天前的日期
  382. function getOneDaysAgo(dateStr) {
  383. const date = new Date(dateStr);
  384. date.setDate(date.getDate() - 1);
  385. return date.toISOString().split('T')[0]; // 返回格式为'YYYY-MM-DD'的字符串
  386. }
  387. // 更新图表数据
  388. function updateChart(res) {
  389. chartData.value = JSON.parse(JSON.stringify(res)); // 更新图表数据
  390. }
  391. </script>
  392. <style scoped>
  393. @import "@/static/font/ygoa/iconfont.css";
  394. .ygoa-icon {
  395. margin-right: 0.5rem;
  396. font-size: 2.1rem;
  397. vertical-align: middle;
  398. /* 确保图标与文本竖直居中 */
  399. }
  400. .attendance-page {
  401. padding: 20px;
  402. background-color: #f9f9f9;
  403. }
  404. .header {
  405. display: flex;
  406. justify-content: space-between;
  407. align-items: center;
  408. margin-bottom: 20px;
  409. }
  410. .title {
  411. font-size: 24px;
  412. font-weight: bold;
  413. color: #333;
  414. }
  415. .picker {
  416. border: 1px solid #3498db;
  417. border-radius: 5px;
  418. padding: 10px;
  419. background-color: #ecf6fc;
  420. color: #3498db;
  421. }
  422. .statistics {
  423. display: flex;
  424. justify-content: center;
  425. }
  426. .statistic-card {
  427. background: white;
  428. border-radius: 10px;
  429. padding: 20px;
  430. box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  431. /* width: 100%; */
  432. max-width: 600px;
  433. }
  434. .statistic-title {
  435. text-align: center;
  436. font-size: 20px;
  437. font-weight: bold;
  438. color: #333;
  439. }
  440. .statistic-item {
  441. display: flex;
  442. justify-content: space-between;
  443. border-bottom: 1px solid #eee;
  444. padding: 10px 0;
  445. }
  446. .label {
  447. color: #666;
  448. }
  449. .value {
  450. font-size: 18px;
  451. font-weight: bold;
  452. color: #333;
  453. }
  454. .value,
  455. .label {
  456. line-height: 50rpx;
  457. /* 与图标高度一致,确保垂直对齐 */
  458. }
  459. .chart-container {
  460. margin-top: 20px;
  461. }
  462. .chart-title {
  463. font-size: 20px;
  464. font-weight: bold;
  465. text-align: center;
  466. margin-bottom: 10px;
  467. color: #333;
  468. }
  469. .charts-box {
  470. width: 100%;
  471. height: 300px;
  472. }
  473. .todayCheckIn {
  474. background-color: #ffffff;
  475. padding: 20px;
  476. box-sizing: border-box;
  477. }
  478. .check-in-container {
  479. width: 100%;
  480. height: 100%;
  481. display: flex;
  482. flex-direction: column;
  483. align-items: center;
  484. justify-content: center;
  485. }
  486. .title2 {
  487. font-size: 22px;
  488. font-weight: bold;
  489. margin-bottom: 20px;
  490. }
  491. .info-row {
  492. width: 100%;
  493. display: flex;
  494. align-items: center;
  495. justify-content: space-between;
  496. margin-bottom: 10px;
  497. }
  498. .todayCheckIn,
  499. .chart-container,
  500. .statistics {
  501. /* 默认隐藏 */
  502. display: none;
  503. }
  504. /* 当需要显示时,可以添加一个额外的类 */
  505. .todayCheckIn.show,
  506. .chart-container.show,
  507. .statistics.show {
  508. display: block;
  509. }
  510. .icon {
  511. width: 50rpx;
  512. height: 50rpx;
  513. margin-right: 20rpx;
  514. vertical-align: middle;
  515. /* 确保图标与文本竖直居中 */
  516. }
  517. </style>