|
|
@@ -11,8 +11,12 @@ import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
|
|
|
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
|
|
|
import okhttp3.*;
|
|
|
import org.springblade.client.feign.IWechatClient;
|
|
|
+import org.springblade.client.wechat.Util.AESUtil;
|
|
|
+import org.springblade.client.wechat.Util.HttpUtil;
|
|
|
import org.springblade.core.tenant.annotation.NonDS;
|
|
|
+import org.springblade.core.tool.api.R;
|
|
|
import org.springblade.core.tool.utils.ObjectUtil;
|
|
|
+import org.springblade.purchase.sales.entity.Biding;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
import springfox.documentation.annotations.ApiIgnore;
|
|
|
|
|
|
@@ -28,119 +32,145 @@ import java.util.Map;
|
|
|
@AllArgsConstructor
|
|
|
@Slf4j
|
|
|
public class WechatClient implements IWechatClient {
|
|
|
-
|
|
|
+ //公众号的appId以及secret
|
|
|
+ private static final String appId = "wxf077390a6ec17f23";
|
|
|
+ private static final String appSecret = "50e84930675a0c06057d45a6d64ec548";
|
|
|
/**
|
|
|
* 发送模板消息
|
|
|
*
|
|
|
- * @param openId 接收此消息的openId
|
|
|
- * @param loadingTime 装柜时间
|
|
|
- * @param details 发送详情
|
|
|
- * @param number 件数
|
|
|
+ * @param unionId 接收此消息的openId
|
|
|
+ * @param biding
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- /*@Override
|
|
|
- public String newsPush(String openId, String loadingTime, String details, String number) {
|
|
|
-
|
|
|
- log.info("进来了!!!!!!!!!!!!!!!!!!!!!!!!");
|
|
|
- String token = null;
|
|
|
- String result = null;
|
|
|
-
|
|
|
- OkHttpClient client = new OkHttpClient();
|
|
|
+ @Override
|
|
|
+ public String newsPush(String unionId, Biding biding) {
|
|
|
//获得token
|
|
|
- Request request = new Request.Builder().url("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx0a0feca24b695636&secret=b3efb530f9ef97626e2ec31fc7a3af56").build();
|
|
|
- try {
|
|
|
+ Map<String,String> tokenMap = this.getToken();
|
|
|
+ //获得用户列表
|
|
|
+ Map<String, Object> openIdMap = this.getUserOpenId(tokenMap.get("accessToken"));
|
|
|
+ List<String> openIdsList = (List<String>) openIdMap.get("openIds");
|
|
|
+ //获得用户unionId
|
|
|
+ for (int i = 0; i < openIdsList.size(); i++ ){
|
|
|
+ String openid = openIdsList.get(i);
|
|
|
+ //获得unionId
|
|
|
+ Map<String, String > userMap = this.getUserInfo(tokenMap.get("accessToken"),openid);
|
|
|
+ if (userMap.get("unionid").equals(unionId)){
|
|
|
+ //1,配置
|
|
|
+ WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
|
|
|
+ wxStorage.setAppId(appId);
|
|
|
+ wxStorage.setSecret(appSecret);
|
|
|
+ WxMpService wxMpService = new WxMpServiceImpl();
|
|
|
+ wxMpService.setWxMpConfigStorage(wxStorage);
|
|
|
|
|
|
- Response responseToken = client.newCall(request).execute();
|
|
|
- //如果请求成功,解析数据
|
|
|
- if (responseToken.isSuccessful()) {
|
|
|
- String body = responseToken.body().string();
|
|
|
- //得到一个JSON对象
|
|
|
- JSONObject object = JSON.parseObject(body);
|
|
|
- String accessToken = object.getString("access_token");
|
|
|
- if (ObjectUtil.isNotEmpty(accessToken)) {
|
|
|
- log.info("通过code获取token异常:" + object.toJSONString());
|
|
|
- token = accessToken;
|
|
|
- } else {
|
|
|
- throw new RuntimeException("access_token获取失败");
|
|
|
+ List<WxMpTemplateData> data = Arrays.asList(
|
|
|
+ new WxMpTemplateData("first", "[提醒]您有新的竞价消息,请尽快报价,过期作废"),
|
|
|
+ new WxMpTemplateData("keyword1", "发布"),
|
|
|
+ new WxMpTemplateData("keyword2", biding.getContractNo()),
|
|
|
+ new WxMpTemplateData("keyword3", biding.getDepartureHarbor() + "至" + biding.getObjectiveHarbor()),
|
|
|
+ new WxMpTemplateData("keyword4", biding.getCargoInformation()),
|
|
|
+ new WxMpTemplateData("keyword5", biding.getLoadingTime().toString()),
|
|
|
+ new WxMpTemplateData("remark", biding.getRemark())
|
|
|
+ );
|
|
|
+ //2,推送消息
|
|
|
+ WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
|
|
|
+ .toUser(openid)//要推送的用户openid
|
|
|
+ .templateId("Ad7zPbJWk4P44J2muNTDJCHwbxr5trgfPeHLq4oklRE")//模版id
|
|
|
+ .url("https://www.baidu.com/")//点击模版消息要访问的网址
|
|
|
+ .data(data)
|
|
|
+ .build();
|
|
|
+ try {
|
|
|
+ wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println("推送失败:" + e.getMessage());
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- log.info("用户获取token异常:" + e);
|
|
|
}
|
|
|
- //整体参数map
|
|
|
- Map<String, Object> paramMap = new HashMap<>();
|
|
|
- //消息主题显示map
|
|
|
- Map<String, Object> dataMap = new HashMap<>();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
|
|
|
- dataMap.put("date1", loadingTime);
|
|
|
- dataMap.put("thing2", details);
|
|
|
- dataMap.put("number3", number);
|
|
|
- paramMap.put("touser", openId);
|
|
|
- paramMap.put("template_id", "GGYopU4NGV055HSSY5SeeaAGKGjP1fXFV3Vag0O7POM");
|
|
|
- paramMap.put("data", dataMap);
|
|
|
+ /**
|
|
|
+ * 获得公众号token
|
|
|
+ */
|
|
|
+ public Map<String, String> getToken() {
|
|
|
|
|
|
+ Map<String,String> tokenMap = new HashMap<>();
|
|
|
+ String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+appSecret;
|
|
|
try {
|
|
|
- // 构建Request请求对象
|
|
|
- log.info("JSON====="+JSON.toJSONString(paramMap));
|
|
|
- RequestBody body = RequestBody.create(MediaType.parse("application/xml;charset=utf-8"), JSON.toJSONString(paramMap));
|
|
|
- Request requestTwo = new Request.Builder().url("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token).post(body).build();
|
|
|
- // 请求接口,拿到token信息
|
|
|
- Response response = client.newCall(requestTwo).execute();
|
|
|
- System.out.println("拿到结果了:" + body);
|
|
|
- // 判断成功与否
|
|
|
- if (response.isSuccessful()) {
|
|
|
- // 拿到相应结果
|
|
|
- result = response.body().string();
|
|
|
- //得到一个JSON对象
|
|
|
- JSONObject object = JSON.parseObject(result);
|
|
|
- log.info("返回信息====="+object);
|
|
|
- if (object.getString("errcode").equals("40003")) {
|
|
|
- throw new RuntimeException("touser字段 openid 为空或者不正确");
|
|
|
- } else if (object.getString("errcode").equals("40037")) {
|
|
|
- throw new RuntimeException("订阅模板 id 为空不正确");
|
|
|
- }else if (object.getString("errcode").equals("47003")) {
|
|
|
- throw new RuntimeException("模板参数不准确,可能为空或者不满足规则" + object.get("errmsg"));
|
|
|
+ String response = HttpUtil.get(requestUrl);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(response);
|
|
|
+ if (ObjectUtil.isEmpty(jsonObject.get("errcode"))){
|
|
|
+ String accessToken = String.valueOf(jsonObject.get("access_token"));
|
|
|
+ if (ObjectUtil.isEmpty(accessToken)){
|
|
|
+ throw new RuntimeException("获取token出现异常");
|
|
|
}else {
|
|
|
- throw new RuntimeException("不合法的telCode(telCode不存在、已过期或者使用过)");
|
|
|
+ tokenMap.put("accessToken", accessToken);
|
|
|
}
|
|
|
+ }else if (jsonObject.getString("errcode").equals("-1")) {
|
|
|
+ throw new RuntimeException("系统繁忙,此时请开发者稍候再试");
|
|
|
+ } else {
|
|
|
+ throw new RuntimeException("不合法的telCode(telCode不存在、已过期或者使用过)");
|
|
|
}
|
|
|
+
|
|
|
} catch (Exception e) {
|
|
|
- throw new SecurityException("发送失败" + e.getMessage());
|
|
|
+ log.info("获取token出现异常");
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
- return result;
|
|
|
- }*/
|
|
|
|
|
|
- @Override
|
|
|
- public String newsPush(String openId, String loadingTime, String details, String number) {
|
|
|
- //1,配置
|
|
|
- WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
|
|
|
- wxStorage.setAppId("wxf077390a6ec17f23");
|
|
|
- wxStorage.setSecret("50e84930675a0c06057d45a6d64ec548");
|
|
|
- WxMpService wxMpService = new WxMpServiceImpl();
|
|
|
- wxMpService.setWxMpConfigStorage(wxStorage);
|
|
|
+ return tokenMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得用户列表
|
|
|
+ */
|
|
|
+ public Map<String, Object> getUserOpenId(String accessToken){
|
|
|
+ Map<String, Object> openIdMap = new HashMap<>();
|
|
|
+ String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/get?access_token="+accessToken;
|
|
|
+ try {
|
|
|
+ String response = HttpUtil.get(requestUrl);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(response);
|
|
|
+ if (ObjectUtil.isEmpty(jsonObject.get("errcode"))){
|
|
|
+ JSONObject data = jsonObject.getJSONObject("data");
|
|
|
+ if (ObjectUtil.isNotEmpty(data.get("openid"))){
|
|
|
+ openIdMap.put("openIds",data.getJSONArray("openid"));
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ throw new RuntimeException("用户获取失败"+jsonObject.get("errmsg"));
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.info("获取用户openid出现异常");
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return openIdMap;
|
|
|
+ }
|
|
|
|
|
|
- List<WxMpTemplateData> data = Arrays.asList(
|
|
|
- new WxMpTemplateData("date1", loadingTime),
|
|
|
- new WxMpTemplateData("thing2", details),
|
|
|
- new WxMpTemplateData("number3", number)
|
|
|
- );
|
|
|
- //2,推送消息
|
|
|
- WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
|
|
|
- //.toUser(openId)//要推送的用户openid
|
|
|
- .toUser("oyHoI6kh6AMgN2F8WHIp6YPdqvAI")//要推送的用户openid
|
|
|
- .templateId("GGYopU4NGV055HSSY5SeeaAGKGjP1fXFV3Vag0O7POM")//模版id
|
|
|
- .url("https://www.baidu.com/")//点击模版消息要访问的网址
|
|
|
- .data(data)
|
|
|
- .build();
|
|
|
+ /**
|
|
|
+ * 获得用户信息
|
|
|
+ */
|
|
|
+ public Map<String, String > getUserInfo(String accessToken,String openId){
|
|
|
+ Map<String,String> userMap = new HashMap<>();
|
|
|
+ String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+accessToken+"&openid="+openId+"&lang=zh_CN";
|
|
|
try {
|
|
|
- wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
|
|
|
+ String response = HttpUtil.get(requestUrl);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(response);
|
|
|
+ if (ObjectUtil.isEmpty(jsonObject.get("errcode"))){
|
|
|
+ String unionid = String.valueOf(jsonObject.get("unionid"));
|
|
|
+ if (ObjectUtil.isEmpty(unionid)){
|
|
|
+ throw new RuntimeException("获取unionid出现异常");
|
|
|
+ }else {
|
|
|
+ userMap.put("unionid", unionid);
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ throw new RuntimeException("用户获取失败"+jsonObject.get("errmsg"));
|
|
|
+ }
|
|
|
+
|
|
|
} catch (Exception e) {
|
|
|
- System.out.println("推送失败:" + e.getMessage());
|
|
|
+ log.info("获取unionid出现异常");
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
- return null;
|
|
|
+ return userMap;
|
|
|
}
|
|
|
|
|
|
}
|