微信公總號訊息推送
* 任務完成給製作者推送訊息
*/
wxUserVo = new WxUserVo();
wxUserVo.setUserId(taskInfo.getUser_id());
wxUserVo = userInfoService.getWxUserInfoByUserId(wxUserVo);
if(wxUserVo != null){
builder = new StringBuilder();
builder.append(map.get("project_name")).append("--")
.append(map.get("subproject_name")).append("--").append(map.get("type_name"))
.append("--").append(map.get("segmengt_name"));
String[] keywords = {"任務付款通知",builder.toString() + "任務已付款"};
sendWxMessageService.payTaskSuccessMessage(wxUserVo.getOpenId(), null, keywords);
}
(sendWxMessageService)層
public String payTaskSuccessMessage(String openId, String tourl,
String[] keywords) {
PostMessage post = new PostMessage();
post.setTemplate_id("QIcwpeifNediKmnRzN1jTo"); //訊息模板程式碼,在微信公總平臺設定得到
post.setTouser(openId);
post.setUrl(tourl);
post.setData2("first", new Data("恭喜,又有一筆任務金到賬啦!", "#bf2934"));
for (int i = 1; i <= keywords.length; i++) {
if(i==keywords.length){
post.setData2("keyword"+i, new Data(keywords[i-1], "#173177"));
}else{
post.setData2("keyword"+i, new Data(keywords[i-1], "#173177"));
}
}
//或者
post.setData2("keyword1", new Data(key1, "#173177"));
post.setData2("keyword2", new Data(key2, "#173177"));
post.setData2("keyword3", new Data(key3, "#173177"));
post.setData2("keyword4", new Data(key4, "#173177"));
post.setData2("remark", new Data("付款完成的任務已進入已完成階段!", "#bf2934"));
return HttpClientUtil.postJson(snendUrl + getToken(), JSONObject.fromObject(post)); //見另一片文章 HttpClientUtil
}
String snendUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";
@Override
public String getToken() {
String access_token = null;
/*具體環境加的判斷
if(yuming.trim().equals(wxyuming)){
//如果是正式環境,直接redis取
access_token = redisUtils.getValue("access_token");
}else{
*/
//非正式環境,需要往正式環境介面請求access_token
String turl = "https://"+網址+"/getAccessToken.json"; /
//返回access_token
@RequestMapping("getAccessToken")
public String getAccessToken(){
return sendWxMessageService.getToken();
}
CloseableHttpClient httpClient = HttpClientUtil.getHttpClient();
HttpGet get = new HttpGet(turl);
try {
CloseableHttpResponse closeableHttpResponse = null;
closeableHttpResponse = httpClient.execute(get);
HttpEntity entity = closeableHttpResponse.getEntity();
if(null!=entity&&closeableHttpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
access_token= EntityUtils.toString(entity,"utf-8");
}
} catch (Exception e) {
e.printStackTrace();
}
}
return access_token;
}
@Override
public String getTicket() {
String access_token = null;
//非正式環境,需要往正式環境介面請求access_token
String turl = "https://"+wxyuming+"getJsapiTicket.json";
CloseableHttpClient httpClient = HttpClientUtil.getHttpClient();
HttpGet get = new HttpGet(turl);
try {
CloseableHttpResponse closeableHttpResponse = null;
closeableHttpResponse = httpClient.execute(get);
HttpEntity entity = closeableHttpResponse.getEntity();
if(null!=entity&&closeableHttpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
access_token= EntityUtils.toString(entity,"utf-8");
}
} catch (Exception e) {
e.printStackTrace();
}
}
return access_token;
}