package com.sztzjy.linkCommerce.controller.stu; import cn.hutool.core.util.IdUtil; import cn.hutool.system.UserInfo; import com.sztzjy.linkCommerce.annotation.AnonymousAccess; import com.sztzjy.linkCommerce.config.Constant; import com.sztzjy.linkCommerce.config.exception.UnAuthorizedException; import com.sztzjy.linkCommerce.config.security.JwtUser; import com.sztzjy.linkCommerce.config.security.LoginResult; import com.sztzjy.linkCommerce.config.security.TokenProvider; import com.sztzjy.linkCommerce.entity.SchoolClass; import com.sztzjy.linkCommerce.entity.SchoolClassExample; import com.sztzjy.linkCommerce.entity.Userinfo; import com.sztzjy.linkCommerce.entity.UserinfoExample; import com.sztzjy.linkCommerce.mapper.SchoolClassMapper; import com.sztzjy.linkCommerce.mapper.UserinfoMapper; import com.sztzjy.linkCommerce.service.SchoolService; import com.sztzjy.linkCommerce.service.UserInfoService; import com.sztzjy.linkCommerce.util.ResultDataEntity; import com.sztzjy.linkCommerce.util.ResultEntity; import com.sztzjy.linkCommerce.util.RsaUtil; import com.sztzjy.linkCommerce.util.TzApi; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.CompletableFuture; @RestController @RequestMapping("/api/user") @Api(tags = "yz-登录相关") public class LoginController { @Resource private AuthenticationManagerBuilder authenticationManagerBuilder; @Autowired UserinfoMapper userinfoMapper; @Autowired SchoolClassMapper schoolClassMapper; @PostMapping("/login") @ApiOperation("登录接口") @AnonymousAccess public ResultEntity login(@RequestParam(required = false) String passwordEncode, @RequestParam(required = false) String userName, HttpServletRequest request, @RequestParam(required = false) String TOKEN) { JwtUser jwtUser; String passWord; if (StringUtils.isBlank(TOKEN)) { try { passWord = RsaUtil.decryptByPrivateKey(passwordEncode); } catch (Exception e) { return new ResultEntity(HttpStatus.BAD_REQUEST, "密码错误"); } try{ UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userName, passWord); Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken); SecurityContextHolder.getContext().setAuthentication(authentication); jwtUser = (JwtUser) authentication.getPrincipal(); String token = TokenProvider.createToken(jwtUser); // 1、子系统直接登录 UserinfoExample stuUserExample = new UserinfoExample(); stuUserExample.createCriteria().andUsernameEqualTo(userName).andPasswordEqualTo(passWord); List userinfos = userinfoMapper.selectByExample(stuUserExample); //不存在返回错误,正确放行 if (userinfos.isEmpty()) { return new ResultEntity(HttpStatus.BAD_REQUEST, "密码错误"); } // 保存用户的登录信息 Map map = new HashMap<>(); Userinfo user = userinfos.get(0); map.put("name", user.getName()); map.put("username", user.getUsername()); map.put("roleId", String.valueOf(user.getRole())); map.put("schoolId", user.getSchoolId()); map.put("classId", user.getSchoolClassId()); if(user.getRole()==4){ SchoolClass schoolClass = schoolClassMapper.selectByPrimaryKey(user.getSchoolClassId()); if (schoolClass == null) { map.put("className",null); }else { map.put("className",schoolClass.getClassName()); } } map.put("userId", user.getUserId()); map.put("token", token); return new ResultEntity(HttpStatus.OK, map); }catch (Exception e){ return new ResultEntity(HttpStatus.BAD_REQUEST, "账号不存在"); } }else { if (!org.springframework.util.StringUtils.hasText(userName) && !org.springframework.util.StringUtils.hasText(passwordEncode) && !org.springframework.util.StringUtils.hasText(TOKEN)) { throw new IllegalArgumentException("请提供登录凭据"); } if (org.springframework.util.StringUtils.hasText(TOKEN)) { jwtUser = TokenProvider.getJWTUserByZhiYun(TOKEN); if (jwtUser == null) { throw new UnAuthorizedException("token 无效!"); } passWord = jwtUser.getPassword(); userName = jwtUser.getUsername(); if (StringUtils.isBlank(userName)){ userName = jwtUser.getUsername(); } if (StringUtils.isBlank(userName)){ userName = jwtUser.getName(); } if (!org.springframework.util.StringUtils.hasText(passWord)) { throw new UnAuthorizedException("token 中缺少用户密码信息!"); } try { jwtUser = TzApi.foreignExchangeTradingLogin(userName, passWord, Constant.SYSTEMNAME); if (jwtUser == null) { throw new UnAuthorizedException("登录失败,用户不存在!"); } if (jwtUser.getRoleId()==4) { Date authorizeTime = jwtUser.getAuthorizeTime(); Date authorizeEndTime = jwtUser.getAuthorizeEndTime(); if (authorizeTime == null || authorizeEndTime == null) { throw new UnAuthorizedException("系统未授权!"); } Date now = new Date(); if (now.before(authorizeTime) || now.after(authorizeEndTime)) { throw new UnAuthorizedException("系统授权已过期!"); } } Authentication authentication = new UsernamePasswordAuthenticationToken(jwtUser, null, jwtUser.getAuthorities()); SecurityContextHolder.getContext().setAuthentication(authentication); String token = TokenProvider.createToken(jwtUser); UserinfoExample stuUserExample = new UserinfoExample(); stuUserExample.createCriteria().andUsernameEqualTo(userName); List userinfos = userinfoMapper.selectByExample(stuUserExample); //不存在返回错误,正确放行 if (userinfos.isEmpty()) { return new ResultEntity(HttpStatus.BAD_REQUEST, "密码错误"); } Map map = new HashMap<>(); Userinfo user = userinfos.get(0); map.put("name", user.getName()); map.put("username", user.getUsername()); map.put("roleId", String.valueOf(user.getRole())); map.put("schoolId", user.getSchoolId()); map.put("classId", user.getSchoolClassId()); if(user.getRole()==4){ SchoolClass schoolClass = schoolClassMapper.selectByPrimaryKey(user.getSchoolClassId()); if (schoolClass == null) { map.put("className",null); }else { map.put("className",schoolClass.getClassName()); } } map.put("userId", user.getUserId()); map.put("token", token); return new ResultEntity(HttpStatus.OK, map); } catch (UnAuthorizedException e) { throw e; // 抛出授权类异常,保留原语义 } catch (Exception e) { // 抛出其他登录异常,比如远程接口失败、系统错误等 throw new RuntimeException("系统登录校验失败", e); } } throw new RuntimeException("系统登录校验失败"); } } @PostMapping("/getClassList") @ApiOperation("获取班级列表") @AnonymousAccess public Map getClassList(@RequestParam String schoolMajorId){ Map map=new HashMap<>(); SchoolClassExample schoolClassExample = new SchoolClassExample(); schoolClassExample.createCriteria().andSchoolMajorIdEqualTo(schoolMajorId); List schoolClasses = schoolClassMapper.selectByExample(schoolClassExample); for (int i = 0; i < schoolClasses.size(); i++) { SchoolClass schoolClass = schoolClasses.get(i); map.put(schoolClass.getSchoolClassId(),schoolClass.getClassName()); } return map; } }