Browse Source

微信公众号推送完善

master
wangpengfei 2 years ago
parent
commit
6b8c955d12
  1. 5
      yxt_supervise/supervise-customer/supervise-customer-biz/src/main/java/com/yxt/supervise/customer/biz/wechat/WechatApiService.java
  2. 85
      yxt_supervise/supervise-customer/supervise-customer-biz/src/main/java/com/yxt/supervise/customer/biz/wechat/WechatRest.java
  3. 2
      yxt_supervise/supervise-customer/supervise-customer-biz/src/main/java/com/yxt/supervise/customer/biz/wechat/WechatService.java

5
yxt_supervise/supervise-customer/supervise-customer-biz/src/main/java/com/yxt/supervise/customer/biz/wechat/WechatApiService.java

@ -53,19 +53,16 @@ public class WechatApiService {
// private Jedis jedis; // private Jedis jedis;
// @Resource // @Resource
// private JedisPool jedisPool; // private JedisPool jedisPool;
public String getAccessToken() throws Exception { public String getAccessToken(String appId,String appsecret) throws Exception {
// SysAppletSet appletSet = appletSetMapper.selectOne(new QueryWrapper<>()); // SysAppletSet appletSet = appletSetMapper.selectOne(new QueryWrapper<>());
// if (null == appletSet) { // if (null == appletSet) {
// throw new ApiMallPlusException("没有设置支付配置"); // throw new ApiMallPlusException("没有设置支付配置");
// } // }
String appId="wx1d44e0fcd110351a";
String appsecret="4764e32722a7d98656dea6afd9405701";
String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+appsecret; String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+appsecret;
RestTemplate restTemplate=new RestTemplate(); RestTemplate restTemplate=new RestTemplate();
String re= restTemplate.getForObject(url,String.class); String re= restTemplate.getForObject(url,String.class);
com.alibaba.fastjson.JSONObject jsonObject= com.alibaba.fastjson.JSONObject.parseObject(re); com.alibaba.fastjson.JSONObject jsonObject= com.alibaba.fastjson.JSONObject.parseObject(re);
String at=jsonObject.getString("access_token"); String at=jsonObject.getString("access_token");
System.out.println(at);
return at; return at;
} }

85
yxt_supervise/supervise-customer/supervise-customer-biz/src/main/java/com/yxt/supervise/customer/biz/wechat/WechatRest.java

@ -1,11 +1,18 @@
package com.yxt.supervise.customer.biz.wechat; package com.yxt.supervise.customer.biz.wechat;
import com.alibaba.fastjson.JSONObject;
import com.yxt.supervise.customer.biz.util.WeixinCheckoutUtil; import com.yxt.supervise.customer.biz.util.WeixinCheckoutUtil;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
/** /**
* @author wangpengfei * @author wangpengfei
@ -15,9 +22,10 @@ import org.springframework.web.bind.annotation.*;
@RestController @RestController
@RequestMapping("v1/wechat") @RequestMapping("v1/wechat")
public class WechatRest { public class WechatRest {
String token="yxthryy";
@Autowired @Autowired
WechatService wechatService; WechatService wechatService;
@Autowired
private WechatApiService wechatApiService;
@ApiOperation("token") @ApiOperation("token")
@GetMapping(value = "token") @GetMapping(value = "token")
public String wechate(String signature,String timestamp,String nonce,String echostr){ public String wechate(String signature,String timestamp,String nonce,String echostr){
@ -43,6 +51,81 @@ public class WechatRest {
} }
return "非法请求"; return "非法请求";
} }
@GetMapping("/sendMessage")
@Test
public void sendMessage() throws Exception {
//String openId="otnrM57kui-WCUmRtChhb1HguOgw";
//String openId ="otnrM5ySvUo7ZZc4qafWzM9eyKBk";
String openId="otnrM544hxIgkMOfaDU8VpJhj_6k";
String appsecret="4764e32722a7d98656dea6afd9405701";
String appId="wx1d44e0fcd110351a";
// 模板参数
Map<String, String> sendMag = new HashMap<String, String>();
// openId代表一个唯一微信用户,即微信消息的接收人
//String openId = "oLcw_5ti_iJpwibmL0QxfK2ZG9qQ";
// 公众号的模板id(也有相应的接口可以查询到)
String templateId = "fVgBuwpLC43YkeGWfl1Vw0mW0Qu5WP2giD77TfKAEc4";
// 微信的基础accessToken
String accessToken=getAccessToken(appId,appsecret);
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
/**
* 其他模板可以从模板库中自己添加
* 模板ID
* B0YStqTYdjHhY9Da9Sy2NM7HXxxxxxxxxxxxxxx
* 开发者调用模板消息接口时需提供模板ID
* 标题
* 产品兑付成功提醒
* 行业
* 金融业 - 证券|基金|理财|信托
* 详细内容
* {{first.DATA}}
* 产品名称{{keyword1.DATA}}
* 当期兑付本金{{keyword2.DATA}}
* 当期兑付利息{{keyword3.DATA}}
* 已兑付期数{{keyword4.DATA}}
* 兑付日期{{keyword5.DATA}}
* {{remark.DATA}}
*/
// sendMag.put("first", new WeChatTemplateMsg("f123"));
// sendMag.put("keyword1", new WeChatTemplateMsg("111"));
// sendMag.put("keyword2", new WeChatTemplateMsg("222"));
// sendMag.put("keyword3", new WeChatTemplateMsg("333"));
// sendMag.put("keyword4", new WeChatTemplateMsg("444"));
sendMag.put("remark", "123465");
RestTemplate restTemplate = new RestTemplate();
//拼接base参数
Map<String, Object> sendBody = new HashMap<>();
sendBody.put("touser", openId); // openId
//sendBody.put("url", "www.baidu.com"); // 点击模板信息跳转地址
sendBody.put("topcolor", "#FF0000"); // 顶色
sendBody.put("data", sendMag); // 模板参数
sendBody.put("template_id", templateId); // 模板Id
ResponseEntity<String> forEntity = restTemplate.postForEntity(url, sendBody, String.class);
//log.info("结果是: {}",forEntity.getBody());
JSONObject jsonObject = JSONObject.parseObject(forEntity.getBody());
// 0
String messageCode = jsonObject.getString("errcode");
// 2431260672639467520
String msgId = jsonObject.getString("msgid");
System.out.println("messageCode : " + messageCode + ", msgId: " +msgId);
//return forEntity.getBody();
}
public String getAccessToken(String appId,String appsecret) throws Exception {
// SysAppletSet appletSet = appletSetMapper.selectOne(new QueryWrapper<>());
// if (null == appletSet) {
// throw new ApiMallPlusException("没有设置支付配置");
// }
String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+appsecret;
RestTemplate restTemplate=new RestTemplate();
String re= restTemplate.getForObject(url,String.class);
com.alibaba.fastjson.JSONObject jsonObject= com.alibaba.fastjson.JSONObject.parseObject(re);
String at=jsonObject.getString("access_token");
return at;
}
} }

2
yxt_supervise/supervise-customer/supervise-customer-biz/src/main/java/com/yxt/supervise/customer/biz/wechat/WechatService.java

@ -38,7 +38,7 @@ public class WechatService {
if (flag) { if (flag) {
try { try {
accessToken = wechatApiService.getAccessToken(); //accessToken = wechatApiService.getAccessToken();
// String templateId = appletSet.getTemplateid1(); // String templateId = appletSet.getTemplateid1();
// Map<String, TemplateData> param = new HashMap<String, TemplateData>(); // Map<String, TemplateData> param = new HashMap<String, TemplateData>();

Loading…
Cancel
Save