clock.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. // pages/clock/clock.js
  2. import {
  3. punchClockNeedInfo,
  4. savePunchClock
  5. } from '../../services/index.js'
  6. import {
  7. imgServerUrl
  8. } from '../../config/config.js'
  9. import { transformFromWGSToGCJ } from '../../utils/util'
  10. import loading from '../../utils/loading.js'
  11. let clickStatus = true
  12. let locationStatus = true
  13. let nowTime = 0
  14. Page({
  15. /**
  16. * 页面的初始数据
  17. */
  18. data: {
  19. latitude: '',
  20. longitude: '',
  21. approveStatus: false,
  22. showCover: true,
  23. settingVisible: false,
  24. imgServerUrl,
  25. navigation: [
  26. {
  27. image: '/images/clock/clock-tongji.png',
  28. text: '统计'
  29. },
  30. {
  31. image: '/images/clock/clock-rule-icon.png',
  32. text: '规则'
  33. },
  34. {
  35. image: '/images/clock/clock-kefu.png',
  36. text: '客服'
  37. },
  38. ],
  39. clockType: 3,
  40. statusImage: `${imgServerUrl}/images/clock/clock-beyond.png`,
  41. statusIcon: `${imgServerUrl}/images/clock/clock-waring.png`,
  42. statusMessage: `请进入企业后再进行打卡`,
  43. startPunch: "未完成",
  44. endPunch: '未完成',
  45. todayMoney: 0,
  46. sumDuration: 0,
  47. sumMoney: 0,
  48. wsPunchClockId: '',
  49. actions: [
  50. {
  51. name: '取消',
  52. color: '#007AFF'
  53. },
  54. {
  55. name: '确认',
  56. color: '#007AFF',
  57. fontWeight: 600
  58. }
  59. ],
  60. userInfo: '',
  61. hpMember: ''
  62. },
  63. closeSettingVisible(data) {
  64. const { index } = data.detail;
  65. if (index === 0) {
  66. this.setData({
  67. settingVisible: false
  68. })
  69. }
  70. },
  71. toRule() {
  72. wx.navigateTo({
  73. url: './clock-rule/clock-rule',
  74. })
  75. },
  76. getLocation() {
  77. return new Promise(reslove => {
  78. wx.getLocation({
  79. type: 'gcj02',
  80. success: data => {
  81. // const { latitude, longitude } = transformFromWGSToGCJ(data.latitude, data.longitude)
  82. // reslove(Object.assign(data, { latitude, longitude }))
  83. reslove(data)
  84. },
  85. fail: res => {
  86. reslove(res)
  87. }
  88. })
  89. })
  90. },
  91. location() {
  92. return new Promise((reslove, reject) => {
  93. wx.getSetting({
  94. success: async res => {
  95. if (res.authSetting['scope.userLocation']) {
  96. const data = await this.getLocation()
  97. const info = wx.getSystemInfoSync()
  98. if (data.errMsg.indexOf('system permission denied') !== -1 || info.locationEnabled === false) {
  99. wx.showModal({
  100. content: '请授权微信获取位置信息权限',
  101. title: '微信未能获取位置信息',
  102. showCancel: false,
  103. confirmColor: '#007AFF',
  104. confirmText: '确认',
  105. })
  106. // this.setData({
  107. // settingVisible: true
  108. // })
  109. reject()
  110. } else {
  111. this.setData({
  112. latitude: data.latitude,
  113. longitude: data.longitude
  114. })
  115. // this.setData({
  116. // longitude: '120.35333936646411',
  117. // latitude: '31.535590759589642'
  118. // })
  119. reslove(data)
  120. }
  121. } else {
  122. wx.authorize({
  123. scope: 'scope.userLocation',
  124. success: async () => {
  125. reslove(await this.getLocation())
  126. },
  127. fail: () => {
  128. wx.showModal({
  129. content: '',
  130. // confirmText: '去授权',
  131. title: '请前往设置打开用户定位权限',
  132. confirmColor: '#007AFF',
  133. cancelColor: '#007AFF',
  134. confirmText: '确认',
  135. success: (result) => {
  136. if (result.confirm) {
  137. wx.openSetting({})
  138. } else {
  139. this.setData({
  140. clockType: 3
  141. })
  142. reject()
  143. }
  144. },
  145. })
  146. }
  147. })
  148. }
  149. }
  150. })
  151. })
  152. },
  153. async click() {
  154. // const data = await this.location()
  155. if (this.data.clockType === 1 && !this.data.wsPunchClockId) {
  156. this.clock(this.data.longitude, this.data.latitude)
  157. // this.clock(120.35333936646411, 31.535590759589642)
  158. }
  159. if (this.data.clockType === 1 && this.data.wsPunchClockId) {
  160. // wx.showModal({
  161. // title: '提示',
  162. // content: '未到下班时间,请刷新页面获取最新状态',
  163. // showCancel: false,
  164. // })
  165. }
  166. if (this.data.clockType === 2 && this.data.wsPunchClockId) {
  167. this.clock(this.data.longitude, this.data.latitude)
  168. }
  169. if (this.data.clockType === 3) {
  170. // wx.showModal({
  171. // title: '提示',
  172. // content: '超出范围,请刷新页面获取当前位置',
  173. // showCancel: false,
  174. // })
  175. }
  176. if (this.data.clockType === 4) {
  177. // wx.showModal({
  178. // title: '提示',
  179. // content: '今日打卡已完成',
  180. // showCancel: false,
  181. // })
  182. }
  183. },
  184. clock(longitude, latitude) {
  185. if (clickStatus) {
  186. clickStatus = false
  187. savePunchClock({ longitude, latitude, clockType: this.data.clockType, wsPunchClockId: this.data.wsPunchClockId }).then(data => {
  188. wx.showToast({
  189. title: '打卡成功',
  190. success: () => {
  191. clickStatus = true
  192. wx.navigateTo({
  193. url: `./clock-success/clock-success?clockType=${this.data.clockType}&&sumMoney=${data.data.sumMoney}&&duration=${data.data.duration}&&todayMoney=${data.data.todayMoney}&&time=${this.data.wsPunchClockId ? data.data.endPunchClock : data.data.startPunchClock}`
  194. })
  195. }
  196. })
  197. }).catch(data => {
  198. clickStatus = true
  199. wx.showModal({
  200. title: '提示',
  201. content: data.errmsg,
  202. showCancel: false,
  203. })
  204. })
  205. } else {
  206. wx.showToast({
  207. icon: 'none',
  208. title: '请勿重复点击',
  209. })
  210. }
  211. },
  212. navigator(e) {
  213. const { index } = e.currentTarget.dataset
  214. if (index === 0) {
  215. wx.navigateTo({
  216. url: './clock-tongji/index',
  217. })
  218. }
  219. if (index === 1) {
  220. wx.navigateTo({
  221. url: './clock-rule/clock-rule',
  222. })
  223. }
  224. if (index === 2) {
  225. this.changeCoverStatus()
  226. }
  227. },
  228. callKefu() {
  229. wx.makePhoneCall({
  230. phoneNumber: '4006920099',
  231. })
  232. },
  233. changeCoverStatus() {
  234. this.setData({
  235. showCover: !this.data.showCover
  236. })
  237. },
  238. async load(showLoading = true) {
  239. try {
  240. if (showLoading) {
  241. wx.showLoading({
  242. title: '定位中...',
  243. })
  244. }
  245. nowTime = new Date().getTime()
  246. punchClockNeedInfo({ longitude: this.data.longitude, latitude: this.data.latitude }).then(data => {
  247. // punchClockNeedInfo({ longitude: 120.3533, latitude: 31.535590759589642 }).then(data => {
  248. locationStatus = true
  249. let {
  250. authenticationStatus,
  251. startPunch,
  252. endPunch,
  253. todayMoney,
  254. clockType, // 1:上班打卡 2:下班打卡 3:超出范围 4:已完成
  255. sumDuration,
  256. sumMoney,
  257. wsPunchClockId,
  258. dailyAvailableMinHour
  259. } = data.data
  260. this.setData({
  261. approveStatus: authenticationStatus, // 认证状态(0:未认证 1:等待认证 2:未通过 3:已认证) ,
  262. startPunch,
  263. endPunch,
  264. todayMoney,
  265. clockType,
  266. sumDuration,
  267. sumMoney,
  268. wsPunchClockId
  269. })
  270. if (clockType === 1 && !wsPunchClockId) {
  271. this.setData({
  272. statusImage: `${imgServerUrl}/images/clock/clock-in.png`,
  273. statusIcon: `${imgServerUrl}/images/clock/clock-can.png`,
  274. statusMessage: '已进入考勤打卡范围'
  275. })
  276. }
  277. if (clockType === 1 && wsPunchClockId) {
  278. this.setData({
  279. statusImage: `${imgServerUrl}/images/clock/clock-out-disabled.png`,
  280. statusIcon: `${imgServerUrl}/images/clock/clock-waring.png`,
  281. statusMessage: `上班${dailyAvailableMinHour}小时后才能进行下班打卡`,
  282. })
  283. }
  284. if (clockType === 2 && wsPunchClockId) {
  285. this.setData({
  286. statusImage: `${imgServerUrl}/images/clock/clock-out.png`,
  287. statusIcon: `${imgServerUrl}/images/clock/clock-can.png`,
  288. statusMessage: `已进入考勤打卡范围`,
  289. })
  290. }
  291. if (clockType === 3) {
  292. this.setData({
  293. statusImage: `${imgServerUrl}/images/clock/clock-beyond.png`,
  294. statusIcon: `${imgServerUrl}/images/clock/clock-waring.png`,
  295. statusMessage: `请进入企业后再进行打卡`,
  296. })
  297. }
  298. if (clockType === 4) {
  299. this.setData({
  300. statusImage: `${imgServerUrl}/images/clock/clock-done.png`,
  301. statusIcon: `${imgServerUrl}/images/clock/clock-can.png`,
  302. statusMessage: `今日打卡已完成`,
  303. })
  304. }
  305. }).catch(data => {
  306. locationStatus = true
  307. }).finally(res => {
  308. if (showLoading) {
  309. wx.hideLoading()
  310. }
  311. })
  312. } catch (e) {
  313. wx.hideLoading()
  314. locationStatus = true
  315. }
  316. },
  317. reload() {
  318. if (locationStatus) {
  319. locationStatus = false
  320. this.load()
  321. } else {
  322. wx.showToast({
  323. icon: 'none',
  324. title: '请勿重复点击',
  325. })
  326. }
  327. },
  328. /**
  329. * 生命周期函数--监听页面加载
  330. */
  331. onLoad: function (options) {
  332. },
  333. /**
  334. * 生命周期函数--监听页面初次渲染完成
  335. */
  336. onReady: function () {
  337. },
  338. /**
  339. * 生命周期函数--监听页面显示
  340. */
  341. onShow: async function () {
  342. this.setData({
  343. userInfo: wx.getStorageSync('userInfo'),
  344. hpMember: wx.getStorageSync('hpMember')
  345. })
  346. await this.location()
  347. this.load()
  348. wx.onLocationChange((result) => {
  349. const { latitude, longitude } = result
  350. this.setData({
  351. latitude,
  352. longitude
  353. }, _ => {
  354. if (this.data.userInfo && this.data.hpMember && new Date().getTime() - nowTime > 60000) {
  355. this.load(false)
  356. }
  357. })
  358. })
  359. wx.startLocationUpdate({
  360. success: (res) => {
  361. console.log(res)
  362. },
  363. })
  364. },
  365. /**
  366. * 生命周期函数--监听页面隐藏
  367. */
  368. onHide: function () {
  369. wx.offLocationChange()
  370. },
  371. /**
  372. * 生命周期函数--监听页面卸载
  373. */
  374. onUnload: function () {
  375. },
  376. /**
  377. * 页面相关事件处理函数--监听用户下拉动作
  378. */
  379. onPullDownRefresh: function () {
  380. this.load()
  381. },
  382. /**
  383. * 页面上拉触底事件的处理函数
  384. */
  385. onReachBottom: function () {
  386. },
  387. /**
  388. * 用户点击右上角分享
  389. */
  390. onShareAppMessage: function () {
  391. }
  392. })