ClockIn.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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="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="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="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="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="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. function Update(data) {
  170. distance.value = data;
  171. }
  172. function Err(data) {
  173. status.value = 0;
  174. Title.value = '定位时发生错误';
  175. subTitle.value = data;
  176. }
  177. const stramptoTime = function (time) {
  178. if (time < 10)
  179. return 'NaN';
  180. return new Date(+time).toLocaleString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit' });
  181. }
  182. function onload() {
  183. document.documentElement.scrollTop = 0;
  184. const id = route.params.id;
  185. if (!App.hasUser()) {
  186. ElMessage.error('登录已过期,请重新登录');
  187. return router.push('/login');
  188. }
  189. const loading = ElLoading.service({
  190. lock: true,
  191. text: '正在加载中,请稍候'
  192. })
  193. ServerAPI.GetAttendanceItemDetail(App.user.uuid, App.user.session, id, (r) => {
  194. if (!r || r.code != 0 || r.data.length === 0) {
  195. Title.value = '获取考勤数据失败,请稍后再试';
  196. loading.close();
  197. return ElMessage.error('获取考勤数据失败!' + (r.msg ? r.msg : ''))
  198. }
  199. data.value = r.data;
  200. userInfo.value = r.userInfo;
  201. records.value = r.records;
  202. thisWeekRecords.value = filterRecords();
  203. noRecord.value = r.data.user.filter(uuid =>
  204. !thisWeekRecords.value.some(record => record.uuid === uuid)
  205. );
  206. loading.close();
  207. if (!r.data.user.includes(App.user.uuid)) {
  208. return Title.value = '您不在考勤名单内';
  209. }
  210. if (records.value.length !== 0 && hasRecord(data.value, records.value)) {
  211. subTitle.value = `打卡时间:${stramptoTime(Number(thisWeekRecords.value.find(record => record.uuid === App.user.uuid).time))}`;
  212. return status.value = 2;
  213. }
  214. const isWithinTime = isWithinAttendanceTime(data.value);
  215. if (!isWithinTime) {
  216. Title.value = '不在考勤时间范围内';
  217. return subTitle.value = `考勤时间:${getWeekday(data.value.day_of_week)} ${data.value.begintime}~${data.value.endtime}`;
  218. }
  219. // if (window.innerWidth > 768 || ) {
  220. // return Title.value = '请使用手机端企业微信进行打卡操作';
  221. // }
  222. status.value = 1
  223. })
  224. }
  225. onload();
  226. function isWithinAttendanceTime(attendanceTimes) {
  227. const now = new Date();
  228. const dayOfWeek = now.getDay();
  229. const nowTime = now.getTime();
  230. function timeToTodayTimestamp(timeStr) {
  231. const [hours, minutes, seconds] = timeStr.split(':').map(Number);
  232. const todayWithTime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), hours, minutes, seconds);
  233. return todayWithTime.getTime();
  234. }
  235. const { createTime, day_of_week, begintime, endtime, loopy } = attendanceTimes
  236. if (dayOfWeek === day_of_week && (loopy || (Number(createTime) + 604800000 - nowTime > 0)) && timeToTodayTimestamp(begintime) <= nowTime && timeToTodayTimestamp(endtime) >= nowTime) {
  237. return true;
  238. }
  239. return false
  240. }
  241. function addRecords() {
  242. if (distance.value > data.value.radius) {
  243. return ElMessage.error('不在打卡范围内!');
  244. }
  245. ServerAPI.AddAttendanceRecord(App.user.uuid, App.user.session, route.params.id, (r) => {
  246. if (r && r.code === 0) {
  247. ElMessage.success('打卡成功!');
  248. onload();
  249. } else {
  250. ElMessage.error('打卡失败!' + (r.msg ? r.msg : ''));
  251. }
  252. })
  253. }
  254. function filterRecords() {
  255. const { day_of_week, begintime, loopy } = data.value;
  256. if (!loopy) {
  257. return records.value;
  258. }
  259. const now = new Date();
  260. const nowTime = now.getTime();
  261. const begin = getTimestamp(day_of_week, begintime);
  262. if (nowTime >= begin) {
  263. return records.value.filter(item => item.time >= begin);
  264. }
  265. return records.value.filter(item => item.time >= begin - 604800000);
  266. }
  267. function getTimestamp(day_of_week, begintime) {
  268. const now = new Date();
  269. const dayOfWeek = now.getDay();
  270. // 计算今天与目标星期几的差值
  271. // 目标星期几应当是从0到6的范围,0是星期日,6是星期六
  272. const daysUntilTarget = (day_of_week - dayOfWeek + 7) % 7;
  273. // 计算目标日期
  274. const targetDate = new Date(now);
  275. targetDate.setDate(now.getDate() + daysUntilTarget);
  276. const [hours, minutes, seconds] = begintime.split(':').map(Number);
  277. targetDate.setHours(hours, minutes, seconds, 0);
  278. return targetDate.getTime();
  279. }
  280. function hasRecord(attendanceData, records) {
  281. const { day_of_week, begintime, loopy } = attendanceData;
  282. if (!loopy) {
  283. return records.some(record => record.uuid === App.user.uuid);
  284. }
  285. const now = new Date();
  286. const nowTime = now.getTime();
  287. const begin = getTimestamp(day_of_week, begintime);
  288. if (nowTime >= begin) {
  289. return records.some(record => record.time >= begin && record.uuid === App.user.uuid);
  290. }
  291. return records.some(record => record.time >= begin - 604800000 && record.uuid === App.user.uuid);
  292. }
  293. function getWeekday(day_of_week) {
  294. switch (day_of_week) {
  295. case 0:
  296. return '星期日'
  297. case 1:
  298. return '星期一'
  299. case 2:
  300. return '星期二'
  301. case 3:
  302. return '星期三'
  303. case 4:
  304. return '星期四'
  305. case 5:
  306. return '星期五'
  307. case 6:
  308. return '星期六'
  309. default:
  310. return '未知星期'
  311. }
  312. }
  313. </script>
  314. <style scoped>
  315. .header {
  316. position: fixed;
  317. z-index: 999;
  318. width: 100%;
  319. }
  320. .container {
  321. position: sticky;
  322. height: 80px;
  323. }
  324. .root {
  325. display: flex;
  326. flex-direction: column;
  327. min-height: 70vh;
  328. gap: 20px;
  329. }
  330. .content {
  331. flex: 1;
  332. width: 70%;
  333. margin: 0 auto;
  334. background-color: #eee;
  335. }
  336. .content .title {
  337. font-size: 1.2em;
  338. text-align: center;
  339. }
  340. .content .item {
  341. padding: 8px 20px 20px 20px;
  342. background-color: #fff;
  343. border-radius: 10px;
  344. margin-bottom: 10px;
  345. color: #337ecc;
  346. animation: fadeIn 0.6s ease-in-out forwards;
  347. }
  348. .content .item .info {
  349. display: flex;
  350. font-size: 14px;
  351. margin-top: 4px;
  352. }
  353. .content .item .info .key {
  354. width: 70px;
  355. }
  356. .content .item .info .value {
  357. color: #777;
  358. flex: 1;
  359. }
  360. .content .item .value2 {
  361. display: flex;
  362. flex-wrap: wrap;
  363. gap: 5px;
  364. }
  365. .content .item .useritem {
  366. display: flex;
  367. align-items: center;
  368. background-color: #d9ecff;
  369. color: #337ecc;
  370. padding: 5px;
  371. border-radius: 10px;
  372. transform: translateY(-7px);
  373. }
  374. .content .item .join {
  375. background-color: #faecd8;
  376. color: #b88230;
  377. }
  378. .content .item .success {
  379. background-color: #e1f3d8;
  380. color: #529b2e
  381. }
  382. .content .item .fail {
  383. background-color: #fde2e2;
  384. color: #c45656
  385. }
  386. .content .item .users {
  387. margin-top: 10px;
  388. }
  389. .hint {
  390. color: #888;
  391. font-size: 0.8em;
  392. text-align: center
  393. }
  394. .err {
  395. display: flex;
  396. align-items: center;
  397. justify-content: center;
  398. }
  399. .el-timeline {
  400. margin-top: 20px;
  401. }
  402. @media only screen and (max-width: 768px) {
  403. .content {
  404. width: 90%;
  405. margin-top: -10px;
  406. }
  407. .container {
  408. height: 60px
  409. }
  410. .el-timeline {
  411. margin-left: -35px;
  412. }
  413. }
  414. </style>