clock.js 9.4 KB

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