添加微信登录获取信息接口

This commit is contained in:
yangjun 2025-05-21 13:43:12 +08:00
parent ad522cd293
commit 1e9648225b
12 changed files with 286 additions and 4 deletions

View File

@ -57,6 +57,11 @@ public class H5ApiAdvisoryInfoController extends JeecgController<NuBizAdvisoryIn
return Result.OK(nuBizAdvisoryInfo);
}
/**
* 获取是否有注册信息
* @param openId
* @return
*/
@GetMapping(value = "/queryByOpenId")
public Result<NuBizAdvisoryInfo> queryByOpenId(@RequestParam(name="openId",required=true) String openId) {
QueryWrapper<NuBizAdvisoryInfo> queryWrapper = new QueryWrapper<>();
@ -97,4 +102,17 @@ public class H5ApiAdvisoryInfoController extends JeecgController<NuBizAdvisoryIn
}
return Result.OK(nuBizAdvisoryInfo);
}
/**
* 获取微信信息
* @param openId
* @param wechatName
* @return
*/
@GetMapping(value = "/queryWeixinInfo")
public Result<NuBizAdvisoryInfo> queryByOpenId(@RequestParam(name="openId",required=true) String openId,@RequestParam(name="wechatName",required=true) String wechatName) {
NuBizAdvisoryInfo nuBizAdvisoryInfo = nuBizAdvisoryInfoService.queryWeixinInfo(openId,wechatName);
return Result.OK(nuBizAdvisoryInfo);
}
}

View File

@ -11,4 +11,5 @@ import com.baomidou.mybatisplus.extension.service.IService;
*/
public interface INuBizAdvisoryInfoService extends IService<NuBizAdvisoryInfo> {
NuBizAdvisoryInfo queryWeixinInfo(String openId, String wechatName);
}

View File

@ -1,5 +1,6 @@
package com.nu.modules.NuBizAdvisoryInfo.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nu.modules.NuBizAdvisoryInfo.entity.NuBizAdvisoryInfo;
import com.nu.modules.NuBizAdvisoryInfo.mapper.NuBizAdvisoryInfoMapper;
import com.nu.modules.NuBizAdvisoryInfo.service.INuBizAdvisoryInfoService;
@ -16,4 +17,19 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@Service
public class NuBizAdvisoryInfoServiceImpl extends ServiceImpl<NuBizAdvisoryInfoMapper, NuBizAdvisoryInfo> implements INuBizAdvisoryInfoService {
@Override
public NuBizAdvisoryInfo queryWeixinInfo(String openId, String wechatName) {
QueryWrapper<NuBizAdvisoryInfo> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("open_id",openId);
queryWrapper.eq("wechat_name",wechatName);
NuBizAdvisoryInfo nuBizAdvisoryInfo = baseMapper.selectOne(queryWrapper);
//查询是否有微信注册信息没有则创建
if(nuBizAdvisoryInfo==null) {
nuBizAdvisoryInfo = new NuBizAdvisoryInfo();
nuBizAdvisoryInfo.setOpenId(openId);
nuBizAdvisoryInfo.setWechatName(wechatName);
baseMapper.insert(nuBizAdvisoryInfo);
}
return nuBizAdvisoryInfo;
}
}

View File

@ -0,0 +1,154 @@
package com.nu.modules.weixin.controller;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nu.modules.appversionconfig.entity.AppVersionConfig;
import com.nu.modules.appversionconfig.service.IAppVersionConfigService;
import com.nu.modules.weixin.utils.SignUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
@RestController
@RequestMapping("/weixin")
@Slf4j
public class WeixinController {
@Value("${wechat.appId}")
private String appId;
@Value("${wechat.appSecret:}")
private String appSecret;
// RestTemplate 用于发送 HTTP 请求
private final RestTemplate restTemplate;
public WeixinController(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
/**
* @description 微信公众号服务器配置校验token
* @author: liyinlong
* @date 2019-05-09 9:38
* @return
*/
@ApiOperation("微信公众号服务器配置校验token")
@RequestMapping("/checkToken")
public void checkToken(HttpServletRequest request,HttpServletResponse response){
//token验证代码段
try{
log.info("请求已到达开始校验token");
if (StringUtils.isNotBlank(request.getParameter("signature"))) {
String signature = request.getParameter("signature");
String timestamp = request.getParameter("timestamp");
String nonce = request.getParameter("nonce");
String echostr = request.getParameter("echostr");
log.info("signature[{}], timestamp[{}], nonce[{}], echostr[{}]", signature, timestamp, nonce, echostr);
if (SignUtil.checkSignature(signature, timestamp, nonce)) {
log.info("数据源为微信后台将echostr[{}]返回!", echostr);
response.getOutputStream().println(echostr);
}
}
}catch (IOException e){
log.error("校验出错");
e.printStackTrace();
}
}
@GetMapping("/auth/callback")
public String wxCallback(String code) {
// 1. 用code换token
// 2. 获取用户信息 return
return "redirect:https://www.focusnu.com/wechat/thd/#/pages/index/callback";
}
@GetMapping("/wechat/callback")
public JsonNode callback(@RequestParam("code") String code, Model model) {
String tokenUrl = String.format(
"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code",
appId, appSecret, code
);
System.out.println(tokenUrl);
RestTemplate restTemplate = new RestTemplate();
System.out.println("-------------------11111111111111111----------");
String tokenResponse = restTemplate.getForObject(tokenUrl, String.class);
System.out.println("-------------------22222222222222222----------"+tokenResponse);
JsonNode retJson = null;
ObjectMapper objectMapper = new ObjectMapper();
try {
System.out.println("-------------------33333333333333----------");
JsonNode tokenNode = objectMapper.readTree(tokenResponse);
System.out.println("-------------------444444444444444----------"+tokenNode);
String accessToken = tokenNode.get("access_token").asText();
System.out.println("-------------------55555555555555----------"+accessToken);
String openId = tokenNode.get("openid").asText();
System.out.println("-------------------666666666666666----------"+openId);
String userInfoUrl = String.format(
"https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN",
accessToken, openId
);
System.out.println("-------------------777777777777777----------"+userInfoUrl);
String userInfoResponse = restTemplate.getForObject(userInfoUrl, String.class);
System.out.println("-------------------888888888888888----------"+userInfoResponse);
JsonNode userInfoNode = objectMapper.readTree(userInfoResponse);
System.out.println("-------------------999999999999999----------"+userInfoNode);
model.addAttribute("openid", openId);
System.out.println("-------------------999999999999999----------"+openId);
model.addAttribute("nickname", userInfoNode.get("nickname").asText());
model.addAttribute("headimgurl", userInfoNode.get("headimgurl").asText());
model.addAttribute("code", code);
retJson = userInfoNode;
} catch (IOException e) {
e.printStackTrace();
model.addAttribute("openid", "Error parsing JSON response");
}
return retJson;
}
}

View File

@ -0,0 +1,66 @@
package com.nu.modules.weixin.utils;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
public class SignUtil {
private static String token = "Blxc20250520";
/**
* 校验签名
* @param signature 签名
* @param timestamp 时间戳
* @param nonce 随机数
* @return 布尔值
*/
public static boolean checkSignature(String signature,String timestamp,String nonce){
String checktext = null;
if (null != signature) {
//对ToKen,timestamp,nonce 按字典排序
String[] paramArr = new String[]{token,timestamp,nonce};
Arrays.sort(paramArr);
//将排序后的结果拼成一个字符串
String content = paramArr[0].concat(paramArr[1]).concat(paramArr[2]);
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
//对接后的字符串进行sha1加密
byte[] digest = md.digest(content.toString().getBytes());
checktext = byteToStr(digest);
} catch (NoSuchAlgorithmException e){
e.printStackTrace();
}
}
//将加密后的字符串与signature进行对比
return checktext !=null ? checktext.equals(signature.toUpperCase()) : false;
}
/**
* 将字节数组转化我16进制字符串
* @param byteArrays 字符数组
* @return 字符串
*/
private static String byteToStr(byte[] byteArrays){
String str = "";
for (int i = 0; i < byteArrays.length; i++) {
str += byteToHexStr(byteArrays[i]);
}
return str;
}
/**
* 将字节转化为十六进制字符串
* @param myByte 字节
* @return 字符串
*/
private static String byteToHexStr(byte myByte) {
char[] Digit = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
char[] tampArr = new char[2];
tampArr[0] = Digit[(myByte >>> 4) & 0X0F];
tampArr[1] = Digit[myByte & 0X0F];
String str = new String(tampArr);
return str;
}
}

