钉钉考勤

This commit is contained in:
张松
2025-12-01 11:31:37 +08:00
parent 2f201c7015
commit 0b94d79a02
3 changed files with 33 additions and 19 deletions

View File

@@ -123,5 +123,7 @@ public class ShopConfig implements Serializable {
* 上菜时间 分钟
*/
private Integer serveTime;
private String dingAppKey;
private String dingAppSecret;
}

View File

@@ -3,6 +3,8 @@ package com.czg.service.market.service.impl;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.czg.account.entity.ShopConfig;
import com.czg.account.service.ShopConfigService;
import com.czg.market.service.AttendanceService;
import com.czg.market.vo.DingAttendanceStatsVO;
import jakarta.annotation.Resource;
@@ -15,6 +17,8 @@ import java.util.Date;
public class AttendanceServiceImpl implements AttendanceService {
@Resource
private DingService dingService;
@Resource
private ShopConfigService shopConfigService;
@Override
public ArrayList<DingAttendanceStatsVO> list(Long shopId, Integer page, Integer size, String name, String startTime, String endTime) {
@@ -31,6 +35,12 @@ public class AttendanceServiceImpl implements AttendanceService {
}else {
end = DateUtil.parse(endTime);
}
return dingService.getUserReport(start, end, name);
ShopConfig config = shopConfigService.getById(shopId);
if (config == null || StrUtil.isBlank(config.getDingAppSecret())) {
return new ArrayList<>();
}
return dingService.getUserReport(shopId, config.getDingAppKey(), config.getDingAppSecret(), start, end, name);
}
}

View File

@@ -28,15 +28,17 @@ public class DingService {
private RedisService redisService;
public String getToken() {
Object accessToken = redisService.get("ding_access_token");
public String getToken(String key, String secret, Long shopId) {
Object accessToken = redisService.get("ding_access_token:" + shopId);
if (accessToken instanceof String && StrUtil.isNotBlank((String) accessToken)) {
return (String) accessToken;
}
DingTalkClient client = new DefaultDingTalkClient("https://api.dingtalk.com/gettoken");
OapiGettokenRequest req = new OapiGettokenRequest();
req.setAppkey("ding9bqzj4890z81o3y2");
req.setAppsecret("PiRU2CQ_KrZH104UoULeBfSbFw4YhPI0fZZC_bUr1aubvbsP6zmMv7nC1xxF9QBS");
// req.setAppkey("ding9bqzj4890z81o3y2");
// req.setAppsecret("PiRU2CQ_KrZH104UoULeBfSbFw4YhPI0fZZC_bUr1aubvbsP6zmMv7nC1xxF9QBS");
req.setAppkey(key);
req.setAppsecret(secret);
req.setHttpMethod("GET");
OapiGettokenResponse rsp;
try {
@@ -47,16 +49,16 @@ public class DingService {
System.out.println(rsp.getBody());
JSONObject data = getData(rsp, true);
Long expiresIn = data.getLong("expires_in");
redisService.set("ding_access_token", data.getString("access_token"), expiresIn - 100);
redisService.set("ding_access_token:" + shopId, data.getString("access_token"), expiresIn - 100);
return data.getString("access_token");
}
public Map<String, String> getColumns() {
public Map<String, String> getColumns(String key, String secret, Long shopId) {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/getattcolumns");
OapiAttendanceGetattcolumnsRequest req = new OapiAttendanceGetattcolumnsRequest();
OapiAttendanceGetattcolumnsResponse rsp = null;
try {
String token = getToken();
String token = getToken(key, secret, shopId);
rsp = client.execute(req, token);
} catch (ApiException e) {
throw new RuntimeException(e);
@@ -96,7 +98,7 @@ public class DingService {
return jsonObject;
}
public List<DingUserVO> getUserByDeptId(Long deptId) throws ApiException {
public List<DingUserVO> getUserByDeptId(Long deptId, String key, String secret, Long shopId) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/list");
OapiV2UserListRequest req = new OapiV2UserListRequest();
req.setDeptId(deptId);
@@ -105,37 +107,37 @@ public class DingService {
req.setOrderField("modify_desc");
req.setContainAccessLimit(true);
req.setLanguage("zh_CN");
OapiV2UserListResponse rsp = client.execute(req, getToken());
OapiV2UserListResponse rsp = client.execute(req, getToken(key, secret, shopId));
JSONObject data = getData(rsp, true);
List<DingUserVO> userVOList = data.getJSONObject("result").getJSONArray("list").toJavaList(DingUserVO.class);
return userVOList;
}
public JSONObject getRepost(Date startTime, Date endTime, String userId, String columIdList) throws ApiException {
public JSONObject getRepost(Date startTime, Date endTime, String userId, String columIdList, String key, String secret, Long shopId) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/getcolumnval");
OapiAttendanceGetcolumnvalRequest req = new OapiAttendanceGetcolumnvalRequest();
req.setUserid(userId);
req.setColumnIdList(columIdList);
req.setFromDate(startTime);
req.setToDate(endTime);
OapiAttendanceGetcolumnvalResponse rsp = client.execute(req, getToken());
OapiAttendanceGetcolumnvalResponse rsp = client.execute(req, getToken(key, secret, shopId));
JSONObject data = getData(rsp, false);
return data == null ? null : data.getJSONObject("result");
}
public ArrayList<DingUserVO> getUserList(String name) {
public ArrayList<DingUserVO> getUserList(String name,String key, String secret, Long shopId) {
try {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
req.setDeptId(1L);
req.setLanguage("zh_CN");
OapiV2DepartmentListsubResponse rsp = client.execute(req, getToken());
OapiV2DepartmentListsubResponse rsp = client.execute(req, getToken(key, secret, shopId));
Set<Long> deptIdList = getData(rsp, true).getJSONArray("result").stream().map(item -> ((JSONObject) item)
.getLong("dept_id")).collect(Collectors.toSet());
ArrayList<DingUserVO> dingUserVOS = new ArrayList<>();
for (Long dptId : deptIdList) {
dingUserVOS.addAll(getUserByDeptId(dptId));
dingUserVOS.addAll(getUserByDeptId(dptId, key, secret, shopId));
}
if (StrUtil.isNotBlank(name)) {
return dingUserVOS.stream().filter(item -> item.getName().contains(name)).collect(Collectors.toCollection(ArrayList::new));
@@ -146,15 +148,15 @@ public class DingService {
}
}
public ArrayList<DingAttendanceStatsVO> getUserReport(DateTime startTime, DateTime endTime, String name) {
Map<String, String> columns = getColumns();
ArrayList<DingUserVO> userList = getUserList(name);
public ArrayList<DingAttendanceStatsVO> getUserReport(Long shopId, String key, String secret, DateTime startTime, DateTime endTime, String name) {
Map<String, String> columns = getColumns(key, secret, shopId);
ArrayList<DingUserVO> userList = getUserList(name, key, secret, shopId);
ArrayList<DingAttendanceStatsVO> statsVOS = new ArrayList<>();
for (DingUserVO item : userList) {
JSONObject repost = null;
try {
repost = getRepost(startTime, endTime, item.getUserid(),
String.join(",", columns.keySet()));
String.join(",", columns.keySet()), key, secret, shopId);
} catch (ApiException e) {
throw new RuntimeException(e);
}