248 lines
8.2 KiB
Java
248 lines
8.2 KiB
Java
package com.yzdx.AiInterviewer.controller;
|
||
|
||
import com.yzdx.AiInterviewer.comment.R;
|
||
import com.yzdx.AiInterviewer.entity.User;
|
||
import com.yzdx.AiInterviewer.service.UserService;
|
||
import com.yzdx.AiInterviewer.utiles.JWT;
|
||
import io.swagger.annotations.*;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
||
import org.springframework.web.bind.annotation.*;
|
||
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
|
||
@RestController
|
||
@RequestMapping("/admin")
|
||
public class UserController {
|
||
|
||
@Autowired
|
||
private UserService userService;
|
||
/**
|
||
* 用户登录
|
||
* @param loginForm 用户输入的账号密码,公司编码
|
||
* @return R
|
||
* */
|
||
@ApiOperation(value = "管理员登录",notes = "")
|
||
@PostMapping("/login")
|
||
public R adminLogin(@RequestBody @ApiParam("传入的值:loginForm:{" +
|
||
"(String)phone," +
|
||
"(String)encoding," +
|
||
"(String)password}") Map<String,Object> loginForm){
|
||
if(loginForm.size()==0){
|
||
return R.error("传来的数据有误,请检查输入");
|
||
|
||
}
|
||
String phone=(String)loginForm.get("phone");
|
||
String encoding=(String)loginForm.get("encoding");
|
||
String password=(String)loginForm.get("password");
|
||
return userService.adminLogin(phone, encoding, password);
|
||
}
|
||
|
||
/**
|
||
* 用户信息
|
||
* @param token 用户登录时返回的token
|
||
* @return R
|
||
*/
|
||
@GetMapping("/get_userInfo")
|
||
@ApiOperation(value = "获取用户信息",notes = "")
|
||
public R getUserInfo( @ApiParam("传入的值:(String)token值") String token) {
|
||
|
||
if(token==null){
|
||
return R.error(401,"非法访问,请重新登陆!");
|
||
}
|
||
Integer userId = JWT.getTokenId(token);
|
||
|
||
User findUser = userService.getUserById(userId);
|
||
if (findUser == null) {
|
||
return R.error(401, "非法访问,请重新登陆");
|
||
}
|
||
User user=new User();
|
||
user.setId(findUser.getId());
|
||
user.setUsername(findUser.getUsername());
|
||
user.setRole(findUser.getRole());
|
||
user.setPhone(findUser.getPhone());
|
||
user.setSex(findUser.getSex());
|
||
user.setAge(findUser.getAge());
|
||
user.setAvatar(findUser.getAvatar());
|
||
user.setEmail(findUser.getEmail());
|
||
user.setCreateTime(findUser.getCreateTime());
|
||
user.setUpdateTime(findUser.getUpdateTime());
|
||
|
||
return R.success(user);
|
||
}
|
||
|
||
/**
|
||
* 用户注销
|
||
* @param token 用户登录时返回的token
|
||
* @return R
|
||
*/
|
||
@GetMapping("/logout")
|
||
@ApiOperation(value = "管理员登出",notes = "")
|
||
public R adminLogout(@ApiParam("传入的值:(String)token值") String token){
|
||
|
||
Integer row = userService.adminLogout(token);
|
||
|
||
if(row==0){
|
||
return R.error("登出失败,请联系管理员!");
|
||
}
|
||
|
||
return R.success("退出成功!");
|
||
|
||
}
|
||
|
||
/**
|
||
* 获取管理员列表
|
||
* @param encoding 公司编码
|
||
* @return R
|
||
*/
|
||
@ApiOperation(value = "根据公司编码获取管理员列表",notes = "")
|
||
@GetMapping("/get_adminList")
|
||
public R getAdminList(@ApiParam("传入的值:(String)encoding") String encoding){
|
||
|
||
List<User> adminList = userService.getAdminList(encoding);
|
||
|
||
return R.success(adminList);
|
||
}
|
||
|
||
/**
|
||
* 搜索管理员
|
||
* @param name 管理员的姓名
|
||
* @param encoding 公司编码
|
||
* @return R
|
||
*/
|
||
@ApiOperation(value = "根据名字搜索管理员",notes = "")
|
||
|
||
@GetMapping("/search_admin")
|
||
public R searchAdmin(@ApiParam("传入的值:(String)name,(String)encoding") String name,String encoding){
|
||
if(name==null&&encoding==null){
|
||
return R.error("查询失败");
|
||
}
|
||
List<User> users = userService.searchAdmin(name, encoding);
|
||
|
||
return R.success(users);
|
||
}
|
||
|
||
/**
|
||
* 添加管理员
|
||
* @param addAdminInfo 添加人的公司编码,操作人的id,添加入的姓名,添加入的手机号/账号,添加人的权限
|
||
* @return R
|
||
*/
|
||
@PostMapping("/add_admin")
|
||
@ApiOperation(value = "添加管理员",notes = "")
|
||
public R addAdmin(@RequestBody @ApiParam("传入的值 addAdminInfo:{(String)encoding,(Integer)userId,(String)username,(String)phone,(String)role}") Map<String ,Object> addAdminInfo){
|
||
String encoding=(String) addAdminInfo.get("encoding");
|
||
Integer userId=(Integer) addAdminInfo.get("userId");
|
||
String username=(String)addAdminInfo.get("username");
|
||
String phone=(String)addAdminInfo.get("phone");
|
||
String role=(String)addAdminInfo.get("role");
|
||
|
||
Integer rows = userService.addAdmin(encoding, userId, username, phone, role);
|
||
|
||
if(rows==-2){
|
||
return R.error("添加人的账号存在,请检查输入!");
|
||
}
|
||
|
||
if(rows==0){
|
||
|
||
return R.error("添加失败,请联系管理员!");
|
||
}
|
||
return R.success("添加管理员成功!");
|
||
}
|
||
|
||
/**
|
||
* 更新管理员信息
|
||
* @param userId 操作人的id
|
||
* @param deleteId 删除人的id
|
||
* @return R
|
||
*/
|
||
@DeleteMapping("/del_admin")
|
||
@ApiOperation(value = "删除管理员",notes = "")
|
||
public R deleteAdmin(@ApiParam("传入的值 (Integer)userId,(Integer)deleteId")Integer deleteId,Integer userId){
|
||
|
||
|
||
Integer rows = userService.deleteAdmin(deleteId, userId);
|
||
if(rows==-3){
|
||
|
||
return R.error("禁止删除本次登陆账号!");
|
||
}
|
||
|
||
if(rows==-2){
|
||
return R.error("权限不足!");
|
||
}
|
||
|
||
if(rows==0){
|
||
return R.error("删除失败!");
|
||
}
|
||
return R.success("删除成功!");
|
||
}
|
||
|
||
/**
|
||
* 更换头像
|
||
* @param updateInfo 更改人的id,更改人头像的地址
|
||
* @return R
|
||
*/
|
||
@PostMapping("/change_avatar")
|
||
@ApiOperation(value = "根据ID修改头像",notes = "")
|
||
|
||
public R changeAvatar(@RequestBody @ApiParam("传入的值为:updateInfo:(Integer)userId,(String)avatar") Map<String,Object> updateInfo){
|
||
Integer userId=(Integer) updateInfo.get("userId") ;
|
||
String avatar=(String)updateInfo.get("avatar") ;
|
||
|
||
User user = userService.changeAvatar(userId, avatar);
|
||
if(user==null){
|
||
return R.error("更新失败!");
|
||
}
|
||
return R.success(user);
|
||
|
||
|
||
}
|
||
|
||
/**
|
||
* 更新密码
|
||
* @param updateInfo 更新人的Id,新密码,旧密码
|
||
* @return R
|
||
*/
|
||
@PostMapping("/reset_password")
|
||
@ApiOperation(value = "根据id更改密码",notes = "")
|
||
|
||
public R resetPassword(@RequestBody @ApiParam("传入的值为:updateInfo:(Integer)userId,(String)oldPassword,(String)password") Map<String,Object> updateInfo){
|
||
|
||
Integer userId=(Integer)updateInfo.get("userId") ;
|
||
String oldPassword=(String)updateInfo.get("oldPassword");
|
||
String password=(String)updateInfo.get("Password") ;
|
||
String resetPassword=(String)updateInfo.get("resetPassword");
|
||
if(!password.equals(resetPassword)){
|
||
return R.error("两次输入的密码不一致!");
|
||
}
|
||
return userService.resetPassword(userId,oldPassword,password);
|
||
}
|
||
|
||
/**
|
||
* 更新用户信息
|
||
* @param updateInfo 操作人的id,更新人的id,修改的名称,修改的年龄,修改的电子邮箱,修改的性别
|
||
* @return R
|
||
*/
|
||
@PostMapping("/upload_userInfo")
|
||
@ApiOperation(value = "根据ID更新用户信息",notes = "")
|
||
public R updateUserInfo(@RequestBody @ApiParam("传入的值为:updateInfo:(Integer)userId,(Integer)updateId,(String)username,(String)age,(String)email,(String)sex") Map<String,Object> updateInfo){
|
||
|
||
Integer userId=(Integer) updateInfo.get("userId");
|
||
Integer updateId=(Integer) updateInfo.get("updateId");
|
||
String username=(String) updateInfo.get("username");
|
||
String age=(String) updateInfo.get("age");
|
||
String email=(String) updateInfo.get("email");
|
||
String sex=(String) updateInfo.get("sex");
|
||
|
||
if(updateId==userId||userId==null||userId.equals("")){
|
||
userId=null;
|
||
User user = userService.updateUserInfo(userId, updateId, username, age, email, sex);
|
||
|
||
return R.success(user);
|
||
}else{
|
||
User user = userService.updateUserInfo(userId, updateId, username, age, email, sex);
|
||
return R.success(user);
|
||
}
|
||
}
|
||
}
|