clockIn.vue 15 KB

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