ClockIn.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <template>
  2. <div class="header">
  3. <Header :color="'black'" />
  4. </div>
  5. <div class="root">
  6. <MapContainer @update="Update" @err="Err" v-if="status === 1 && data != null" :radius="data.radius"
  7. :position="data.position" :address="data.address" />
  8. <div class="container" v-else></div>
  9. <div class="content">
  10. <div class="item">
  11. <el-result icon="success" title="您在打卡范围内" sub-title="现在可进行打卡操作"
  12. v-if="status === 1 && distance <= data.radius">
  13. <template #extra>
  14. <el-button type="success" @click="addRecords">立即打卡</el-button>
  15. </template>
  16. </el-result>
  17. <el-result icon="warning" title="您不在打卡范围内" :sub-title="'距打卡点' + distance + '米'"
  18. v-else-if="status === 1 && distance > data.radius">
  19. <template #extra>
  20. <el-button type="warning" @click="$router.go(0)">重新定位</el-button>
  21. </template>
  22. </el-result>
  23. <el-result icon="success" title="打卡成功" :sub-title="subTitle" v-else-if="status === 2">
  24. <template #extra>
  25. <el-button type="success" @click="$router.push('/')">返回首页</el-button>
  26. </template>
  27. </el-result>
  28. <div class="err" v-else>
  29. <el-result icon="error" :title="Title ? Title : '当前无法进行打卡操作'" :sub-title="subTitle">
  30. <template #extra>
  31. <el-button type="danger" @click="onload">重新尝试</el-button>
  32. </template>
  33. </el-result>
  34. </div>
  35. </div>
  36. <div class="item" v-if="data">
  37. <p class="title">项目详情</p>
  38. <div class="info">
  39. <div class="key">项目编号:</div>
  40. <div class="value">{{ data.id }}</div>
  41. </div>
  42. <div class="info">
  43. <div class="key">项目名称:</div>
  44. <div class="value">{{ data.name }}</div>
  45. </div>
  46. <div class="info">
  47. <div class="key">创建时间:</div>
  48. <div class="value">{{ stramptoTime(data.createTime) }}</div>
  49. </div>
  50. <div class="info">
  51. <div class="key">打卡星期:</div>
  52. <div class="value">{{ getWeekday(data.day_of_week) }}</div>
  53. </div>
  54. <div class="info">
  55. <div class="key">每周循环:</div>
  56. <div class="value">{{ data.loopy ? '是' : '否' }}</div>
  57. </div>
  58. <div class="info">
  59. <div class="key">打卡地点:</div>
  60. <div class="value">{{ data.address }}</div>
  61. </div>
  62. <div class="info">
  63. <div class="key">打卡时间:</div>
  64. <div class="value">
  65. {{ data.begintime }}~{{ data.endtime }}
  66. </div>
  67. </div>
  68. <div class="info users">
  69. <div class="key">发起人:</div>
  70. <div class="value2">
  71. <div class="useritem">
  72. <el-avatar :size="23" :src="apiUrl + userInfo[data.createUser].avatar" />
  73. {{ userInfo[data.createUser] !=
  74. undefined ? userInfo[data.createUser].username : `用户${data.createUser.slice(0, 4)}` }}
  75. </div>
  76. </div>
  77. </div>
  78. <div class="info users">
  79. <div class="key">管理员:</div>
  80. <div class="value2">
  81. <div v-if="data.admin && data.admin.length === 0">无</div>
  82. <div class="useritem" v-for="(item, index) in data.admin">
  83. <el-avatar :size="23" :src="apiUrl + userInfo[item].avatar" />
  84. {{ userInfo[item] !=
  85. undefined ? userInfo[item].username : `用户${item.slice(0, 4)}` }}
  86. </div>
  87. </div>
  88. </div>
  89. <div class="info users">
  90. <div class="key">参与人:</div>
  91. <div class="value2">
  92. <div class="useritem join" v-for="(item, index) in data.user">
  93. <el-avatar :size="23" :src="apiUrl + userInfo[item].avatar" />
  94. {{ userInfo[item] !=
  95. undefined ? userInfo[item].username : `用户${item.slice(0, 4)}` }}
  96. </div>
  97. </div>
  98. </div>
  99. <div class="info users">
  100. <div class="key">未打卡:</div>
  101. <div class="value2">
  102. <div v-if="noRecord.length === 0">无</div>
  103. <div class="useritem fail" v-for="(item, index) in noRecord">
  104. <el-avatar :size="23" :src="apiUrl + userInfo[item].avatar" />
  105. {{ userInfo[item] !=
  106. undefined ? userInfo[item].username : `用户${item.slice(0, 4)}` }}
  107. </div>
  108. </div>
  109. </div>
  110. <div class="info users">
  111. <div class="key">已打卡:</div>
  112. <div class="value2">
  113. <div v-if="thisWeekRecords.length ===0">无
  114. </div>
  115. <div class="useritem success" v-for="(item, index) in thisWeekRecords">
  116. <el-avatar :size="23" :src="apiUrl + userInfo[item.uuid].avatar" />
  117. {{ userInfo[item.uuid] !=
  118. undefined ? userInfo[item.uuid].username : `用户${item.uuid.slice(0, 4)}` }}
  119. </div>
  120. </div>
  121. </div>
  122. </div>
  123. <div class="item" v-if="data">
  124. <p class="title">打卡记录</p>
  125. <el-timeline>
  126. <el-timeline-item type="success" :timestamp="stramptoTime(data.createTime)">
  127. 打卡项目已创建
  128. </el-timeline-item>
  129. <el-timeline-item type="danger" v-if="(showAll ? records : thisWeekRecords).length == 0">
  130. 暂时没有打卡记录
  131. </el-timeline-item>
  132. <el-timeline-item type="success" v-for="(item, index) in showAll ? records : thisWeekRecords"
  133. :timestamp="stramptoTime(item.time)">
  134. {{ userInfo[item.uuid] !==
  135. undefined ? userInfo[item.uuid].username : `用户${item.uuid.slice(0, 4)}` }}已打卡{{ item.commit ?
  136. ('(' + item.commit + ')')
  137. : '' }}
  138. </el-timeline-item>
  139. </el-timeline>
  140. <div v-if="data.loopy === 1">
  141. <div class="hint" v-if="!showAll">当前显示本周期记录 | <span @click="showAll = true">查看全部</span></div>
  142. <div class="hint" v-else>当前显示全部记录 | <span @click="showAll = false">查看当前周期</span></div>
  143. </div>
  144. </div>
  145. </div>
  146. </div>
  147. <el-backtop :right="70" :bottom="70" />
  148. <Footer />
  149. </template>
  150. <script setup>
  151. import Header from '../../components/Header.vue';
  152. import Footer from '../../components/Footer.vue';
  153. import { App } from '../../app/app';
  154. import { ServerAPI } from '../../app/lib/ServerAPI';
  155. const MapContainer = defineAsyncComponent(() => import('./components/MapContainer.vue'));
  156. import { useRoute, useRouter } from 'vue-router';
  157. const route = useRoute();
  158. const router = useRouter();
  159. let status = ref(0);
  160. let Title = ref('');
  161. let subTitle = ref('');
  162. let distance = ref();
  163. let data = ref();
  164. let userInfo = ref({});
  165. let records = ref([]);
  166. let thisWeekRecords = ref([]);
  167. let showAll = ref(false);
  168. let noRecord = ref([]);
  169. let apiUrl = import.meta.env.VITE_API_URL;
  170. function Update(data) {
  171. distance.value = data;
  172. }
  173. function Err(data) {
  174. status.value = 0;
  175. Title.value = '定位时发生错误';
  176. subTitle.value = data;
  177. }
  178. const stramptoTime = function (time) {
  179. if (time < 10)
  180. return 'NaN';
  181. return new Date(+time).toLocaleString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit' });
  182. }
  183. function onload() {
  184. document.documentElement.scrollTop = 0;
  185. const id = route.params.id;
  186. if (!App.hasUser()) {
  187. ElMessage.error('登录已过期,请重新登录');
  188. return router.push('/login');
  189. }
  190. const loading = ElLoading.service({
  191. lock: true,
  192. text: '正在加载中,请稍候'
  193. })
  194. ServerAPI.GetAttendanceItemDetail(App.user.uuid, App.user.session, id, (r) => {
  195. if (!r || r.code != 0 || r.data.length === 0) {
  196. Title.value = '获取考勤数据失败,请稍后再试';
  197. loading.close();
  198. return ElMessage.error('获取考勤数据失败!' + (r.msg ? r.msg : ''))
  199. }
  200. data.value = r.data;
  201. userInfo.value = r.userInfo;
  202. records.value = r.records;
  203. thisWeekRecords.value = filterRecords();
  204. noRecord.value = r.data.user.filter(uuid =>
  205. !thisWeekRecords.value.some(record => record.uuid === uuid)
  206. );
  207. loading.close();
  208. if (!r.data.user.includes(App.user.uuid)) {
  209. return Title.value = '您不在考勤名单内';
  210. }
  211. if (records.value.length !== 0 && hasRecord(data.value, records.value)) {
  212. subTitle.value = `打卡时间:${stramptoTime(Number(thisWeekRecords.value.find(record => record.uuid === App.user.uuid).time))}`;
  213. return status.value = 2;
  214. }
  215. const isWithinTime = isWithinAttendanceTime(data.value);
  216. if (!isWithinTime) {
  217. Title.value = '不在考勤时间范围内';
  218. return subTitle.value = `考勤时间:${getWeekday(data.value.day_of_week)} ${data.value.begintime}~${data.value.endtime}`;
  219. }
  220. status.value = 1
  221. })
  222. }
  223. onload();
  224. function isWithinAttendanceTime(attendanceTimes) {
  225. const now = new Date();
  226. const dayOfWeek = now.getDay();
  227. const nowTime = now.getTime();
  228. function timeToTodayTimestamp(timeStr) {
  229. const [hours, minutes, seconds] = timeStr.split(':').map(Number);
  230. const todayWithTime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), hours, minutes, seconds);
  231. return todayWithTime.getTime();
  232. }
  233. const { createTime, day_of_week, begintime, endtime, loopy } = attendanceTimes
  234. if (dayOfWeek === day_of_week && (loopy || (Number(createTime) + 604800000 - nowTime > 0)) && timeToTodayTimestamp(begintime) <= nowTime && timeToTodayTimestamp(endtime) >= nowTime) {
  235. return true;
  236. }
  237. return false
  238. }
  239. function addRecords() {
  240. if (distance.value > data.value.radius) {
  241. return ElMessage.error('不在打卡范围内!');
  242. }
  243. ServerAPI.AddAttendanceRecord(App.user.uuid, App.user.session, route.params.id, (r) => {
  244. if (r && r.code === 0) {
  245. ElMessage.success('打卡成功!');
  246. onload();
  247. } else {
  248. ElMessage.error('打卡失败!' + (r.msg ? r.msg : ''));
  249. }
  250. })
  251. }
  252. function filterRecords() {
  253. const { day_of_week, begintime, loopy } = data.value;
  254. if (!loopy) {
  255. return records.value;
  256. }
  257. const now = new Date();
  258. const nowTime = now.getTime();
  259. const begin = getTimestamp(day_of_week, begintime);
  260. if (nowTime >= begin) {
  261. return records.value.filter(item => item.time >= begin);
  262. }
  263. return records.value.filter(item => item.time >= begin - 604800000);
  264. }
  265. function getTimestamp(day_of_week, begintime) {
  266. const now = new Date();
  267. const dayOfWeek = now.getDay();
  268. // 计算今天与目标星期几的差值
  269. // 目标星期几应当是从0到6的范围,0是星期日,6是星期六
  270. const daysUntilTarget = (day_of_week - dayOfWeek + 7) % 7;
  271. // 计算目标日期
  272. const targetDate = new Date(now);
  273. targetDate.setDate(now.getDate() + daysUntilTarget);
  274. const [hours, minutes, seconds] = begintime.split(':').map(Number);
  275. targetDate.setHours(hours, minutes, seconds, 0);
  276. return targetDate.getTime();
  277. }
  278. function hasRecord(attendanceData, records) {
  279. const { day_of_week, begintime, loopy } = attendanceData;
  280. if (!loopy) {
  281. return records.some(record => record.uuid === App.user.uuid);
  282. }
  283. const now = new Date();
  284. const nowTime = now.getTime();
  285. const begin = getTimestamp(day_of_week, begintime);
  286. if (nowTime >= begin) {
  287. return records.some(record => record.time >= begin && record.uuid === App.user.uuid);
  288. }
  289. return records.some(record => record.time >= begin - 604800000 && record.uuid === App.user.uuid);
  290. }
  291. function getWeekday(day_of_week) {
  292. switch (day_of_week) {
  293. case 0:
  294. return '星期日'
  295. case 1:
  296. return '星期一'
  297. case 2:
  298. return '星期二'
  299. case 3:
  300. return '星期三'
  301. case 4:
  302. return '星期四'
  303. case 5:
  304. return '星期五'
  305. case 6:
  306. return '星期六'
  307. default:
  308. return '未知星期'
  309. }
  310. }
  311. </script>
  312. <style scoped>
  313. .header {
  314. position: fixed;
  315. z-index: 999;
  316. width: 100%;
  317. }
  318. .container {
  319. position: sticky;
  320. height: 80px;
  321. }
  322. .root {
  323. display: flex;
  324. flex-direction: column;
  325. min-height: 70vh;
  326. gap: 20px;
  327. }
  328. .content {
  329. flex: 1;
  330. width: 70%;
  331. margin: 0 auto;
  332. background-color: #eee;
  333. }
  334. .content .title {
  335. font-size: 1.2em;
  336. text-align: center;
  337. }
  338. .content .item {
  339. padding: 8px 20px 20px 20px;
  340. background-color: #fff;
  341. border-radius: 10px;
  342. margin-bottom: 10px;
  343. color: #337ecc;
  344. animation: fadeIn 0.6s ease-in-out forwards;
  345. }
  346. .content .item .info {
  347. display: flex;
  348. font-size: 14px;
  349. margin-top: 4px;
  350. }
  351. .content .item .info .key {
  352. width: 70px;
  353. }
  354. .content .item .info .value {
  355. color: #777;
  356. flex: 1;
  357. }
  358. .content .item .value2 {
  359. display: flex;
  360. flex-wrap: wrap;
  361. gap: 5px;
  362. }
  363. .content .item .useritem {
  364. display: flex;
  365. align-items: center;
  366. background-color: #d9ecff;
  367. color: #337ecc;
  368. padding: 5px;
  369. border-radius: 10px;
  370. transform: translateY(-7px);
  371. }
  372. .content .item .join {
  373. background-color: #faecd8;
  374. color: #b88230;
  375. }
  376. .content .item .success {
  377. background-color: #e1f3d8;
  378. color: #529b2e
  379. }
  380. .content .item .fail {
  381. background-color: #fde2e2;
  382. color: #c45656
  383. }
  384. .content .item .users {
  385. margin-top: 10px;
  386. }
  387. .hint {
  388. color: #888;
  389. font-size: 0.8em;
  390. text-align: center
  391. }
  392. .err {
  393. display: flex;
  394. align-items: center;
  395. justify-content: center;
  396. }
  397. .el-timeline {
  398. margin-top: 20px;
  399. }
  400. @media only screen and (max-width: 768px) {
  401. .content {
  402. width: 90%;
  403. margin-top: -10px;
  404. }
  405. .container {
  406. height: 60px
  407. }
  408. .el-timeline {
  409. margin-left: -35px;
  410. }
  411. }
  412. </style>