Compare commits
	
		
			2 Commits 
		
	
	
		
			a02cf3dc62
			...
			b1aec4d9f8
		
	
	| Author | SHA1 | Date | 
|---|---|---|
|  | b1aec4d9f8 | 9 months ago | 
|  | 769aa8e2b4 | 9 months ago | 
				 2 changed files with 100 additions and 0 deletions
			
			
		| @ -0,0 +1,33 @@ | ||||
| package cn.iocoder.yudao.module.system.controller.admin.weixin; | ||||
| 
 | ||||
| import cn.iocoder.yudao.module.system.service.weixin.WeChatService; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.web.bind.annotation.GetMapping; | ||||
| import org.springframework.web.bind.annotation.RequestMapping; | ||||
| import org.springframework.web.bind.annotation.RestController; | ||||
| 
 | ||||
| import javax.annotation.security.PermitAll; | ||||
| 
 | ||||
| @RestController | ||||
| @RequestMapping("/wechat") | ||||
| @PermitAll | ||||
| public class WeChatController { | ||||
| 
 | ||||
|     @Autowired | ||||
|     private WeChatService weChatService; | ||||
| 
 | ||||
|     @GetMapping("/send") | ||||
|     @PermitAll | ||||
|     public String sendMessage() { | ||||
|         try { | ||||
|             String accessToken = weChatService.getAccessToken(); | ||||
|             String openId = "ogEz46_ADXNd_SmC9f57tYZkswRg"; // 替换为用户的 OpenID
 | ||||
|             String templateId = "E8RK91cPLMios6ZHoKx6FJOV4H2kodx6yPWYp7jpLJY"; // 替换为订阅消息模板 ID
 | ||||
|             weChatService.sendSubscribeMessage(accessToken, openId, templateId); | ||||
|             return "Message sent successfully"; | ||||
|         } catch (Exception e) { | ||||
|             e.printStackTrace(); | ||||
|             return "Failed to send message"; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,67 @@ | ||||
| package cn.iocoder.yudao.module.system.service.weixin; | ||||
| import com.fasterxml.jackson.databind.node.ObjectNode; | ||||
| import org.apache.http.client.methods.HttpPost; | ||||
| import org.apache.http.entity.StringEntity; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.beans.factory.annotation.Value; | ||||
| import org.springframework.stereotype.Service; | ||||
| import org.apache.http.client.methods.HttpGet; | ||||
| import org.apache.http.impl.client.CloseableHttpClient; | ||||
| import org.apache.http.impl.client.HttpClients; | ||||
| import org.apache.http.util.EntityUtils; | ||||
| import com.fasterxml.jackson.databind.JsonNode; | ||||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||||
| 
 | ||||
| @Service | ||||
| public class WeChatService { | ||||
|     @Autowired | ||||
|     private ObjectMapper objectMapper; | ||||
|     @Value("${wx.miniapp.appid}") | ||||
|     private String APP_ID ; // 替换为你的小程序 AppID
 | ||||
|     @Value("${wx.miniapp.secret}") | ||||
|     private  String APP_SECRET; // 替换为你的小程序 AppSecret
 | ||||
| 
 | ||||
|     /** | ||||
|      * 获取微信 Access Token | ||||
|      */ | ||||
|     public String getAccessToken() throws Exception { | ||||
|         String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APP_ID + "&secret=" + APP_SECRET; | ||||
|         CloseableHttpClient httpClient = HttpClients.createDefault(); | ||||
|         HttpGet httpGet = new HttpGet(url); | ||||
|         String response = EntityUtils.toString(httpClient.execute(httpGet).getEntity()); | ||||
|         httpClient.close(); | ||||
| 
 | ||||
|         ObjectMapper mapper = new ObjectMapper(); | ||||
|         JsonNode node = mapper.readTree(response); | ||||
|         return node.get("access_token").asText(); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     /** | ||||
|      * 发送订阅消息 | ||||
|      */ | ||||
|     public void sendSubscribeMessage(String accessToken, String openId, String templateId) throws Exception { | ||||
|         String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken; | ||||
| 
 | ||||
|         // 构建消息内容
 | ||||
|         ObjectNode data = objectMapper.createObjectNode(); | ||||
|         data.put("touser", openId); // 用户的 OpenID
 | ||||
|         data.put("template_id", templateId); // 订阅消息模板 ID
 | ||||
|         data.put("page", "pages/index/index"); // 点击消息跳转的小程序页面
 | ||||
|         data.put("data", objectMapper.createObjectNode() | ||||
|                 .put("thing1", objectMapper.createObjectNode().put("value", "Hello, World!")) | ||||
|                 .at("thing2")); | ||||
| 
 | ||||
|         // 发送 HTTP 请求
 | ||||
|         CloseableHttpClient httpClient = HttpClients.createDefault(); | ||||
|         HttpPost httpPost = new HttpPost(url); | ||||
|         httpPost.setHeader("Content-Type", "application/json"); | ||||
|         httpPost.setEntity(new StringEntity(data.toString())); | ||||
|         String response = EntityUtils.toString(httpClient.execute(httpPost).getEntity()); | ||||
|         httpClient.close(); | ||||
| 
 | ||||
|         System.out.println("订阅消息发送结果: " + response); | ||||
|     } | ||||
| 
 | ||||
|      | ||||
| } | ||||
					Loading…
					
					
				
		Reference in new issue