diff --git a/yxt_supervise/supervise-customer/supervise-customer-biz/src/main/java/com/yxt/supervise/customer/biz/wechat/WechatApiService.java b/yxt_supervise/supervise-customer/supervise-customer-biz/src/main/java/com/yxt/supervise/customer/biz/wechat/WechatApiService.java index 821e7931..871a5fb1 100644 --- a/yxt_supervise/supervise-customer/supervise-customer-biz/src/main/java/com/yxt/supervise/customer/biz/wechat/WechatApiService.java +++ b/yxt_supervise/supervise-customer/supervise-customer-biz/src/main/java/com/yxt/supervise/customer/biz/wechat/WechatApiService.java @@ -53,21 +53,18 @@ public class WechatApiService { // private Jedis jedis; // @Resource // private JedisPool jedisPool; - public String getAccessToken() throws Exception { +public String getAccessToken(String appId,String appsecret) throws Exception { // SysAppletSet appletSet = appletSetMapper.selectOne(new QueryWrapper<>()); // if (null == appletSet) { // 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; - 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"); - System.out.println(at); - return at; - } + 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; +} // public WechatApiService() { // RequestConfig config = RequestConfig.custom() diff --git a/yxt_supervise/supervise-customer/supervise-customer-biz/src/main/java/com/yxt/supervise/customer/biz/wechat/WechatRest.java b/yxt_supervise/supervise-customer/supervise-customer-biz/src/main/java/com/yxt/supervise/customer/biz/wechat/WechatRest.java index 4ca3c9b6..825572c0 100644 --- a/yxt_supervise/supervise-customer/supervise-customer-biz/src/main/java/com/yxt/supervise/customer/biz/wechat/WechatRest.java +++ b/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; +import com.alibaba.fastjson.JSONObject; import com.yxt.supervise.customer.biz.util.WeixinCheckoutUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang3.StringUtils; +import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import org.springframework.web.client.RestTemplate; + +import java.util.HashMap; +import java.util.Map; /** * @author wangpengfei @@ -15,9 +22,10 @@ import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("v1/wechat") public class WechatRest { - String token="yxthryy"; @Autowired WechatService wechatService; + @Autowired + private WechatApiService wechatApiService; @ApiOperation("token") @GetMapping(value = "token") public String wechate(String signature,String timestamp,String nonce,String echostr){ @@ -43,6 +51,81 @@ public class WechatRest { } 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 sendMag = new HashMap(); + + // 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 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 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; + } } diff --git a/yxt_supervise/supervise-customer/supervise-customer-biz/src/main/java/com/yxt/supervise/customer/biz/wechat/WechatService.java b/yxt_supervise/supervise-customer/supervise-customer-biz/src/main/java/com/yxt/supervise/customer/biz/wechat/WechatService.java index 12c9bd33..8da9ab77 100644 --- a/yxt_supervise/supervise-customer/supervise-customer-biz/src/main/java/com/yxt/supervise/customer/biz/wechat/WechatService.java +++ b/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) { try { - accessToken = wechatApiService.getAccessToken(); + //accessToken = wechatApiService.getAccessToken(); // String templateId = appletSet.getTemplateid1(); // Map param = new HashMap();