checkin.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. package checkin
  2. import (
  3. "fmt"
  4. "github.com/silenceper/wechat/v2/util"
  5. )
  6. const (
  7. // setScheduleListURL 为打卡人员排班
  8. setScheduleListURL = "https://qyapi.weixin.qq.com/cgi-bin/checkin/setcheckinschedulist?access_token=%s"
  9. // punchCorrectionURL 为打卡人员补卡
  10. punchCorrectionURL = "https://qyapi.weixin.qq.com/cgi-bin/checkin/punch_correction?access_token=%s"
  11. // addUserFaceURL 录入打卡人员人脸信息
  12. addUserFaceURL = "https://qyapi.weixin.qq.com/cgi-bin/checkin/addcheckinuserface?access_token=%s"
  13. // addOptionURL 创建打卡规则
  14. addOptionURL = "https://qyapi.weixin.qq.com/cgi-bin/checkin/add_checkin_option?access_token=%s"
  15. // updateOptionURL 修改打卡规则
  16. updateOptionURL = "https://qyapi.weixin.qq.com/cgi-bin/checkin/update_checkin_option?access_token=%s"
  17. // clearOptionURL 清空打卡规则数组元素
  18. clearOptionURL = "https://qyapi.weixin.qq.com/cgi-bin/checkin/clear_checkin_option_array_field?access_token=%s"
  19. // delOptionURL 删除打卡规则
  20. delOptionURL = "https://qyapi.weixin.qq.com/cgi-bin/checkin/del_checkin_option?access_token=%s"
  21. // addRecordURL 添加打卡记录
  22. addRecordURL = "https://qyapi.weixin.qq.com/cgi-bin/checkin/add_checkin_record?access_token=%s"
  23. )
  24. // SetScheduleListRequest 为打卡人员排班请求
  25. type SetScheduleListRequest struct {
  26. GroupID int64 `json:"groupid"`
  27. Items []SetScheduleListItem `json:"items"`
  28. YearMonth int64 `json:"yearmonth"`
  29. }
  30. // SetScheduleListItem 排班表信息
  31. type SetScheduleListItem struct {
  32. UserID string `json:"userid"`
  33. Day int64 `json:"day"`
  34. ScheduleID int64 `json:"schedule_id"`
  35. }
  36. // SetScheduleList 为打卡人员排班
  37. // see https://developer.work.weixin.qq.com/document/path/93385
  38. func (r *Client) SetScheduleList(req *SetScheduleListRequest) error {
  39. var (
  40. accessToken string
  41. err error
  42. )
  43. if accessToken, err = r.GetAccessToken(); err != nil {
  44. return err
  45. }
  46. var response []byte
  47. if response, err = util.PostJSON(fmt.Sprintf(setScheduleListURL, accessToken), req); err != nil {
  48. return err
  49. }
  50. return util.DecodeWithCommonError(response, "SetScheduleList")
  51. }
  52. // PunchCorrectionRequest 为打卡人员补卡请求
  53. type PunchCorrectionRequest struct {
  54. UserID string `json:"userid"`
  55. ScheduleDateTime int64 `json:"schedule_date_time"`
  56. ScheduleCheckinTime int64 `json:"schedule_checkin_time"`
  57. CheckinTime int64 `json:"checkin_time"`
  58. Remark string `json:"remark"`
  59. }
  60. // PunchCorrection 为打卡人员补卡
  61. // see https://developer.work.weixin.qq.com/document/path/95803
  62. func (r *Client) PunchCorrection(req *PunchCorrectionRequest) error {
  63. var (
  64. accessToken string
  65. err error
  66. )
  67. if accessToken, err = r.GetAccessToken(); err != nil {
  68. return err
  69. }
  70. var response []byte
  71. if response, err = util.PostJSON(fmt.Sprintf(punchCorrectionURL, accessToken), req); err != nil {
  72. return err
  73. }
  74. return util.DecodeWithCommonError(response, "PunchCorrection")
  75. }
  76. // AddUserFaceRequest 录入打卡人员人脸信息请求
  77. type AddUserFaceRequest struct {
  78. UserID string `json:"userid"`
  79. UserFace string `json:"userface"`
  80. }
  81. // AddUserFace 录入打卡人员人脸信息
  82. // see https://developer.work.weixin.qq.com/document/path/93378
  83. func (r *Client) AddUserFace(req *AddUserFaceRequest) error {
  84. var (
  85. accessToken string
  86. err error
  87. )
  88. if accessToken, err = r.GetAccessToken(); err != nil {
  89. return err
  90. }
  91. var response []byte
  92. if response, err = util.PostJSON(fmt.Sprintf(addUserFaceURL, accessToken), req); err != nil {
  93. return err
  94. }
  95. return util.DecodeWithCommonError(response, "AddUserFace")
  96. }
  97. // AddOptionRequest 创建打卡规则请求
  98. type AddOptionRequest struct {
  99. EffectiveNow bool `json:"effective_now,omitempty"`
  100. Group OptionGroupRule `json:"group,omitempty"`
  101. }
  102. // OptionGroupRule 打卡规则字段
  103. type OptionGroupRule struct {
  104. GroupID int64 `json:"groupid,omitempty"`
  105. GroupType int64 `json:"grouptype"`
  106. GroupName string `json:"groupname"`
  107. CheckinDate []OptionGroupRuleCheckinDate `json:"checkindate,omitempty"`
  108. SpeWorkdays []OptionGroupSpeWorkdays `json:"spe_workdays,omitempty"`
  109. SpeOffDays []OptionGroupSpeOffDays `json:"spe_offdays,omitempty"`
  110. SyncHolidays bool `json:"sync_holidays,omitempty"`
  111. NeedPhoto bool `json:"need_photo,omitempty"`
  112. NoteCanUseLocalPic bool `json:"note_can_use_local_pic,omitempty"`
  113. WifiMacInfos []OptionGroupWifiMacInfos `json:"wifimac_infos,omitempty"`
  114. LocInfos []OptionGroupLocInfos `json:"loc_infos,omitempty"`
  115. AllowCheckinOffWorkday bool `json:"allow_checkin_offworkday,omitempty"`
  116. AllowApplyOffWorkday bool `json:"allow_apply_offworkday,omitempty"`
  117. Range []OptionGroupRange `json:"range"`
  118. WhiteUsers []string `json:"white_users,omitempty"`
  119. Type int64 `json:"type,omitempty"`
  120. ReporterInfo OptionGroupReporterInfo `json:"reporterinfo,omitempty"`
  121. AllowApplyBkCnt int64 `json:"allow_apply_bk_cnt,omitempty"`
  122. AllowApplyBkDayLimit int64 `json:"allow_apply_bk_day_limit,omitempty"`
  123. BukaLimitNextMonth int64 `json:"buka_limit_next_month,omitempty"`
  124. OptionOutRange int64 `json:"option_out_range,omitempty"`
  125. ScheduleList []OptionGroupScheduleList `json:"schedulelist,omitempty"`
  126. OffWorkIntervalTime int64 `json:"offwork_interval_time,omitempty"`
  127. UseFaceDetect bool `json:"use_face_detect,omitempty"`
  128. OpenFaceLiveDetect bool `json:"open_face_live_detect,omitempty"`
  129. OtInfoV2 OptionGroupOtInfoV2 `json:"ot_info_v2,omitempty"`
  130. SyncOutCheckin bool `json:"sync_out_checkin,omitempty"`
  131. BukaRemind OptionGroupBukaRemind `json:"buka_remind,omitempty"`
  132. BukaRestriction int64 `json:"buka_restriction,omitempty"`
  133. CheckinMethodType int64 `json:"checkin_method_type,omitempty"`
  134. SpanDayTime int64 `json:"span_day_time,omitempty"`
  135. StandardWorkDuration int64 `json:"standard_work_duration,omitempty"`
  136. }
  137. // OptionGroupRuleCheckinDate 固定时间上下班打卡时间
  138. type OptionGroupRuleCheckinDate struct {
  139. Workdays []int64 `json:"workdays"`
  140. CheckinTime []OptionGroupRuleCheckinTime `json:"checkintime"`
  141. FlexTime int64 `json:"flex_time"`
  142. AllowFlex bool `json:"allow_flex"`
  143. FlexOnDutyTime int64 `json:"flex_on_duty_time"`
  144. FlexOffDutyTime int64 `json:"flex_off_duty_time"`
  145. MaxAllowArriveEarly int64 `json:"max_allow_arrive_early"`
  146. MaxAllowArriveLate int64 `json:"max_allow_arrive_late"`
  147. LateRule OptionGroupLateRule `json:"late_rule"`
  148. Biweekly OptionGroupBiweekly `json:"biweekly,omitempty"`
  149. }
  150. // OptionGroupRuleCheckinTime 工作日上下班打卡时间信息
  151. type OptionGroupRuleCheckinTime struct {
  152. TimeID int64 `json:"time_id"`
  153. WorkSec int64 `json:"work_sec"`
  154. OffWorkSec int64 `json:"off_work_sec"`
  155. RemindWorkSec int64 `json:"remind_work_sec"`
  156. RemindOffWorkSec int64 `json:"remind_off_work_sec"`
  157. AllowRest bool `json:"allow_rest"`
  158. RestBeginTime int64 `json:"rest_begin_time"`
  159. RestEndTime int64 `json:"rest_end_time"`
  160. EarliestWorkSec int64 `json:"earliest_work_sec"`
  161. LatestWorkSec int64 `json:"latest_work_sec"`
  162. EarliestOffWorkSec int64 `json:"earliest_off_work_sec"`
  163. LatestOffWorkSec int64 `json:"latest_off_work_sec"`
  164. NoNeedCheckOn bool `json:"no_need_checkon"`
  165. NoNeedCheckOff bool `json:"no_need_checkoff"`
  166. RestTimes []OptionGroupRuleRestTimes `json:"rest_times,omitempty"`
  167. }
  168. // OptionGroupRuleRestTimes 多组休息时间
  169. type OptionGroupRuleRestTimes struct {
  170. RestBeginTime int64 `json:"rest_begin_time,omitempty"`
  171. RestEndTime int64 `json:"rest_end_time,omitempty"`
  172. }
  173. // OptionGroupLateRule 晚走晚到时间规则信息
  174. type OptionGroupLateRule struct {
  175. OffWorkAfterTime int64 `json:"offwork_after_time"`
  176. OnWorkFlexTime int64 `json:"onwork_flex_time"`
  177. AllowOffWorkAfterTime int64 `json:"allow_offwork_after_time"`
  178. TimeRules []OptionGroupTimeRule `json:"timerules"`
  179. }
  180. // OptionGroupTimeRule 晚走晚到时间规则
  181. type OptionGroupTimeRule struct {
  182. OffWorkAfterTime int64 `json:"offwork_after_time"`
  183. OnWorkFlexTime int64 `json:"onwork_flex_time"`
  184. }
  185. // OptionGroupBiweekly 大小周规则
  186. type OptionGroupBiweekly struct {
  187. EnableWeekdayRecurrence bool `json:"enable_weekday_recurrence"`
  188. OddWorkdays []int64 `json:"odd_workdays"`
  189. EvenWorkdays []int64 `json:"even_workdays"`
  190. }
  191. // OptionGroupSpeWorkdays 特殊工作日
  192. type OptionGroupSpeWorkdays struct {
  193. Timestamp int64 `json:"timestamp"`
  194. Notes string `json:"notes"`
  195. CheckinTime []OptionGroupCheckinTime `json:"checkintime"`
  196. Type int64 `json:"type"`
  197. BegTime int64 `json:"begtime"`
  198. EndTime int64 `json:"endtime"`
  199. }
  200. // OptionGroupCheckinTime 特殊工作日的上下班打卡时间配置
  201. type OptionGroupCheckinTime struct {
  202. TimeID int64 `json:"time_id"`
  203. WorkSec int64 `json:"work_sec"`
  204. OffWorkSec int64 `json:"off_work_sec"`
  205. RemindWorkSec int64 `json:"remind_work_sec"`
  206. RemindOffWorkSec int64 `json:"remind_off_work_sec"`
  207. }
  208. // OptionGroupSpeOffDays 特殊非工作日
  209. type OptionGroupSpeOffDays struct {
  210. Timestamp int64 `json:"timestamp"`
  211. Notes string `json:"notes"`
  212. Type int64 `json:"type"`
  213. BegTime int64 `json:"begtime"`
  214. EndTime int64 `json:"endtime"`
  215. }
  216. // OptionGroupWifiMacInfos WIFI信息
  217. type OptionGroupWifiMacInfos struct {
  218. WifiName string `json:"wifiname"`
  219. WifiMac string `json:"wifimac"`
  220. }
  221. // OptionGroupLocInfos 地点信息
  222. type OptionGroupLocInfos struct {
  223. Lat int64 `json:"lat"`
  224. Lng int64 `json:"lng"`
  225. LocTitle string `json:"loc_title"`
  226. LocDetail string `json:"loc_detail"`
  227. Distance int64 `json:"distance"`
  228. }
  229. // OptionGroupRange 人员信息
  230. type OptionGroupRange struct {
  231. PartyID []string `json:"party_id"`
  232. UserID []string `json:"userid"`
  233. TagID []int64 `json:"tagid"`
  234. }
  235. // OptionGroupReporterInfo 汇报人
  236. type OptionGroupReporterInfo struct {
  237. Reporters []OptionGroupReporters `json:"reporters"`
  238. }
  239. // OptionGroupReporters 汇报对象
  240. type OptionGroupReporters struct {
  241. UserID string `json:"userid"`
  242. TagID int64 `json:"tagid"`
  243. }
  244. // OptionGroupScheduleList 自定义排班规则所有排班
  245. type OptionGroupScheduleList struct {
  246. ScheduleID int64 `json:"schedule_id"`
  247. ScheduleName string `json:"schedule_name"`
  248. TimeSection []OptionGroupTimeSection `json:"time_section"`
  249. AllowFlex bool `json:"allow_flex"`
  250. FlexOnDutyTime int64 `json:"flex_on_duty_time"`
  251. FlexOffDutyTime int64 `json:"flex_off_duty_time"`
  252. LateRule OptionGroupLateRule `json:"late_rule"`
  253. MaxAllowArriveEarly int64 `json:"max_allow_arrive_early"`
  254. MaxAllowArriveLate int64 `json:"max_allow_arrive_late"`
  255. }
  256. // OptionGroupTimeSection 班次上下班时段信息
  257. type OptionGroupTimeSection struct {
  258. TimeID int64 `json:"time_id"`
  259. WorkSec int64 `json:"work_sec"`
  260. OffWorkSec int64 `json:"off_work_sec"`
  261. RemindWorkSec int64 `json:"remind_work_sec"`
  262. RemindOffWorkSec int64 `json:"remind_off_work_sec"`
  263. RestBeginTime int64 `json:"rest_begin_time"`
  264. RestEndTime int64 `json:"rest_end_time"`
  265. AllowRest bool `json:"allow_rest"`
  266. EarliestWorkSec int64 `json:"earliest_work_sec"`
  267. LatestWorkSec int64 `json:"latest_work_sec"`
  268. EarliestOffWorkSec int64 `json:"earliest_off_work_sec"`
  269. LatestOffWorkSec int64 `json:"latest_off_work_sec"`
  270. NoNeedCheckOn bool `json:"no_need_checkon"`
  271. NoNeedCheckOff bool `json:"no_need_checkoff"`
  272. }
  273. // OptionGroupOtInfoV2 加班配置
  274. type OptionGroupOtInfoV2 struct {
  275. WorkdayConf OptionGroupWorkdayConf `json:"workdayconf"`
  276. }
  277. // OptionGroupWorkdayConf 工作日加班配置
  278. type OptionGroupWorkdayConf struct {
  279. AllowOt bool `json:"allow_ot"`
  280. Type int64 `json:"type"`
  281. }
  282. // OptionGroupBukaRemind 补卡提醒
  283. type OptionGroupBukaRemind struct {
  284. OpenRemind bool `json:"open_remind"`
  285. BukaRemindDay int64 `json:"buka_remind_day"`
  286. BukaRemindMonth int64 `json:"buka_remind_month"`
  287. }
  288. // AddOption 创建打卡规则
  289. // see https://developer.work.weixin.qq.com/document/path/98041#%E5%88%9B%E5%BB%BA%E6%89%93%E5%8D%A1%E8%A7%84%E5%88%99
  290. func (r *Client) AddOption(req *AddOptionRequest) error {
  291. var (
  292. accessToken string
  293. err error
  294. )
  295. if accessToken, err = r.GetAccessToken(); err != nil {
  296. return err
  297. }
  298. var response []byte
  299. if response, err = util.PostJSON(fmt.Sprintf(addOptionURL, accessToken), req); err != nil {
  300. return err
  301. }
  302. return util.DecodeWithCommonError(response, "AddOption")
  303. }
  304. // UpdateOptionRequest 修改打卡规则请求
  305. type UpdateOptionRequest struct {
  306. EffectiveNow bool `json:"effective_now,omitempty"`
  307. Group OptionGroupRule `json:"group,omitempty"`
  308. }
  309. // UpdateOption 修改打卡规则
  310. // see https://developer.work.weixin.qq.com/document/path/98041#%E4%BF%AE%E6%94%B9%E6%89%93%E5%8D%A1%E8%A7%84%E5%88%99
  311. func (r *Client) UpdateOption(req *UpdateOptionRequest) error {
  312. var (
  313. accessToken string
  314. err error
  315. )
  316. if accessToken, err = r.GetAccessToken(); err != nil {
  317. return err
  318. }
  319. var response []byte
  320. if response, err = util.PostJSON(fmt.Sprintf(updateOptionURL, accessToken), req); err != nil {
  321. return err
  322. }
  323. return util.DecodeWithCommonError(response, "UpdateOption")
  324. }
  325. // ClearOptionRequest 清空打卡规则数组元素请求
  326. type ClearOptionRequest struct {
  327. GroupID int64 `json:"groupid"`
  328. ClearField []int64 `json:"clear_field"`
  329. EffectiveNow bool `json:"effective_now"`
  330. }
  331. // ClearOption 清空打卡规则数组元素
  332. // see https://developer.work.weixin.qq.com/document/path/98041#%E6%B8%85%E7%A9%BA%E6%89%93%E5%8D%A1%E8%A7%84%E5%88%99%E6%95%B0%E7%BB%84%E5%85%83%E7%B4%A0
  333. func (r *Client) ClearOption(req *ClearOptionRequest) error {
  334. var (
  335. accessToken string
  336. err error
  337. )
  338. if accessToken, err = r.GetAccessToken(); err != nil {
  339. return err
  340. }
  341. var response []byte
  342. if response, err = util.PostJSON(fmt.Sprintf(clearOptionURL, accessToken), req); err != nil {
  343. return err
  344. }
  345. return util.DecodeWithCommonError(response, "ClearOption")
  346. }
  347. // DelOptionRequest 删除打卡规则请求
  348. type DelOptionRequest struct {
  349. GroupID int64 `json:"groupid"`
  350. EffectiveNow bool `json:"effective_now"`
  351. }
  352. // DelOption 删除打卡规则
  353. // see https://developer.work.weixin.qq.com/document/path/98041#%E5%88%A0%E9%99%A4%E6%89%93%E5%8D%A1%E8%A7%84%E5%88%99
  354. func (r *Client) DelOption(req *DelOptionRequest) error {
  355. var (
  356. accessToken string
  357. err error
  358. )
  359. if accessToken, err = r.GetAccessToken(); err != nil {
  360. return err
  361. }
  362. var response []byte
  363. if response, err = util.PostJSON(fmt.Sprintf(delOptionURL, accessToken), req); err != nil {
  364. return err
  365. }
  366. return util.DecodeWithCommonError(response, "DelOption")
  367. }
  368. // AddRecordRequest 添加打卡记录请求
  369. type AddRecordRequest struct {
  370. Records []Record `json:"records"`
  371. }
  372. // Record 打卡记录
  373. type Record struct {
  374. UserID string `json:"userid"`
  375. CheckinTime int64 `json:"checkin_time"`
  376. LocationTitle string `json:"location_title"`
  377. LocationDetail string `json:"location_detail"`
  378. MediaIDS []string `json:"mediaids"`
  379. Notes string `json:"notes"`
  380. DeviceType int `json:"device_type"`
  381. Lat int64 `json:"lat"`
  382. Lng int64 `json:"lng"`
  383. DeviceDetail string `json:"device_detail"`
  384. WifiName string `json:"wifiname"`
  385. WifiMac string `json:"wifimac"`
  386. }
  387. // AddRecord 添加打卡记录
  388. // see https://developer.work.weixin.qq.com/document/path/99647
  389. func (r *Client) AddRecord(req *AddRecordRequest) error {
  390. var (
  391. accessToken string
  392. err error
  393. )
  394. if accessToken, err = r.GetAccessToken(); err != nil {
  395. return err
  396. }
  397. var response []byte
  398. if response, err = util.PostJSON(fmt.Sprintf(addRecordURL, accessToken), req); err != nil {
  399. return err
  400. }
  401. return util.DecodeWithCommonError(response, "AddRecord")
  402. }