clock.js 7.8 KB

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