|
@ -0,0 +1,110 @@ |
|
|
|
|
|
package org.jeecg.api.service.impl; |
|
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
|
|
|
|
import org.apache.http.client.methods.CloseableHttpResponse; |
|
|
|
|
|
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.HttpClients; |
|
|
|
|
|
import org.apache.http.util.EntityUtils; |
|
|
|
|
|
import org.jeecg.api.service.ApiMessageService; |
|
|
|
|
|
import org.jeecg.common.api.vo.Result; |
|
|
|
|
|
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember; |
|
|
|
|
|
import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService; |
|
|
|
|
|
import org.jeecg.modules.popularizeOrderTuiLog.service.IPopularizeOrderTuiLogService; |
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
import java.util.HashMap; |
|
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
|
|
@Service |
|
|
|
|
|
public class ApiMessageServiceImpl implements ApiMessageService { |
|
|
|
|
|
private static final String APP_ID = "wx797abcfb479c75ec"; |
|
|
|
|
|
private static final String APP_SECRET = "7f87f7668da3ee1bef7ded80063f99c1"; |
|
|
|
|
|
private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APP_ID + "&secret=" + APP_SECRET; |
|
|
|
|
|
private static final String TEMPLATE_MESSAGE_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private IHanHaiMemberService hanHaiMemberService; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//模板消息推送 |
|
|
|
|
|
@Override |
|
|
|
|
|
public Result<?> sendTemplateMessage(String userId) { |
|
|
|
|
|
|
|
|
|
|
|
String templateId = "SQd2axWZD7KCw3jkw--tumMz9-cmP_R2FUhJzByxrP8"; |
|
|
|
|
|
HanHaiMember hanHaiMember = hanHaiMemberService.getById(userId); |
|
|
|
|
|
String openid = hanHaiMember.getAppletOpenid(); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
String accessToken = getAccessToken2(); |
|
|
|
|
|
if (accessToken != null && !accessToken.isEmpty()) { |
|
|
|
|
|
this.sendTemplateMessage2(accessToken, openid, templateId, createTemplateData()); |
|
|
|
|
|
return Result.OK("发送成功"); |
|
|
|
|
|
} else { |
|
|
|
|
|
return Result.error("无法获取Access Token"); |
|
|
|
|
|
} |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
return Result.error("发送失败:" + e.getMessage()); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public String getAccessToken2() throws IOException { |
|
|
|
|
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) { |
|
|
|
|
|
HttpPost httpPost = new HttpPost(TOKEN_URL); |
|
|
|
|
|
CloseableHttpResponse response = httpClient.execute(httpPost); |
|
|
|
|
|
String responseString = EntityUtils.toString(response.getEntity()); |
|
|
|
|
|
Map<String, Object> responseMap = new ObjectMapper().readValue(responseString, Map.class); |
|
|
|
|
|
return (String) responseMap.get("access_token"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void sendTemplateMessage2(String accessToken, String toUser, String templateId, Map<String, Object> data) throws IOException { |
|
|
|
|
|
String url = TEMPLATE_MESSAGE_URL + accessToken; |
|
|
|
|
|
|
|
|
|
|
|
Map<String, Object> requestBody = new HashMap<>(); |
|
|
|
|
|
requestBody.put("touser", toUser); |
|
|
|
|
|
requestBody.put("template_id", templateId); |
|
|
|
|
|
requestBody.put("data", data); |
|
|
|
|
|
|
|
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper(); |
|
|
|
|
|
String jsonRequest = objectMapper.writeValueAsString(requestBody); |
|
|
|
|
|
|
|
|
|
|
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) { |
|
|
|
|
|
HttpPost httpPost = new HttpPost(url); |
|
|
|
|
|
httpPost.setEntity(new StringEntity(jsonRequest, "UTF-8")); |
|
|
|
|
|
httpPost.setHeader("Content-Type", "application/json"); |
|
|
|
|
|
|
|
|
|
|
|
CloseableHttpResponse response = httpClient.execute(httpPost); |
|
|
|
|
|
String responseString = EntityUtils.toString(response.getEntity()); |
|
|
|
|
|
System.out.println("Response: " + responseString); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private Map<String, Object> createTemplateData() { |
|
|
|
|
|
Map<String, Object> data = new HashMap<>(); |
|
|
|
|
|
data.put("first", new HashMap<String, String>() {{ |
|
|
|
|
|
put("value", "Hello, this is a test message!"); |
|
|
|
|
|
put("color", "#173177"); |
|
|
|
|
|
}}); |
|
|
|
|
|
data.put("keyword1", new HashMap<String, String>() {{ |
|
|
|
|
|
put("value", "Keyword1 Value"); |
|
|
|
|
|
put("color", "#173177"); |
|
|
|
|
|
}}); |
|
|
|
|
|
data.put("keyword2", new HashMap<String, String>() {{ |
|
|
|
|
|
put("value", "Keyword2 Value"); |
|
|
|
|
|
put("color", "#173177"); |
|
|
|
|
|
}}); |
|
|
|
|
|
data.put("remark", new HashMap<String, String>() {{ |
|
|
|
|
|
put("value", "This is a remark."); |
|
|
|
|
|
put("color", "#173177"); |
|
|
|
|
|
}}); |
|
|
|
|
|
return data; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |