|
|
|
@ -5,11 +5,11 @@ package com.ruoyi.jjh.declaration.single.controller;
|
|
|
|
|
* @since 2024/5/22 15:50
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import cn.hutool.crypto.digest.DigestUtil;
|
|
|
|
|
import cn.hutool.http.HttpResponse;
|
|
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
|
|
import com.ruoyi.common.core.redis.RedisCache;
|
|
|
|
|
import com.ruoyi.common.exception.ServiceException;
|
|
|
|
@ -32,7 +32,10 @@ import javax.annotation.Resource;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
import java.util.Base64;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
@ -44,14 +47,13 @@ import java.util.concurrent.TimeUnit;
|
|
|
|
|
@RequestMapping("/system/singlelogin")
|
|
|
|
|
public class SingleLoginController {
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(SingleLoginController.class);
|
|
|
|
|
|
|
|
|
|
@Value("${url}")
|
|
|
|
|
private String url;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${infoUrl}")
|
|
|
|
|
private String infoUrl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${logoutUrl}")
|
|
|
|
|
private String logoutUrl;
|
|
|
|
|
|
|
|
|
@ -69,16 +71,17 @@ public class SingleLoginController {
|
|
|
|
|
@Value("${enterpriseAppId}")
|
|
|
|
|
private String enterpriseAppId;
|
|
|
|
|
|
|
|
|
|
@Value("${enterpriseAppsecret}")
|
|
|
|
|
private String enterpriseAppsecret;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 统一登陆
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
@ApiOperation("统一登陆")
|
|
|
|
|
@GetMapping("/login")
|
|
|
|
|
public void login(HttpServletResponse response) throws IOException {
|
|
|
|
|
//变成参数模式,appid 用于校验提交的来源地址,生产环境会校验,试用环境暂不校验
|
|
|
|
|
String scUrl = "clientUrl=" + URLEncoder.encode(returnUrl, "UTF-8") + "?appid="+enterpriseAppId;
|
|
|
|
|
String scUrl = "clientUrl=" + URLEncoder.encode(returnUrl, "UTF-8") + "?appid=" + enterpriseAppId;
|
|
|
|
|
//加密后的客户端地址
|
|
|
|
|
scUrl = Base64.getEncoder().encodeToString(scUrl.getBytes());
|
|
|
|
|
//最终跳转地址
|
|
|
|
@ -86,10 +89,6 @@ public class SingleLoginController {
|
|
|
|
|
// 添加请求头
|
|
|
|
|
response.setHeader("Content-Type", "application/json");
|
|
|
|
|
response.setHeader("X-Requested-With", "XMLHttpRequest");
|
|
|
|
|
// Map<String, String> stringStringMap = singleLoginService.generationHeader();
|
|
|
|
|
// response.setHeader("time",stringStringMap.get("time"));
|
|
|
|
|
// response.setHeader("appid",stringStringMap.get("appid"));
|
|
|
|
|
// response.setHeader("sign",stringStringMap.get("sign"));
|
|
|
|
|
response.sendRedirect(jumpurl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -97,16 +96,16 @@ public class SingleLoginController {
|
|
|
|
|
/**
|
|
|
|
|
* 获取用户信息
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
* @return 用户信息
|
|
|
|
|
*/
|
|
|
|
|
@ApiOperation(value = "获取用户信息",response = UserResponse.class)
|
|
|
|
|
@ApiOperation(value = "获取用户信息", response = UserResponse.class)
|
|
|
|
|
@GetMapping("/getInfo")
|
|
|
|
|
public AjaxResult getInfo(@RequestParam("clientToken") String clientToken) throws Exception {
|
|
|
|
|
String url = infoUrl + "?clienttoken=" + clientToken;
|
|
|
|
|
HttpResponse response = HttpUtil.createGet(url)
|
|
|
|
|
// .addHeaders(singleLoginService.generationHeader())
|
|
|
|
|
public AjaxResult getInfo(String clientToken) throws Exception {
|
|
|
|
|
HttpResponse response = HttpUtil.createGet(infoUrl)
|
|
|
|
|
.form("clienttoken", clientToken)
|
|
|
|
|
// .addHeaders(generationHeader())
|
|
|
|
|
.execute();
|
|
|
|
|
String responseBody = null;
|
|
|
|
|
String responseBody;
|
|
|
|
|
// 获取响应状态码
|
|
|
|
|
int statusCode = response.getStatus();
|
|
|
|
|
if (statusCode == 200) {
|
|
|
|
@ -119,30 +118,28 @@ public class SingleLoginController {
|
|
|
|
|
// 将用户信息存储到Redis中
|
|
|
|
|
String key = "user:" + clientToken;
|
|
|
|
|
//判断是法人用户
|
|
|
|
|
if("ep".equals(dataObj.get("usertype"))){
|
|
|
|
|
if ("ep".equals(dataObj.get("usertype"))) {
|
|
|
|
|
JSONObject mainobj = dataObj.getJSONObject("mainbody");
|
|
|
|
|
UserResponse res = JSONUtil.toBean(dataObj, UserResponse.class);
|
|
|
|
|
UserMainResponse req = JSONUtil.toBean(mainobj, UserMainResponse.class);
|
|
|
|
|
//如果在企业库里面找不到该企业就返回错误
|
|
|
|
|
QueryWrapper<BmsEnterpriseBasicInfo> queryWrapper = new QueryWrapper<>();
|
|
|
|
|
queryWrapper.eq("tyshxydm", req.getUscc());
|
|
|
|
|
BmsEnterpriseBasicInfo bmsEnterpriseBasicInfo = bmsEnterpriseBasicInfoMapper.selectOne(queryWrapper);
|
|
|
|
|
BmsEnterpriseBasicInfo bmsEnterpriseBasicInfo = bmsEnterpriseBasicInfoMapper.findByQytyxydm(req.getUscc());
|
|
|
|
|
Long id;
|
|
|
|
|
if (bmsEnterpriseBasicInfo == null) {
|
|
|
|
|
throw new ServiceException("该企业不在库!请稍后再试!");
|
|
|
|
|
}else {
|
|
|
|
|
} else {
|
|
|
|
|
id = bmsEnterpriseBasicInfo.getId();
|
|
|
|
|
}
|
|
|
|
|
// 设置过期时间为8小时
|
|
|
|
|
String token = singleLoginService.singleLogin(req.getUscc(),req.getEpname(),id,"01",null);
|
|
|
|
|
String token = singleLoginService.singleLogin(req.getUscc(), req.getEpname(), id, "01", null);
|
|
|
|
|
res.setToken(token);
|
|
|
|
|
res.setMain(req);
|
|
|
|
|
redisCache.setCacheObject(key, res, expirationSeconds, TimeUnit.MINUTES);
|
|
|
|
|
|
|
|
|
|
return AjaxResult.success(res);
|
|
|
|
|
}else {
|
|
|
|
|
} else {
|
|
|
|
|
log.error(response.body());
|
|
|
|
|
throw new ServiceException("未知异常请联系管理员");
|
|
|
|
|
throw new ServiceException("该用户不是企业法人");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log.error(response.body());
|
|
|
|
@ -150,24 +147,44 @@ public class SingleLoginController {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 退出登录
|
|
|
|
|
*
|
|
|
|
|
* @param clienttoken clienttoken
|
|
|
|
|
* @return 响应类
|
|
|
|
|
*/
|
|
|
|
|
@ApiOperation(value = "退出登录")
|
|
|
|
|
@GetMapping("/enterpriseLogout")
|
|
|
|
|
public AjaxResult enterpriseLogout(@RequestParam("clienttoken") String clienttoken) {
|
|
|
|
|
public AjaxResult enterpriseLogout(@RequestParam("clienttoken") String clienttoken) {
|
|
|
|
|
String url = logoutUrl + "?clienttoken=" + clienttoken;
|
|
|
|
|
HttpResponse execute = HttpUtil.createGet(url)
|
|
|
|
|
// .addHeaders(singleLoginService.generationHeader())
|
|
|
|
|
// .addHeaders(generationHeader())
|
|
|
|
|
.execute();
|
|
|
|
|
String str;
|
|
|
|
|
// 获取响应状态码
|
|
|
|
|
int statusCode = execute.getStatus();
|
|
|
|
|
if(statusCode==200){
|
|
|
|
|
if (statusCode == 200) {
|
|
|
|
|
str = "退出成功";
|
|
|
|
|
}else {
|
|
|
|
|
} else {
|
|
|
|
|
str = "退出失败";
|
|
|
|
|
}
|
|
|
|
|
return AjaxResult.success(str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 正式环境需要的请求头
|
|
|
|
|
*
|
|
|
|
|
* @return 请求头
|
|
|
|
|
*/
|
|
|
|
|
private Map<String, String> generationHeader() {
|
|
|
|
|
// 定义时间格式化器
|
|
|
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
|
|
|
|
|
String format = LocalDateTime.now().format(formatter);
|
|
|
|
|
//正式端需要请求头
|
|
|
|
|
HashMap<String, String> headers = new HashMap<>();//存放请求头,可以存放多个请求头
|
|
|
|
|
headers.put("appid", enterpriseAppId);
|
|
|
|
|
headers.put("time", format);
|
|
|
|
|
headers.put("sign", DigestUtil.md5Hex(enterpriseAppId + enterpriseAppsecret + format));
|
|
|
|
|
return headers;
|
|
|
|
|
}
|
|
|
|
|
}
|