clock.js 8.4 KB

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