|  | @ -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);
 | 
	
	
		
			
				|  | @ -201,6 +221,35 @@ 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");
 | 
	
	
		
			
				|  | @ -283,16 +332,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);
 |