clockIn.vue 16 KB

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