clockIn.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. <template>
  2. <page-meta root-font-size="system" />
  3. <view class="container">
  4. <!-- 时间日期信息 -->
  5. <view class="header">
  6. <text class="day">{{ currentDay }}</text>
  7. <text class="date">{{ currentDate }}</text>
  8. <text class="nowTime">{{ nowTime }}</text>
  9. </view>
  10. <!-- 打卡记录 -->
  11. <view class="record">
  12. <view class="record-item" v-if="nowTime != '' && nowTime <= '12:00:00' || signName == '上班签到'">
  13. <text class="ygoa-icon icon-goToWork"></text>
  14. <text class="title">上班</text>
  15. <text class="time">{{ signInTime || '未打卡' }}</text>
  16. <text class="signStatus" v-if="!isSignInStatusDisabled">({{ signInStatusName }})</text>
  17. </view>
  18. <view class="record-item" v-else-if="nowTime > '12:00:00' && signName == '下班签退'">
  19. <text class="ygoa-icon icon-afterWork"></text>
  20. <text class="title">下班</text>
  21. <text class="time">{{ signOutTime || '未打卡' }}</text>
  22. <text class="signStatus" v-if="!isSignOutStatusDisabled" style="color: #e64340;">({{ signOutStatusName
  23. }})</text>
  24. </view>
  25. <view class="record-item" @click="getAddress">
  26. <text class="ygoa-icon icon-location"></text>
  27. <text class="title">当前位置</text>
  28. <text class="time">{{ address || '点击获取定位' }}</text>
  29. </view>
  30. </view>
  31. <!-- 地图 -->
  32. <atl-map :mapKey="config.mapKey" :mapType="mapType" :longitude="longitude" :latitude="latitude" :marker="marker"
  33. :polygons="polygons" :isPolygons="true">
  34. <template v-slot:content>
  35. </template>
  36. </atl-map>
  37. <!-- 打卡按钮 -->
  38. <view class="footer">
  39. <button type="default" @click="signInOrOut" style="background-color: #3c9cff; color: #fcfcfc;"
  40. v-if="signName == '上班签到'">上班签到</button>
  41. <button type="default" @click="signInOrOut" style="background-color: #1aad19; color: #fcfcfc;"
  42. v-else-if="signName == '下班签退'">下班签退</button>
  43. </view>
  44. </view>
  45. </template>
  46. <script setup lang="ts">
  47. import { onBeforeUnmount, onMounted, reactive, ref } from 'vue';
  48. // 导入地理位置相关函数
  49. import booleanPointInPolygon from "@turf/boolean-point-in-polygon";
  50. import { point, polygon } from "@turf/helpers";
  51. import { useUserStore } from '@/store/user';
  52. import { useConfigStore } from '@/store/config'
  53. import $modal from '@/plugins/modal.js';
  54. import $tab from '@/plugins/tab.js';
  55. import config from '@/config.js';
  56. import { createAttendance, tranAddress, checkAttendance, getAttendanceRule, getAttendanceSegment } from '@/api/mine.js'
  57. const userStore = useUserStore();
  58. const configStore = useConfigStore();
  59. const thisUser = userStore.user;
  60. const intervalId = ref(null); // 定时器ID
  61. onBeforeUnmount(() => {
  62. clearInterval(intervalId.value);
  63. })
  64. // 组件挂载后初始化当前日期与位置
  65. onMounted(() => {
  66. // getAttRule();
  67. dateInit();
  68. getAddress();
  69. });
  70. function showNowTime() {
  71. intervalId.value = setInterval(() => {
  72. nowTime.value = getNowTime()
  73. }, 1000); // 每1秒更新一次
  74. }
  75. // 当前日期和时间相关信息
  76. const currentDate = ref('2023-04-01');
  77. const currentDay = ref('星期三');
  78. const nowTime = ref(''); // 当前时间
  79. const weekArr = reactive(['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']); // 星期数组
  80. // 初始化日期
  81. function dateInit() {
  82. showNowTime();
  83. const nowDate = new Date();
  84. const year = nowDate.getFullYear(); // 获取年份
  85. const month = nowDate.getMonth() + 1; // 获取月份
  86. const day = nowDate.getDate(); // 获取日期
  87. // 格式化月份和日期为两位数
  88. const formattedMonth = month < 10 ? '0' + month : month;
  89. const formattedDay = day < 10 ? '0' + day : day;
  90. currentDate.value = `${year}-${formattedMonth}-${formattedDay}`; // 设置当前日期
  91. const dayOfWeek = nowDate.getDay(); // 获取当前星期
  92. currentDay.value = weekArr[dayOfWeek]; // 设置当前星期
  93. getTodayAtt();
  94. // if (nowTime.value > '12:00') {
  95. // signName.value = '下班签退'
  96. // }
  97. }
  98. const signInTime = ref(''); // 上班签到时间
  99. const signOutTime = ref(''); // 下班签到时间
  100. //打卡按钮文字显示
  101. const signName = ref('')
  102. //迟到早退状态,默认隐藏
  103. const signInStatusName = ref('迟到')
  104. const signOutStatusName = ref('早退')
  105. const isSignInStatusDisabled = ref(true)
  106. const isSignOutStatusDisabled = ref(true)
  107. //获取今天考勤状态
  108. function getTodayAtt() {
  109. const params = {
  110. universalid: thisUser.useId,
  111. rizi: currentDate.value
  112. }
  113. checkAttendance(params).then(res => {
  114. if ("success" == res.returnMsg) {
  115. // console.log("kaoqin",res);
  116. const attList = res.returnParams.list;
  117. let time1;
  118. let time2;
  119. const filteredRecord1 = attList.filter(item => item.att_type_id === '1');
  120. if (filteredRecord1.length > 0) {
  121. time1 = filteredRecord1.reduce((latest, current) => {
  122. const latestTime = new Date(latest.att_time);
  123. const currentTime = new Date(current.att_time);
  124. return (currentTime < latestTime) ? current : latest;
  125. });
  126. }
  127. //拿到所有签退数据后,取最大时间的那个
  128. const filteredRecord2 = attList.filter(item => item.att_type_id === '2');
  129. if (filteredRecord2.length > 0) {
  130. time2 = filteredRecord2.reduce((latest, current) => {
  131. const latestTime = new Date(latest.att_time);
  132. const currentTime = new Date(current.att_time);
  133. return (currentTime > latestTime) ? current : latest;
  134. });
  135. }
  136. if (time1 !== undefined) {
  137. // console.log('getTime1',time1);
  138. signInTime.value = time1.att_time.split(' ')[1];
  139. signName.value = '下班签退';
  140. if (isTimeInRange(signInTime.value, ...configStore.lateTimeRange)) {//迟到
  141. signInStatusName.value = '迟到';
  142. isSignInStatusDisabled.value = false;
  143. } else if (isTimeInRange(signInTime.value, configStore.lateTimeRange[1], '23:59:59')) {
  144. signInStatusName.value = '旷工';
  145. isSignInStatusDisabled.value = false;
  146. }
  147. } else if (new Date().getHours() >= 12) {
  148. signName.value = '下班签退'
  149. } else {
  150. signName.value = '上班签到';
  151. }
  152. if (time2 !== undefined) {
  153. signOutTime.value = time2.att_time.split(' ')[1];
  154. if (isTimeInRange(signOutTime.value, "00:00:00", configStore.earlyTimeRange[0])) {//旷工
  155. signOutStatusName.value = '旷工';
  156. isSignOutStatusDisabled.value = false;
  157. }
  158. else if (isTimeInRange(signOutTime.value, ...configStore.earlyTimeRange)) {
  159. signOutStatusName.value = '早退';
  160. isSignOutStatusDisabled.value = false;
  161. }
  162. else {
  163. isSignOutStatusDisabled.value = true; //隐藏早退状态
  164. }
  165. }
  166. }
  167. })
  168. }
  169. //上班卡或下班卡
  170. function signInOrOut() {
  171. if ('上班签到' == signName.value) {
  172. signIn();
  173. } else {
  174. signOut();
  175. }
  176. }
  177. //上班签到
  178. function signIn() {
  179. const now = getNowTime();
  180. const signIn = 1;
  181. getAddress().then(() => {
  182. clockIn(signIn);
  183. });
  184. };
  185. // 下班签退
  186. function signOut() {
  187. const now = getNowTime();
  188. const signOut = 2;
  189. //判断打卡时间是否符合
  190. if (!isTimeInRange(now, ...configStore.signOutTimeRange)) {
  191. $modal.confirm('当前非正常签退时间,是否继续?').then(res => {
  192. if (res) {
  193. getAddress().then(() => {
  194. clockIn(signOut); // 执行打卡并获取结果
  195. });
  196. }
  197. }).catch(() => { })
  198. } else {
  199. getAddress().then(() => {
  200. clockIn(signOut);
  201. });
  202. }
  203. };
  204. //获取当前时间
  205. function getNowTime() {
  206. return new Date().toLocaleTimeString('en-US', {
  207. hour12: false
  208. }).substring(0, 8)
  209. }
  210. //判断时间是否在区间内
  211. function isTimeInRange(time, startTime, endTime) {
  212. // 将时间字符串转换为Date对象
  213. const timeObj = new Date('1970-01-01T' + time + 'Z');
  214. const startObj = new Date('1970-01-01T' + startTime + 'Z');
  215. const endObj = new Date('1970-01-01T' + endTime + 'Z');
  216. // 比较时间
  217. return timeObj >= startObj && timeObj <= endObj;
  218. }
  219. // 地图配置信息
  220. // const mapKey = config.mapKey; // 地图API密钥
  221. const mapType = ref('tmap'); // 地图类型
  222. const address = ref(''); // 初始化地址
  223. // 打卡定位信息
  224. const longitude = ref(); // 经度
  225. const latitude = ref(); // 纬度
  226. // 标记信息
  227. const marker = reactive({
  228. id: 1,
  229. latitude: 25.958812,
  230. longitude: 119.213036,
  231. width: 50, // 标记宽度
  232. height: 50, // 标记高度
  233. title: '宇光同行' // 标记标题
  234. });
  235. // 获取当前定位经纬度
  236. function getlocation() {
  237. return new Promise((resolve, reject) => {
  238. //判断用户是否打开位置权限
  239. uni.getSetting({
  240. success(res) {
  241. // 如果用户未授权
  242. if (!res.authSetting['scope.userLocation']) {
  243. $modal.confirm('需要您授权获取地理位置信息').then(res => {
  244. if (res) {
  245. // 打开权限设置页面
  246. uni.openSetting({
  247. success: (settingData) => {
  248. if (settingData.authSetting[
  249. 'scope.userLocation']) {
  250. $modal.msg('授权后请重新打开此页面')
  251. // reject("用户未授权");
  252. }
  253. }
  254. });
  255. }
  256. })
  257. .catch(err => {
  258. //用户点击取消
  259. if (!err) {
  260. uni.showToast({
  261. title: '获取地理位置授权失败',
  262. icon: 'none',
  263. success: () => {
  264. // 返回上一页
  265. setTimeout(() => {
  266. $tab.navigateBack()
  267. }, 1000);
  268. }
  269. });
  270. // reject("用户未授权");
  271. }
  272. })
  273. } else {
  274. // 已授权,获取位置
  275. fetchLocation(resolve, reject);
  276. }
  277. },
  278. fail(err) {
  279. reject(err); // 拒绝 Promise
  280. }
  281. });
  282. });
  283. }
  284. // 封装获取位置的函数
  285. function fetchLocation(resolve, reject) {
  286. wx.getLocation({
  287. success: (res) => {
  288. longitude.value = res.longitude; // 保存经度
  289. latitude.value = res.latitude; // 保存纬度
  290. resolve(); // 解析 Promise
  291. },
  292. fail: (err) => {
  293. if (err.errMsg.includes("频繁调用")) {
  294. $modal.msg('请勿频繁调用');
  295. // reject("频繁调用错误");
  296. return;
  297. }
  298. console.log('getLocationErr', err);
  299. reject(err); // 拒绝 Promise
  300. },
  301. });
  302. }
  303. // 获取当前位置地址
  304. function getAddress() {
  305. return new Promise((resolve, reject) => {
  306. getlocation()
  307. .then(res => {
  308. tranLocationToAddress(); // 执行地址转换
  309. resolve(res); // 地址获取成功
  310. })
  311. .catch(err => {
  312. // console.error('获取地址失败', err);
  313. reject(err); // 地址获取失败
  314. });
  315. });
  316. }
  317. // 经纬度转地址
  318. function tranLocationToAddress() {
  319. let locationStr = latitude.value + ',' + longitude.value; // 组合经纬度
  320. tranAddress(locationStr).then(res => {
  321. address.value = res.result.address; // 保存地址
  322. })
  323. .catch(err => {
  324. console.log('地址转换请求失败', err); // 请求错误处理
  325. })
  326. }
  327. // 多边形区域设置
  328. const polygons = reactive([{
  329. points: [{
  330. latitude: 25.9591401,
  331. longitude: 119.21292356
  332. },
  333. {
  334. latitude: 25.95828592,
  335. longitude: 119.21261955
  336. },
  337. {
  338. latitude: 25.9576709,
  339. longitude: 119.21458294
  340. },
  341. {
  342. latitude: 25.95845106,
  343. longitude: 119.21486162
  344. },
  345. {
  346. latitude: 25.9591401,
  347. longitude: 119.21292356
  348. }
  349. ],
  350. strokeWidth: 1, // 边框宽度
  351. strokeColor: "#ff000066", // 边框颜色
  352. fillColor: "#ff000016", // 填充颜色
  353. }]);
  354. // 打卡
  355. function clockIn(attType) {
  356. // 发起打卡请求相关业务逻辑
  357. //判断是否已经打过卡
  358. if (isClockIn(attType)) {
  359. return;
  360. }
  361. var now = new Date();
  362. // 使用toISOString()获取一个ISO格式的字符串,然后替换T为空格,并去除最后的Z(表示UTC时间)
  363. var formatted = now.toISOString().replace('T', ' ').replace('Z', '');
  364. var localString = new Date(formatted + '-08:00').toISOString().replace('T', ' ').replace('Z', '');
  365. const params = {
  366. user_name: thisUser.name,
  367. user_id: thisUser.useId,
  368. kaoqin_type: attType,
  369. now_date: localString,
  370. longitude: longitude.value,
  371. latitude: latitude.value,
  372. address: address.value,
  373. unitId: thisUser.unitId
  374. };
  375. createAttendance(params).then(res => {
  376. if ("success" == res.returnMsg) {
  377. getTodayAtt(); //更新今日考勤数据
  378. isToFillClock(attType, localString.substring(11, 19));
  379. // return true;
  380. } else {
  381. uni.showToast({
  382. title: "未在指定范围,打卡失败",
  383. icon: 'none'
  384. });
  385. // return false;
  386. }
  387. })
  388. // const _polygons = polygons.map((polygon) => {
  389. // return polygon.points.map((i) => [
  390. // Number(i.longitude),
  391. // Number(i.latitude),
  392. // ]);
  393. // });
  394. // const _point = point([longitude.value, latitude.value]); // 用当前坐标创建点
  395. // const _polygon = polygon(_polygons); // 创建多边形
  396. // const f = booleanPointInPolygon(_point, _polygon); // 判断点是否在多边形内
  397. }
  398. //是否补卡、请假判断
  399. function isToFillClock(attType, time) {
  400. // console.log('isToFillClock',attType,time);
  401. //早上迟到跳补卡
  402. let status = '';
  403. if (attType == 1) {
  404. if (isTimeInRange(time, ...configStore.lateTimeRange)) {
  405. status = '迟到'
  406. } else if (isTimeInRange(time, configStore.lateTimeRange[1], '23:59:59')) {
  407. status = '旷工'
  408. } else {
  409. $modal.msgSuccess('打卡成功')
  410. setTimeout(() => {
  411. $tab.navigateTo('/pages/mine/checkIn/checkIn')
  412. }, 1000)
  413. return
  414. }
  415. $modal.confirm('当前状态为' + status + '!\n是否跳转补卡页面').then(() => {
  416. $tab.reLaunch('/pages/work/index')
  417. })
  418. .catch(() => {
  419. $tab.navigateTo('/pages/mine/checkIn/checkIn')
  420. })
  421. } else if (attType == 2) {
  422. //早退请假提示
  423. if (isTimeInRange(time, '00:00:00', configStore.earlyTimeRange[0])) {
  424. status = '旷工'
  425. } else if (isTimeInRange(time, ...configStore.earlyTimeRange)) {
  426. status = '早退'
  427. } else {
  428. $modal.msgSuccess('打卡成功')
  429. setTimeout(() => {
  430. $tab.navigateTo('/pages/mine/checkIn/checkIn')
  431. }, 1000)
  432. return
  433. }
  434. $modal.confirm('当前状态为' + status + '!\n是否跳转请假页面').then(() => {
  435. $tab.reLaunch('/pages/work/index')
  436. })
  437. .catch(() => {
  438. $tab.navigateTo('/pages/mine/checkIn/checkIn')
  439. })
  440. }
  441. }
  442. //判断是否已经打卡
  443. function isClockIn(attType) {
  444. var attTypeData = '';
  445. switch (attType) {
  446. case 1:
  447. attTypeData = signInTime.value;
  448. break;
  449. case 2:
  450. break;
  451. }
  452. if ('' !== attTypeData) {
  453. $modal.showToast('请勿重复打卡')
  454. return true;
  455. }
  456. return false;
  457. }
  458. </script>
  459. <style lang="scss" scoped>
  460. // @import "@/static/font/ygoa/iconfont.css";
  461. .ygoa-icon {
  462. margin-right: 20rpx;
  463. /* font-size: calc(26px + .5(1rem - 16px)); */
  464. font-size: calc(1.625rem + 0px) !important;
  465. }
  466. .container {
  467. display: flex;
  468. flex-direction: column;
  469. align-items: center;
  470. padding: 20rpx;
  471. .header {
  472. width: 100%;
  473. height: 200rpx;
  474. background-color: #f8f8f8;
  475. display: flex;
  476. flex-direction: column;
  477. align-items: center;
  478. justify-content: center;
  479. .day {
  480. font-size: calc(1.5rem + 0px);
  481. color: #333;
  482. /* font-weight: bold; */
  483. }
  484. .date,
  485. .nowTime {
  486. font-size: calc(1rem + 0px);
  487. color: #666;
  488. }
  489. // .nowTime {
  490. // font-size: 16px;
  491. // color: #666;
  492. // }
  493. }
  494. }
  495. .record {
  496. width: 100%;
  497. margin-top: 20rpx;
  498. border-top: 1px solid #ddd;
  499. border-bottom: 1px solid #ddd;
  500. padding: 0 15rpx;
  501. .record-item {
  502. display: flex;
  503. align-items: center;
  504. height: 100rpx;
  505. border-bottom: 1px solid #eee;
  506. .icon {
  507. width: 50rpx;
  508. height: 50rpx;
  509. margin-right: 10rpx;
  510. }
  511. .title {
  512. font-size: calc(0.9rem + 0px);
  513. }
  514. .time {
  515. font-size: calc(0.8rem + 0px);
  516. color: #999;
  517. margin-left: auto;
  518. }
  519. .signStatus {
  520. font-size: calc(0.9rem + 0px);
  521. color: orange;
  522. /* margin-left: auto; */
  523. }
  524. }
  525. }
  526. ::v-deep.map-content {
  527. .map {
  528. margin-top: 20rpx;
  529. height: 56vh !important;
  530. }
  531. }
  532. .footer {
  533. width: 100%;
  534. margin-top: 20rpx;
  535. display: flex;
  536. justify-content: space-around;
  537. button {
  538. font-size: calc(18px + .5*(1rem - 16px));
  539. width: 100%;
  540. }
  541. }
  542. </style>