|
@@ -3,16 +3,26 @@ package com.dayou.service.impl;
|
|
|
import cn.dev33.satoken.stp.SaLoginModel;
|
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
-import cn.hutool.http.Header;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
-import com.dayou.dto.LoginByOADTO;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
|
+import com.dayou.auth.RoleEnum;
|
|
|
+import com.dayou.auth.extra.LoginUserInfo;
|
|
|
+import com.dayou.common.BaseEntity;
|
|
|
+import com.dayou.dto.OALoginDTO;
|
|
|
import com.dayou.entity.User;
|
|
|
+import com.dayou.entity.UserPost;
|
|
|
import com.dayou.mapper.UserMapper;
|
|
|
+import com.dayou.mapper.UserPostMapper;
|
|
|
import com.dayou.service.AuthService;
|
|
|
import com.dayou.vo.UserVO;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
@Service
|
|
|
public class AuthServiceImpl implements AuthService {
|
|
@@ -23,41 +33,74 @@ public class AuthServiceImpl implements AuthService {
|
|
|
@Autowired
|
|
|
private UserMapper userMapper;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private UserPostMapper userPostMapper;
|
|
|
+
|
|
|
/**
|
|
|
* 通过oa系统的信息登录到生产力平台,并同步用户信息
|
|
|
- * @param loginByOADTO 登录信息
|
|
|
+ * @param oaToken oa系统token
|
|
|
* @return UserVO
|
|
|
*/
|
|
|
@Override
|
|
|
- public UserVO loginByOAInfo(LoginByOADTO loginByOADTO) {
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public UserVO loginByOAInfo(String oaToken) {
|
|
|
|
|
|
- // 带上oa系统的token,调用oa系统判断token合法性的接口(返回Boolean)
|
|
|
- // TODO:改成OA返回用户信息,生产力平台保存,只需要前端提交token
|
|
|
- String result = HttpRequest.post(oaApiUrl + "/auth/checkOaToken?token=" + loginByOADTO.getOaToken())
|
|
|
+ // 带上oa系统的token,调用oa系统判断token合法性以及获取用户信息
|
|
|
+ String result = HttpRequest.post(oaApiUrl + "/auth/checkOaToken?token=" + oaToken)
|
|
|
.timeout(20000)//超时,毫秒
|
|
|
.execute().body();
|
|
|
- boolean tokenStatus = Boolean.parseBoolean(result);
|
|
|
-
|
|
|
- // 判断token是否过期(true:过期或无效,false:未过期)
|
|
|
- if (tokenStatus){
|
|
|
- // 同步oa的user信息
|
|
|
- User user = BeanUtil.copyProperties(loginByOADTO, User.class);
|
|
|
- userMapper.insert(user);
|
|
|
-
|
|
|
- // 在生产力平台登录
|
|
|
- StpUtil.login(loginByOADTO.getUserOaId(), new SaLoginModel()
|
|
|
- .setDevice("PC") // 此次登录的客户端设备类型, 用于[同端互斥登录]时指定此次登录的设备类型
|
|
|
- .setIsLastingCookie(true) // 是否为持久Cookie(临时Cookie在浏览器关闭时会自动删除,持久Cookie在重新打开后依然存在)
|
|
|
- .setToken(loginByOADTO.getOaToken()) // 指定此次登录生成的Token(使用oa系统的token)
|
|
|
- .setIsWriteHeader(false) // 是否在登录后将 Token 写入到响应头
|
|
|
- .setActiveTimeout(86400) // 指定此次登录token的最低活跃频率, 单位:秒,设置此参数需要在配置文件打开dynamicActiveTimeout=true
|
|
|
- );
|
|
|
- UserVO userVO = BeanUtil.copyProperties(user, UserVO.class);
|
|
|
- userVO.setTokenName(StpUtil.getTokenName());
|
|
|
- userVO.setTokenValue(StpUtil.getTokenValue());
|
|
|
- return userVO;
|
|
|
- }else {
|
|
|
- return null;
|
|
|
+ JSONObject jsonObject = JSONUtil.parseObj(result);
|
|
|
+ OALoginDTO oaLoginDTO = jsonObject.toBean(OALoginDTO.class);
|
|
|
+
|
|
|
+ // 判断是否拿到了登录信息
|
|
|
+ if (ObjectUtil.isNotNull(oaLoginDTO)){
|
|
|
+
|
|
|
+ // 判断生产力平台的该用户是否存在或最后更新信息的时间是否在oa系统之前
|
|
|
+ User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUserOaId, oaLoginDTO.getUserOaId()).eq(BaseEntity::getDeleteStatus, false));
|
|
|
+ if (ObjectUtil.isNull(user)) {
|
|
|
+ // 新增oa的user信息到生产力平台
|
|
|
+ setUserInfo(oaLoginDTO, user);
|
|
|
+ userMapper.insert(user);
|
|
|
+ UserPost userPost = new UserPost().setUserId(user.getId());
|
|
|
+ setUserPostInfo(oaLoginDTO,userPost);
|
|
|
+ userPostMapper.insert(userPost);
|
|
|
+ }else if (user.getUpdateTime().before(oaLoginDTO.getLastUpdateTime())){
|
|
|
+ // 更新oa的user信息到生产力平台
|
|
|
+ setUserInfo(oaLoginDTO, user);
|
|
|
+ userMapper.updateById(user);
|
|
|
+ UserPost userPost = new UserPost().setUserId(user.getId());
|
|
|
+ setUserPostInfo(oaLoginDTO,userPost);
|
|
|
+ userPostMapper.update(userPost, new UpdateWrapper<UserPost>().eq("user_oa_id", oaLoginDTO.getUserOaId()).eq("delete_status", false));
|
|
|
+ }
|
|
|
+ UserVO userVO = userMapper.getUserInfoById(user.getId());
|
|
|
+ if (ObjectUtil.isNotNull(userVO)){
|
|
|
+ // 在生产力平台登录
|
|
|
+ StpUtil.login(userVO.getId(), new SaLoginModel()
|
|
|
+ .setDevice("PC") // 此次登录的客户端设备类型, 用于[同端互斥登录]时指定此次登录的设备类型
|
|
|
+ .setIsLastingCookie(true) // 是否为持久Cookie(临时Cookie在浏览器关闭时会自动删除,持久Cookie在重新打开后依然存在)
|
|
|
+ .setToken(oaToken) // 指定此次登录生成的Token(使用oa系统的token)
|
|
|
+ .setExtra(userVO.getId().toString(), BeanUtil.toBean(userVO, LoginUserInfo.class)) // 设置额外登录信息
|
|
|
+ );
|
|
|
+ return userVO;
|
|
|
+ }else {
|
|
|
+ throw new RuntimeException("登录失败!Token无效!");
|
|
|
+ }
|
|
|
}
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void setUserInfo(OALoginDTO oaLoginDTO, User user) {
|
|
|
+ user.setUserOaId(oaLoginDTO.getUserOaId())
|
|
|
+ .setName(oaLoginDTO.getName())
|
|
|
+ .setStaffNo(oaLoginDTO.getStaffNo());
|
|
|
+ }
|
|
|
+
|
|
|
+ private void setUserPostInfo(OALoginDTO oaLoginDTO, UserPost userPost){
|
|
|
+ String jsonDepartmentName = JSONUtil.toJsonStr(oaLoginDTO.getDepartmentNameList());
|
|
|
+ String jsonPostName = JSONUtil.toJsonStr(oaLoginDTO.getPostNameList());
|
|
|
+ userPost.setUserId(userPost.getUserId())
|
|
|
+ .setDepartmentName(jsonDepartmentName)
|
|
|
+ .setPostName(jsonPostName)
|
|
|
+ .setRole(jsonPostName.contains("超级管理员岗") ? RoleEnum.ADMIN.getCode() : RoleEnum.USER.getCode());
|
|
|
}
|
|
|
}
|