View File

@ -108,6 +108,7 @@ public class ShiroConfig {
filterChainDefinitionMap.put("/sys/dict/getDictItems/**", "anon");//获取字典数据
filterChainDefinitionMap.put("/sys/sysDepart/queryInstitutionsList", "anon");//授权接口排除
filterChainDefinitionMap.put("/h5Api/nuBizAdvisoryInfo/**", "anon"); //授权接口排除
filterChainDefinitionMap.put("/weixin/**", "anon"); //授权接口排除
filterChainDefinitionMap.put("/sys/getLoginQrcode/**", "anon"); //登录二维码

View File

@ -703,6 +703,7 @@ public class SysDepartController {
map.put("id",depart.getId());
map.put("departName",depart.getDepartName());
map.put("serverUrl",depart.getServerUrl());
map.put("picUrl",depart.getPicUrl());
result.add(map);
}
return result;
@ -724,4 +725,5 @@ public class SysDepartController {
return result;
}
}

View File

@ -116,6 +116,8 @@ public class SysDepart implements Serializable {
/**各机构服务器后台接口地址*/
private String serverUrl;
/**各机构服务器后台接口地址*/
private String picUrl;
//update-begin---author:wangshuai ---date:20200308 for[JTC-119]在部门管理菜单下设置部门负责人新增字段负责人ids和旧的负责人ids
/**部门负责人的ids*/

View File

@ -87,6 +87,8 @@ public class SysDepartTreeModel implements Serializable{
private Date updateTime;
private String picUrl;
//update-begin---author:wangshuai ---date:20200308 for[JTC-119]在部门管理菜单下设置部门负责人新增字段部门负责人ids
/**部门负责人ids*/
private String directorUserIds;
@ -130,6 +132,7 @@ public class SysDepartTreeModel implements Serializable{
this.updateBy = sysDepart.getUpdateBy();
this.updateTime = sysDepart.getUpdateTime();
this.directorUserIds = sysDepart.getDirectorUserIds();
this.picUrl = sysDepart.getPicUrl();
if(0 == sysDepart.getIzLeaf()){
this.isLeaf = false;
}else{
@ -408,6 +411,14 @@ public class SysDepartTreeModel implements Serializable{
this.directorUserIds = directorUserIds;
}
public String getPicUrl() {
return picUrl;
}
public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}
/**
* 重写equals方法
*/

View File

@ -361,3 +361,9 @@ justauth:
type: default
prefix: 'demo::'
timeout: 1h
# 微信
wechat:
appId: wx8fc3e4305d2fbf0b
appSecret: 3bf3dd4ec72f591432db6b28c2c044e5

View File

@ -1,5 +1,5 @@
server:
port: 8082
port: 8080
tomcat:
max-swallow-size: -1
error:
@ -7,7 +7,7 @@ server:
include-stacktrace: ALWAYS
include-message: ALWAYS
servlet:
context-path: /nursing-unit-nacos
context-path: /nursing-unit
compression:
enabled: true
min-response-size: 1024
@ -360,3 +360,8 @@ justauth:
type: default
prefix: 'demo::'
timeout: 1h
# 微信
wechat:
appId: wx8fc3e4305d2fbf0b
appSecret: 3bf3dd4ec72f591432db6b28c2c044e5

View File

@ -454,7 +454,7 @@
<id>dev</id>
<activation>
<!--默认激活配置-->
<activeByDefault>true</activeByDefault>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<!--当前环境-->
@ -466,7 +466,7 @@
<id>uat</id>
<activation>
<!--默认激活配置-->
<activeByDefault>false</activeByDefault>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!--当前环境-->