|
|
|
@ -0,0 +1,147 @@
|
|
|
|
|
package com.sztzjy.money_management.controller.common;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.IdUtil;
|
|
|
|
|
import com.sztzjy.money_management.annotation.AnonymousAccess;
|
|
|
|
|
import com.sztzjy.money_management.config.exception.UnAuthorizedException;
|
|
|
|
|
import com.sztzjy.money_management.config.security.JwtUser;
|
|
|
|
|
import com.sztzjy.money_management.config.security.LoginResult;
|
|
|
|
|
import com.sztzjy.money_management.config.security.TokenProvider;
|
|
|
|
|
import com.sztzjy.money_management.entity.UserInfo;
|
|
|
|
|
import com.sztzjy.money_management.entity.UserInfoExample;
|
|
|
|
|
import com.sztzjy.money_management.entity.zy.ZYUserInfo;
|
|
|
|
|
import com.sztzjy.money_management.mapper.UserInfoMapper;
|
|
|
|
|
import com.sztzjy.money_management.util.ResultEntity;
|
|
|
|
|
import com.sztzjy.money_management.util.RsaUtil;
|
|
|
|
|
import com.sztzjy.money_management.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.web.bind.annotation.*;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/api/user")
|
|
|
|
|
@Api(tags = "用户登录、账号相关")
|
|
|
|
|
public class UserController {
|
|
|
|
|
@Resource
|
|
|
|
|
private AuthenticationManagerBuilder authenticationManagerBuilder;
|
|
|
|
|
@Autowired
|
|
|
|
|
UserInfoMapper userInfoMapper;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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, "密码错误");
|
|
|
|
|
}
|
|
|
|
|
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<UserInfo> stuUsers = userInfoMapper.selectByExample(stuUserExample);
|
|
|
|
|
//不存在返回错误,正确放行
|
|
|
|
|
if (stuUsers.isEmpty()) {
|
|
|
|
|
return new ResultEntity(HttpStatus.BAD_REQUEST, "密码错误");
|
|
|
|
|
}
|
|
|
|
|
// 保存用户的登录信息
|
|
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
|
|
UserInfo user = stuUsers.get(0);
|
|
|
|
|
map.put("name", user.getName());
|
|
|
|
|
map.put("username", user.getUsername());
|
|
|
|
|
map.put("roleId", user.getRoleId());
|
|
|
|
|
map.put("schoolId", user.getSchoolId());
|
|
|
|
|
map.put("classId", user.getClassId());
|
|
|
|
|
map.put("userId", user.getUserid());
|
|
|
|
|
map.put("token", token);
|
|
|
|
|
return new ResultEntity(HttpStatus.OK, map);
|
|
|
|
|
} else { // 2、智云单点登录
|
|
|
|
|
jwtUser = TokenProvider.getJWTUserByZhiYun(TOKEN);
|
|
|
|
|
|
|
|
|
|
jwtUser = TzApi.foreignExchangeTradingLogin(jwtUser.getUsername(), jwtUser.getPassword());
|
|
|
|
|
if (jwtUser == null) {
|
|
|
|
|
throw new UnAuthorizedException("用户名或密码错误");
|
|
|
|
|
}
|
|
|
|
|
Authentication authentication = new UsernamePasswordAuthenticationToken(jwtUser, null, jwtUser.getAuthorities());
|
|
|
|
|
SecurityContextHolder.getContext().setAuthentication(authentication);
|
|
|
|
|
String token = TokenProvider.createToken(jwtUser);
|
|
|
|
|
LoginResult loginResult = LoginResult.create(jwtUser, token);
|
|
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
|
|
map.put("name", loginResult.getName());
|
|
|
|
|
map.put("username", loginResult.getUsername());
|
|
|
|
|
map.put("roleId", loginResult.getRoleId());
|
|
|
|
|
map.put("schoolId", loginResult.getSchoolId());
|
|
|
|
|
if (StringUtils.isNotBlank(loginResult.getUserId())) {
|
|
|
|
|
map.put("classId", loginResult.getClassId());
|
|
|
|
|
}
|
|
|
|
|
map.put("userId", loginResult.getUserId());
|
|
|
|
|
map.put("token", token);
|
|
|
|
|
return new ResultEntity(HttpStatus.OK, map); //todo 从智云登录后将用户添加到本地用户表
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ApiOperation("根据用户Code查询该用户是否存在个人赛用户数据,存在则返回,不存在则新增后返回,用于智云3.0创建用户后调用该接口创建用户个人赛")
|
|
|
|
|
@PostMapping("/checkOrCreateForexSimulationUser")
|
|
|
|
|
@AnonymousAccess
|
|
|
|
|
public String checkOrCreateForexSimulationUser(@RequestBody List<ZYUserInfo> zyUserInfoList,
|
|
|
|
|
@RequestParam String systemOwner) {
|
|
|
|
|
System.out.println(systemOwner);
|
|
|
|
|
List<UserInfo> users = new ArrayList<>();
|
|
|
|
|
for (int i = 0; i < zyUserInfoList.size(); i++) {
|
|
|
|
|
ZYUserInfo zyUserInfo = zyUserInfoList.get(i);
|
|
|
|
|
Integer zyUserId = zyUserInfo.getUserId();
|
|
|
|
|
String username = zyUserInfo.getUsername(); // studentid
|
|
|
|
|
String name = zyUserInfo.getName();
|
|
|
|
|
String password = zyUserInfo.getPassword();
|
|
|
|
|
Integer roleId = zyUserInfo.getRoleId(); //3教师 4学生
|
|
|
|
|
|
|
|
|
|
UserInfo userInfo = userInfoMapper.selectByPrimaryKey(String.valueOf(zyUserId));
|
|
|
|
|
if (userInfo!=null) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
UserInfo stuUser = new UserInfo();
|
|
|
|
|
stuUser.setUserid(IdUtil.randomUUID());
|
|
|
|
|
stuUser.setStudentId(zyUserInfo.getUsername());
|
|
|
|
|
stuUser.setName(name);
|
|
|
|
|
stuUser.setUsername(username);
|
|
|
|
|
stuUser.setClassId(zyUserInfo.getClassId().toString());
|
|
|
|
|
stuUser.setPassword(password);
|
|
|
|
|
stuUser.setPhone(zyUserInfo.getPhone());
|
|
|
|
|
stuUser.setMajor(zyUserInfo.getMajorName());
|
|
|
|
|
stuUser.setRoleId(roleId);
|
|
|
|
|
stuUser.setSchoolId(zyUserInfo.getSchoolId().toString());
|
|
|
|
|
stuUser.setSchoolName(zyUserInfo.getSchoolName());
|
|
|
|
|
stuUser.setCreateTime(new Date());
|
|
|
|
|
users.add(stuUser);
|
|
|
|
|
}
|
|
|
|
|
if (users.isEmpty()) {
|
|
|
|
|
return "账号已全部存在";
|
|
|
|
|
}
|
|
|
|
|
userInfoMapper.batchInsertUserInfo(users);
|
|
|
|
|
return "ok";
|
|
|
|
|
}
|
|
|
|
|
}
|