checkin.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. }
  149. // OptionGroupRuleCheckinTime 工作日上下班打卡时间信息
  150. type OptionGroupRuleCheckinTime struct {
  151. TimeID int64 `json:"time_id"`
  152. WorkSec int64 `json:"work_sec"`
  153. OffWorkSec int64 `json:"off_work_sec"`
  154. RemindWorkSec int64 `json:"remind_work_sec"`
  155. RemindOffWorkSec int64 `json:"remind_off_work_sec"`
  156. AllowRest bool `json:"allow_rest"`
  157. RestBeginTime int64 `json:"rest_begin_time"`
  158. RestEndTime int64 `json:"rest_end_time"`
  159. EarliestWorkSec int64 `json:"earliest_work_sec"`
  160. LatestWorkSec int64 `json:"latest_work_sec"`
  161. EarliestOffWorkSec int64 `json:"earliest_off_work_sec"`
  162. LatestOffWorkSec int64 `json:"latest_off_work_sec"`
  163. NoNeedCheckOn bool `json:"no_need_checkon"`
  164. NoNeedCheckOff bool `json:"no_need_checkoff"`
  165. }
  166. // OptionGroupLateRule 晚走晚到时间规则信息
  167. type OptionGroupLateRule struct {
  168. OffWorkAfterTime int64 `json:"offwork_after_time"`
  169. OnWorkFlexTime int64 `json:"onwork_flex_time"`
  170. AllowOffWorkAfterTime int64 `json:"allow_offwork_after_time"`
  171. TimeRules []OptionGroupTimeRule `json:"timerules"`
  172. }
  173. // OptionGroupTimeRule 晚走晚到时间规则
  174. type OptionGroupTimeRule struct {
  175. OffWorkAfterTime int64 `json:"offwork_after_time"`
  176. OnWorkFlexTime int64 `json:"onwork_flex_time"`
  177. }
  178. // OptionGroupSpeWorkdays 特殊工作日
  179. type OptionGroupSpeWorkdays struct {
  180. Timestamp int64 `json:"timestamp"`
  181. Notes string `json:"notes"`
  182. CheckinTime []OptionGroupCheckinTime `json:"checkintime"`
  183. Type int64 `json:"type"`
  184. BegTime int64 `json:"begtime"`
  185. EndTime int64 `json:"endtime"`
  186. }
  187. // OptionGroupCheckinTime 特殊工作日的上下班打卡时间配置
  188. type OptionGroupCheckinTime struct {
  189. TimeID int64 `json:"time_id"`
  190. WorkSec int64 `json:"work_sec"`
  191. OffWorkSec int64 `json:"off_work_sec"`
  192. RemindWorkSec int64 `json:"remind_work_sec"`
  193. RemindOffWorkSec int64 `json:"remind_off_work_sec"`
  194. }
  195. // OptionGroupSpeOffDays 特殊非工作日
  196. type OptionGroupSpeOffDays struct {
  197. Timestamp int64 `json:"timestamp"`
  198. Notes string `json:"notes"`
  199. Type int64 `json:"type"`
  200. BegTime int64 `json:"begtime"`
  201. EndTime int64 `json:"endtime"`
  202. }
  203. // OptionGroupWifiMacInfos WIFI信息
  204. type OptionGroupWifiMacInfos struct {
  205. WifiName string `json:"wifiname"`
  206. WifiMac string `json:"wifimac"`
  207. }
  208. // OptionGroupLocInfos 地点信息
  209. type OptionGroupLocInfos struct {
  210. Lat int64 `json:"lat"`
  211. Lng int64 `json:"lng"`
  212. LocTitle string `json:"loc_title"`
  213. LocDetail string `json:"loc_detail"`
  214. Distance int64 `json:"distance"`
  215. }
  216. // OptionGroupRange 人员信息
  217. type OptionGroupRange struct {
  218. PartyID []string `json:"party_id"`
  219. UserID []string `json:"userid"`
  220. TagID []int64 `json:"tagid"`
  221. }
  222. // OptionGroupReporterInfo 汇报人
  223. type OptionGroupReporterInfo struct {
  224. Reporters []OptionGroupReporters `json:"reporters"`
  225. }
  226. // OptionGroupReporters 汇报对象
  227. type OptionGroupReporters struct {
  228. UserID string `json:"userid"`
  229. TagID int64 `json:"tagid"`
  230. }
  231. // OptionGroupScheduleList 自定义排班规则所有排班
  232. type OptionGroupScheduleList struct {
  233. ScheduleID int64 `json:"schedule_id"`
  234. ScheduleName string `json:"schedule_name"`
  235. TimeSection []OptionGroupTimeSection `json:"time_section"`
  236. AllowFlex bool `json:"allow_flex"`
  237. FlexOnDutyTime int64 `json:"flex_on_duty_time"`
  238. FlexOffDutyTime int64 `json:"flex_off_duty_time"`
  239. LateRule OptionGroupLateRule `json:"late_rule"`
  240. MaxAllowArriveEarly int64 `json:"max_allow_arrive_early"`
  241. MaxAllowArriveLate int64 `json:"max_allow_arrive_late"`
  242. }
  243. // OptionGroupTimeSection 班次上下班时段信息
  244. type OptionGroupTimeSection struct {
  245. TimeID int64 `json:"time_id"`
  246. WorkSec int64 `json:"work_sec"`
  247. OffWorkSec int64 `json:"off_work_sec"`
  248. RemindWorkSec int64 `json:"remind_work_sec"`
  249. RemindOffWorkSec int64 `json:"remind_off_work_sec"`
  250. RestBeginTime int64 `json:"rest_begin_time"`
  251. RestEndTime int64 `json:"rest_end_time"`
  252. AllowRest bool `json:"allow_rest"`
  253. EarliestWorkSec int64 `json:"earliest_work_sec"`
  254. LatestWorkSec int64 `json:"latest_work_sec"`
  255. EarliestOffWorkSec int64 `json:"earliest_off_work_sec"`
  256. LatestOffWorkSec int64 `json:"latest_off_work_sec"`
  257. NoNeedCheckOn bool `json:"no_need_checkon"`
  258. NoNeedCheckOff bool `json:"no_need_checkoff"`
  259. }
  260. // OptionGroupOtInfoV2 加班配置
  261. type OptionGroupOtInfoV2 struct {
  262. WorkdayConf OptionGroupWorkdayConf `json:"workdayconf"`
  263. }
  264. // OptionGroupWorkdayConf 工作日加班配置
  265. type OptionGroupWorkdayConf struct {
  266. AllowOt bool `json:"allow_ot"`
  267. Type int64 `json:"type"`
  268. }
  269. // OptionGroupBukaRemind 补卡提醒
  270. type OptionGroupBukaRemind struct {
  271. OpenRemind bool `json:"open_remind"`
  272. BukaRemindDay int64 `json:"buka_remind_day"`
  273. BukaRemindMonth int64 `json:"buka_remind_month"`
  274. }
  275. // AddOption 创建打卡规则
  276. // 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
  277. func (r *Client) AddOption(req *AddOptionRequest) error {
  278. var (
  279. accessToken string
  280. err error
  281. )
  282. if accessToken, err = r.GetAccessToken(); err != nil {
  283. return err
  284. }
  285. var response []byte
  286. if response, err = util.PostJSON(fmt.Sprintf(addOptionURL, accessToken), req); err != nil {
  287. return err
  288. }
  289. return util.DecodeWithCommonError(response, "AddOption")
  290. }
  291. // UpdateOptionRequest 修改打卡规则请求
  292. type UpdateOptionRequest struct {
  293. EffectiveNow bool `json:"effective_now,omitempty"`
  294. Group OptionGroupRule `json:"group,omitempty"`
  295. }
  296. // UpdateOption 修改打卡规则
  297. // 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
  298. func (r *Client) UpdateOption(req *UpdateOptionRequest) error {
  299. var (
  300. accessToken string
  301. err error
  302. )
  303. if accessToken, err = r.GetAccessToken(); err != nil {
  304. return err
  305. }
  306. var response []byte
  307. if response, err = util.PostJSON(fmt.Sprintf(updateOptionURL, accessToken), req); err != nil {
  308. return err
  309. }
  310. return util.DecodeWithCommonError(response, "UpdateOption")
  311. }
  312. // ClearOptionRequest 清空打卡规则数组元素请求
  313. type ClearOptionRequest struct {
  314. GroupID int64 `json:"groupid"`
  315. ClearField []int64 `json:"clear_field"`
  316. EffectiveNow bool `json:"effective_now"`
  317. }
  318. // ClearOption 清空打卡规则数组元素
  319. // 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
  320. func (r *Client) ClearOption(req *ClearOptionRequest) error {
  321. var (
  322. accessToken string
  323. err error
  324. )
  325. if accessToken, err = r.GetAccessToken(); err != nil {
  326. return err
  327. }
  328. var response []byte
  329. if response, err = util.PostJSON(fmt.Sprintf(clearOptionURL, accessToken), req); err != nil {
  330. return err
  331. }
  332. return util.DecodeWithCommonError(response, "ClearOption")
  333. }
  334. // DelOptionRequest 删除打卡规则请求
  335. type DelOptionRequest struct {
  336. GroupID int64 `json:"groupid"`
  337. EffectiveNow bool `json:"effective_now"`
  338. }
  339. // DelOption 删除打卡规则
  340. // 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
  341. func (r *Client) DelOption(req *DelOptionRequest) error {
  342. var (
  343. accessToken string
  344. err error
  345. )
  346. if accessToken, err = r.GetAccessToken(); err != nil {
  347. return err
  348. }
  349. var response []byte
  350. if response, err = util.PostJSON(fmt.Sprintf(delOptionURL, accessToken), req); err != nil {
  351. return err
  352. }
  353. return util.DecodeWithCommonError(response, "DelOption")
  354. }
  355. // AddRecordRequest 添加打卡记录请求
  356. type AddRecordRequest struct {
  357. Records []Record `json:"records"`
  358. }
  359. // Record 打卡记录
  360. type Record struct {
  361. UserID string `json:"userid"`
  362. CheckinTime int64 `json:"checkin_time"`
  363. LocationTitle string `json:"location_title"`
  364. LocationDetail string `json:"location_detail"`
  365. MediaIDS []string `json:"mediaids"`
  366. Notes string `json:"notes"`
  367. DeviceType int `json:"device_type"`
  368. Lat int64 `json:"lat"`
  369. Lng int64 `json:"lng"`
  370. DeviceDetail string `json:"device_detail"`
  371. WifiName string `json:"wifiname"`
  372. WifiMac string `json:"wifimac"`
  373. }
  374. // AddRecord 添加打卡记录
  375. // see https://developer.work.weixin.qq.com/document/path/99647
  376. func (r *Client) AddRecord(req *AddRecordRequest) error {
  377. var (
  378. accessToken string
  379. err error
  380. )
  381. if accessToken, err = r.GetAccessToken(); err != nil {
  382. return err
  383. }
  384. var response []byte
  385. if response, err = util.PostJSON(fmt.Sprintf(addRecordURL, accessToken), req); err != nil {
  386. return err
  387. }
  388. return util.DecodeWithCommonError(response, "AddRecord")
  389. }