This commit is contained in:
zjf committed 2025-01-09 12:07:17 +08:00
1 parent a4c45192c3
commit cf4b5e561e
6 files changed
+220 -7

No files matched your search

@@ -0,0 +1 @@
com.bsd.pjcloud.thirdparty.api.feign.RemoterThirdPartyAuthService
+4
View File
@@ -13,6 +13,10 @@
<artifactId>pjcloud-thirdparty-biz</artifactId>
<dependencies>
<dependency>
<groupId>com.bsd</groupId>
<artifactId>pjcloud-upms-api</artifactId>
</dependency>
<!--必备: undertow容器-->
<dependency>
<groupId>org.springframework.boot</groupId>
@@ -1,17 +1,34 @@
package com.bsd.pjcloud.thirdparty.controller;
import com.bsd.pjcloud.admin.api.dto.UserDTO;
import com.bsd.pjcloud.admin.api.dto.UserTokenDTO;
import com.bsd.pjcloud.admin.api.entity.SysUser;
import com.bsd.pjcloud.admin.api.feign.RemoteTokenService;
import com.bsd.pjcloud.admin.api.feign.RemoteUserService;
import com.bsd.pjcloud.common.core.util.R;
import com.bsd.pjcloud.common.security.annotation.Inner;
import com.bsd.pjcloud.thirdparty.dto.SHJNSSODTO;
import com.bsd.pjcloud.thirdparty.dto.ZZDUserDTO;
import com.bsd.pjcloud.thirdparty.entity.ThirdPartyUserEntity;
import com.bsd.pjcloud.thirdparty.service.ThirdPartyOAuth2Service;
import com.bsd.pjcloud.thirdparty.service.ThirdPartyUserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.apache.seata.spring.annotation.GlobalTransactional;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequiredArgsConstructor
@@ -22,6 +39,11 @@ public class ThirdPartyOAth2Cotroller {
private final ThirdPartyOAuth2Service thirdPartyOAuth2Service;
private final RemoteUserService remoteUserService;
private final RemoteTokenService remoteTokenService;
private final ThirdPartyUserService thirdPartyUserService;
@Operation(summary = "浙政钉token" )
@@ -37,4 +59,171 @@ public class ThirdPartyOAth2Cotroller {
return R.ok(thirdPartyOAuth2Service.shjnsso(accountId,sessionId));
}
/*
*
* @param token 浙政钉前端获取的token
* @return
*/
@GetMapping("/zzdsso/{token}")
@Operation(summary = "浙政钉单点登录")
@Inner(value = false)
@GlobalTransactional
@Transactional(rollbackFor = Exception.class)
//todo 引入seata分布式事务
public R zzdsso(@PathVariable(name = "token") String token) {
//获取数字政务token通过浙政钉前端获取的token,调试阶段先写死
String json = "{" +
"\"username\": \"李宗相\"," +
"\"organizationId\": 2," +
"\"userId\": 1340," +
"\"nickName\": \"李宗相\"," +
"\"mobile\": \"18576496089\"," +
"\"regionCode\": null," +
"\"ext\": {" +
"\"employeeCode\": \"GE_ae12bdf21598498eb48bf2cf2c45a3e1\"," +
"\"organizationCode\": \"GO_63e77077d5e041789bbbe1d0d20bcf41\"," +
"\"nickNameCn\": \"李宗相\"," +
"\"openid\": \"1b51f0e11fc73b8adddcbaf7982daab6\"," +
"\"realmId\": \"196729\"," +
"\"mobile\": null" +
"}" +
"}";
ObjectMapper objectMapper = new ObjectMapper();
ZZDUserDTO zzdUserDTO;
try {
zzdUserDTO = objectMapper.readValue(json, ZZDUserDTO.class);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
//暂时先注释,模拟一下就行
// R zzdsso = remoterThirdPartyAuthService.zzdsso(token);
// ZZDUserDTO zzdUserDTO = (ZZDUserDTO) zzdsso.getData();
ZZDUserDTO.Ext ext = zzdUserDTO.getExt();
String employeeCode = ext.getEmployeeCode();
String mobile = zzdUserDTO.getMobile();
//两种方式,手机号+员工钉钉号,查到相同就视为同一个人,然后登录
//先系统表里面查看是否有,在第三方数据库表里面看下是否有
//1.根据手机号或者钉钉号先查询是否有这个人
R<List<SysUser>> userByMobileOrEmployeeCode = remoteUserService.getUserByMobileOrEmployeeCode(employeeCode, mobile);
List<SysUser> sysUserList = userByMobileOrEmployeeCode.getData();
if(!CollectionUtils.isEmpty(sysUserList)){
//登录成功生成token
UserTokenDTO userTokenDTO = new UserTokenDTO();
userTokenDTO.setSysUser(sysUserList.get(0));
remoteTokenService.generateToken(userTokenDTO);
}
//2.根据浙政钉的员工钉钉号查询是否有这个人
List<ThirdPartyUserEntity> thirdPartyUserEntityList = thirdPartyUserService.getUserByEmployeeCode(employeeCode);
UserDTO userDTO = new UserDTO();
//写死,新加入到部门表的全部归类到其他部门里面,不按照浙政钉的部门
userDTO.setDeptId(1865990541445263361L);
userDTO.setUsername(zzdUserDTO.getUsername());
userDTO.setName(zzdUserDTO.getUsername());
userDTO.setNickname(zzdUserDTO.getUsername());
//浙里办用户
userDTO.setSystemType("1");
//取上面的手机号
userDTO.setPhone(mobile);
userDTO.setDingtalkEmployeeCode(employeeCode);
//随便写死给个密码
userDTO.setPassword("pj"+mobile);
if(!CollectionUtils.isEmpty(thirdPartyUserEntityList)){
//todo 这一步现在是没用的,这个最终要考虑是否通过浙政钉里面的人员来限制移动端的使用用户来决定
//第三方数据库里面有
//这里只在第三方库里查到,所以要对系统用户表进行新增当前用户,插入一条信息进去
R<Boolean> booleanR = remoteUserService.registerUser(userDTO);
//通过唯一手机号查询到刚注册的用户信息,生成token
R<List<SysUser>> userByMobile=remoteUserService.getUserByMobile(mobile);
SysUser sysUser = userByMobile.getData().get(0);
UserTokenDTO userTokenDTO = new UserTokenDTO();
userTokenDTO.setSysUser(sysUser);
//登录成功,根据新注册的用户信息返回token
remoteTokenService.generateToken(userTokenDTO);
}
//两个表都没查到人员不能够使用移动端的系统
// log.info("用户不存在,尝试创建用户");
R<Boolean> booleanR = remoteUserService.registerUser(userDTO);
//通过唯一手机号查询到刚注册的用户信息,生成token
R<List<SysUser>> userByMobile=remoteUserService.getUserByMobile(mobile);
SysUser sysUser = userByMobile.getData().get(0);
// int i=8/0;
UserTokenDTO userTokenDTO = new UserTokenDTO();
userTokenDTO.setSysUser(sysUser);
//登录成功,根据新注册的用户信息返回token
return remoteTokenService.generateToken(userTokenDTO);
}
/**
* 诗画sdk获取参数
*
* @param
* @return
*/
@PostMapping("/shjnsso")
@Inner(value = false)
@Operation(summary = "诗画江南单点登录")
public R shjnsso(@RequestBody @Valid SHJNSSODTO shjnsso) {
ZZDUserDTO zzdUserDTO = thirdPartyOAuth2Service.shjnsso(shjnsso.getAccountId(), shjnsso.getSessionId());
ZZDUserDTO.Ext ext = zzdUserDTO.getExt();
String mobile = zzdUserDTO.getMobile();
R<List<SysUser>> rListSysUser = remoteUserService.getUserByMobileOrEmployeeCode(null, mobile);
List<SysUser> sysUsers = rListSysUser.getData();
if(!CollectionUtils.isEmpty(sysUsers)){
//登录成功生成token
UserTokenDTO userTokenDTO = new UserTokenDTO();
userTokenDTO.setSysUser(sysUsers.get(0));
remoteTokenService.generateToken(userTokenDTO);
}
// log.info("用户不存在,尝试创建用户");
// CreateUserDto createUserDto = new CreateUserDto();
// createUserDto.setUsername(zzdUserDTO.getUsername());
// createUserDto.setMobile(mobile);
// createUserDto.setNickname(zzdUserDTO.getNickName());
// createUserDto.setAvatar(ext.getHeadImgUrl());
// //todo 换成枚举
// //诗画用户
// createUserDto.setUserType("2");
// AdminUserRespDTO adminUserRespDTONew=adminUserApi.createSysUser(createUserDto);
// log.info("创建用户成功,用户id为:{}",adminUserRespDTONew.getId());
// return createTokenAfterLoginSuccess(adminUserRespDTONew.getId(), mobile, LoginLogTypeEnum.LOGIN_SSO);
return R.failed("不存在用户");
}
@Inner(value = false)
@GetMapping("/getAuthToken/{token}")
public R info(@PathVariable(name = "token") String token) {
//调用第三方获取基础信息与数据库进行匹配
// 拼接请求参数
UserTokenDTO userTokenDTO = new UserTokenDTO();
userTokenDTO.setUsername("admin"); // 只需要用户名,就可以生成 token
userTokenDTO.setAuthorities(List.of("sys_user_add")); // 权限列表的字符串,能调用的接口范围
// feign 调用 auth 模块生成 token
R<Map> authenticationTokenR = remoteTokenService.generateToken(userTokenDTO);
// // 客户端信息
// RegisteredClient registeredClient = data.getRegisteredClient();
Object accessToken = authenticationTokenR.getData().get("accessToken");
Object refreshToken = authenticationTokenR.getData().get("refreshToken");
Map<String, Object> stringObjectHashMap = new HashMap<>();
stringObjectHashMap.put("accessToken", accessToken);
stringObjectHashMap.put("refreshToken", refreshToken);
return R.ok(stringObjectHashMap);
}
}
@@ -122,9 +122,7 @@ public class ThirdPartyUserController {
@GetMapping("/getUserByEmployeeCode" )
@Inner
public R<List<ThirdPartyUserEntity>> getUserByEmployeeCode(@RequestParam("employeeCode")String employeeCode) {
LambdaQueryWrapper<ThirdPartyUserEntity> wrapper = Wrappers.lambdaQuery();
wrapper.eq(ThirdPartyUserEntity::getDingtalkEmployeeCode, employeeCode);
List<ThirdPartyUserEntity> list = thirdPartyUserService.list(wrapper);
List<ThirdPartyUserEntity> list = thirdPartyUserService.getUserByEmployeeCode(employeeCode);
return R.ok(list);
}
}
@@ -3,6 +3,8 @@ package com.bsd.pjcloud.thirdparty.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.bsd.pjcloud.thirdparty.entity.ThirdPartyUserEntity;
import java.util.List;
public interface ThirdPartyUserService extends IService<ThirdPartyUserEntity> {
/**
@@ -10,4 +12,11 @@ public interface ThirdPartyUserService extends IService<ThirdPartyUserEntity> {
*/
void truncateTable();
/**
* 根据员工编号查询用户信息
* @param employeeCode
* @return
*/
List<ThirdPartyUserEntity> getUserByEmployeeCode(String employeeCode);
}
@@ -1,10 +1,15 @@
package com.bsd.pjcloud.thirdparty.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bsd.pjcloud.thirdparty.entity.ThirdPartyUserEntity;
import com.bsd.pjcloud.thirdparty.mapper.ThirdPartyUserMapper;
import com.bsd.pjcloud.thirdparty.service.ThirdPartyUserService;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 浙政钉用户表
*
@@ -18,4 +23,11 @@ public class ThirdPartyUserServiceImpl extends ServiceImpl<ThirdPartyUserMapper,
baseMapper.truncateTable();
}
@Override
public List<ThirdPartyUserEntity> getUserByEmployeeCode(String employeeCode) {
LambdaQueryWrapper<ThirdPartyUserEntity> wrapper = Wrappers.lambdaQuery();
wrapper.eq(ThirdPartyUserEntity::getDingtalkEmployeeCode, employeeCode);
return baseMapper.selectList(wrapper);
}
}