package com.trafficaudit.system.controller;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.trafficaudit.common.Result;
|
import com.trafficaudit.security.utils.JwtUtils;
|
import com.trafficaudit.system.entity.User;
|
import com.trafficaudit.system.mapper.UserMapper;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.web.bind.annotation.*;
|
|
import javax.annotation.Resource;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/** 用户管理(含负责报表模块配置:freight/passenger/invest/energy/cityPassenger) */
|
@RestController
|
@RequestMapping("/api/user")
|
public class UserController {
|
|
@Resource
|
private UserMapper userMapper;
|
@Resource
|
private PasswordEncoder passwordEncoder;
|
@Resource
|
private JwtUtils jwtUtils;
|
|
@GetMapping("/list")
|
public Result<List<Map<String, Object>>> list(@RequestParam(value = "keyword", required = false) String keyword) {
|
LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
|
if (keyword != null && !keyword.trim().isEmpty()) {
|
String k = keyword.trim();
|
qw.and(w -> w.like(User::getUsername, k).or().like(User::getRealName, k));
|
}
|
qw.orderByAsc(User::getId);
|
List<Map<String, Object>> data = new ArrayList<>();
|
for (User u : userMapper.selectList(qw)) {
|
Map<String, Object> m = new HashMap<>();
|
m.put("id", u.getId());
|
m.put("username", u.getUsername());
|
m.put("realName", u.getRealName());
|
m.put("phone", u.getPhone());
|
m.put("status", u.getStatus());
|
m.put("modules", u.getModules());
|
m.put("createdAt", u.getCreatedAt() == null ? null : u.getCreatedAt().toString());
|
data.add(m);
|
}
|
return Result.ok(data);
|
}
|
|
/** 新增/编辑用户;编辑时 password 留空则不修改密码;modules 为模块 key 数组 */
|
@PostMapping("/save")
|
public Result<Void> save(@RequestBody Map<String, Object> params) {
|
Object idObj = params.get("id");
|
Long id = idObj == null ? null : Long.valueOf(idObj.toString());
|
String username = str(params.get("username"));
|
if (username == null || username.trim().isEmpty()) return Result.error(400, "用户名不能为空");
|
username = username.trim();
|
String password = str(params.get("password"));
|
String realName = str(params.get("realName"));
|
String phone = str(params.get("phone"));
|
Integer status = params.get("status") == null ? 1 : Integer.valueOf(params.get("status").toString());
|
List<String> moduleKeys = new ArrayList<>();
|
Object modulesObj = params.get("modules");
|
if (modulesObj instanceof List) {
|
for (Object o : (List<?>) modulesObj) if (o != null) moduleKeys.add(o.toString());
|
}
|
String modules = String.join(",", moduleKeys);
|
|
LambdaQueryWrapper<User> dup = new LambdaQueryWrapper<User>().eq(User::getUsername, username);
|
if (id != null) dup.ne(User::getId, id);
|
if (userMapper.selectCount(dup) != null && userMapper.selectCount(dup) > 0) return Result.error(400, "用户名已存在");
|
|
if (id == null) {
|
if (password == null || password.isEmpty()) return Result.error(400, "新增用户必须设置密码");
|
User u = new User();
|
u.setUsername(username);
|
u.setRealName(realName);
|
u.setPhone(phone);
|
u.setStatus(status);
|
u.setModules(modules);
|
u.setPassword(passwordEncoder.encode(password));
|
userMapper.insert(u);
|
} else {
|
User u = userMapper.selectById(id);
|
if (u == null) return Result.error(404, "用户不存在");
|
u.setRealName(realName);
|
u.setPhone(phone);
|
u.setStatus(status);
|
u.setModules(modules);
|
if (password != null && !password.isEmpty()) u.setPassword(passwordEncoder.encode(password));
|
userMapper.updateById(u);
|
}
|
return Result.ok(null);
|
}
|
|
/** admin 重置他人密码为 123456 */
|
@PostMapping("/reset-password")
|
public Result<Void> resetPassword(@RequestHeader(value = "Authorization", required = false) String auth,
|
@RequestBody Map<String, Object> params) {
|
String operator = jwtUtils.resolveUsername(auth);
|
if (operator == null) {
|
return Result.error(401, "未登录或登录已过期");
|
}
|
if (!"admin".equals(operator)) {
|
return Result.error(403, "仅 admin 可以重置其他用户密码");
|
}
|
Object idObj = params.get("id");
|
if (idObj == null) {
|
return Result.error(400, "缺少用户 id");
|
}
|
Long id = Long.valueOf(idObj.toString());
|
User u = userMapper.selectById(id);
|
if (u == null) {
|
return Result.error(404, "用户不存在");
|
}
|
if ("admin".equals(u.getUsername())) {
|
return Result.error(400, "admin 密码不允许重置");
|
}
|
u.setPassword(passwordEncoder.encode("123456"));
|
userMapper.updateById(u);
|
return Result.ok(null);
|
}
|
|
@PostMapping("/delete")
|
public Result<Void> delete(@RequestParam("id") Long id) {
|
User u = userMapper.selectById(id);
|
if (u == null) return Result.error(404, "用户不存在");
|
if ("admin".equals(u.getUsername())) return Result.error(400, "admin 不允许删除");
|
userMapper.deleteById(id);
|
return Result.ok(null);
|
}
|
|
private String str(Object o) {
|
return o == null ? null : o.toString();
|
}
|
}
|