180 lines
5.6 KiB
Java
180 lines
5.6 KiB
Java
package com.muyu.controller;
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
import lombok.extern.log4j.Log4j2;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import com.muyu.service.WxService;
|
|
import com.muyu.util.WechatUtil;
|
|
import io.micrometer.common.util.StringUtils;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.security.MessageDigest;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.util.*;
|
|
|
|
/**
|
|
* @className: GetWxController ️✈️
|
|
* @author: Yang 鹏 🦅
|
|
* @date: 2024/9/16 20:38 ⏰
|
|
* @Version: 1.0
|
|
* @description:
|
|
*/
|
|
@Log4j2
|
|
@RestController
|
|
public class WxController {
|
|
|
|
// @Autowired
|
|
// private RedisService redisService;
|
|
|
|
@Autowired
|
|
private WxService wxService;
|
|
|
|
@GetMapping("/")
|
|
public String check(@RequestParam("signature") String signature,@RequestParam("timestamp") String timestamp ,
|
|
@RequestParam("nonce") String nonce, @RequestParam("echostr") String echostr){
|
|
|
|
String token ="YangPeng";
|
|
List<String> list = Arrays.asList(token, timestamp, nonce);
|
|
Collections.sort(list);
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
for (String s : list) {
|
|
stringBuilder.append(s);
|
|
}
|
|
try {
|
|
|
|
MessageDigest instance = MessageDigest.getInstance("sha1");
|
|
//使用sha1进行加密,获得byte数组
|
|
byte[] digest = instance.digest(stringBuilder.toString().getBytes());
|
|
StringBuilder sum = new StringBuilder();
|
|
for (byte b : digest) {
|
|
sum.append(Integer.toHexString((b>>4)&15));
|
|
sum.append(Integer.toHexString(b&15));
|
|
}
|
|
|
|
if (!StringUtils.isEmpty(signature) && signature.equals(sum.toString())){
|
|
return echostr;
|
|
}
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
|
|
/**
|
|
* 接收消息并自动回复
|
|
* @param request
|
|
* @return
|
|
*/
|
|
@PostMapping("/")
|
|
public String receiveMessage(HttpServletRequest request){
|
|
|
|
|
|
String message = wxService.handleMessage(request);
|
|
|
|
return message;
|
|
}
|
|
|
|
|
|
@GetMapping("/getSignUpUserInfo")
|
|
public String getSignUpUserInfo(HttpServletRequest request){
|
|
|
|
String code = request.getParameter("code");
|
|
|
|
return wxService.getSignUpUserInfo(code);
|
|
}
|
|
|
|
|
|
@GetMapping("/send")
|
|
public void sendWxMsg(@RequestParam("toUser") String toUser,@RequestParam("content") String content) {
|
|
WechatUtil.send(toUser,content);
|
|
}
|
|
|
|
|
|
// @GetMapping("/getWxUserList")
|
|
// public List getUserList(){
|
|
// String accessToken = TokenUtil.getAccessToken();
|
|
// String url="https://api.weixin.qq.com/cgi-bin/user/get?access_token="+accessToken;
|
|
// String request = OkHttpUtils.sendGetRequest(url);
|
|
//
|
|
// try {
|
|
// ObjectMapper mapper = new ObjectMapper();
|
|
// JsonNode rootNode = mapper.readTree(request);
|
|
//
|
|
// int total = rootNode.get("total").asInt();
|
|
// int count = rootNode.get("count").asInt();
|
|
// JsonNode itemsNode = rootNode.get("data");
|
|
// JsonNode openIdNode = itemsNode.get("openid");
|
|
// List<String> openids = new ArrayList<>();
|
|
//
|
|
// // 遍历 JSON 数组中的每个元素
|
|
// for (JsonNode item : openIdNode) {
|
|
// // 添加到集合中
|
|
// openids.add(item.asText());
|
|
// }
|
|
//
|
|
// String nextOpenid = openids.isEmpty() ? null : openids.get(openids.size() - 1);
|
|
//
|
|
// // 构建最终的JSON对象
|
|
// Map<String, Object> map = new HashMap<>();
|
|
// map.put("total", total);
|
|
// map.put("count", count);
|
|
// Map<String, List<String>> data = new HashMap<>();
|
|
// data.put("openid", openids);
|
|
// map.put("data", data);
|
|
// map.put("next_openid", nextOpenid);
|
|
//
|
|
// // 根据openid获取用户信息
|
|
// for (String openid : openids) {
|
|
// String getUserUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + accessToken + "&openid=" + openid + "&lang=zh_CN";
|
|
// String userInfoRequest = OkHttpUtils.sendGetRequest(getUserUrl);
|
|
//
|
|
// // 解析用户信息
|
|
// JsonNode userInfoNode = mapper.readTree(userInfoRequest);
|
|
// System.out.println("用户信息: " + userInfoNode);
|
|
// }
|
|
// } catch (IOException e) {
|
|
// // 处理解析错误
|
|
// e.printStackTrace();
|
|
// }
|
|
// return null;
|
|
// }
|
|
|
|
|
|
|
|
|
|
// @PostMapping("/test")
|
|
// public void test(){
|
|
// String accessToken = TokenUtil.getAccessToken();
|
|
// System.out.println(accessToken);
|
|
// }
|
|
|
|
|
|
|
|
|
|
// String message="<xml><ToUserName><![CDATA[gh_746d58eabb43]]></ToUserName><FromUserName><![CDATA[oUjOm6hQ2WPWn1d8YO2-ovVJpfdE]]></FromUserName><CreateTime>12345678</CreateTime><MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[你好]]></Content></xml>";
|
|
// @PostMapping("/")
|
|
// public String receiveMessage(HttpServletRequest request){
|
|
//
|
|
//
|
|
//
|
|
// try {
|
|
// ServletInputStream inputStream = null;
|
|
// inputStream = request.getInputStream();
|
|
//
|
|
// byte[] bytes = new byte[1024];
|
|
// int len = 0;
|
|
// while ((len=inputStream.read(bytes))!=-1){
|
|
// System.out.println(new String(bytes, 0, len));
|
|
// }
|
|
//
|
|
// } catch (IOException e) {
|
|
// throw new RuntimeException(e);
|
|
// }
|
|
//
|
|
// return "";
|
|
// }
|
|
|
|
}
|