index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. <template>
  2. <view class="tabPage" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
  3. <cus-header title='打卡' bgColor="transparent" :showback="false"></cus-header>
  4. <image class="topbg" :src="imgBase+'clockingin/topbg.png'" mode="widthFix"></image>
  5. <div class="info">
  6. <div class="i_avatar">
  7. <image :src="imgBase+'clockingin/default_avatar.png'"></image>
  8. </div>
  9. <div class="i_name_date">
  10. <p>{{username}}</p>
  11. <p class="date">{{dateweek}}</p>
  12. </div>
  13. </div>
  14. <div class="card">
  15. <div class="c_title">今日打卡</div>
  16. <div class="c_worktime">
  17. <div class="cw_pre">
  18. <p>上班{{workingTime||'08:30'}}</p>
  19. <div class="cwp_time">
  20. <image :src="imgBase+'clockingin/checked_active.png'" v-if="sbClockTime"></image>
  21. <image :src="imgBase+'clockingin/checked_inactive.png'" v-else></image>
  22. <span>打卡{{sbClockTime||'- -'}}</span>
  23. </div>
  24. <div class="cwp_status" :class="{'active':sbClockTime,'inactive':!sbClockTime}">{{sbClockTime?'已打卡':'未打卡'}}</div>
  25. </div>
  26. <div class="cw_pre">
  27. <p>下班{{offWorkTime||'17:30'}}</p>
  28. <div class="cwp_time">
  29. <image :src="imgBase+'clockingin/checked_active.png'" v-if="xbClockTime"></image>
  30. <image :src="imgBase+'clockingin/checked_inactive.png'" v-else></image>
  31. <span>{{xbClockTime||'- -'}}</span>
  32. </div>
  33. <div class="cwp_status" :class="{'active':xbClockTime,'inactive':!xbClockTime}">{{xbClockTime?'已打卡':'未打卡'}}</div>
  34. </div>
  35. </div>
  36. <div class="c_clock">
  37. <!-- 将按钮点击统一走 clock();禁用时不响应 -->
  38. <div class="cc_box"
  39. :style="{'background-image':'url('+imgBase+'clockingin/clock_active_bg.png)'}"
  40. @tap="!canClick?null:clock">
  41. <p>{{isSW?'上班':'下班'}}打卡</p>
  42. <p class="time">{{currentSFM}}</p>
  43. </div>
  44. <div class="cc_location">
  45. <image :src="imgBase+'clockingin/location_active.png'"></image>
  46. <span v-if="canClock">已进入考勤范围:谷锐特设备自动化有限公司</span>
  47. <span v-else>当前定位不在考勤范围内</span>
  48. </div>
  49. </div>
  50. </div>
  51. <u-popup :show="show" @close="close" mode="center" :round="32" :customStyle="{'width':'calc(100% - 120rpx)'}">
  52. <div class="up_box">
  53. <div class="upb_close">
  54. <image :src="imgBase+'clockingin/close.png'" @tap="close"></image>
  55. </div>
  56. <image class="upb_text" :src="imgBase+'clockingin/clock_success.png'"></image>
  57. <p class="upb_time">打卡时间 {{clockTime}}</p>
  58. <image class="upb_img" :src="imgBase+'clockingin/clock_success2.png'"></image>
  59. <div class="upb_btn" @tap="close">我知道了</div>
  60. </div>
  61. </u-popup>
  62. <Tabbar :tabbarIndex="0"></Tabbar>
  63. </view>
  64. </template>
  65. <script>
  66. var timer = null, timer2 = null;
  67. const QQMapWX = require('../static/lib/qqmap-wx-jssdk.min.js');
  68. const qqmapsdk = new QQMapWX({
  69. key: 'V3NBZ-6NRKT-QOQXY-LU6RY-PNHJ5-QDBTQKEY'
  70. });
  71. import { wgs84togcj02 } from '@/utils/coordtransform.js';
  72. import Tabbar from '@/components/CusTabbar/clock.vue'
  73. export default {
  74. components:{ Tabbar },
  75. data(){
  76. return {
  77. username:'',
  78. dateweek:'',
  79. workingTime:'',
  80. offWorkTime:'',
  81. sbClockTime:'',
  82. xbClockTime:'',
  83. isSW:true, // true: 上班打卡;false: 下班打卡
  84. currentSFM:new Date().Format('hh:mm:ss'),
  85. show:false,
  86. canClock:false, // 是否在考勤范围
  87. clockTime:'',
  88. distance: null,
  89. targetLocation: { //谷锐特 31.962084,117.020446 /跨境电商大厦 31.865786,117.15297
  90. latitude: 31.962084,
  91. longitude: 117.020446
  92. },
  93. userInfo:null,
  94. canClick:true, // 防连点:统一入口控制
  95. navigating:false,// 防止重复 navigateTo
  96. }
  97. },
  98. created() {
  99. this.getDateWeek();
  100. this.getCurrentSFM();
  101. this.getCurrentHour();
  102. if(uni.getStorageSync('userInfo')){
  103. this.userInfo = JSON.parse(uni.getStorageSync('userInfo'));
  104. this.username = this.userInfo.realName;
  105. }
  106. },
  107. onUnload() {
  108. clearInterval(timer)
  109. clearInterval(timer2)
  110. },
  111. mounted() {
  112. this.getRules();
  113. this.getCurrentLocation();
  114. timer2 = setInterval(()=>{
  115. this.getCurrentLocation();
  116. },5000)
  117. this.getUserInfo();
  118. },
  119. methods:{
  120. getRules(){
  121. this.$api.get('/wms/outsourced/attendance/rule').then(res=>{
  122. if(res.data.code!==0) return this.$showToast(res.data.msg)
  123. this.workingTime = res.data.data.workingTime.slice(0,5);
  124. this.offWorkTime = res.data.data.offWorkTime.slice(0,5);
  125. }).catch(()=>{})
  126. },
  127. getUserInfo(){
  128. // 回读服务端状态
  129. this.$api.get('/wms/outsourced/attendance/getAttendanceByUserId/'+this.userInfo?.id).then(res=>{
  130. if(res.data.code!==0) return this.$showToast(res.data.msg)
  131. this.sbClockTime = res.data.data?.checkInTime;
  132. this.xbClockTime = res.data.data?.clockOutTime;
  133. if(new Date().getHours()<12 && !this.sbClockTime) this.isSW = true
  134. else this.isSW = false;
  135. }).catch(()=>{})
  136. },
  137. getDateWeek(){
  138. let date = new Date().Format('yyyy年MM月dd日');
  139. let cfg = {0:'天',1:'一',2:'二',3:'三',4:'四',5:'五',6:'六'};
  140. let week = cfg[new Date().getDay()];
  141. this.dateweek = date+' 星期'+week;
  142. },
  143. getCurrentSFM(){
  144. clearInterval(timer)
  145. timer = setInterval(()=>{
  146. this.currentSFM = new Date().Format('hh:mm:ss');
  147. },1000)
  148. },
  149. getCurrentHour(){
  150. this.isSW = new Date().getHours()<12;
  151. },
  152. async clock(){
  153. if(!this.canClick) return;
  154. this.canClick = false; // 一进入就关点击
  155. const isSWNow = this.isSW; // 固定本次动作类型
  156. try {
  157. const url = isSWNow ? '/wms/outsourced/attendance/checkIn'
  158. : '/wms/outsourced/attendance/clockOut';
  159. const params = { outUserId: this.userInfo?.id };
  160. params[isSWNow ? 'checkInTime' : 'clockOutTime'] = new Date().Format('yyyy-MM-dd hh:mm');
  161. // 不在范围:跳转到外勤页面;防重复跳转
  162. if(!this.canClock){
  163. if(this.navigating){
  164. return;
  165. }
  166. this.navigating = true;
  167. uni.navigateTo({
  168. url:`/pagesClockin/fieldService?url=${url}&outUserId=${this.userInfo?.id}&isSW=${isSWNow?1:0}`,
  169. complete: ()=>{ /* 交给子页处理,当前页可允许再次点击 */
  170. this.canClick = true;
  171. }
  172. })
  173. return;
  174. }
  175. // 发请求
  176. const res = await this.$api.post(url, params);
  177. if(res.data.code!==0){
  178. this.$showToast(res.data.msg || '打卡失败');
  179. return;
  180. }
  181. // 成功:先本地更新,立刻切换态,避免在回读之前再次点到同一类型
  182. this.clockTime = new Date().Format('hh:mm');
  183. if(isSWNow){
  184. this.sbClockTime = this.clockTime;
  185. this.isSW = false; // 立刻切到“下班打卡”
  186. }else{
  187. this.xbClockTime = this.clockTime;
  188. }
  189. // 展示成功弹窗,弹窗期间保持禁用
  190. this.show = true;
  191. } catch (e) {
  192. this.$showToast('网络异常,请重试');
  193. } finally {
  194. // 若弹窗展示中,保持禁用;否则放开
  195. if(!this.show){
  196. this.canClick = true;
  197. }
  198. }
  199. },
  200. // 弹窗关闭:这里再回读一次,并恢复点击
  201. close(){
  202. this.show = false;
  203. this.getUserInfo(); // 与后端对齐一次
  204. this.canClick = true; // 现在再允许点击
  205. this.navigating = false;
  206. },
  207. // 获取当前位置
  208. async getCurrentLocation() {
  209. try {
  210. const res = await uni.getLocation({
  211. type: 'gcj02', // 腾讯地图使用GCJ-02坐标系
  212. isHighAccuracy: true
  213. });
  214. let origin = wgs84togcj02(res[1].longitude,res[1].latitude);
  215. let target = wgs84togcj02(this.targetLocation.longitude,this.targetLocation.latitude);
  216. this.calculateDistance(
  217. origin[1],
  218. origin[0],
  219. target[1],
  220. target[0]
  221. );
  222. } catch (err) {
  223. uni.showToast({
  224. title: '获取位置失败,请检查权限设置',
  225. icon: 'none'
  226. });
  227. }
  228. },
  229. // 计算两点间距离
  230. calculateDistance(lat1, lng1, lat2, lng2) {
  231. qqmapsdk.calculateDistance({
  232. mode: 'straight', // 直线距离
  233. from: `${lat1},${lng1}`,
  234. to: `${lat2},${lng2}`,
  235. success: (res) => {
  236. if(res.result && res.result.elements.length > 0) {
  237. this.distance = res.result.elements[0].distance;
  238. this.canClock = this.distance <= 50;
  239. }
  240. },
  241. fail: () => {
  242. uni.showToast({
  243. title: '距离计算失败',
  244. icon: 'none'
  245. });
  246. }
  247. });
  248. }
  249. }
  250. }
  251. </script>
  252. <style scoped lang="less">
  253. .tabPage{
  254. padding: 0 24rpx 188rpx;
  255. background: #F4F8FB;
  256. .topbg{
  257. width: 100%;
  258. position: fixed;
  259. top: 0;
  260. left: 0;
  261. z-index: 0;
  262. }
  263. .info{
  264. position: relative;
  265. background: #FFFFFF;
  266. border-radius: 16rpx;
  267. margin-top: 20rpx;
  268. padding: 48rpx 24rpx;
  269. display: flex;
  270. align-items: center;
  271. .i_avatar{
  272. width: 98rpx;
  273. height: 98rpx;
  274. image{
  275. width: 100%;
  276. height: 100%;
  277. }
  278. }
  279. .i_name_date{
  280. padding-left: 24rpx;
  281. p{
  282. font-family: PingFang-SC, PingFang-SC;
  283. font-weight: bold;
  284. font-size: 36rpx;
  285. color: #1D2129;
  286. line-height: 36rpx;
  287. &.date{
  288. font-size: 28rpx;
  289. color: #657588;
  290. line-height: 28rpx;
  291. margin-top: 22rpx;
  292. }
  293. }
  294. }
  295. }
  296. .card{
  297. position: relative;
  298. background: #FFFFFF;
  299. border-radius: 16rpx;
  300. padding: 44rpx 24rpx 211rpx;
  301. margin-top: 20rpx;
  302. .c_title{
  303. font-family: PingFang-SC, PingFang-SC;
  304. font-weight: bold;
  305. font-size: 36rpx;
  306. color: #1D2129;
  307. line-height: 36rpx;
  308. }
  309. .c_worktime{
  310. margin-top: 40rpx;
  311. display: flex;
  312. justify-content: space-between;
  313. .cw_pre{
  314. width: calc(50% - 15rpx);
  315. background: #F5F8FA;
  316. border-radius: 16rpx;
  317. position: relative;
  318. padding: 36rpx 20rpx;
  319. box-sizing: border-box;
  320. &>p{
  321. font-family: PingFangSC, PingFang SC;
  322. font-weight: 400;
  323. font-size: 32rpx;
  324. color: #1D2129;
  325. line-height: 32rpx;
  326. }
  327. .cwp_time{
  328. display: flex;
  329. align-items: center;
  330. margin-top: 32rpx;
  331. image{
  332. width: 28rpx;
  333. height: 28rpx;
  334. }
  335. span{
  336. font-family: PingFangSC, PingFang SC;
  337. font-weight: 400;
  338. font-size: 28rpx;
  339. color: #86909C;
  340. line-height: 36rpx;
  341. margin-left: 12rpx;
  342. }
  343. }
  344. .cwp_status{
  345. width: 108rpx;
  346. height: 54rpx;
  347. border-radius: 0rpx 16rpx 0rpx 12rpx;
  348. font-family: PingFang-SC, PingFang-SC;
  349. font-weight: bold;
  350. font-size: 24rpx;
  351. line-height: 54rpx;
  352. text-align: center;
  353. position: absolute;
  354. top: 0;
  355. right: 0;
  356. &.active{
  357. color: #14D08E;
  358. background: rgba(20, 204, 140, 0.08);
  359. }
  360. &.inactive{
  361. color: #86909C;
  362. background: #EFEFEF;
  363. }
  364. }
  365. }
  366. }
  367. .c_clock{
  368. display: flex;
  369. flex-direction: column;
  370. align-items: center;
  371. margin-top: 117rpx;
  372. .cc_box{
  373. width: 306rpx;
  374. height: 306rpx;
  375. background-repeat: no-repeat;
  376. background-size: 100% 100%;
  377. display: flex;
  378. flex-direction: column;
  379. align-items: center;
  380. justify-content: center;
  381. p{
  382. font-family: PingFang-SC, PingFang-SC;
  383. font-weight: bold;
  384. font-size: 40rpx;
  385. color: #FFFFFF;
  386. line-height: 56rpx;
  387. &.time{
  388. font-size: 32rpx;
  389. line-height: 45rpx;
  390. margin-top: 8px;
  391. }
  392. }
  393. }
  394. .cc_location{
  395. margin-top: 64rpx;
  396. display: flex;
  397. align-items: center;
  398. image{
  399. width: 36rpx;
  400. height: 36rpx;
  401. }
  402. span{
  403. font-family: PingFangSC, PingFang SC;
  404. font-weight: 400;
  405. font-size: 26rpx;
  406. color: #86909C;
  407. line-height: 26rpx;
  408. margin-left: 10rpx;
  409. }
  410. }
  411. }
  412. }
  413. .up_box{
  414. background: linear-gradient( 134deg, #E7F4FD 0%, #FFFFFF 100%);
  415. border-radius: 32rpx;
  416. padding: 40rpx;
  417. display: flex;
  418. flex-direction: column;
  419. align-items: center;
  420. .upb_close{
  421. width: 100%;
  422. display: flex;
  423. justify-content: flex-end;
  424. image{
  425. width: 32rpx;
  426. height: 32rpx;
  427. }
  428. }
  429. .upb_text{
  430. width: 210rpx;
  431. height: 50rpx;
  432. margin-top: 16rpx;
  433. }
  434. .upb_time{
  435. font-family: PingFangSC, PingFang SC;
  436. font-weight: 400;
  437. font-size: 28rpx;
  438. color: #86909C;
  439. line-height: 32rpx;
  440. margin-top: 36rpx;
  441. }
  442. .upb_img{
  443. width: 344rpx;
  444. height: 194rpx;
  445. margin-top: 216rpx;
  446. }
  447. .upb_btn{
  448. width: 446rpx;
  449. height: 88rpx;
  450. background: #0171F6;
  451. border-radius: 32rpx;
  452. font-family: PingFang-SC, PingFang-SC;
  453. font-weight: bold;
  454. font-size: 32rpx;
  455. color: #FFFFFF;
  456. line-height: 88rpx;
  457. text-align: center;
  458. margin-top: 112rpx;
  459. letter-spacing: 2rpx;
  460. }
  461. }
  462. }
  463. </style>