Commit 165b886d authored by gaoyingwei's avatar gaoyingwei

新增公众号推送

parent aaac466e
...@@ -50,4 +50,16 @@ public class CyWxMiniCons { ...@@ -50,4 +50,16 @@ public class CyWxMiniCons {
public static final String OPE_PROFILE_NOT_LOGIN = "用户未登录,请先登录"; public static final String OPE_PROFILE_NOT_LOGIN = "用户未登录,请先登录";
// 微信获取 小程序access_token
public static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
public static final String ACCESS_TOKEN = "access_token";
public static final String EXPIRES_IN = "expires_in";
// 微信发送公众号推送
public static final String UNIFORM_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=%s";
public static final String ERRCODE = "errcode";
public static final String ERRMSG = "errmsg";
} }
...@@ -6,8 +6,13 @@ import lombok.extern.slf4j.Slf4j; ...@@ -6,8 +6,13 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
...@@ -31,6 +36,7 @@ import org.rcisoft.tencent.dto.UserDto; ...@@ -31,6 +36,7 @@ import org.rcisoft.tencent.dto.UserDto;
import org.rcisoft.tencent.service.CyWxMiniService; import org.rcisoft.tencent.service.CyWxMiniService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
...@@ -97,6 +103,13 @@ public class CyWxMiniServiceImpl implements CyWxMiniService { ...@@ -97,6 +103,13 @@ public class CyWxMiniServiceImpl implements CyWxMiniService {
@Autowired(required = false) @Autowired(required = false)
private SysMenuRbacRepository sysMenuRbacRepository; private SysMenuRbacRepository sysMenuRbacRepository;
// appId
@Value("${wx.publicNumberAppId:}")
private String publicNumberAppId;
// secret
@Value("${wx.publicNumberSecret:}")
private String publicNumberSecret;
@Override @Override
public UserDto getUserInfo(Integer userId) { public UserDto getUserInfo(Integer userId) {
...@@ -426,4 +439,140 @@ public class CyWxMiniServiceImpl implements CyWxMiniService { ...@@ -426,4 +439,140 @@ public class CyWxMiniServiceImpl implements CyWxMiniService {
resultMap.put(CyWxMiniCons.DATA, sysUserRbac); resultMap.put(CyWxMiniCons.DATA, sysUserRbac);
return resultMap; return resultMap;
} }
//获取微信小程序token
public String getAccessToken(){
String getTokenUrl = String.format(CyWxMiniCons.ACCESS_TOKEN_URL, appId, secret);
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
String access_token = "";
try {
// 创建http GET请求
HttpGet httpGet = new HttpGet(getTokenUrl);
client = HttpClients.createDefault();
// 执行请求
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();//得到返回数据
String result = EntityUtils.toString(entity);
log.error("微信返回---------------------" + result);
JSONObject json_test = JSONUtil.parseObj(result);
access_token = json_test.getStr(CyWxMiniCons.ACCESS_TOKEN);
String expires_in = json_test.getStr(CyWxMiniCons.EXPIRES_IN);
log.error("openId---------------------" + access_token);
log.error("sessionKey---------------------" + expires_in);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
if (client != null) {
try {
client.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
return access_token;
}
//{
// "touser": "ozuQB5oBBWpRHFRqc5TMpwzl8oow",
// "mp_template_msg": {
// "appid": "wx43e887ca5100a510",
// "template_id": "-VMqiq8wDBcN4VabMg5377QVlBk2N_7CYloqJwdd2Tg",
// "url": "http://weixin.qq.com/download",
// "miniprogram": {
// "appid": "wx16cfb2d12e4ab57c",
// "path": "pages/init/init?flag=1&id=120"
// },
// "data": {
// "first": {
// "value": "恭喜你购买成功!",
// "color": "#173177"
// },
// "keyword1": {
// "value": "巧克力",
// "color": "#173177"
// },
// "keyword2": {
// "value": "123",
// "color": "#173177"
// },
// "keyword3": {
// "value": "123",
// "color": "#173177"
// },
// "remark": {
// "value": "12",
// "color": "#173177"
// }
// }
// }
//}
//发送微信公众号消息
public void pushWeChatMessage(String accessToken,String openId,String templateId,String path, Map<String,String> dataMap){
// String accessToken = getAccessToken();
if (StringUtils.isNotBlank(accessToken)){
String sendUrl = String.format(CyWxMiniCons.UNIFORM_SEND_URL, accessToken);
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
// String access_token = "";
try {
// 创建http post请求
HttpPost httpPost = new HttpPost(sendUrl);
client = HttpClients.createDefault();
JSONObject pushData = new JSONObject();
Map<String,Object> map = new HashMap<>();
pushData.put("touser", openId);
pushData.put("mp_template_msg", map);
map.put("appid",publicNumberAppId);
map.put("template_id",templateId);
// Map<String,String> miniProgram = new HashMap<>();
// map.put("miniprogram",miniProgram);
// miniProgram.put("appid",appId);
// miniProgram.put("path",path);
map.put("data",dataMap);
log.error("公众号消息传参---------------------" + pushData.toString());
StringEntity entity = new StringEntity(pushData.toString(), "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
response = client.execute(httpPost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(response.getEntity());
log.error("微信返回---------------------" + result);
JSONObject json_test = JSONUtil.parseObj(result);
if ("ok".equals(json_test.getStr(CyWxMiniCons.ERRMSG)))
log.error("公众号推送成功---------------------");
else
log.error("公众号推送失败---------------------"+json_test.getStr(CyWxMiniCons.ERRMSG));
}
} catch (Exception e) {
log.error(e.getMessage());
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
if (client != null) {
try {
client.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
}else {
log.error("获取微信token失败");
}
}
} }
...@@ -107,6 +107,8 @@ wx: ...@@ -107,6 +107,8 @@ wx:
appId: wx16cfb2d12e4ab57c appId: wx16cfb2d12e4ab57c
secret: 37556072ad536b8d9d6cf5fb638fec88 secret: 37556072ad536b8d9d6cf5fb638fec88
timeOut: 3600 timeOut: 3600
publicNumberAppId: wx43e887ca5100a510
publicNumberSecret: 20fe153245c3e72ab96bd4b672f75910
jieLink: jieLink:
apiUrl: http://192.168.18.192:8091 apiUrl: http://192.168.18.192:8091
......
...@@ -104,6 +104,8 @@ wx: ...@@ -104,6 +104,8 @@ wx:
appId: wxeaf4e41658aad634 appId: wxeaf4e41658aad634
secret: 1e7727cec4a9a85fe009a2ee6ee5aaf7 secret: 1e7727cec4a9a85fe009a2ee6ee5aaf7
timeOut: 3600 timeOut: 3600
publicNumberAppId: wx43e887ca5100a510
publicNumberSecret: 20fe153245c3e72ab96bd4b672f75910
jieLink: jieLink:
apiUrl: http://192.168.18.192:8091 apiUrl: http://192.168.18.192:8091
......
...@@ -104,6 +104,8 @@ wx: ...@@ -104,6 +104,8 @@ wx:
appId: wxeaf4e41658aad634 appId: wxeaf4e41658aad634
secret: 1e7727cec4a9a85fe009a2ee6ee5aaf7 secret: 1e7727cec4a9a85fe009a2ee6ee5aaf7
timeOut: 3600 timeOut: 3600
publicNumberAppId: wx43e887ca5100a510
publicNumberSecret: 20fe153245c3e72ab96bd4b672f75910
jieLink: jieLink:
apiUrl: http://192.168.18.192:8091 apiUrl: http://192.168.18.192:8091
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment