
4 changed files with 350 additions and 1 deletions
@ -0,0 +1,36 @@ |
|||
package com.supervise.rms.api.wechat; |
|||
|
|||
import lombok.Data; |
|||
|
|||
/** |
|||
* @author feikefei |
|||
* @create 2023-06-08-9:17 |
|||
*/ |
|||
@Data |
|||
public class TemplateData { |
|||
private String value; |
|||
private String color; |
|||
|
|||
public TemplateData(String value) { |
|||
this.value = value; |
|||
color = "#FF0000"; |
|||
} |
|||
|
|||
public TemplateData(String value, String color) { |
|||
this.value = value; |
|||
this.color = color; |
|||
} |
|||
|
|||
public String getValue() { |
|||
return value; |
|||
} |
|||
public void setValue(String value) { |
|||
this.value = value; |
|||
} |
|||
public String getColor() { |
|||
return color; |
|||
} |
|||
public void setColor(String color) { |
|||
this.color = color; |
|||
} |
|||
} |
@ -0,0 +1,56 @@ |
|||
package com.supervise.rms.api.wechat; |
|||
|
|||
import lombok.Data; |
|||
|
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* @author feikefei |
|||
* @create 2023-06-08-9:17 |
|||
*/ |
|||
@Data |
|||
public class WeChatTemplateMsg { |
|||
private String template_id; |
|||
private String touser; |
|||
private String url; |
|||
|
|||
private String client_msg_id; |
|||
|
|||
Map<String, String> miniprogram = new HashMap<>(); |
|||
|
|||
//private Miniprogram miniprogram;
|
|||
private String topcolor; |
|||
private Map<String,TemplateData> data; |
|||
|
|||
public String getTemplate_id() { |
|||
return template_id; |
|||
} |
|||
public void setTemplate_id(String template_id) { |
|||
this.template_id = template_id; |
|||
} |
|||
public String getTouser() { |
|||
return touser; |
|||
} |
|||
public void setTouser(String touser) { |
|||
this.touser = touser; |
|||
} |
|||
public String getUrl() { |
|||
return url; |
|||
} |
|||
public void setUrl(String url) { |
|||
this.url = url; |
|||
} |
|||
public String getTopcolor() { |
|||
return topcolor; |
|||
} |
|||
public void setTopcolor(String topcolor) { |
|||
this.topcolor = topcolor; |
|||
} |
|||
public Map<String,TemplateData> getData() { |
|||
return data; |
|||
} |
|||
public void setData(Map<String,TemplateData> data) { |
|||
this.data = data; |
|||
} |
|||
} |
@ -0,0 +1,152 @@ |
|||
package com.supervise.rms.biz.wechat; |
|||
|
|||
import com.alibaba.fastjson.JSONObject; |
|||
import com.supervise.rms.api.businessriskdate.BusinessRiskDate; |
|||
import com.supervise.rms.api.pusinformation.PushInformation; |
|||
import com.supervise.rms.api.wechat.TemplateData; |
|||
import com.supervise.rms.api.wechat.WeChatTemplateMsg; |
|||
import com.supervise.rms.biz.businessriskdate.BusinessRiskDateService; |
|||
import com.supervise.rms.biz.pusinformation.PushInformationService; |
|||
import io.swagger.annotations.Api; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.springframework.http.ResponseEntity; |
|||
import org.springframework.web.bind.annotation.PostMapping; |
|||
import org.springframework.web.bind.annotation.RequestBody; |
|||
import org.springframework.web.bind.annotation.RequestMapping; |
|||
import org.springframework.web.bind.annotation.RestController; |
|||
import org.springframework.web.client.RestTemplate; |
|||
|
|||
import java.util.HashMap; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* @author feikefei |
|||
* @create 2023-06-08-10:17 |
|||
*/ |
|||
@Api(tags = "测试") |
|||
@RestController |
|||
@RequestMapping("v1/wechat") |
|||
public class WechatRest { |
|||
@Autowired |
|||
private PushInformationService pushInformationService; |
|||
@Autowired |
|||
private BusinessRiskDateService businessRiskDateService; |
|||
@Value("${wechat.appId}") |
|||
private String appId; |
|||
@Value("${wechat.sppSecret}") |
|||
private String sppSecret; |
|||
@Value("${wechat.WX_URL_ACCESS_TOKEN}") |
|||
private String WX_URL_ACCESS_TOKEN; |
|||
@Value("${wechat.WX_URL_MESSAGE_SEND}") |
|||
private String WX_URL_MESSAGE_SEND; |
|||
|
|||
//0 30 9 ? * 2 每周一九点半
|
|||
@PostMapping("/sendMessage") |
|||
public void sendMessage(@RequestBody WeChatTemplateMsg data) throws Exception { |
|||
String t = "imkugi6AM3SgT-GUciOMlkhPQUbfAiooDWxslzgO4Lc"; |
|||
String accessToken = getAccessToken(appId,sppSecret); |
|||
String wxUrl = WX_URL_MESSAGE_SEND.replace("ACCESS_TOKEN", accessToken); |
|||
//查询库中openId
|
|||
List<PushInformation> pushInformations = pushInformationService.selectPushInformationList(); |
|||
for (PushInformation pushInformation : pushInformations) { |
|||
//查询库中查询到所有企业的信息
|
|||
List<BusinessRiskDate> businessRiskDate = businessRiskDateService.selectBusinessRiskDateByName(pushInformation.getEnterpriseName()); |
|||
BusinessRiskDate riskDate = businessRiskDate.get(0); |
|||
RestTemplate restTemplate = new RestTemplate(); |
|||
Map<String, Object> sendBody = new HashMap<>(); |
|||
setTemplateStyle(riskDate,sendBody,t,wxUrl,pushInformation,restTemplate,riskDate.getVerifyResult(),businessRiskDate); |
|||
} |
|||
// String openId="o7sGD6r8GP3VEjA9iJz3us6Xg0aM";
|
|||
// // 模板参数
|
|||
// Map<String, Object> sendMag = new HashMap<String, Object>();
|
|||
// // openId代表一个唯一微信用户,即微信消息的接收人
|
|||
// //String openId = "oLcw_5ti_iJpwibmL0QxfK2ZG9qQ";
|
|||
// // 公众号的模板id(也有相应的接口可以查询到)
|
|||
// String templateId = "fVgBuwpLC43YkeGWfl1Vw0mW0Qu5WP2giD77TfKAEc4";
|
|||
//// String t = "EOXA31TBtu_v8wHRq4OBo2kTUeBtiIOi4MIEpWNP4bM";
|
|||
// // 微信的基础accessToken
|
|||
//// String accessToken = getAccessToken(appId,sppSecret);
|
|||
//// String wxUrl = WX_URL_MESSAGE_SEND.replace("ACCESS_TOKEN", accessToken);
|
|||
// /**
|
|||
// * 其他模板可以从模板库中自己添加
|
|||
// * 模板ID
|
|||
// * B0YStqTYdjHhY9Da9Sy2NM7HXxxxxxxxxxxxxxx
|
|||
// * 开发者调用模板消息接口时需提供模板ID
|
|||
// * 标题
|
|||
// * 产品兑付成功提醒
|
|||
// * 行业
|
|||
// * 金融业 - 证券|基金|理财|信托
|
|||
// * 详细内容
|
|||
// * {{first.DATA}}
|
|||
// * 产品名称:{{keyword1.DATA}}
|
|||
// * 当期兑付本金:{{keyword2.DATA}}
|
|||
// * 当期兑付利息:{{keyword3.DATA}}
|
|||
// * 已兑付期数:{{keyword4.DATA}}
|
|||
// * 兑付日期:{{keyword5.DATA}}
|
|||
// * {{remark.DATA}}
|
|||
// */
|
|||
// sendMag.put("buName", new TemplateData("石家庄瀚川商贸有限公司"));
|
|||
// sendMag.put("addReason", new TemplateData("未依照《企业信息公示暂行条例》第八条规定的期限公示年度报告的"));
|
|||
// sendMag.put("addDate", new TemplateData("2017-07-03 00:00:00"));
|
|||
// sendMag.put("romoveReason", new TemplateData(""));
|
|||
// sendMag.put("removeDate", new TemplateData(""));
|
|||
// sendMag.put("decisionOffice", new TemplateData("监督管理局"));
|
|||
// sendMag.put("removeDecisionOffice", new TemplateData("123456"));
|
|||
//// Map<String, String> miniprogram = new HashMap<>();
|
|||
//// miniprogram.put("appid","wx0f6062989ab28c91");
|
|||
//// miniprogram.put("pagepath","pages/index/index");
|
|||
// RestTemplate restTemplate = new RestTemplate();
|
|||
// //拼接base参数
|
|||
// Map<String, Object> sendBody = new HashMap<>();
|
|||
//// sendBody.put("miniprogram",miniprogram);
|
|||
// sendBody.put("touser", openId); // openId
|
|||
// sendBody.put("url", "www.baidu.com"); // 点击模板信息跳转地址
|
|||
// sendBody.put("topcolor", "#FF0000"); // 顶色
|
|||
// sendBody.put("data", sendMag); // 模板参数
|
|||
// sendBody.put("template_id", t);// 模板Id
|
|||
//
|
|||
// ResponseEntity<String> forEntity = restTemplate.postForEntity(wxUrl, sendBody, String.class);
|
|||
// JSONObject jsonObject = JSONObject.parseObject(forEntity.getBody());
|
|||
// String messageCode = jsonObject.getString("errcode");
|
|||
// String msgId = jsonObject.getString("msgid");
|
|||
// System.out.println("messageCode : " + messageCode + ", msgId: " +msgId);
|
|||
// //return forEntity.getBody();
|
|||
} |
|||
|
|||
public String getAccessToken(String appId,String appsecret){ |
|||
String url = WX_URL_ACCESS_TOKEN.replace("APPID", appId).replace("APPSECRET", appsecret); |
|||
RestTemplate restTemplate=new RestTemplate(); |
|||
String re= restTemplate.getForObject(url,String.class); |
|||
JSONObject jsonObject= JSONObject.parseObject(re); |
|||
String at=jsonObject.getString("access_token"); |
|||
return at; |
|||
} |
|||
|
|||
public void setTemplateStyle(BusinessRiskDate riskDate, Map<String, Object> sendBody,String t,String wxUrl,PushInformation pushInformation,RestTemplate restTemplate,String verifyResult,List<BusinessRiskDate> businessRiskDate){ |
|||
Map<String, Object> sendMag = new HashMap<>(); |
|||
sendMag.put("thing2", new TemplateData(riskDate.getBusinessName())); |
|||
if (verifyResult.equals("0")){ |
|||
sendMag.put("thing5", new TemplateData("该公司暂无风险")); |
|||
}else { |
|||
sendMag.put("thing5", new TemplateData("该企业有"+businessRiskDate.size())+"条风险"); |
|||
} |
|||
sendMag.put("time6", new TemplateData(riskDate.getExecutionTime())); |
|||
Map<String, String> miniprogram = new HashMap<>(); |
|||
miniprogram.put("appid","wx11565021714ba796"); |
|||
miniprogram.put("pagepath","pages/index/enterpriseRisk?id=1111"); |
|||
//拼接base参数 ?businessName="+riskDate.getBusinessName()+"&executionTime="+riskDate.getExecutionTime()
|
|||
sendBody.put("miniprogram",miniprogram); |
|||
sendBody.put("touser", pushInformation.getOpenId()); // openId
|
|||
sendBody.put("url", ""); // 点击模板信息跳转地址
|
|||
sendBody.put("topcolor", "#FF0000"); // 顶色
|
|||
sendBody.put("data", sendMag); // 模板参数
|
|||
sendBody.put("template_id", t);// 模板Id
|
|||
ResponseEntity<String> forEntity = restTemplate.postForEntity(wxUrl, sendBody, String.class); |
|||
JSONObject jsonObject = JSONObject.parseObject(forEntity.getBody()); |
|||
String messageCode = jsonObject.getString("errcode"); |
|||
String msgId = jsonObject.getString("msgid"); |
|||
System.out.println("messageCode : " + messageCode + ", msgId: " +msgId); |
|||
} |
|||
} |
Loading…
Reference in new issue