| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- package jnpf.message.controller;
- import cn.hutool.core.util.ObjectUtil;
- import com.alibaba.fastjson.JSONObject;
- import io.swagger.v3.oas.annotations.Parameter;
- import io.swagger.v3.oas.annotations.Parameters;
- import io.swagger.v3.oas.annotations.tags.Tag;
- import io.swagger.v3.oas.annotations.Operation;
- import jakarta.servlet.http.HttpServletRequest;
- import jakarta.servlet.http.HttpServletResponse;
- import jnpf.base.entity.SysConfigEntity;
- import jnpf.base.service.SysconfigService;
- import jnpf.message.entity.AccountConfigEntity;
- import jnpf.message.entity.WechatUserEntity;
- import jnpf.message.service.AccountConfigService;
- import jnpf.message.service.WechatUserService;
- import jnpf.permission.entity.SocialsUserEntity;
- import jnpf.permission.service.SocialsUserService;
- import jnpf.permission.service.UserService;
- import jnpf.util.*;
- import jnpf.util.wxutil.mp.WXGZHWebChatUtil;
- import jnpf.util.wxutil.mp.aes.WXBizMsgCrypt;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.*;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- /**
- * 发送消息模型
- */
- @Tag(name = "微信公众号事件接收", description = "WechatOpen")
- @Controller
- @RequestMapping("/api/message/WechatOpen")
- @Slf4j
- public class WxGZHFunctionController {
- @Autowired
- private UserService userService;
- @Autowired
- private SysconfigService sysconfigService;
- @Autowired
- private AccountConfigService accountConfigService;
- @Autowired
- private SocialsUserService socialsUserService;
- @Autowired
- private WechatUserService wechatUserService;
- /**
- * 服务器基本配置链接微信公众号验证
- *
- * @param request 请求对象
- * @param response 响应对象
- * @return
- */
- @Operation(summary = "服务器基本配置链接微信公众号验证")
- @ResponseBody
- @Parameters({
- @Parameter(name = "enCode", description = "微信公众号账号配置编码", required = true)
- })
- @GetMapping("/token/{enCode}")
- public String token(@PathVariable("enCode") String enCode, HttpServletRequest request, HttpServletResponse response) throws Exception {
- //获取微信公众号账号配置
- AccountConfigEntity accountConfigEntity = accountConfigService.getInfoByEnCode(enCode,"7");
- if(ObjectUtil.isEmpty(accountConfigEntity)){
- log.info("未找到与编码相对应的微信公众号配置");
- return "";
- }
- //微信公众号服务器配置token
- String wxToken = accountConfigEntity.getAgentId();
- String signature = request.getParameter("signature");
- String echostr = XSSEscape.escape(request.getParameter("echostr"));
- String timestamp = request.getParameter("timestamp");
- String nonce = request.getParameter("nonce");
- String sortStr = WXGZHWebChatUtil.sort(wxToken,timestamp,nonce);
- String mySinStr = WXGZHWebChatUtil.shal(sortStr);
- if(StringUtil.isNotBlank(signature) && mySinStr.equals(signature)){
- return echostr;
- }else {
- log.info("微信公众号链接失败");
- return echostr;
- }
- }
- /**
- * 微信公众号事件请求
- *
- * @param request 请求对象
- * @param response 响应对象
- * @return
- * @throws Exception
- */
- @Operation(summary = "微信公众号事件请求")
- @ResponseBody
- @PostMapping("/token/{enCode}")
- /**
- * 微信公众号事件请求
- */
- public String tokenPost(@PathVariable("enCode") String enCode,HttpServletRequest request, HttpServletResponse response) throws Exception {
- log.info("微信公众号请求事件");
- //获取微信公众号账号配置
- AccountConfigEntity accountConfigEntity = accountConfigService.getInfoByEnCode(enCode,"7");
- if(ObjectUtil.isEmpty(accountConfigEntity)){
- log.info("未找到与编码相对应的微信公众号配置");
- return "";
- }
- //微信公众号服务器配置token
- String wxToken = accountConfigEntity.getAgentId();
- //微信公众号服务器配置EncodingAesKey
- String encodingAesKey = accountConfigEntity.getBearer();
- //微信公众号AppId
- String wxAppId = accountConfigEntity.getAppId();
- // 获取系统配置
- String msgSignature = request.getParameter("msg_signature");
- String encrypt_type = request.getParameter("encrypt_type");
- String signature = request.getParameter("signature");
- String echostr = XSSEscape.escape(request.getParameter("echostr"));
- String timestamp = request.getParameter("timestamp");
- String nonce = request.getParameter("nonce");
- String sortStr = WXGZHWebChatUtil.sort(wxToken,timestamp,nonce);
- String mySinStr = WXGZHWebChatUtil.shal(sortStr);
- //验签
- if(StringUtil.isNotBlank(signature) && mySinStr.equals(signature)){
- //事件信息
- Map<String ,Object> map = WXGZHWebChatUtil.parseXml(request);
- //事件信息
- String Event = String.valueOf(map.get("Event"));
- String openid = String.valueOf(map.get("FromUserName"));
- //公众号原始id
- String gzhId = String.valueOf(map.get("ToUserName"));
- if("aes".equals(encrypt_type)) {
- WXBizMsgCrypt pc = new WXBizMsgCrypt(wxToken, encodingAesKey, wxAppId);
- String encrypt = String.valueOf(map.get("Encrypt"));
- String format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%1$s]]></Encrypt></xml>";
- String fromXML = String.format(format, encrypt);
- // 获取解密后消息明文
- String result = pc.decryptMsg(msgSignature, timestamp, nonce, fromXML);
- Map<String, Object> resultMap = WXGZHWebChatUtil.xmlToMap(result);
- // 获取解密后事件信息
- Event = String.valueOf(resultMap.get("Event"));
- openid = String.valueOf(resultMap.get("FromUserName"));
- gzhId = String.valueOf(resultMap.get("ToUserName"));
- }
- String appId = accountConfigEntity.getAppId();
- String appsecret = accountConfigEntity.getAppSecret();
- String token = WXGZHWebChatUtil.getAccessToken(appId,appsecret);
- if("subscribe".equals(Event)){
- //用户关注事件
- if(StringUtil.isNotBlank(token)){
- JSONObject rstObj = WXGZHWebChatUtil.getUsetInfo(token,openid);
- if(rstObj.containsKey("unionid")){
- String unionid = rstObj.getString("unionid");
- SocialsUserEntity socialsUserEntity = socialsUserService.getInfoBySocialId(unionid,"wechat_open");
- if(socialsUserEntity==null){
- log.info("微信公众号未绑定系统账号,请登录小程序绑定");
- return "";
- }else{
- WechatUserEntity wechatUserEntity = wechatUserService.getInfoByGzhId(socialsUserEntity.getUserId(),gzhId);
- if(wechatUserEntity==null){
- WechatUserEntity entity = new WechatUserEntity();
- entity.setId(RandomUtil.uuId());
- entity.setUserId(socialsUserEntity.getUserId());
- entity.setGzhId(gzhId);
- entity.setCloseMark(1);
- entity.setCreatorTime(DateUtil.getNowDate());
- entity.setOpenId(openid);
- wechatUserService.create(entity);
- return "";
- }else {
- if(wechatUserEntity.getCloseMark()==0){
- wechatUserEntity.setCloseMark(1);
- }
- wechatUserEntity.setOpenId(openid);
- wechatUserEntity.setLastModifyTime(DateUtil.getNowDate());
- wechatUserService.update(wechatUserEntity.getId(),wechatUserEntity);
- }
- return "";
- }
- }else{
- log.info("微信公众号未绑定系统账号,请登录小程序绑定");
- return "";
- }
- }else{
- log.error("微信公众号token错误,请查看配置");
- return "";
- }
- }else if("unsubscribe".equals(Event)){
- //用户取消关注事件
- if(StringUtil.isNotBlank(token)){
- JSONObject rstObj = WXGZHWebChatUtil.getUsetInfo(token,openid);
- if(rstObj.containsKey("unionid")){
- String unionid = rstObj.getString("unionid");
- SocialsUserEntity socialsUserEntity = socialsUserService.getInfoBySocialId(unionid,"wechat_open");
- if(socialsUserEntity==null){
- log.info("微信公众号未绑定系统账号,请登录小程序绑定");
- }else{
- WechatUserEntity wechatUserEntity = wechatUserService.getInfoByGzhId(socialsUserEntity.getUserId(),gzhId);
- if(wechatUserEntity==null){
- WechatUserEntity entity = new WechatUserEntity();
- entity.setId(RandomUtil.uuId());
- entity.setUserId(socialsUserEntity.getUserId());
- entity.setGzhId(gzhId);
- entity.setCloseMark(0);
- entity.setCreatorTime(DateUtil.getNowDate());
- entity.setOpenId(openid);
- wechatUserService.create(entity);
- return "";
- }else {
- if(wechatUserEntity.getCloseMark()==1){
- wechatUserEntity.setCloseMark(0);
- }
- wechatUserEntity.setOpenId(openid);
- wechatUserEntity.setLastModifyTime(DateUtil.getNowDate());
- wechatUserService.update(wechatUserEntity.getId(),wechatUserEntity);
- return "";
- }
- }
- }else{
- log.info("微信公众号未绑定系统账号,请登录小程序绑定");
- return "";
- }
- }else{
- log.error("微信公众号token错误,请查看配置");
- return "";
- }
- return "";
- }else {
- return "";
- }
- }else {
- log.info("微信公众号事件请求失败");
- return echostr;
- }
- }
- /**
- * 获取系统配置
- */
- private Map<String, String> getSystemConfig() {
- // 获取系统配置
- List<SysConfigEntity> configList = sysconfigService.getList("SysConfig");
- Map<String, String> objModel = new HashMap<>(16);
- for (SysConfigEntity entity : configList) {
- objModel.put(entity.getFkey(), entity.getValue());
- }
- return objModel;
- }
- }
|