package com.lf.server.controller.sys;
|
|
import com.lf.server.aspect.SysLog;
|
import com.lf.server.controller.all.BaseController;
|
import com.lf.server.entity.all.ResponseMsg;
|
import com.lf.server.entity.sys.UsersEntity;
|
import com.lf.server.helper.Md5Helper;
|
import com.lf.server.helper.StringHelper;
|
import com.lf.server.service.sys.TokenService;
|
import com.lf.server.service.sys.UsersService;
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiImplicitParam;
|
import io.swagger.annotations.ApiImplicitParams;
|
import io.swagger.annotations.ApiOperation;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.*;
|
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
import java.util.List;
|
|
/**
|
* 用户表
|
* @author sws
|
* @date 2022-09-27
|
*/
|
@Api(tags = "运维管理\\用户管理")
|
@RestController
|
@RequestMapping("/user")
|
public class UsersController extends BaseController {
|
@Autowired
|
UsersService userService;
|
|
@Autowired
|
TokenService tokenService;
|
|
@SysLog()
|
@ApiOperation(value = "查询记录数")
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "uname", value = "用户名", dataType = "String", paramType = "query", required = false, example = "sys_user")
|
})
|
@GetMapping({"/selectCount"})
|
public ResponseMsg<Integer> selectCount(String uname) {
|
try {
|
int count = userService.selectCount(uname);
|
|
return success(count);
|
} catch (Exception ex) {
|
return fail(ex.getMessage(), -1);
|
}
|
}
|
|
@SysLog()
|
@ApiOperation(value = "分页查询")
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "uname", value = "用戶名", dataType = "String", paramType = "query", example = "sys_user"),
|
@ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "Integer", paramType = "query", example = "10"),
|
@ApiImplicitParam(name = "pageIndex", value = "分页数(从1开始)", dataType = "Integer", paramType = "query", example = "1")
|
})
|
@GetMapping(value = "/selectByPage")
|
public ResponseMsg<List<UsersEntity>> selectByPage(String uname, Integer pageSize, Integer pageIndex) {
|
try {
|
if (pageSize < 1 || pageIndex < 1) {
|
return fail("每页页数或分页数小于1", null);
|
}
|
|
List<UsersEntity> rs = userService.selectByPage(uname, pageSize, pageSize * (pageIndex - 1));
|
|
return success(rs);
|
} catch (Exception ex) {
|
return fail(ex.getMessage(), null);
|
}
|
}
|
|
@SysLog()
|
@ApiOperation(value = "分页查询并返回记录数")
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "uname", value = "用户名", dataType = "String", paramType = "query", example = "sys_dict"),
|
@ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "Integer", paramType = "query", example = "10"),
|
@ApiImplicitParam(name = "pageIndex", value = "分页数(从1开始)", dataType = "Integer", paramType = "query", example = "1")
|
})
|
@GetMapping(value = "/selectByPageAndCount")
|
public ResponseMsg<List<UsersEntity>> selectByPageAndCount(String uname, Integer pageSize, Integer pageIndex) {
|
try {
|
if (pageSize < 1 || pageIndex < 1) {
|
return fail("每页页数或分页数小于1", null);
|
}
|
int count = userService.selectCount(uname);
|
if (count == 0) {
|
return success(0, null);
|
}
|
List<UsersEntity> rs = userService.selectByPage(uname, pageSize, pageSize * (pageIndex - 1));
|
|
return success(count, rs);
|
} catch (Exception ex) {
|
return fail(ex.getMessage(), null);
|
}
|
}
|
|
@SysLog()
|
@ApiOperation(value = "插入字典")
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "userEntity", value = "字典实体类", dataType = "com.lf.server.entity.data.UserEntity", paramType = "body", example = "")
|
})
|
@PostMapping(value = "/insertUser", produces = "application/json; charset=UTF-8")
|
public ResponseMsg<Integer> insertUser(@RequestBody UsersEntity usersEntity) {
|
try {
|
int count = userService.insertUser(usersEntity);
|
|
return success(count);
|
} catch (Exception ex) {
|
return fail(ex.getMessage(), -1);
|
}
|
}
|
|
@SysLog()
|
@ApiOperation(value = "插入多条字典")
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "userEntity", value = "字典实体类集合", dataType = "List<UserEntity>", paramType = "body", example = "")
|
})
|
@PostMapping(value = "/insertUsers", produces = "application/json; charset=UTF-8")
|
public ResponseMsg<Integer> insertUsers(@RequestBody List<UsersEntity> usersEntity) {
|
try {
|
int count = userService.insertUsers(usersEntity);
|
|
return success(count);
|
} catch (Exception ex) {
|
return fail(ex.getMessage(), -1);
|
}
|
}
|
|
@SysLog()
|
@ApiOperation(value = "删除一条字典")
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "id", value = "字典ID", dataType = "Integer", paramType = "query", example = "1")
|
})
|
@GetMapping(value = "/deleteUser")
|
public ResponseMsg<Integer> deleteUser(int id) {
|
try {
|
int count = userService.deleteUser(id);
|
|
return success(count);
|
} catch (Exception ex) {
|
return fail(ex.getMessage(), -1);
|
}
|
}
|
|
@SysLog()
|
@ApiOperation(value = "删除多条字典")
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "ids", value = "字典ID集合", dataType = "List<Integer>", paramType = "query", example = "1,2")
|
})
|
@GetMapping(value = "/deleteUsers")
|
public ResponseMsg<Integer> deleteUsers(@RequestParam List<Integer> ids) {
|
try {
|
if (ids == null || ids.isEmpty()) {
|
return fail("id数组不能为空", -1);
|
}
|
|
int count = userService.deleteUsers(ids);
|
|
return success(count);
|
} catch (Exception ex) {
|
return fail(ex.getMessage(), -1);
|
}
|
}
|
|
@SysLog()
|
@ApiOperation(value = "更新一条字典")
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "userEntity", value = "字典ID集合", dataType = "UserEntity", paramType = "body", example = "")
|
})
|
@ResponseBody
|
@PostMapping(value = "/updateUsers", produces = "application/json; charset=UTF-8")
|
public ResponseMsg<Integer> updateUsers(@RequestBody UsersEntity usersEntity) {
|
try {
|
int count = userService.updateUsers(usersEntity);
|
|
return success(count);
|
} catch (Exception ex) {
|
return fail(ex.getMessage(), -1);
|
}
|
}
|
|
@SysLog()
|
@ApiOperation(value = "根据ID查询字典")
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "id", value = "字典ID", dataType = "Integer", paramType = "query", example = "1")
|
})
|
@GetMapping(value = "/selectUser")
|
public ResponseMsg<UsersEntity> selectUser(int id) {
|
try {
|
UsersEntity usersEntity = userService.selectUser(id);
|
|
return success(usersEntity);
|
} catch (Exception ex) {
|
return fail(ex.getMessage(), null);
|
}
|
}
|
|
@SysLog()
|
@ApiOperation(value = "查询所有字典")
|
@GetMapping(value = "/selectUserAll")
|
public ResponseMsg<List<UsersEntity>> selectUserAll() {
|
try {
|
List<UsersEntity> list = userService.selectUserAll();
|
|
return success(list);
|
} catch (Exception ex) {
|
return fail(ex.getMessage(), null);
|
}
|
}
|
|
@SysLog()
|
@ApiOperation(value = "更新用户密码")
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "user", value = "用户实体类", dataType = "UsersEntity", paramType = "body", example = "")
|
})
|
@PostMapping(value = "/updateUserPwd")
|
public ResponseMsg<Boolean> updateUserPwd(@RequestBody UsersEntity user, HttpServletRequest req, HttpServletResponse res) {
|
try {
|
if (user == null) {
|
return fail("请提交用户信息!", false);
|
}
|
if (StringHelper.isEmpty(user.getPwd())) {
|
return fail("请输入用户密码!", false);
|
}
|
if (StringHelper.isEmpty(user.getSalt())) {
|
return fail("请输入管理员密码!", false);
|
}
|
if (!StringHelper.checkPwdValid(user.getPwd())) {
|
return fail("新密码不符合规则要求!", false);
|
}
|
|
UsersEntity ue = tokenService.getCurrentUser(req);
|
if (ue == null) {
|
return fail("没有登录或登录超时!", false);
|
}
|
if (!Md5Helper.validatePassword(user.getSalt(), ue.getPwd())) {
|
return fail("管理员密码不正确!", false);
|
}
|
|
UsersEntity usersEntity = userService.selectUser(user.getId());
|
if (usersEntity == null) {
|
return fail("没有找到要修改的用户!", false);
|
}
|
// 设置新密码
|
String md5 = Md5Helper.reverse(Md5Helper.generate(user.getPwd()));
|
usersEntity.setPwd(md5);
|
// 设置更新信息
|
usersEntity.setUpdateUser(ue.getId());
|
|
Integer rows = userService.updateUsers(usersEntity);
|
|
return success(rows > 0 ? "更新成功" : "更新失败", rows > 0);
|
} catch (Exception ex) {
|
return fail(ex.getMessage(), false);
|
}
|
}
|
}
|