home.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. <template>
  2. <view class="container">
  3. <view class="placeholder"></view>
  4. <!-- 头部学生信息区域 -->
  5. <view class="header">
  6. <view class="img">
  7. <img v-if="userInfo.headImage" :src="userInfo.headImage" />
  8. <img v-else src="../../static/imgs/headImage.png" />
  9. </view>
  10. <view class="msg">
  11. <view class="name">{{ userInfo.name || '用户' }}</view>
  12. <view class="major">{{ userInfo.college || '南昌交通学院' }}</view>
  13. </view>
  14. </view>
  15. <!-- 主体打卡区域 -->
  16. <view class="body">
  17. <!-- 卡片区域 -->
  18. <view class="card" v-if="list.length">
  19. <!-- 每一个卡片区域 -->
  20. <view :class="activeid == item.id ? 'active_box' : 'item'" v-for="item in list" :key="item.id" @click="handleClick(item)">
  21. <view class="item_box">
  22. <view class="title">{{ item.ruleName }}</view>
  23. <view class="time">{{ item.timeRange }}</view>
  24. <view class="type">
  25. <img v-if="item.status == 4" src="./imgs/success.png" />
  26. <span v-if="item.status == 4">{{ format_time(item.updateTime) }}</span>
  27. <span v-if="item.status == 4">已打卡</span>
  28. <span v-if="item.status == 3">已超时</span>
  29. <span v-if="item.status == 1 || item.status == 2">未打卡</span>
  30. </view>
  31. </view>
  32. </view>
  33. </view>
  34. <!-- list数组为空时的占位盒子 -->
  35. <view class="card" v-else></view>
  36. <!-- 打卡区域 -->
  37. <view :class="{ clock: flags, active: !flags }" @click="handlePunch(contrastObj)">
  38. <view class="info" v-if="flags">打卡</view>
  39. <view class="info" v-else>无法打卡</view>
  40. <view class="time">{{ nowTime }}</view>
  41. </view>
  42. <!-- 提示信息位置 -->
  43. <view class="address" v-if="flags">{{ address }}</view>
  44. <view class="address" v-else>{{ notes }}</view>
  45. </view>
  46. <!-- 底部导航栏区域 -->
  47. <view class="tab_bar">
  48. <navigator open-type="redirect" url="/pages/home/home" class="tab_box">
  49. <img v-if="pageUrl == 'pages/home/home'" src="../../static/imgs/home_active.png" />
  50. <img v-else src="../../static/imgs/home.png" />
  51. <view v-if="pageUrl == 'pages/home/home'" class="tab_title_active">首页</view>
  52. <view v-else class="tab_title">首页</view>
  53. </navigator>
  54. <navigator open-type="redirect" url="/pages/stat/stat" class="tab_box">
  55. <img v-if="pageUrl == 'pages/stat/stat'" src="../../static/imgs/stat_active.png" />
  56. <img v-else src="../../static/imgs/stat.png" />
  57. <view v-if="pageUrl == 'pages/stat/stat'" class="tab_title_active">统计</view>
  58. <view v-else class="tab_title">统计</view>
  59. </navigator>
  60. <navigator open-type="redirect" v-if="showTab" url="/pages/my/my" class="tab_box">
  61. <img v-if="pageUrl == 'pages/my/my'" src="../../static/imgs/my_active.png" />
  62. <img v-else src="../../static/imgs/my.png" />
  63. <view v-if="pageUrl == 'pages/my/my'" class="tab_title_active">我的</view>
  64. <view v-else class="tab_title">我的</view>
  65. </navigator>
  66. </view>
  67. <!-- 认证结果弹窗 -->
  68. <uni-popup ref="popup" :is-mask-click="false">
  69. <view class="popup-content">
  70. <view class="title">
  71. <view class="icon"><img src="./imgs/success2.png" /></view>
  72. <view class="title_info">打卡成功</view>
  73. </view>
  74. <view class="time">{{ nowTime_pop }}</view>
  75. <view class="popup_button" @click="handleGoHome">我知道了</view>
  76. </view>
  77. </uni-popup>
  78. </view>
  79. </template>
  80. <script>
  81. export default {
  82. data() {
  83. return {
  84. // 用户信息
  85. userInfo: {},
  86. // 打卡规则列表
  87. list: [],
  88. // 当前时间
  89. nowTime: '',
  90. // 弹窗当前时间
  91. nowTime_pop: '',
  92. // 当前定位位置信息
  93. address: '',
  94. // 定时器标识
  95. timer: null,
  96. // 提示信息
  97. notes: '',
  98. // 当前显示的是哪个规则id
  99. activeid: null,
  100. // 当前时间的时间戳
  101. timestamp: null,
  102. // 当前显示的规则具体信息
  103. contrastObj: {},
  104. // 当前用户定位经度
  105. myLng: 0,
  106. // 当前用户定位纬度
  107. myLat: 0,
  108. // 签到点中心经度
  109. centerLng: 0,
  110. // 签到点中心纬度
  111. centerLat: 0,
  112. // 签到半径
  113. radius: 0,
  114. // 距离签到点的距离
  115. distance: 0,
  116. // 是否可以打卡的标识
  117. flags: false,
  118. // 当前页面的路由地址
  119. pageUrl: '',
  120. // 是否显示底部 我的 导航栏
  121. showTab: false,
  122. // 是否需要人脸识别
  123. faceRecognition: true,
  124. // 是否需要拍摄场景照片
  125. takePicture: true
  126. }
  127. },
  128. onLoad() {
  129. // 获取用户的个人信息数据
  130. this.getUserInfo()
  131. // 获取当前系统时间
  132. this.getNowTime()
  133. },
  134. onShow() {
  135. // 获取当前页面路径
  136. this.getPageUrl()
  137. // 获取当前位置的详细信息
  138. this.getLocationData()
  139. // 获取当前时间的时间戳
  140. this.getTimestamp()
  141. },
  142. onUnload() {
  143. if (this.timer) {
  144. clearInterval(this.timer)
  145. }
  146. },
  147. // 下拉刷新
  148. onPullDownRefresh() {
  149. uni.removeStorageSync('manager')
  150. uni.removeStorageSync('sub-administrator')
  151. this.getNowTime()
  152. this.getTimestamp()
  153. this.getUserInfo()
  154. this.getLocationData()
  155. setTimeout(() => {
  156. uni.stopPullDownRefresh()
  157. }, 1500)
  158. },
  159. methods: {
  160. getPageUrl() {
  161. // 获取当前打开过的页面路由数组
  162. let routes = getCurrentPages()
  163. // 获取当前页面路由,也就是最后一个打开的页面路由
  164. let curRoute = routes[routes.length - 1].route
  165. this.pageUrl = curRoute
  166. },
  167. // 获取当前时间
  168. getNowTime() {
  169. if (this.timer) {
  170. clearInterval(this.timer)
  171. }
  172. this.timer = setInterval(() => {
  173. let date = new Date()
  174. let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  175. let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  176. let seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  177. this.nowTime = hours + ':' + minutes + ':' + seconds
  178. }, 1000)
  179. },
  180. // 获取当前时间的时间戳
  181. getTimestamp() {
  182. let dates = new Date()
  183. let times = dates.getTime()
  184. this.timestamp = times
  185. },
  186. // 获取用户详细信息
  187. async getUserInfo() {
  188. let res = await this.$myRequest_clockIn({
  189. url: `/attendance/api/system/user/detail`
  190. })
  191. // console.log(res)
  192. if (res.code == 200) {
  193. this.userInfo = res.data
  194. uni.setStorageSync('clockIn_userInfo', this.userInfo)
  195. if (this.userInfo.roles) {
  196. let temList = []
  197. this.userInfo.roles.forEach(ele => {
  198. temList.push(ele.id)
  199. })
  200. let flag = temList.includes('2' || 2)
  201. let flag2 = temList.includes('3' || 3)
  202. if (flag) {
  203. uni.setStorageSync('manager', flag)
  204. } else {
  205. uni.removeStorageSync('manager')
  206. }
  207. if (flag2) {
  208. uni.setStorageSync('sub-administrator', flag2)
  209. } else {
  210. uni.removeStorageSync('sub-administrator')
  211. }
  212. let flag_show = uni.getStorageSync('manager')
  213. let flag_show2 = uni.getStorageSync('sub-administrator')
  214. if (flag_show || flag_show2) {
  215. this.showTab = true
  216. } else {
  217. this.showTab = false
  218. }
  219. }
  220. }
  221. },
  222. // 获取当前定位位置信息
  223. getLocationData() {
  224. uni.showLoading({
  225. title: '定位中',
  226. mask: true
  227. })
  228. // 获取经纬度
  229. uni.getLocation({
  230. type: 'gcj02',
  231. success: res => {
  232. const KEY = 'X57BZ-ZISE3-KTN3O-3P45H-C3J7Q-D5B67'
  233. let url = 'https://apis.map.qq.com/ws/geocoder/v1'
  234. let locationdata = res.latitude + ',' + res.longitude
  235. this.$jsonp(url, {
  236. key: KEY,
  237. output: 'jsonp',
  238. location: locationdata
  239. })
  240. .then(json => {
  241. // console.log(json)
  242. if (json.status == 0) {
  243. // 获取详细地址信息 经纬度
  244. this.address = json.result.address
  245. this.myLat = json.result.location.lat
  246. this.myLng = json.result.location.lng
  247. // 获取当天的打卡列表数组
  248. this.getRulesList()
  249. } else {
  250. uni.showToast({
  251. title: `${json.message},请求定位失败`,
  252. icon: 'none'
  253. })
  254. }
  255. })
  256. .catch(err => {
  257. console.log(err)
  258. })
  259. }
  260. })
  261. },
  262. // 获取打卡规则列表
  263. async getRulesList() {
  264. let res = await this.$myRequest_clockIn({
  265. url: '/attendance/api/sign/check/in/list/today'
  266. })
  267. // console.log(res)
  268. if (res.code == 200) {
  269. if (res.data.length == 0) {
  270. this.flags = false
  271. this.notes = '无打卡任务无需打卡'
  272. } else {
  273. this.flags = true
  274. this.list = res.data
  275. this.activeid = this.list[0].id
  276. this.faceRecognition = this.list[0].faceRecognition
  277. this.takePicture = this.list[0].takePicture
  278. this.contrastObj = this.list[0]
  279. this.changeType()
  280. }
  281. }
  282. },
  283. // 对比信息改变打卡的状态显示
  284. changeType() {
  285. if (this.contrastObj.status == 4) {
  286. this.flags = false
  287. this.notes = '已打卡,无需再次打卡'
  288. } else {
  289. // 没有到打卡时间 或者 超过打卡时间 的状态
  290. if (this.timestamp < this.contrastObj.beginTime || this.timestamp > this.contrastObj.endTime) {
  291. this.flags = false
  292. this.notes = '不在打卡时间段内,无法打卡'
  293. }
  294. // 到了打卡时间,判断是否在打卡范围内
  295. else {
  296. if (this.contrastObj.locations.length) {
  297. let temList = []
  298. temList = this.contrastObj.locations.map(ele => {
  299. this.centerLat = ele.lat
  300. this.centerLng = ele.lng
  301. this.radius = ele.radius
  302. let red1 = (this.myLat * Math.PI) / 180.0
  303. let red2 = (this.centerLat * Math.PI) / 180.0
  304. let a = red1 - red2
  305. let b = (this.myLng * Math.PI) / 180.0 - (this.centerLng * Math.PI) / 180.0
  306. let R = 6378137
  307. let distance = R * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(red1) * Math.cos(red2) * Math.pow(Math.sin(b / 2), 2)))
  308. this.distance = distance.toFixed(2) * 1
  309. // console.log(this.distance);
  310. // console.log(this.radius);
  311. if (this.distance <= this.radius) {
  312. return 1
  313. } else {
  314. return 2
  315. }
  316. })
  317. let temFlag = temList.indexOf(1)
  318. if (temFlag == -1) {
  319. this.flags = false
  320. this.notes = '不在管理员设定范围内,无法打卡'
  321. } else {
  322. this.flags = true
  323. }
  324. }
  325. }
  326. }
  327. },
  328. // 点击每一个打卡规则回调
  329. handleClick(item) {
  330. // console.log(item);
  331. this.getTimestamp()
  332. this.contrastObj = item
  333. this.activeid = item.id
  334. this.faceRecognition = item.faceRecognition
  335. this.takePicture = item.takePicture
  336. this.changeType()
  337. },
  338. // 点击打卡按钮回调
  339. handlePunch(info) {
  340. if (this.flags) {
  341. let obj = JSON.stringify(info)
  342. // 如果场景照片和人脸识别都需要
  343. if (this.faceRecognition && this.takePicture) {
  344. uni.navigateTo({
  345. url: `/pages/location/location?obj=${obj}`
  346. })
  347. }
  348. // 如果都不需要
  349. else if (!this.faceRecognition && !this.takePicture) {
  350. this.handleUploading()
  351. }
  352. // 如果只需要场景照片
  353. else if (this.takePicture) {
  354. uni.navigateTo({
  355. url: `/pages/location/location?obj=${obj}&flag=1`
  356. })
  357. }
  358. // 如果只需要人脸识别
  359. else if (this.faceRecognition) {
  360. uni.navigateTo({
  361. url: `/pages/authentication/authentication?id=${this.contrastObj.id}&address=${this.address}&latitude=${this.myLat}&longitude=${this.myLng}&flag=1`
  362. })
  363. }
  364. }
  365. },
  366. // 打卡请求
  367. async handleUploading() {
  368. let res = await this.$myRequest_clockIn({
  369. url: '/attendance/api/sign/check/in/update',
  370. method: 'put',
  371. header: {
  372. 'content-type': 'application/json'
  373. },
  374. data: {
  375. id: this.contrastObj.id,
  376. lat: this.myLat,
  377. lng: this.myLng,
  378. location: this.address
  379. }
  380. })
  381. // console.log(res);
  382. if (res.code == 200) {
  383. this.getNowTimePop()
  384. this.$refs.popup.open()
  385. }
  386. },
  387. // 点击 我知道了按钮 跳回首页
  388. handleGoHome() {
  389. this.$refs.popup.close()
  390. uni.reLaunch({
  391. url: '/pages/home/home'
  392. })
  393. },
  394. getNowTimePop() {
  395. let date = new Date()
  396. let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  397. let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  398. let seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  399. this.nowTime_pop = hours + ':' + minutes + ':' + seconds
  400. },
  401. // 格式化时间
  402. format_time(timestamp) {
  403. //时间戳为10位需*1000,时间戳为13位的话不需乘1000
  404. var date = new Date(timestamp)
  405. var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
  406. var m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  407. let strDate = h + m
  408. return strDate
  409. }
  410. }
  411. }
  412. </script>
  413. <style lang="scss" scoped>
  414. .container {
  415. width: 100vw;
  416. height: 100vh;
  417. background-color: #f2f2f2;
  418. .placeholder {
  419. height: 20rpx;
  420. }
  421. .header {
  422. display: flex;
  423. margin: 0 30rpx 30rpx 30rpx;
  424. width: 690rpx;
  425. height: 130rpx;
  426. background-color: #fff;
  427. .img {
  428. margin: 30rpx;
  429. width: 70rpx;
  430. height: 70rpx;
  431. img {
  432. width: 100%;
  433. height: 100%;
  434. }
  435. }
  436. .msg {
  437. margin: 30rpx 0;
  438. height: 70rpx;
  439. line-height: 36rpx;
  440. .name {
  441. font-size: 32rpx;
  442. font-weight: 400;
  443. }
  444. .major {
  445. font-size: 24rpx;
  446. font-weight: 400;
  447. color: #a6a6a6;
  448. }
  449. }
  450. }
  451. .body {
  452. margin: 0 30rpx;
  453. width: 690rpx;
  454. height: 75vh;
  455. background-color: #fff;
  456. .card {
  457. padding-top: 30rpx;
  458. white-space: nowrap;
  459. height: 160rpx;
  460. overflow-x: auto;
  461. .active_box {
  462. display: inline-block;
  463. margin: 0 15rpx;
  464. width: 300rpx;
  465. height: 130rpx;
  466. line-height: 12rpx;
  467. border-radius: 8rpx;
  468. font-weight: 400;
  469. background-color: #e6e6e6;
  470. border: 1rpx solid #0094fc;
  471. .item_box {
  472. display: flex;
  473. flex-direction: column;
  474. justify-content: space-evenly;
  475. height: 130rpx;
  476. padding-left: 30rpx;
  477. .title {
  478. font-size: 28rpx;
  479. }
  480. .time {
  481. font-size: 24rpx;
  482. }
  483. .type {
  484. font-size: 24rpx;
  485. color: #808080;
  486. img {
  487. margin-right: 10rpx;
  488. width: 20rpx;
  489. height: 20rpx;
  490. }
  491. }
  492. }
  493. }
  494. .item {
  495. display: inline-block;
  496. margin: 0 15rpx;
  497. width: 300rpx;
  498. height: 130rpx;
  499. line-height: 12rpx;
  500. border-radius: 8rpx;
  501. font-weight: 400;
  502. background-color: #e6e6e6;
  503. .item_box {
  504. display: flex;
  505. flex-direction: column;
  506. justify-content: space-evenly;
  507. height: 130rpx;
  508. padding-left: 30rpx;
  509. .title {
  510. font-size: 28rpx;
  511. }
  512. .time {
  513. font-size: 24rpx;
  514. }
  515. .type {
  516. font-size: 24rpx;
  517. color: #808080;
  518. img {
  519. margin-right: 10rpx;
  520. width: 20rpx;
  521. height: 20rpx;
  522. }
  523. }
  524. }
  525. }
  526. }
  527. .clock {
  528. display: flex;
  529. flex-direction: column;
  530. justify-content: center;
  531. align-items: center;
  532. margin: auto;
  533. margin-top: 145rpx;
  534. width: 300rpx;
  535. height: 300rpx;
  536. border-radius: 150rpx;
  537. text-align: center;
  538. color: #fff;
  539. background: linear-gradient(180deg, #00acfc 0%, #0082fc 100%);
  540. .info {
  541. height: 58rpx;
  542. font-size: 40rpx;
  543. font-weight: 700;
  544. }
  545. .time {
  546. height: 41rpx;
  547. font-size: 28rpx;
  548. font-weight: 400;
  549. }
  550. }
  551. .active {
  552. display: flex;
  553. flex-direction: column;
  554. justify-content: center;
  555. align-items: center;
  556. margin: auto;
  557. margin-top: 145rpx;
  558. width: 300rpx;
  559. height: 300rpx;
  560. border-radius: 150rpx;
  561. text-align: center;
  562. color: #fff;
  563. background: linear-gradient(180deg, #cccccc 0%, #b3b3b3 100%);
  564. .info {
  565. height: 58rpx;
  566. font-size: 40rpx;
  567. font-weight: 700;
  568. }
  569. .time {
  570. height: 41rpx;
  571. font-size: 28rpx;
  572. font-weight: 400;
  573. }
  574. }
  575. .address {
  576. margin-top: 34rpx;
  577. height: 35rpx;
  578. line-height: 35rpx;
  579. text-align: center;
  580. font-size: 24rpx;
  581. font-weight: 400;
  582. color: #808080;
  583. }
  584. }
  585. .tab_bar {
  586. position: fixed;
  587. left: 0;
  588. bottom: 0;
  589. display: flex;
  590. width: 750rpx;
  591. height: 128rpx;
  592. border-top: 1rpx solid #ccc;
  593. background-color: #fff;
  594. .tab_box {
  595. flex: 1;
  596. display: flex;
  597. flex-direction: column;
  598. justify-content: center;
  599. align-items: center;
  600. img {
  601. margin-bottom: 10rpx;
  602. width: 54rpx;
  603. height: 48rpx;
  604. }
  605. .tab_title {
  606. font-size: 20rpx;
  607. }
  608. .tab_title_active {
  609. font-size: 20rpx;
  610. color: #0082fc;
  611. }
  612. }
  613. }
  614. .popup-content {
  615. display: flex;
  616. flex-direction: column;
  617. justify-content: space-around;
  618. align-items: center;
  619. width: 630rpx;
  620. height: 376rpx;
  621. border-radius: 33rpx;
  622. background-color: #fff;
  623. .title {
  624. display: flex;
  625. justify-content: center;
  626. align-items: center;
  627. .icon {
  628. width: 40rpx;
  629. height: 40rpx;
  630. img {
  631. width: 100%;
  632. height: 100%;
  633. }
  634. }
  635. .title_info {
  636. margin-left: 8rpx;
  637. font-size: 36rpx;
  638. font-weight: 800;
  639. color: #0082fc;
  640. }
  641. }
  642. .time {
  643. font-size: 32rpx;
  644. }
  645. .popup_button {
  646. width: 50%;
  647. text-align: center;
  648. font-size: 32rpx;
  649. color: #0082fc;
  650. }
  651. }
  652. }
  653. </style>