clockIn.vue 15 KB

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