|  | @ -7,15 +7,11 @@ import com.yihu.jw.entity.base.doctor.BaseDoctorDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.hospital.enterprise.WxEnterpriseDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.hospital.enterprise.WxEnterpriseTokenDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.hospital.enterprise.WxEnterpriseUserDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.hospital.prescription.service.entrance.EntranceService;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.date.DateUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.wechat.wxhttp.HttpUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.utils.StringUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.wechat.enterprise.dao.WxEnterpriseDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.wechat.enterprise.dao.WxEnterpriseTokenDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.wechat.enterprise.dao.WxEnterpriseUserDao;
 | 
	
		
			
				|  |  | import com.yihu.utils.network.HttpResponse;
 | 
	
		
			
				|  |  | import com.yihu.utils.network.HttpUtils;
 | 
	
		
			
				|  |  | import org.apache.commons.lang3.StringUtils;
 | 
	
		
			
				|  |  | import org.slf4j.Logger;
 | 
	
		
			
				|  |  | import org.slf4j.LoggerFactory;
 | 
	
	
		
			
				|  | @ -24,8 +20,7 @@ import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.util.Date;
 | 
	
		
			
				|  |  | import java.util.HashMap;
 | 
	
		
			
				|  |  | import java.util.Map;
 | 
	
		
			
				|  |  | import java.util.List;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * Created by Trick on 2020/2/8.
 | 
	
	
		
			
				|  | @ -59,10 +54,16 @@ public class EnterpriseService {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         WxEnterpriseDO wxEnterpriseDO = wxEnterpriseDao.findOne(enterpriseId);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         WxEnterpriseTokenDO wxEnterpriseTokenDO = wxEnterpriseTokenDao.findByCode(wxEnterpriseDO.getCorpid());
 | 
	
		
			
				|  |  |         List<WxEnterpriseTokenDO> wxEnterpriseTokenDOs = wxEnterpriseTokenDao.findByCodeOrderByCreateTimeDesc(wxEnterpriseDO.getCorpid());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         if(wxEnterpriseTokenDO!=null&&wxEnterpriseTokenDO.getExpiresTime().after(new Date())){
 | 
	
		
			
				|  |  |             return wxEnterpriseTokenDO.getAccessToken();
 | 
	
		
			
				|  |  |         if(wxEnterpriseTokenDOs!=null&&wxEnterpriseTokenDOs.size()>0) {
 | 
	
		
			
				|  |  |             logger.info("token size:"+wxEnterpriseTokenDOs.size());
 | 
	
		
			
				|  |  |             WxEnterpriseTokenDO tokenDO = wxEnterpriseTokenDOs.get(0);
 | 
	
		
			
				|  |  |             if (tokenDO.getExpiresTime().after(new Date())) {
 | 
	
		
			
				|  |  |                 return tokenDO.getAccessToken();
 | 
	
		
			
				|  |  |             }else{
 | 
	
		
			
				|  |  |                 wxEnterpriseTokenDao.delete(wxEnterpriseTokenDOs);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String result = HttpUtil.sendGet("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+wxEnterpriseDO.getCorpid()+"&corpsecret="+wxEnterpriseDO.getCorpsecret());
 | 
	
	
		
			
				|  | @ -71,17 +72,13 @@ public class EnterpriseService {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         JSONObject json = JSONObject.parseObject(result);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         if(wxEnterpriseTokenDO!=null){
 | 
	
		
			
				|  |  |             wxEnterpriseTokenDao.delete(wxEnterpriseTokenDO);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //保存当前token
 | 
	
		
			
				|  |  |         WxEnterpriseTokenDO token = new WxEnterpriseTokenDO();
 | 
	
		
			
				|  |  |         Date date = new Date();
 | 
	
		
			
				|  |  |         token.setAccessToken(json.getString("access_token"));
 | 
	
		
			
				|  |  |         token.setCreateTime(date);
 | 
	
		
			
				|  |  |         token.setExpiresTime(DateUtil.setDateHours(date,2));
 | 
	
		
			
				|  |  |         token.setCode(corpid);
 | 
	
		
			
				|  |  |         token.setCode(wxEnterpriseDO.getCorpid());
 | 
	
		
			
				|  |  |         wxEnterpriseTokenDao.save(token);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return token.getAccessToken();
 | 
	
	
		
			
				|  | @ -115,6 +112,27 @@ public class EnterpriseService {
 | 
	
		
			
				|  |  |         return rs;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public String sendMKMesByDoctor(String enterpriseId,String doctor,String content)throws Exception {
 | 
	
		
			
				|  |  |         BaseDoctorDO baseDoctorDO = baseDoctorDao.findById(doctor);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         if (baseDoctorDO != null && StringUtils.isNotBlank(baseDoctorDO.getMobile())) {
 | 
	
		
			
				|  |  |             WxEnterpriseUserDO user = wxEnterpriseUserDao.findByEnterpriseIdAndMobile(enterpriseId, baseDoctorDO.getMobile());
 | 
	
		
			
				|  |  |             if (user != null) {
 | 
	
		
			
				|  |  |                 return sendMKMes(enterpriseId, user.getUserid(), content);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return "";
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public String sendMKMesByMobile(String enterpriseId,String mobile,String content)throws Exception{
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         WxEnterpriseUserDO user = wxEnterpriseUserDao.findByEnterpriseIdAndMobile(enterpriseId,mobile);
 | 
	
		
			
				|  |  |         if(user!=null){
 | 
	
		
			
				|  |  |             return sendMKMes(enterpriseId,user.getUserid(),content);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return "";
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public String sendTWMesByDoctor(String enterpriseId,String doctor,String title,String description,String url)throws Exception{
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         BaseDoctorDO baseDoctorDO = baseDoctorDao.findById(doctor);
 | 
	
	
		
			
				|  | @ -166,6 +184,8 @@ public class EnterpriseService {
 | 
	
		
			
				|  |  |         param.put("enable_id_trans",0);
 | 
	
		
			
				|  |  |         param.put("enable_duplicate_check",0);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         logger.info("TWparam:"+param.toJSONString());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String rs = HttpUtil.sendPost(mesurl,param.toJSONString());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         logger.info("TWMes:"+rs);
 | 
	
	
		
			
				|  | @ -192,7 +212,7 @@ public class EnterpriseService {
 | 
	
		
			
				|  |  |         if(depts!=null&&depts.size()>0){
 | 
	
		
			
				|  |  |             for(int i=0;i<depts.size();i++){
 | 
	
		
			
				|  |  |                 JSONObject dept = (JSONObject) depts.get(i);
 | 
	
		
			
				|  |  |                 deptStr+=dept.getString("id")+"|";
 | 
	
		
			
				|  |  |                 deptStr+=dept.getString("id")+",";
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             deptStr = deptStr.substring(0,deptStr.length()-1);
 | 
	
		
			
				|  |  |         }
 | 
	
	
		
			
				|  | @ -201,9 +221,38 @@ public class EnterpriseService {
 | 
	
		
			
				|  |  |         return deptStr;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public String sendMKMes(String enterpriseId,String userId,String content)throws Exception{
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String mesurl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+getToken(enterpriseId);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         JSONObject param = new JSONObject();
 | 
	
		
			
				|  |  |         param.put("touser",userId);
 | 
	
		
			
				|  |  |         param.put("toparty","");
 | 
	
		
			
				|  |  |         param.put("totag","");
 | 
	
		
			
				|  |  |         param.put("msgtype","markdown");
 | 
	
		
			
				|  |  |         WxEnterpriseDO wxEnterpriseDO = wxEnterpriseDao.findOne(enterpriseId);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         param.put("agentid",wxEnterpriseDO.getAgentid());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         JSONObject markdown = new JSONObject();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         markdown.put("content",content);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         param.put("markdown",markdown);
 | 
	
		
			
				|  |  |         param.put("enable_duplicate_check",0);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         logger.info("MKparam:"+param.toJSONString());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String rs = HttpUtil.sendPost(mesurl,param.toJSONString());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         logger.info("MKMes:"+rs);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return rs;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public JSONArray getUserInfo(String enterpriseId,String dept)throws Exception{
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         logger.info("getUserInfo");
 | 
	
		
			
				|  |  |         logger.info("getUserInfo dept:"+dept);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         if(StringUtils.isNotBlank(dept)){
 | 
	
		
			
				|  |  | 
 | 
	
	
		
			
				|  | @ -229,7 +278,8 @@ public class EnterpriseService {
 | 
	
		
			
				|  |  |         String depts = getDeptsId(enterpriseId);
 | 
	
		
			
				|  |  |         if(StringUtils.isNotBlank(depts)){
 | 
	
		
			
				|  |  |             //多部门过滤
 | 
	
		
			
				|  |  |             String dept[] = depts.split("|");
 | 
	
		
			
				|  |  |             String dept[] = depts.split(",");
 | 
	
		
			
				|  |  |             logger.info("dept size:"+dept.length);
 | 
	
		
			
				|  |  |             if(dept!=null&&dept.length>0){
 | 
	
		
			
				|  |  |                 for(String dp : dept){
 | 
	
		
			
				|  |  |                     JSONArray usrs = getUserInfo(enterpriseId,dp);
 | 
	
	
		
			
				|  | @ -283,16 +333,16 @@ public class EnterpriseService {
 | 
	
		
			
				|  |  | //        param.put("touser","MuOu");
 | 
	
		
			
				|  |  | //        param.put("toparty","");
 | 
	
		
			
				|  |  | //        param.put("totag","");
 | 
	
		
			
				|  |  | //        param.put("msgtype","text");
 | 
	
		
			
				|  |  | //        param.put("msgtype","markdown");
 | 
	
		
			
				|  |  | //        param.put("agentid",agentId);
 | 
	
		
			
				|  |  | //        JSONObject markdown = new JSONObject();
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //        JSONObject text = new JSONObject();
 | 
	
		
			
				|  |  | //        text.put("content","测试MuOu成员企业推送");
 | 
	
		
			
				|  |  | //        param.put("text",text);
 | 
	
		
			
				|  |  | //        param.put("safe",0);
 | 
	
		
			
				|  |  | //        param.put("enable_id_trans",0);
 | 
	
		
			
				|  |  | //        markdown.put("content","dd[查看详情](www.baidu.com)");
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //        param.put("markdown",markdown);
 | 
	
		
			
				|  |  | //        param.put("enable_duplicate_check",0);
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //        String rs = HttpUtil.sendPost(url,param.toJSONString());
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //        System.out.println(rs);
 | 
	
	
		
			
				|  | @ -343,18 +393,18 @@ public class EnterpriseService {
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //    }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | //    public static void main(String args[])throws Exception{
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //        String corpsecret = "SKuRnq7cZqE5ax1YxwBwkCa-jdujkzbpMjWDCYU2raE";
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //        String corpid = "wwd7335a5d80a9b14f";
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //        String result = HttpUtil.sendGet("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+corpid+"&corpsecret="+corpsecret);
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //        JSONObject json = JSONObject.parseObject(result);
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //        String token = json.getString("access_token");
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  |     public static void main(String args[])throws Exception{
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String corpsecret = "Uo-QnbYthpUC0tZt--l71n3QEafT3g2oH_j4mLjCokU";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String corpid = "ww4bf28f7a26e67fee";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String result = HttpUtil.sendGet("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+corpid+"&corpsecret="+corpsecret);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         JSONObject json = JSONObject.parseObject(result);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String token = json.getString("access_token");
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | //        String url = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token="+token;
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //        String rs = HttpUtil.sendGet(url);
 | 
	
	
		
			
				|  | @ -373,18 +423,18 @@ public class EnterpriseService {
 | 
	
		
			
				|  |  | //           deptStr = deptStr.substring(0,deptStr.length()-1);
 | 
	
		
			
				|  |  | //        }
 | 
	
		
			
				|  |  | //        System.out.println("deptStr"+deptStr);
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //        String userUrl ="https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token="+token+"&department_id="+deptStr+"&fetch_child=FETCH_CHILD";
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //        String users = HttpUtil.sendGet(userUrl);
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //        JSONObject us = JSONObject.parseObject(users);
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //        JSONArray list = us.getJSONArray("userlist");
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //        System.out.println("list"+list.size());
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //    }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String userUrl ="https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token="+token+"&department_id=20&fetch_child=FETCH_CHILD";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String users = HttpUtil.sendGet(userUrl);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         JSONObject us = JSONObject.parseObject(users);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         JSONArray list = us.getJSONArray("userlist");
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         System.out.println("list"+list.size());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | }
 |