获取企业微信打开API上面的数据
根据企业ID和打卡模块的secret获取access_token
打卡传参body为json格式的字符传
创建打卡对象把参数写入,useridlist为list格式。转json
返回数据
企业微信调用代码
package com.westvalley.rrl.wechat.service; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.jfinal.kit.LogKit; import com.westvalley.ctrl.apprrl.fixedassets.Cache.CacheManager; import com.westvalley.ctrl.apprrl.fixedassets.bean.Cache; import com.westvalley.http.HttpXmlClientProxy; import com.westvalley.rrl.fixedassets.utils.EncryptUtils; import com.westvalley.rrl.wechat.bean.WechatUser; import com.westvalley.rrl.wechat.bean.WechatWork; import com.westvalley.rrl.wechat.prop.WechatProp; import com.westvalley.rrl.wechat.utils.DateUtils; public class WechatService { private WechatProp wechatProp = WechatProp.me(); private String corpid; private String secret; /** * 打卡 * @return */ public String getWorkDate(){ corpid = getWXstr("corpid"); secret = getWXstr("punchCardSecret"); String accessTokenVal = isCache("WORKACCESSTOKEN"); //查询打卡记录 //List<Record> users = Db.find("select id from hrmresource where status=0"); List<String> useridlist = getAddressBook(); LogKit.error("users---->"+useridlist); String starttime = DateUtils.getSECBeginTimes(new Date()); String endtime = DateUtils.getSECEndTimes(new Date()); WechatWork wechatWork = new WechatWork(); wechatWork.setOpencheckindatatype("3"); wechatWork.setStarttime(starttime); wechatWork.setEndtime(endtime); wechatWork.setUseridlist(useridlist); String body = JSON.toJSONString(wechatWork); LogKit.error("users-body--->"+body); String work = HttpXmlClientProxy.httpPost("https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckindata?access_token="+accessTokenVal+"", body); LogKit.error("work------------->"+work); return work; } /** * 通讯录 * @return */ public List<String> getAddressBook(){ corpid = getWXstr("corpid"); secret = getWXstr("addressBookSecret"); String accessTokenVal = isCache("ADDRESSBOOK"); String body = HttpXmlClientProxy.httpGet("https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token="+accessTokenVal+"&department_id=1&fetch_child=1"); String userlist = JSON.parseObject(body).getString("userlist"); @SuppressWarnings("unchecked") List<WechatUser> wechatUsers = JSONObject.parseArray((userlist), WechatUser.class); List<String> lists = new ArrayList<String>(); for(int i=0 ; i < wechatUsers.size() ; i++){ lists.add(wechatUsers.get(i).getUserid()); } return lists; } /** * 获取配置的参数 * @param key * @return */ public String getWXstr(String key){ return wechatProp.getPropValue(key); } /** * AccessToken进行判断获取 * @param secretKey * @return */ public String isCache(String secretKey){ Cache cacheAccessToken = CacheManager.getCacheInfo(secretKey); LogKit.error("获取企业微信打卡的cacheAccessToken------------>"+JSON.toJSONString(cacheAccessToken)); String accessTokenVal = ""; if(cacheAccessToken == null){ accessTokenVal = getAccessToken(); CacheManager.putCacheInfo(secretKey, accessTokenVal, 1000*60*60*2, false); }else{ if(cacheAccessToken.getValue() == null){ accessTokenVal = getAccessToken(); CacheManager.putCacheInfo(secretKey, accessTokenVal, 1000*60*60*2, false); }else{ accessTokenVal = cacheAccessToken.getValue().toString(); } } return accessTokenVal; } /** * 获取企业微信的AccessToken * @return */ public String getAccessToken(){ String body = HttpXmlClientProxy.httpGet("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+corpid+"&corpsecret="+secret); return JSON.parseObject(body).getString("access_token"); } /** * 获取企业微信的jsapiTicket * @param accessToken * @return */ public String getJsapiTicket(String accessToken){ String body = HttpXmlClientProxy.httpGet("https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token="+accessToken); return JSON.parseObject(body).getString("ticket"); } /** * 签名加密 * @param jsapiTicket * @return */ public String signature(String jsapiTicket,String nowUrl){ try { String noncestr = UUID.randomUUID().toString().replace("-", "").substring(0, 16); String timestamp=String.valueOf(new Date().getTime() / 1000); nowUrl = URLDecoder.decode(nowUrl, "UTF-8"); String appendStr = "jsapi_ticket="+jsapiTicket+"&noncestr="+noncestr+"×tamp="+timestamp+"&url="+nowUrl; String signature = EncryptUtils.SHA1(appendStr); Map<String,Object> map = new HashMap<String,Object>(); map.put("noncestr",noncestr); map.put("timestamp",timestamp); map.put("signature",signature); map.put("appid", corpid); String shaSignature = JSON.toJSONString(map); LogKit.error("获取企业微信的Signature"+shaSignature); return shaSignature; } catch (UnsupportedEncodingException e) { return e.getMessage(); } } }