|
@ -0,0 +1,390 @@
|
|
|
package com.yihu.jw.wechat.enterprise;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yihu.jw.doctor.dao.BaseDoctorDao;
|
|
|
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;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* Created by Trick on 2020/2/8.
|
|
|
*/
|
|
|
@Service
|
|
|
@Transactional
|
|
|
public class EnterpriseService {
|
|
|
|
|
|
private Logger logger = LoggerFactory.getLogger(EnterpriseService.class);
|
|
|
|
|
|
|
|
|
private String corpsecret = "SKuRnq7cZqE5ax1YxwBwkCa-jdujkzbpMjWDCYU2raE";
|
|
|
|
|
|
private String corpid = "wwd7335a5d80a9b14f";
|
|
|
|
|
|
private String agentId = "1000003";
|
|
|
|
|
|
@Autowired
|
|
|
private WxEnterpriseTokenDao wxEnterpriseTokenDao;
|
|
|
@Autowired
|
|
|
private WxEnterpriseUserDao wxEnterpriseUserDao;
|
|
|
@Autowired
|
|
|
private WxEnterpriseDao wxEnterpriseDao;
|
|
|
@Autowired
|
|
|
private BaseDoctorDao baseDoctorDao;
|
|
|
|
|
|
|
|
|
public String getToken(String enterpriseId)throws Exception{
|
|
|
|
|
|
logger.info("getToken");
|
|
|
|
|
|
WxEnterpriseDO wxEnterpriseDO = wxEnterpriseDao.findOne(enterpriseId);
|
|
|
|
|
|
WxEnterpriseTokenDO wxEnterpriseTokenDO = wxEnterpriseTokenDao.findByCode(wxEnterpriseDO.getCorpid());
|
|
|
|
|
|
if(wxEnterpriseTokenDO!=null&&wxEnterpriseTokenDO.getExpiresTime().after(new Date())){
|
|
|
return wxEnterpriseTokenDO.getAccessToken();
|
|
|
}
|
|
|
|
|
|
String result = HttpUtil.sendGet("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+wxEnterpriseDO.getCorpid()+"&corpsecret="+wxEnterpriseDO.getCorpsecret());
|
|
|
|
|
|
logger.info("getToken result:"+result);
|
|
|
|
|
|
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);
|
|
|
wxEnterpriseTokenDao.save(token);
|
|
|
|
|
|
return token.getAccessToken();
|
|
|
}
|
|
|
|
|
|
public String sendTextMes(String enterpriseId,String userId,String content)throws Exception{
|
|
|
|
|
|
String url = "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","text");
|
|
|
|
|
|
WxEnterpriseDO wxEnterpriseDO = wxEnterpriseDao.findOne(enterpriseId);
|
|
|
|
|
|
param.put("agentid",wxEnterpriseDO.getAgentid());
|
|
|
|
|
|
JSONObject text = new JSONObject();
|
|
|
text.put("content",content);
|
|
|
param.put("text",text);
|
|
|
param.put("safe",0);
|
|
|
param.put("enable_id_trans",0);
|
|
|
param.put("enable_duplicate_check",0);
|
|
|
|
|
|
String rs = HttpUtil.sendPost(url,param.toJSONString());
|
|
|
|
|
|
logger.info("Mes:"+rs);
|
|
|
|
|
|
return rs;
|
|
|
}
|
|
|
|
|
|
public String sendTWMesByDoctor(String enterpriseId,String doctor,String title,String description,String url)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 sendTWMes(enterpriseId,user.getUserid(),title,description,url);
|
|
|
}
|
|
|
}
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
public String sendTWMesBymobile(String enterpriseId,String mobile,String title,String description,String url)throws Exception{
|
|
|
WxEnterpriseUserDO user = wxEnterpriseUserDao.findByEnterpriseIdAndMobile(enterpriseId,mobile);
|
|
|
if(user!=null){
|
|
|
return sendTWMes(enterpriseId,user.getUserid(),title,description,url);
|
|
|
}
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
public String sendTWMes(String enterpriseId,String userId,String title,String description,String url)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","news");
|
|
|
WxEnterpriseDO wxEnterpriseDO = wxEnterpriseDao.findOne(enterpriseId);
|
|
|
|
|
|
param.put("agentid",wxEnterpriseDO.getAgentid());
|
|
|
|
|
|
JSONObject news = new JSONObject();
|
|
|
|
|
|
JSONArray articles = new JSONArray();
|
|
|
JSONObject article = new JSONObject();
|
|
|
|
|
|
article.put("title",title);
|
|
|
article.put("description",description);
|
|
|
article.put("url",url);
|
|
|
article.put("picurl","");
|
|
|
articles.add(article);
|
|
|
|
|
|
news.put("articles",articles);
|
|
|
|
|
|
param.put("news",news);
|
|
|
param.put("enable_id_trans",0);
|
|
|
param.put("enable_duplicate_check",0);
|
|
|
|
|
|
String rs = HttpUtil.sendPost(mesurl,param.toJSONString());
|
|
|
|
|
|
logger.info("TWMes:"+rs);
|
|
|
|
|
|
return rs;
|
|
|
}
|
|
|
|
|
|
public String getDeptsId(String enterpriseId)throws Exception{
|
|
|
|
|
|
logger.info("getDeptsId");
|
|
|
|
|
|
String url = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token="+getToken(enterpriseId);
|
|
|
|
|
|
String rs = HttpUtil.sendGet(url);
|
|
|
|
|
|
logger.info("dept rs:"+rs);
|
|
|
|
|
|
JSONObject r = JSONObject.parseObject(rs);
|
|
|
|
|
|
JSONArray depts = r.getJSONArray("department");
|
|
|
|
|
|
String deptStr ="";
|
|
|
|
|
|
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 = deptStr.substring(0,deptStr.length()-1);
|
|
|
}
|
|
|
logger.info("deptStr"+deptStr);
|
|
|
|
|
|
return deptStr;
|
|
|
}
|
|
|
|
|
|
public JSONArray getUserInfo(String enterpriseId,String dept)throws Exception{
|
|
|
|
|
|
logger.info("getUserInfo");
|
|
|
|
|
|
if(StringUtils.isNotBlank(dept)){
|
|
|
|
|
|
String userUrl ="https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token="+getToken(enterpriseId)+"&department_id="+dept+"&fetch_child=FETCH_CHILD";
|
|
|
|
|
|
String users = HttpUtil.sendGet(userUrl);
|
|
|
|
|
|
logger.info("usrs:"+users);
|
|
|
|
|
|
JSONObject us = JSONObject.parseObject(users);
|
|
|
|
|
|
JSONArray list = us.getJSONArray("userlist");
|
|
|
|
|
|
logger.info("list size:"+list.size());
|
|
|
|
|
|
return list;
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
public String saveAllUser(String enterpriseId)throws Exception{
|
|
|
logger.info("saveAllUser");
|
|
|
String depts = getDeptsId(enterpriseId);
|
|
|
if(StringUtils.isNotBlank(depts)){
|
|
|
//多部门过滤
|
|
|
String dept[] = depts.split("|");
|
|
|
if(dept!=null&&dept.length>0){
|
|
|
for(String dp : dept){
|
|
|
JSONArray usrs = getUserInfo(enterpriseId,dp);
|
|
|
if(usrs!=null&&usrs.size()>0){
|
|
|
for(int i=0;i<usrs.size();i++){
|
|
|
JSONObject user = (JSONObject) usrs.get(i);
|
|
|
String mobile =user.getString("mobile");
|
|
|
if(StringUtils.isNotBlank(mobile)){
|
|
|
WxEnterpriseUserDO wxEnterpriseUserDO = wxEnterpriseUserDao.findByEnterpriseIdAndMobile(enterpriseId,mobile);
|
|
|
if(wxEnterpriseUserDO!=null){
|
|
|
wxEnterpriseUserDO.setName(user.getString("name"));
|
|
|
wxEnterpriseUserDO.setUserid(user.getString("userid"));
|
|
|
wxEnterpriseUserDao.save(wxEnterpriseUserDO);
|
|
|
}else{
|
|
|
WxEnterpriseUserDO userDO = new WxEnterpriseUserDO();
|
|
|
userDO.setEnterpriseId(enterpriseId);
|
|
|
userDO.setMobile(mobile);
|
|
|
userDO.setUserid(user.getString("userid"));
|
|
|
userDO.setName(user.getString("name"));
|
|
|
wxEnterpriseUserDao.save(userDO);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return "ok";
|
|
|
}
|
|
|
|
|
|
|
|
|
// public String send
|
|
|
|
|
|
// public static void main(String args[])throws Exception{
|
|
|
//
|
|
|
// String corpsecret = "SKuRnq7cZqE5ax1YxwBwkCa-jdujkzbpMjWDCYU2raE";
|
|
|
//
|
|
|
// String corpid = "wwd7335a5d80a9b14f";
|
|
|
//
|
|
|
// String agentId = "1000003";
|
|
|
//
|
|
|
// 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/message/send?access_token="+token;
|
|
|
//
|
|
|
// JSONObject param = new JSONObject();
|
|
|
// param.put("touser","MuOu");
|
|
|
// param.put("toparty","");
|
|
|
// param.put("totag","");
|
|
|
// param.put("msgtype","text");
|
|
|
// param.put("agentid",agentId);
|
|
|
//
|
|
|
// JSONObject text = new JSONObject();
|
|
|
// text.put("content","测试MuOu成员企业推送");
|
|
|
// param.put("text",text);
|
|
|
// param.put("safe",0);
|
|
|
// param.put("enable_id_trans",0);
|
|
|
// param.put("enable_duplicate_check",0);
|
|
|
//
|
|
|
// String rs = HttpUtil.sendPost(url,param.toJSONString());
|
|
|
//
|
|
|
// System.out.println(rs);
|
|
|
// }
|
|
|
|
|
|
|
|
|
// public static void main(String args[])throws Exception{
|
|
|
//
|
|
|
// String corpsecret = "SKuRnq7cZqE5ax1YxwBwkCa-jdujkzbpMjWDCYU2raE";
|
|
|
//
|
|
|
// String corpid = "wwd7335a5d80a9b14f";
|
|
|
//
|
|
|
// String agentId = "1000003";
|
|
|
//
|
|
|
// 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/message/send?access_token="+token;
|
|
|
//
|
|
|
// JSONObject param = new JSONObject();
|
|
|
// param.put("touser","ShenZaiXin");
|
|
|
// param.put("toparty","");
|
|
|
// param.put("totag","");
|
|
|
// param.put("msgtype","news");
|
|
|
// param.put("agentid",agentId);
|
|
|
//
|
|
|
// JSONObject news = new JSONObject();
|
|
|
//
|
|
|
// JSONArray articles = new JSONArray();
|
|
|
// JSONObject article = new JSONObject();
|
|
|
//
|
|
|
// article.put("title","标题");
|
|
|
// article.put("description","图文描述");
|
|
|
// article.put("url","www.baidu.com");
|
|
|
// article.put("picurl","");
|
|
|
// articles.add(article);
|
|
|
//
|
|
|
// news.put("articles",articles);
|
|
|
//
|
|
|
// param.put("news",news);
|
|
|
// param.put("enable_id_trans",0);
|
|
|
// param.put("enable_duplicate_check",0);
|
|
|
//
|
|
|
// String rs = HttpUtil.sendPost(url,param.toJSONString());
|
|
|
//
|
|
|
// }
|
|
|
|
|
|
// 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");
|
|
|
//
|
|
|
// String url = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token="+token;
|
|
|
//
|
|
|
// String rs = HttpUtil.sendGet(url);
|
|
|
//
|
|
|
// JSONObject r = JSONObject.parseObject(rs);
|
|
|
//
|
|
|
// JSONArray depts = r.getJSONArray("department");
|
|
|
//
|
|
|
// String deptStr ="";
|
|
|
//
|
|
|
// 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 = 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());
|
|
|
//
|
|
|
// }
|
|
|
|
|
|
|
|
|
}
|