package com.ycl.service.auth.impl; import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson.JSONObject; import com.ycl.common.dingding.DingCommon; import com.ycl.entity.depart.UmsDepart; import com.ycl.entity.dingding.DingUserInfo; import com.ycl.entity.user.UmsAdmin; import com.ycl.entity.user.UmsDepartManage; import com.ycl.exception.ApiException; import com.ycl.service.depart.UmsDepartService; import com.ycl.service.user.UmsAdminService; import com.ycl.service.user.UmsDepartManageService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.Objects; @Slf4j @Service public class AuthService implements com.ycl.service.auth.AuthService { @Autowired private UmsAdminService adminService; @Autowired private PasswordEncoder passwordEncoder; @Autowired private UmsDepartService departService; @Autowired private DingCommon dingCommon; @Autowired private UmsDepartManageService departManageService; @Override @Transactional(rollbackFor = Exception.class) public String dingLogin(DingUserInfo dingUserInfo) { String openid = dingUserInfo.getOpenid(); //openid UmsAdmin dingUser = adminService.getByOpenid(openid); //根据openid查询 UmsAdmin byId = adminService.getById(dingUserInfo.getUserId()); String token = null; if (ObjectUtil.isNotEmpty(dingUser)) { //钉钉已绑定 token = adminService.getOAuthToken(dingUser.getUsername()); //调用系统登录接口 } else if (Objects.nonNull(byId)) { byId.setOpenid(openid); adminService.updateById(byId); token = adminService.getOAuthToken(byId.getUsername()); //调用系统登录接口 } else { //钉钉未绑定 //按照id查询 UmsAdmin userParam = new UmsAdmin(); userParam.setId(dingUserInfo.getUserId()); userParam.setNickName(dingUserInfo.getNickNameCn()); //昵称 userParam.setUsername(dingUserInfo.getEmployeeName()); //姓名 userParam.setSex(dingUserInfo.getEmpGender().byteValue()); //性别 userParam.setOpenid(openid); //openid userParam.setIsDy(Byte.parseByte("0")); userParam.setMobile(""); String encodePassword = passwordEncoder.encode("123456"); userParam.setPassword(encodePassword); //密码 userParam.setStatus(1); //状态 adminService.save(userParam); dingCommon.getEmpInfoByCode(dingUserInfo.getEmployeeCode()); //查询部门是否存在, UmsDepart depart = departService.getById(dingUserInfo.getOrgId()); //员工信息,关联组织 if (Objects.nonNull(depart)) { UmsDepartManage departManage = new UmsDepartManage(); departManage.setDepartId(depart.getId()); departManage.setUserId(dingUserInfo.getUserId()); departManage.setIsLeader(0); departManageService.save(departManage); } //根据openid查询 dingUser = adminService.getByOpenid(openid); if (ObjectUtil.isEmpty(dingUser)) { log.info("【auth用户认证:】_AUTH_DING_USER_" + JSONObject.toJSONString(userParam)); throw new ApiException("auth用户认证失败"); } token = null; //钉钉已绑定 if (ObjectUtil.isNotEmpty(dingUser)) { token = adminService.getOAuthToken(dingUser.getUsername()); //调用系统登录接口 } } return token; } }