获取公众号用户的openId 和 unionId
我这里就不写获取小程序的openId 和 unionId和用户信息了,获取小程序的代码有点杂,是以前写的,不会的可以上网找一下如何获取,如果不绑定开放平台时获取不到unionId的,可以看一下官方文档https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/union-id.html
下面是获取公众号的openId和unionId代码:根据关注的用户进行获取 这个接口就是服务器配置的URL,上面验证的时候也使用过一次,不过验证时get请求,这次是post请求。 Controller层
@Autowired
private WeChatMPService weChatMPService;
@ApiOperation(value = "处理微信服务器发来的消息", notes = "处理微信服务器发来的消息")
@PostMapping("/mp/serverCheck")
public String doPost(HttpServletRequest request, HttpServletResponse response) {
// 调用核心服务类接收处理请求
return weChatMPService.processRequest(request);
}
Service接口层
String processRequest(HttpServletRequest request, String projectId);
ServiceImpl实现类
import com.minapp.management.config.WeChatContant;
import com.minapp.management.service.TdSysMpStaffLoginService;
import com.minapp.management.service.WeChatMPService;
import com.minapp.management.utils.WeChatUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
* @ClassName: WeChatServiceImpl
* @Description: 可以结合官网的api看是什么意思 ↓ 消息管理 -> 接受事件推送
* https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_standard_messages.html
* @Authror: XQD
* @Date: 2021/1/4 15:42
*/
@Slf4j
@Service
public class WeChatMPServiceImpl implements WeChatMPService {
@Resource
private TdSysMpStaffLoginService mpStaffLoginService;
@Override
public String processRequest(HttpServletRequest request) {
// xml格式的消息数据
String respXml = null;
// 默认返回的文本消息内容
String respContent;
try {
// 调用parseXml方法解析请求消息
Map
// 消息类型
String msgType = requestMap.get(WeChatContant.MsgType);
log.info("\n消息类型:{}", msgType);
String mes = null;
// 文本消息
if (msgType.equals("text")) {
respContent = "您发送的是文本消息!";
respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
}
// 图片消息
else if (msgType.equals("image")) {
respContent = "您发送的是图片消息!";
respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
}
// 语音消息
else if (msgType.equals("voice")) {
respContent = "您发送的是语音消息!";
respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
}
// 视频消息
else if (msgType.equals("video")) {
respContent = "您发送的是视频消息!";
respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
}
// 地理位置消息
else if (msgType.equals("location")) {
respContent = "您发送的是地理位置消息!";
respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
}
// 链接消息
else if (msgType.equals("link")) {
respContent = "您发送的是链接消息!";
respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
}
// 事件推送
else if (msgType.equals("event")) {
// 事件类型
String eventType = requestMap.get("Event");
log.info("\n事件类型为:{}", eventType);
// 关注
if (eventType.equals("subscribe")) {
mpStaffLoginService.subscribeMPUserInfo(requestMap.get(WeChatContant.FromUserName));
respContent = "谢谢您的关注!";
respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
}
// 取消关注
else if (eventType.equals("unsubscribe")) {
mpStaffLoginService.unsubscribeMPUserInfo(requestMap.get(WeChatContant.FromUserName));
// TODO 取消订阅后用户不会再收到公众账号发送的消息,因此不需要回复
}
// 扫描带参数二维码
else if (eventType.equals("SCAN")) {
// TODO 处理扫描带参数二维码事件
}
// 上报地理位置
else if (eventType.equals("LOCATION")) {
// TODO 处理上报地理位置事件
}
// 自定义菜单
else if (eventType.equals("CLICK")) {
// TODO 处理菜单点击事件
}
}
mes = mes == null ? "不知道你在干嘛" : mes;
if (respXml == null) {
respXml = WeChatUtil.sendTextMsg(requestMap, mes);
}
log.info("\n"+respXml);
return respXml;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
}
WeChatUtil工具类
package com.minapp.management.utils;
import com.minapp.management.config.WeChatContant;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
/**
* @ClassName: WeChatUtil
* @Description: 请求校验工具类
* @Authror: XQD
* @Date: 2021/1/4 15:35
*/
public class WeChatUtil {
/**
* 将字节数组转换为十六进制字符串
*
* @param byteArray
* @return
*/
private static String byteToStr(byte[] byteArray) {
String strDigest = "";
for (int i = 0; i < byteArray.length; i++) {
strDigest += byteToHexStr(byteArray[i]);
}
return strDigest;
}
/**
* 将字节转换为十六进制字符串
*
* @param mByte
* @return
*/
private static String byteToHexStr(byte mByte) {
char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
char[] tempArr = new char[2];
tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
tempArr[1] = Digit[mByte & 0X0F];
String s = new String(tempArr);
return s;
}
private static void sort(String a[]) {
for (int i = 0; i < a.length - 1; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[j].compareTo(a[i]) < 0) {
String temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
/**
* 解析微信发来的请求(xml)
*
* @param request
* @return
* @throws Exception
*/
@SuppressWarnings({ "unchecked"})
public static Map
// 将解析结果存储在HashMap中
Map
// 从request中取得输入流
InputStream inputStream = request.getInputStream();
// 读取输入流
SAXReader reader = new SAXReader();
Document document = reader.read(inputStream);
// 得到xml根元素
Element root = document.getRootElement();
// 得到根元素的所有子节点
List
// 遍历所有子节点
for (Element e : elementList){
map.put(e.getName(), e.getText());
}
// 释放资源
inputStream.close();
inputStream = null;
return map;
}
public static String mapToXML(Map map) {
StringBuffer sb = new StringBuffer();
sb.append("
mapToXML2(map, sb);
sb.append("");
try {
return sb.toString();
} catch (Exception e) {
}
return null;
}
private static void mapToXML2(Map map, StringBuffer sb) {
Set set = map.keySet();
for (Iterator it = set.iterator(); it.hasNext();) {
String key = (String) it.next();
Object value = map.get(key);
if (null == value){
value = "";
}
if (value.getClass().getName().equals("java.util.ArrayList")) {
ArrayList list = (ArrayList) map.get(key);
sb.append("<" + key + ">");
for (int i = 0; i < list.size(); i++) {
HashMap hm = (HashMap) list.get(i);
mapToXML2(hm, sb);
}
sb.append("" + key + ">");
} else {
if (value instanceof HashMap) {
sb.append("<" + key + ">");
mapToXML2((HashMap) value, sb);
sb.append("" + key + ">");
} else {
sb.append("<" + key + ">" + key + ">");
}
}
}
}
/**
* 回复文本消息
* @param requestMap
* @param content
* @return
*/
public static String sendTextMsg(Map
Map
map.put("ToUserName", requestMap.get(WeChatContant.FromUserName));
map.put("FromUserName", requestMap.get(WeChatContant.ToUserName));
map.put("MsgType", WeChatContant.RESP_MESSAGE_TYPE_TEXT);
map.put("CreateTime", System.currentTimeMillis());
map.put("Content", content);
return mapToXML(map);
}
}
在用户关注了,会触发关注事件,微信服务器通过接口给你发送上面的一段xml信息,里面包括了openId和一些其他信息,解析出来后,可以通过openId 和access_token (access_token获取方法在我推送服务通知的博客里面) 获取到用户的昵称,头像,unionId 等等一些信息,把这些信息存入数据库中就可以。代码如下 service接口
import com.alibaba.fastjson.JSONObject;
import com.minapp.management.entity.TdSysMpStaffLogin;
import com.baomidou.mybatisplus.extension.service.IService;
/**
*
* 服务类
*
*
* @author XQD
* @since 2021-01-04
*/
public interface TdSysMpStaffLoginService extends IService
// 关注公众号事件调用的接口
public void subscribeMPUserInfo(String openId);
// 取消关注公众号事件调用的接口
public void unsubscribeMPUserInfo(String openId);
// 发送模板信息接口
boolean sendTemplateMsg(String staffIds, String messageType, JSONObject param);
}
ServiceImpl实现类
@Resource
private ObjectMapper objectMapper;
/**
* @Description: 订阅的公众号用户信息存入数据库
* @Param: [projectId, openId]
* @return: void
* @Author: XQD
* @Date:2021/1/4 17:45
*/
@Override
public void subscribeMPUserInfo(String openId) {
String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+getAccessToken()+"&openid="+openId+"&lang=zh_CN";
String mpUserInfo = HttpClientUtil.get(url);
Map
try {
map = objectMapper.readValue(mpUserInfo, Map.class);
} catch (IOException e) {
log.error("公众号异常通知-获取用户信息转化异常", e);
}
TdSysMpStaffLogin mpStaffLogin = new TdSysMpStaffLogin();
mpStaffLogin.setId(GeneratorIDUtil.generatorId())
.setOpenId(openId)
.setProjectId(projectId)
.setNickName((String) map.get("nickname"))
.setHeadImageUrl((String) map.get("headimgurl"))
.setSex((Integer) map.get("sex"))
.setSubscribe((Integer) map.get("subscribe"))
.setUnionId((String) map.get("unionid"))
.setCountry((String) map.get("country"))
.setProvince((String) map.get("province"))
.setCity((String) map.get("city"))
.setSubscribeScene((String) map.get("subscribe_scene"));
UpdateWrapper
mpStaffLoginUpdateWrapper.set("subscribe",1)
.eq("open_id",openId);
//数据库操作
if (mpStaffLoginService.saveOrUpdate(mpStaffLogin, mpStaffLoginUpdateWrapper)){
log.info("\n关注的用户信息添加成功 openId = {}", openId);
}else {
log.info("\n关注的用户信息添加失败 openId = {}", openId);
}
}
/**
* @Description: 取消订阅用户
* @Param: [projectId, openId]
* @return: void
* @Author: XQD
* @Date:2021/1/4 17:45
*/
@Override
public void unsubscribeMPUserInfo(String openId) {
// 数据库操作,就是把关注的状态改一下
UpdateWrapper
mpStaffLoginUpdateWrapper.set("subscribe",0)
.eq("open_id",openId);
if (mpStaffLoginService.update(mpStaffLoginUpdateWrapper)){
log.info("\n取消关注的用户操作成功 openId = {}", openId);
}else {
log.info("\n取消关注的用户操作失败 openId = {}", openId);
}
}
HttpClientUtil类就是用来调用外部接口的,就不贴出来了,