You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
udi-spms-java/src/main/java/com/glxp/api/controller/auth/LoginController.java

362 lines
15 KiB
Java

package com.glxp.api.controller.auth;
import cn.hutool.core.util.StrUtil;
import com.glxp.api.annotation.AuthRuleAnnotation;
import com.glxp.api.annotation.Log;
import com.glxp.api.constant.BusinessType;
import com.glxp.api.constant.Constant;
import com.glxp.api.controller.BaseController;
import com.glxp.api.dao.auth.AuthLicenseDao;
import com.glxp.api.dao.auth.InvWarehouseDao;
import com.glxp.api.entity.auth.*;
import com.glxp.api.entity.system.CompanyEntity;
import com.glxp.api.entity.system.SysPdaKeyEntity;
import com.glxp.api.exception.JsonException;
import com.glxp.api.req.auth.FilterInvWarehouseRequest;
import com.glxp.api.req.auth.LoginRequest;
import com.glxp.api.req.auth.PCLoginRequest;
import com.glxp.api.req.auth.UpdatePasswordRequest;
import com.glxp.api.res.auth.LoginResponse;
import com.glxp.api.res.auth.LoginUserInfoResponse;
import com.glxp.api.service.system.CompanyService;
import com.glxp.api.service.monitor.LogininforService;
import com.glxp.api.common.enums.ResultEnum;
import com.glxp.api.common.res.BaseResponse;
import com.glxp.api.common.util.ResultVOUtils;
import com.glxp.api.service.auth.*;
import com.glxp.api.util.*;
import com.glxp.api.vo.inv.InvWarehouseTreeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.*;
/**
* 登录相关
*/
@RestController
@Slf4j
public class LoginController extends BaseController {
@Resource
private AuthAdminService authAdminService;
@Resource
private LogininforService logininforService;
@Resource
private AuthCheckService authCheckService;
@Resource
SysPdaKeyService sysPdaKeyService;
@Resource
SysPermissionService sysPermissionService;
@Resource
private CompanyService companyService;
@Resource
private AuthLicenseDao authLicenseDao;
@Resource
private UserRegisterService userRegisterService;
/**
* 用户登录
*
* @return
*/
@PostMapping(value = "/login")
public BaseResponse index(@RequestBody @Valid LoginRequest loginRequest,
BindingResult bindingResult,
HttpServletRequest request) {
if (bindingResult.hasErrors()) {
return ResultVOUtils.error(ResultEnum.PARAM_VERIFY_FALL, bindingResult.getFieldError().getDefaultMessage());
}
AuthAdmin authAdmin = authAdminService.findByUserName(loginRequest.getUsername());
if (authAdmin == null) {
UserRegisterEntity userRegisterEntity = userRegisterService.selectByUserName(loginRequest.getUsername());
if (userRegisterEntity != null) {
throw new JsonException(ResultEnum.DATA_NOT, "该账号未通过审核!");
} else {
throw new JsonException(ResultEnum.DATA_NOT, "该账号未注册!");
}
}
if (!PasswordUtils.authAdminPwd(loginRequest.getPassword()).equals(authAdmin.getPassWord())) {
throw new JsonException(ResultEnum.DATA_NOT, "用户名或密码错误");
}
if (authAdmin.getUserFlag() == 0) {
throw new JsonException(ResultEnum.DATA_NOT, "该用户已被禁用!");
}
if (StrUtil.isNotEmpty(loginRequest.getImei())) {
SysPdaKeyEntity sysPdaKeyEntity = sysPdaKeyService.findDeviceByImei(loginRequest.getImei());
if (sysPdaKeyEntity == null) {
return ResultVOUtils.error(410, "该设备未注册");
} else if (sysPdaKeyEntity.getIsCheck() == 0) {
return ResultVOUtils.error(411, "该设备登记审核中,请等待,或联系管理员");
} else if (sysPdaKeyEntity.getIsCheck() == 2) {
return ResultVOUtils.error(412, "该设备被拒绝登录,请联系管理员!");
}
}
// 更新登录状态
AuthAdmin authAdminUp = new AuthAdmin();
authAdminUp.setId(authAdmin.getId());
authAdminUp.setLastLoginTime(new Date());
authAdminUp.setLastLoginIp(IpUtils.getIpAddr(request));
authAdminService.updateAuthAdmin(authAdminUp);
// 登录成功后获取权限,这里面会设置到缓存
// authLoginService.listRuleByAdminId(authAdmin.getId());
Map<String, Object> claims = new HashMap<>();
claims.put("admin_id", authAdmin.getId());
String token = JwtUtils.createToken(claims, 86400L); // 一天后过期
Map<String, Object> map = new HashMap<>();
map.put("id", authAdmin.getId());
map.put("token", token);
map.put("time", DateUtil.getDateTime());
LoginResponse loginResponse = new LoginResponse();
loginResponse.setId(authAdmin.getId() + "");
loginResponse.setToken(token);
loginResponse.setDept(authAdmin.getLocDeptCode());
loginResponse.setDeptName(authAdmin.getDeptName());
logininforService.recordLogininfor(authAdmin.getEmployeeName(), Constant.LOGIN_SUCCESS, "登录成功!", request);
return ResultVOUtils.success(loginResponse);
}
/**
* 用户登录
*
* @return
*/
@PostMapping(value = "/pc/login")
public BaseResponse pcLogin(@RequestBody @Valid PCLoginRequest loginRequest,
BindingResult bindingResult,
HttpServletRequest request) {
if (bindingResult.hasErrors()) {
return ResultVOUtils.error(ResultEnum.PARAM_VERIFY_FALL, bindingResult.getFieldError().getDefaultMessage());
}
AuthCheckEntity authCheckEntity = authCheckService.findByMachine(loginRequest.getMachineInfo());
if (authCheckEntity == null) {
authCheckEntity = new AuthCheckEntity();
authCheckEntity.setMachineInfo(loginRequest.getMachineInfo().trim());
authCheckEntity.setCreateDate(new Date());
authCheckService.insertDevices(authCheckEntity);
throw new JsonException(ResultEnum.DATA_NOT, "该软件未注册!请联系管理员");
} else {
if (authCheckEntity.getRegisterCode() == null) {
throw new JsonException(ResultEnum.DATA_NOT, "该软件未注册!请联系管理员");
} else {
try {
String data = RsaUtils.publicKeyDecrypt(authCheckEntity.getRegisterCode(), RsaUtils.publicKey);
if (!data.equals(authCheckEntity.getMachineInfo())) {
throw new JsonException(ResultEnum.DATA_NOT, "注册码不匹配,请联系管理员!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
AuthAdmin authAdmin = authAdminService.findByUserName(loginRequest.getUsername());
if (authAdmin == null) {
throw new JsonException(ResultEnum.DATA_NOT, "用户名或密码错误");
}
if (!PasswordUtils.authAdminPwd(loginRequest.getPassword()).equals(authAdmin.getPassWord())) {
throw new JsonException(ResultEnum.DATA_NOT, "用户名或密码错误");
}
// 更新登录状态
AuthAdmin authAdminUp = new AuthAdmin();
authAdminUp.setId(authAdmin.getId());
authAdminUp.setLastLoginTime(new Date());
authAdminUp.setLastLoginIp(IpUtils.getIpAddr(request));
authAdminService.updateAuthAdmin(authAdminUp);
// 登录成功后获取权限,这里面会设置到缓存
// authLoginService.listRuleByAdminId(authAdmin.getId());
Map<String, Object> claims = new HashMap<>();
claims.put("admin_id", authAdmin.getId());
String token = JwtUtils.createToken(claims, 86400L); // 一天后过期
Map<String, Object> map = new HashMap<>();
map.put("id", authAdmin.getId());
map.put("token", token);
map.put("time", DateUtil.getDateTime());
return ResultVOUtils.success(map);
}
@Resource
DeptService deptService;
@Resource
InvWarehouseService invWarehouseService;
@AuthRuleAnnotation("")
@GetMapping("/admin/auth/login/getInv")
public BaseResponse getInv(HttpServletRequest request) {
String adminId = request.getHeader("ADMIN_ID");
Long id = Long.valueOf(adminId);
AuthAdmin authAdmin = authAdminService.findById(id);
LoginUserInfoResponse loginUserInfoResponse = new LoginUserInfoResponse();
BeanUtils.copyProperties(authAdmin, loginUserInfoResponse);
DeptEntity deptEntity = deptService.selectByCode(authAdmin.getLocDeptCode());
InvWarehouseEntity invWarehouseEntity = invWarehouseService.findByInvSubByCode(authAdmin.getLocInvCode());
loginUserInfoResponse.setLocDeptName(deptEntity.getName());
loginUserInfoResponse.setLocInvName(invWarehouseEntity.getName());
return ResultVOUtils.success(loginUserInfoResponse);
}
@Resource
ISysRoleService sysRoleService;
@AuthRuleAnnotation("")
@GetMapping("/getInfo")
public BaseResponse getUserInfo(HttpServletRequest request) {
String adminId = request.getHeader("ADMIN_ID");
Long id = Long.valueOf(adminId);
AuthAdmin authAdmin = authAdminService.findById(id);
LoginUserInfoResponse loginUserInfoResponse = new LoginUserInfoResponse();
BeanUtils.copyProperties(authAdmin, loginUserInfoResponse);
CompanyEntity companyEntity = companyService.findCompany(authAdmin.getCustomerId());
if ("110".equals(authAdmin.getCustomerId())) {
loginUserInfoResponse.setCompanyName(authAdmin.getEmployeeName());
} else {
loginUserInfoResponse.setCompanyName(companyEntity.getCompanyName());
}
loginUserInfoResponse.setLocDeptName(deptService.getInvName(loginUserInfoResponse.getLocDeptCode()));
loginUserInfoResponse.setLocInvName(invWarehouseService.getSubInvName(loginUserInfoResponse.getLocInvCode()));
// 角色集合
Set<String> roles = sysPermissionService.getRolePermission(authAdmin);
List<SysRole> sysRoles = sysRoleService.selectRolesByUserId(id);
authAdmin.setRoles(sysRoles);
// 权限集合
Set<String> permissions = sysPermissionService.getMenuPermission(authAdmin);
loginUserInfoResponse.setRoles(roles);
loginUserInfoResponse.setPermissions(permissions);
return ResultVOUtils.success(loginUserInfoResponse);
}
@Resource
ISysMenuService menuService;
@GetMapping("/spms/getRouters")
public BaseResponse getRouters() {
AuthAdmin authAdmin = getUser();
List<SysMenu> menus = menuService.selectMenuTreeByUserId(authAdmin.getId());
return ResultVOUtils.success(menuService.buildMenus(menus));
}
/**
* 登出
*
* @return
*/
@PostMapping("/admin/auth/login/out")
public BaseResponse out() {
return ResultVOUtils.success();
}
/**
* 修改密码
*
* @return
*/
@AuthRuleAnnotation("") // 需要登录验证,但是不需要权限验证时,value 值填空字符串
@PostMapping("/admin/auth/login/password")
@Log(title = "用户管理", businessType = BusinessType.UPDATE)
public BaseResponse password(@RequestBody @Valid UpdatePasswordRequest updatePasswordRequest,
BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return ResultVOUtils.error(ResultEnum.PARAM_VERIFY_FALL.getCode(),
bindingResult.getFieldError().getDefaultMessage());
}
AuthAdmin authAdmin = authAdminService.findById(updatePasswordRequest.getAdminId());
if (authAdmin == null) {
throw new JsonException(ResultEnum.DATA_NOT);
}
String oldPwd = PasswordUtils.authAdminPwd(updatePasswordRequest.getOldPassword());
// 旧密码不对
if (authAdmin.getPassWord() != null
&& !authAdmin.getPassWord().equals(oldPwd)) {
throw new JsonException(ResultEnum.DATA_NOT, "旧密码匹配失败");
}
AuthAdmin authAdminUp = new AuthAdmin();
authAdminUp.setId(authAdmin.getId());
String newPwd = PasswordUtils.authAdminPwd(updatePasswordRequest.getNewPassword());
authAdminUp.setPassWord(newPwd);
authAdmin.setLastModifyTime(new Date());
boolean b = authAdminService.updateAuthAdmin(authAdminUp);
if (b) {
return ResultVOUtils.success();
}
return ResultVOUtils.error(ResultEnum.DATA_CHANGE);
}
//------------------------------------------UDIMS接口-----------------------------------------------------------
@Autowired
private InvWarehouseDao invWarehouseDao;
/**
* 平台验证
*
* @return
*/
@ResponseBody
@PostMapping(value = "/verify")
public BaseResponse verify(@RequestBody Map<String, Object> params,
HttpServletRequest request) {
AuthAdmin authAdmin;
authAdmin = authAdminService.findByUserName(params.get("username").toString());
if (authAdmin == null) {
throw new JsonException(ResultEnum.DATA_NOT, "用户名或密码错误");
}
if (!PasswordUtils.authAdminPwd(params.get("password").toString()).equals(authAdmin.getPassWord())) {
throw new JsonException(ResultEnum.DATA_NOT, "用户名或密码错误");
}
AuthLicense authLicense = new AuthLicense();
if (params.get("appid") != null) {
authLicense = authLicenseDao.get(params.get("appid").toString());
} else {
String appid = AppUtils.getAppid();
authLicense.setId(appid);
authLicense.setAppid(appid);
authLicense.setApiKey(appid);
if (params.get("name") != null)
authLicense.setName(params.get("name").toString());
authLicense.setSecretKey(AppUtils.getSecretKey(appid));
authLicense.setCustomerId(authAdmin.getCustomerId() + "");
CompanyEntity companyEntity = companyService.findCompany(authAdmin.getCustomerId());
authLicense.setCompanyName(companyEntity.getCompanyName());
authLicense.setCreateDate(new Date());
authLicenseDao.romveByCustomerId(authAdmin.getCustomerId() + "");
authLicenseDao.save(authLicense);
}
FilterInvWarehouseRequest filterInvWarehouseRequest = new FilterInvWarehouseRequest();
filterInvWarehouseRequest.setSpUse(true);
List<InvWarehouseTreeVo> invList = invWarehouseDao.selectInvListTreeByUser(filterInvWarehouseRequest);
Map<String, Object> res = new WeakHashMap<>(2);
res.put("license", authLicense);
res.put("invList", invList);
return ResultVOUtils.success(res);
}
}