业务系统收到用户提交的信息后,通过微信公众号,给对应管理人员发送微信通知,提醒。
根据实际业务去做修改
private void pushMessage(LabelFarmVo param) {
log.info("推送客户经理 -> {} 微信通知", "张三");
final DpxdataFarmWechatPush wechatPush = new DpxdataFarmWechatPush();
try {
final Map<String, TemplateData> data = new HashMap<>();
// 详细内容{{first.DATA}}
ParamVo paramVo = unitService.querySysParam("wechat_message", "first");
if (paramVo == null) {
paramVo = new ParamVo();
paramVo.setName("尊敬的管理员先生(女士),您有一条用户申请待处理");
}
final TemplateData info = new TemplateData(paramVo.getName());
data.put("first", info);
//申请单编号:{{keyword1.DATA}}
final TemplateData num = new TemplateData("手机尾号" + param.getPhone().substring(7));
data.put("keyword1", num);
//申请单类型:{{keyword2.DATA}}
paramVo = unitService.querySysParam("wechat_message", "pushType");
if (paramVo == null) {
paramVo = new ParamVo();
paramVo.setName("用户申请");
}
final TemplateData pushType = new TemplateData(paramVo.getName());
data.put("keyword2", pushType);
//申请人:{{keyword3.DATA}}
final TemplateData user = new TemplateData(param.getName().substring(0, 1) + "**");
data.put("keyword3", user);
//申请时间:{{keyword4.DATA}}
final TemplateData time = new TemplateData(DateUtils.getStrDate());
data.put("keyword4", time);
//{{remark.DATA}}
paramVo = unitService.querySysParam("wechat_message", "remark");
if (paramVo == null) {
paramVo = new ParamVo();
paramVo.setName("详情请登录平台查看,点击可进行快速处理");
}
final TemplateData remark = new TemplateData(paramVo.getName());
data.put("remark", remark);
final JSONObject jsonObject = this.sendMessage("openID", "y9_6zZu4UvorK1kjAo_xQjsE-KUAG-AoI1hOd727PTs", data);
// "errcode": 0,
// "errmsg": "ok",
// "msgid": 2583399535058583555
wechatPush.setStatus(jsonObject.get("errcode").toString());
wechatPush.setCreateTime(new Date());
wechatPush.setMessage(JSON.toJSONString(data));
wechatPush.setRemark(jsonObject.toJSONString());
} catch (Exception e) {
// 发送异常
log.info("微信通知失败");
wechatPush.setStatus("2");
wechatPush.setRemark(e.getMessage());
} finally {
// 本地保存微信推送消息记录
wechatPushService.save(wechatPush);
}
}
/**
*
* @param openId 用户openID
* @param templateId 消息模板ID
* @param data 消息参数
* @return
*/
private JSONObject sendMessage(String openId, String templateId, Map<String, TemplateData> data) {
// 调用开发接口,token根据自己的code和秘钥进行获取
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + WechatUtils.getAccessToken();
SubscribeMessage subscribeMessage = new SubscribeMessage();
// 推送人openID
// subscribeMessage.setTouser("推送人openID");
subscribeMessage.setTouser(openId);
// 模板ID
//subscribeMessage.setTemplate_id("TjsO9zeHeIWfoEBL5-HBIWivZaqfj7YC-0iIKPRYcI");
subscribeMessage.setTemplate_id(templateId);
// subscribeMessage.setUrl("www.baidu.com");
final Miniprogram miniprogram = new Miniprogram();
// 根据字典值获取小程序code
ParamVo paramVo = unitService.querySysParam("wechat_message", "appid");
// 点击消息可以跳转对应的小程序,小程序必须和公众号关联
if (paramVo == null) {
paramVo = new ParamVo();
paramVo.setName("小程序code");
}
miniprogram.setAppid(paramVo.getName());
subscribeMessage.setMiniprogram(miniprogram);
subscribeMessage.setPage("");
subscribeMessage.setData(data);
final String content = JSONObject.toJSONString(subscribeMessage);
return JSON.parseObject(HttpUtil.post(url, content));
}
/**
* @Author: LiSaiHang
* @Date: 2022/8/16 10:09 上午
*/
public class TemplateData {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public TemplateData(String value) {
this.value = value;
}
}