记java开发企业微信,同步OA数据

2019-02-20 21:47:38  卢浮宫  版权声明:本文为站长原创文章,转载请写明出处


一、背景

    前天做企业微信同步OA数据,这里简单记一下(主要是数据处理及JSON的一下操作),上算是留个痕迹吧。


二、有官方文档一切好说

      2.1、实体类信息如下(省略get和set已经toString 方法)      

    
        public class OaData {
private String userid;
private String groupname;
private String checkin_type;
private String exception_type;
private String checkin_time;
private String location_title;
private String location_detail;
private String wifiname;
private String notes;
private String wifimac;
private String mediaids;
。。。
}

      2.2、dao层如下      


        package com.caretop.dao;
import com.caretop.model.OaData;

public interface OaDataDao {
public void addOaData(OaData oaData);
}


@Repository
public class OaDataDaoImpl implements OaDataDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void addOaData(OaData oaData) {
String userid = oaData.getUserid(),groupname = oaData.getGroupname(),checkin_type = oaData.getCheckin_type(), exception_type ==oaData.getException_type(),
checkin_time = oaData.getCheckin_time(),location_title = oaData.getLocation_title(),location_detail = oaData.getLocation_detail(),
wifiname = oaData.getWifiname(), notes = oaData.getNotes(),wifimac = oaData.getWifimac(),mediaids = oaData.getMediaids();
String sql = "call fun_kq_wx_addOaData(""+ userid + "","" + groupname + "","" +checkin_type + "","" + exception_type + "","" + checkin_time + "","" + location_title + "","" + location_detail + "","" + wifiname + "","" + notes + "","" + wifimac + "","" + mediaids + "");";
jdbcTemplate.execute(sql);
}
}

    3、service层代码如下          

    
        public interface OaDataService {
public void addOaData(OaData oaData);
}

@Service
public class OaDataServiceImpl implements OaDataService {
@Autowired
private OaDataDao oaDataDao;
@Override
public void addOaData(OaData oaData) {
oaDataDao.addOaData(oaData);
}
}

    4、controller类如下(这里在JSON解析时用的是阿里巴巴的fastJson包)    

        
        @Controller
public class OaDataController extends HttpAction {

protected final transient Log log = LogFactory.getLog(OaDataController.class);
@Autowired
private OaDataService oaDataService;
@Autowired
private WxInfoService wxInfoService;
@RequestMapping("addOaData")
public @ResponseBody void addOaData() throws ParseException {
String[] userList = getAllEmps();
List<WxInfo> wxInfo = wxInfoService.getOaInfo();
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" +
wxInfo.get(0).getCorpid().trim() + "&corpsecret=" +
wxInfo.get(0).getSecrect().trim();
String temp = getHttpResp(url); // 获取标识码
String access_token = getAccessToken(temp);
String getCheckUrl = "https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckindata?access_token=" +
access_token;

//获取月初月末时间戳
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
Calendar cale = Calendar.getInstance();
int year = cale.get(Calendar.YEAR);
int month = cale.get(Calendar.MONTH) + 1;
cale.set(Calendar.DAY_OF_MONTH,
cale.getActualMaximum(Calendar.DAY_OF_MONTH));

String last = dateformat.format(cale.getTime());
Long startTime = dateformat.parse(year + "-" + month + "-01").getTime() / 1000;
Long endTime = dateformat.parse(last).getTime() / 1000;
JSONObject param = new JSONObject();
param.put("opencheckindatatype", 3);
param.put("starttime", startTime);
param.put("endtime", endTime);
param.put("useridlist", userList);

String rlt = postHttpRespForJson(getCheckUrl, param);
JSONObject obj = JSONObject.parseObject(rlt);
List<Object> checkindata = obj.getJSONArray("checkindata");

for (int i = 0; i < checkindata.size(); i++) {
OaData oaData = new OaData();
oaData.setUserid((String) ((JSONObject) checkindata.get(i)).get("userid"));
oaData.setGroupname((String) ((JSONObject) checkindata.get(i)).get("groupname"));
oaData.setCheckin_type((String) ((JSONObject) checkindata.get(i)).get("checkin_type"));
oaData.setException_type((String) ((JSONObject) checkindata.get(i)).get("exception_type"));
oaData.setCheckin_time(dateFormat((String) ((JSONObject) checkindata.get(i)).get("checkin_time").toString()));
oaData.setLocation_title((String) ((JSONObject) checkindata.get(i)).get("location_title"));
oaData.setLocation_detail((String) ((JSONObject) checkindata.get(i)).get("location_detail"));
oaData.setWifiname((String) ((JSONObject) checkindata.get(i)).get("wifiname"));
oaData.setNotes((String) ((JSONObject) checkindata.get(i)).get("notes"));
oaData.setWifimac((String) ((JSONObject) checkindata.get(i)).get("wifimac"));
oaData.setMediaids((String) ((JSONObject) checkindata.get(i)).get("mediaids").toString());
oaDataService.addOaData(oaData);
}
}

private String[] getAllEmps() {
List<WxInfo> wxInfo = wxInfoService.getWxInfo();
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" +
wxInfo.get(0).getCorpid().trim() + "&corpsecret=" +
wxInfo.get(0).getSecrect().trim();
String temp = getHttpResp(url); // ?????
String access_token = getAccessToken(temp);
String getCheckUrl = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=" +
access_token + "&department_id=1&fetch_child=1";
String rlt = getHttpResp(getCheckUrl);
JSONObject obj = JSONObject.parseObject(rlt);
String[] userArr = obj.get("userlist").toString().split("userid");
String[] result = new String[userArr.length];

for (int i = 1; i < userArr.length; i++) {
String strTmp = userArr[i].replace(""", "").replace("{", "").replace("}", "").replace("]", "");
String userId = strTmp.split(":")[1].toString().trim().split(",")[0].toString()
.trim();
result[i - 1] = userId;
}

return result;
}

private String dateFormat(String val) {
String date;
Long timestamp = Long.parseLong(val) * 1000;
date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(new Date(timestamp));

return date;
}
}

    5、同时在定时计划中创建(addOaData中就是controller类中的内容能即可)。           


        @Scheduled(cron = "0/60 * * * * ? ") // 间隔60秒执行
public void taskCycle() throws ParseException {
// getNeedAddDepartAndUpload(access_token);
// addUserListTask(access_token);
// delUserListTask(access_token);
// sendMsgToWx();
addOaData();
}


三、后记


    在2020年9月11日10:13:46重新做这个文章维护时真的是忍俊不禁。当时这个是有点菜菜的。哈哈...

不过我也不准备做过多的修改,就当做时光长河中的一朵别致的浪花吧...







更多精彩请关注guangmuhua.com


最新评论:

评论测试2019年2月21日21:02:46
2019-02-22 05:02:48
1楼