|
@ -12,6 +12,7 @@ import com.yihu.wlyy.wechat.util.WeiXinAccessTokenUtils;
|
|
|
import com.yihu.wlyy.wechat.util.WeiXinOpenIdUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.json.JSONArray;
|
|
|
import org.json.JSONException;
|
|
|
import org.json.JSONObject;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
@ -23,6 +24,7 @@ import org.springframework.stereotype.Component;
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
|
|
|
|
@ -37,12 +39,20 @@ public class PushMsgTask {
|
|
|
private HttpUtil httpUtil;
|
|
|
@Value("${server.server_url}")
|
|
|
private String server_url;
|
|
|
@Value("${wechat.message.template_consult_notice}")
|
|
|
private String template_consult_notice;
|
|
|
@Value("${pushMes.method}")
|
|
|
private String putMesMethod;
|
|
|
@Value("${pushMes.redis_prescription_title}")
|
|
|
private String redisQueue;
|
|
|
@Value("${putMesType.wechat}")
|
|
|
private String putMesType;
|
|
|
|
|
|
//模板id
|
|
|
@Value("${wechat.message.template_sign_success}")
|
|
|
private String template_sign_success;
|
|
|
@Value("${wechat.message.template_sign_failed}")
|
|
|
private String template_sign_failed;
|
|
|
@Value("${wechat.message.template_consult_notice}")
|
|
|
private String template_consult_notice;
|
|
|
@Value("${wechat.message.template_health_notice}")
|
|
|
private String template_health_notice;
|
|
|
@Value("${wechat.message.template_termination}")
|
|
@ -61,16 +71,15 @@ public class PushMsgTask {
|
|
|
private String template_doctor_survey;
|
|
|
@Value("${wechat.message.template_doctor_audit}")
|
|
|
private String template_doctor_audit;//审核结果通知
|
|
|
@Value("${wechat.message.template_physical_examination}")
|
|
|
private String template_physical_examination;//体检提醒
|
|
|
@Value("${wechat.message.template_doctor_service}")
|
|
|
private String template_doctor_service;//服务结果通知
|
|
|
@Value("${pushMes.method}")
|
|
|
private String putMesMethod;
|
|
|
@Value("${pushMes.redis_prescription_title}")
|
|
|
private String redisQueue;
|
|
|
@Value("${putMesType.wechat}")
|
|
|
private String putMesType;
|
|
|
@Value("${wechat.message.template_physical_examination}")
|
|
|
private String template_physical_examination;//体检提醒
|
|
|
@Value("${wechat.message.doctor_invitel_template}")
|
|
|
private String doctor_invitel_template;
|
|
|
@Value("${wechat.message.template_deal_with}")
|
|
|
private String template_deal_with;
|
|
|
|
|
|
@Autowired
|
|
|
private StringRedisTemplate redisTemplate;
|
|
|
@Autowired
|
|
@ -137,6 +146,41 @@ public class PushMsgTask {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据type及场景值添加微信消息
|
|
|
* @param access_token
|
|
|
* @param type
|
|
|
* @param scene
|
|
|
* @param openid
|
|
|
* @param name
|
|
|
* @param data packageTemplate方法打包后的模板数据
|
|
|
*/
|
|
|
public void putWxMsg(String access_token, int type,String scene, String openid, String name, JSONObject data) {
|
|
|
try {
|
|
|
JSONObject json = new JSONObject();
|
|
|
json.put("wx", true);
|
|
|
json.put("access_token", access_token);
|
|
|
json.put("type", type);
|
|
|
json.put("scene", scene);
|
|
|
json.put("openid", openid);
|
|
|
json.put("name", name);
|
|
|
json.put("data", data);
|
|
|
//如果是内网推送到redis,如果是外网推送到内存队列
|
|
|
if (putMesMethod.equals("1")) {
|
|
|
JSONObject mes = new JSONObject();
|
|
|
mes.put("title", putMesType);
|
|
|
mes.put("value", json.toString());
|
|
|
redisTemplate.opsForList().leftPush(redisQueue, mes.toString());
|
|
|
} else {
|
|
|
queue.put(json);
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
logger.error("添加到微信消息列队列失败!", e);
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void put(JSONArray array) {
|
|
|
if (array == null || array.length() == 0) {
|
|
|
return;
|
|
@ -186,12 +230,14 @@ public class PushMsgTask {
|
|
|
if (type == -1) {
|
|
|
continue;
|
|
|
}
|
|
|
String scene = json.has("scene") ? json.getString("scene") : "";
|
|
|
String access_token = json.has("access_token") ? json.getString("access_token") : "";
|
|
|
String openid = json.has("openid") ? json.getString("openid") : "";
|
|
|
String name = json.has("name") ? json.getString("name") : "";
|
|
|
// String name = data.has("name") ? json.getString("name") : "";
|
|
|
// 发送消息到微信端
|
|
|
sendWeixinMessage(access_token, type, openid, name, data);
|
|
|
// sendWeixinMessage(access_token, type,scene, openid, name, data);
|
|
|
} else {
|
|
|
// 推送平台消息
|
|
|
String receiver = json.has("receiver") ? json.getString("receiver") : "";
|
|
@ -242,7 +288,6 @@ public class PushMsgTask {
|
|
|
* type==20时:{"first":"消息主题","keyword1":"服务项目","keyword2":"操作医生","keyword3":"服务时间","remark":"消息备注"}
|
|
|
* @return
|
|
|
*/
|
|
|
// private boolean sendWeixinMessage(String access_token, int type, String openid, String name, JSONObject json) {
|
|
|
public boolean sendWeixinMessage(String access_token, int type, String openid, String name, JSONObject json) {
|
|
|
try {
|
|
|
if (StringUtils.isEmpty(openid)) {
|
|
@ -290,6 +335,64 @@ public class PushMsgTask {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
* @param access_token
|
|
|
* @param type
|
|
|
* @param openid
|
|
|
* @param name
|
|
|
* @param json
|
|
|
* @return
|
|
|
*/
|
|
|
public boolean sendWeixinMessage(String access_token, int type,String scene, String openid, String name, JSONObject json) {
|
|
|
try {
|
|
|
if (StringUtils.isEmpty(openid)) {
|
|
|
logger.error("send wechat message failed:openid is empty");
|
|
|
return false;
|
|
|
}
|
|
|
if (access_token != null) {
|
|
|
String token_url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token;
|
|
|
String params = json.toString();
|
|
|
if (params == "") {
|
|
|
logger.error("参数错误!error");
|
|
|
return false;
|
|
|
}
|
|
|
WechatPushLog log = new WechatPushLog();
|
|
|
log.setType(type);
|
|
|
log.setScene(scene);
|
|
|
log.setCreateTime(new Date());
|
|
|
log.setName(name);
|
|
|
log.setOpenid(openid);
|
|
|
if (!json.isNull("toUser")) {
|
|
|
log.setPatient(json.getString("toUser"));
|
|
|
}
|
|
|
log.setRequest(json.toString());
|
|
|
String result = httpUtil.sendPost(token_url, params);
|
|
|
JSONObject jsonResult = new JSONObject(result);
|
|
|
log.setResponse(result);
|
|
|
if (Integer.parseInt(jsonResult.get("errcode").toString()) == 0) {
|
|
|
logger.info("微信信息推送成功!success");
|
|
|
log.setStatus(1);
|
|
|
wechatPushLogDao.save(log);
|
|
|
return true;
|
|
|
} else {
|
|
|
log.setStatus(0);
|
|
|
wechatPushLogDao.save(log);
|
|
|
logger.error("错误编码:" + jsonResult.get("errcode").toString() + " 错误提示:" + jsonResult.get("errmsg").toString());
|
|
|
return false;
|
|
|
}
|
|
|
} else {
|
|
|
logger.error("获取access_token失败!");
|
|
|
return false;
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
logger.error("微信信息推送失败!");
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 拼接参数
|
|
|
*
|
|
@ -757,4 +860,182 @@ public class PushMsgTask {
|
|
|
return flag;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 添加含发送代理人微信消息
|
|
|
*
|
|
|
* @param code 发送对象code
|
|
|
* @param type 模板类型
|
|
|
* @param scene 模板场景值
|
|
|
* @param wechatTemplate 微信消息模板推送内容
|
|
|
* @return
|
|
|
*/
|
|
|
public Boolean putAgentWxMsg(String code, int type,String scene, WechatTemplate wechatTemplate) {
|
|
|
Boolean flag = false;
|
|
|
try {
|
|
|
Patient patient = patientDao.findByCode(code);
|
|
|
String name = patient.getName();
|
|
|
String openId = patient.getOpenid();
|
|
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
String data = mapper.writeValueAsString(wechatTemplate);
|
|
|
JSONObject wechatContent = new JSONObject(data);
|
|
|
|
|
|
if (StringUtils.isNotEmpty(openId) && !("undefined".equals(openId))) {
|
|
|
putWxMsg(accessTokenUtils.getAccessToken(), type, scene, openId, name, wechatContent);
|
|
|
flag = true;
|
|
|
} else {
|
|
|
//发送代理人
|
|
|
org.json.JSONArray jsonArray = weiXinOpenIdUtils.getAgentOpenId(code, openId);
|
|
|
if (jsonArray != null && jsonArray.length() > 0) {
|
|
|
for (int i = 0; i < jsonArray.length(); i++) {
|
|
|
org.json.JSONObject j = jsonArray.getJSONObject(i);
|
|
|
Patient member = (Patient) j.get("member");
|
|
|
|
|
|
Map<String, WechatTemplateData> contentData = wechatTemplate.getData();
|
|
|
WechatTemplateData firstData = contentData.get("first");
|
|
|
String url = wechatTemplate.getUrl();
|
|
|
String first = firstData.getValue();
|
|
|
|
|
|
WechatTemplateData firstKeyword = new WechatTemplateData();
|
|
|
firstKeyword.setColor("#000000");
|
|
|
firstKeyword.setValue(weiXinOpenIdUtils.getTitleMes(patient, j.getInt("relation"), name) + first);
|
|
|
contentData.put("first", firstKeyword);
|
|
|
|
|
|
//替换掉原toUser、toName的值
|
|
|
int start = url.indexOf("&toUser=");
|
|
|
int end = url.indexOf("&", start + 1);
|
|
|
String touser = url.substring(start, end);
|
|
|
url = url.replace(touser, "&toUser=" + member.getCode());
|
|
|
|
|
|
wechatTemplate.setTouser(member.getOpenid());
|
|
|
wechatTemplate.setUrl(url);
|
|
|
wechatTemplate.setData(contentData);
|
|
|
|
|
|
String data1 = mapper.writeValueAsString(wechatTemplate);
|
|
|
JSONObject wechatContent1 = new JSONObject(data1);
|
|
|
if (StringUtils.isNotEmpty(member.getOpenid()) && !("undefined".equals(member.getOpenid()))) {
|
|
|
putWxMsg(accessTokenUtils.getAccessToken(), type, scene, openId, name, wechatContent1);
|
|
|
flag = true;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return flag;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 根据type 匹配对应模板ID
|
|
|
*
|
|
|
* @param type 见WechatPushLog说明
|
|
|
* @return
|
|
|
*/
|
|
|
private String getTemplateId(int type) throws Exception {
|
|
|
String templateId = null;
|
|
|
switch (type) {
|
|
|
case 1:
|
|
|
templateId = template_sign_success;
|
|
|
break;
|
|
|
case 2:
|
|
|
templateId = template_sign_failed;
|
|
|
break;
|
|
|
case 3:
|
|
|
templateId = template_consult_notice;
|
|
|
break;
|
|
|
case 4:
|
|
|
templateId = template_health_notice;
|
|
|
break;
|
|
|
case 5:
|
|
|
templateId = template_termination;
|
|
|
break;
|
|
|
case 6:
|
|
|
templateId = template_appoint_success;
|
|
|
break;
|
|
|
case 7:
|
|
|
templateId = template_appoint_failed;
|
|
|
break;
|
|
|
case 8:
|
|
|
templateId = template_expenses_remind;
|
|
|
break;
|
|
|
case 9:
|
|
|
templateId = template_healthy_article;
|
|
|
break;
|
|
|
case 10:
|
|
|
templateId = template_doctor_change;
|
|
|
break;
|
|
|
case 11:
|
|
|
templateId = template_doctor_survey;
|
|
|
break;
|
|
|
case 12:
|
|
|
templateId = template_doctor_audit;
|
|
|
break;
|
|
|
case 13:
|
|
|
templateId = template_doctor_service;
|
|
|
break;
|
|
|
case 14:
|
|
|
templateId = template_physical_examination;
|
|
|
break;
|
|
|
case 15:
|
|
|
templateId = doctor_invitel_template;
|
|
|
break;
|
|
|
case 16:
|
|
|
templateId = template_deal_with;
|
|
|
break;
|
|
|
}
|
|
|
return templateId;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 构建微信模板数据 默认字体黑色
|
|
|
*
|
|
|
* @param type 模板类型见日志实体类
|
|
|
* @param openid 发送对象openid
|
|
|
* @param first 消息头
|
|
|
* @param remark 备注
|
|
|
* @param url 带参全路径跳转链接
|
|
|
* @param keywords 消息体
|
|
|
* @return
|
|
|
*/
|
|
|
public WechatTemplate packageTemplate(int type,String openid, String first, String remark, String url, List<String> keywords) throws Exception{
|
|
|
|
|
|
WechatTemplate temp = new WechatTemplate();
|
|
|
try {
|
|
|
String templateId = getTemplateId(type);
|
|
|
|
|
|
Map<String, WechatTemplateData> m = new HashMap<String, WechatTemplateData>();
|
|
|
|
|
|
WechatTemplateData firstKeyword = new WechatTemplateData();
|
|
|
firstKeyword.setColor("#000000");
|
|
|
firstKeyword.setValue(first);
|
|
|
m.put("first", firstKeyword);
|
|
|
WechatTemplateData remarkKeyword = new WechatTemplateData();
|
|
|
remarkKeyword.setColor("#000000");
|
|
|
remarkKeyword.setValue(remark);
|
|
|
m.put("remark", remarkKeyword);
|
|
|
for (int i=0;i<keywords.size();i++){
|
|
|
WechatTemplateData tempKeyword = new WechatTemplateData();
|
|
|
tempKeyword.setColor("#000000");
|
|
|
tempKeyword.setValue(keywords.get(i));
|
|
|
m.put("keyword"+(i+1), tempKeyword);
|
|
|
}
|
|
|
|
|
|
|
|
|
temp.setTouser(openid);
|
|
|
temp.setTemplate_id(templateId);
|
|
|
temp.setTopcolor("#000000");
|
|
|
temp.setUrl(url);
|
|
|
temp.setData(m);
|
|
|
|
|
|
/* ObjectMapper mapper = new ObjectMapper();
|
|
|
String data = mapper.writeValueAsString(temp);
|
|
|
result = new JSONObject(data);*/
|
|
|
} catch (JSONException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return temp;
|
|
|
}
|
|
|
|
|
|
}
|