
2 changed files with 96 additions and 3 deletions
@ -0,0 +1,92 @@ |
|||||
|
package com.supervise.rms.config; |
||||
|
|
||||
|
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.biz.businessriskdate.BusinessRiskDateService; |
||||
|
import com.supervise.rms.biz.pusinformation.PushInformationService; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.beans.factory.annotation.Value; |
||||
|
import org.springframework.http.ResponseEntity; |
||||
|
import org.springframework.scheduling.annotation.EnableScheduling; |
||||
|
import org.springframework.scheduling.annotation.Scheduled; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
import org.springframework.web.client.RestTemplate; |
||||
|
|
||||
|
import java.util.HashMap; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
/** |
||||
|
* @author feikefei |
||||
|
* @create 2023-06-20-14:47 |
||||
|
*/ |
||||
|
@Component |
||||
|
public class WechatConfig { |
||||
|
@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/2 * * * * ?
|
||||
|
// @Scheduled(cron = "0 30 9 ? * 2")
|
||||
|
public void sendMessage(){ |
||||
|
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); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
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?businessName="+riskDate.getBusinessName()+"&executionTime="+riskDate.getExecutionTime()); |
||||
|
//拼接base参数
|
||||
|
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