| | |
| | | package org.example.controller; |
| | | |
| | | import cn.hutool.core.convert.Convert; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.fasterxml.jackson.core.JsonProcessingException; |
| | | import com.fasterxml.jackson.core.type.TypeReference; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.google.gson.Gson; |
| | | import com.google.gson.GsonBuilder; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang.StringUtils; |
| | | import org.example.common.ServerResponse; |
| | | import org.example.dao.ConfigCurrencyMapper; |
| | | import org.example.dao.LogMapper; |
| | | import org.example.pojo.ConfigCurrency; |
| | | import org.example.pojo.Log; |
| | | import org.example.pojo.User; |
| | | import org.example.dao.MenuMapper; |
| | | import org.example.pojo.*; |
| | | import org.example.pojo.vo.DeleteConfigVo; |
| | | import org.example.pojo.vo.SaveConfigVo; |
| | | import org.example.pojo.vo.UpdateUserVo; |
| | | import org.example.server.impl.UserServiceImpl; |
| | | import org.example.util.IpAddressUtil; |
| | | import org.example.util.JwtUtil; |
| | | import org.example.util.MD5Util; |
| | | import org.example.util.RedisUtil; |
| | | import org.example.util.*; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | |
| | | import java.security.SecureRandom; |
| | | import java.sql.Date; |
| | | import java.time.LocalDate; |
| | | import java.util.Base64; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @program: demo |
| | |
| | | **/ |
| | | @RestController |
| | | @RequestMapping("/user") |
| | | @CrossOrigin(origins = "*") |
| | | @Slf4j |
| | | public class UserController { |
| | | |
| | | @Autowired |
| | |
| | | @Autowired |
| | | private LogMapper logMapper; |
| | | |
| | | @Autowired |
| | | private MenuMapper menuMapper; |
| | | |
| | | @PostMapping("/login") |
| | | @ResponseBody |
| | | public ServerResponse saveUser(@RequestParam("account") String account |
| | | , @RequestParam("password") String password, HttpServletRequest request) { |
| | | , @RequestParam("password") String password, HttpServletRequest request) throws JsonProcessingException { |
| | | try { |
| | | User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getAccount, account)); |
| | | if(null == user){ |
| | | return ServerResponse.createByErrorMsg("用户不存在"); |
| | | } |
| | | |
| | | User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getAccount, account).eq(User::getIsRoot,0)); |
| | | if(null == user){ |
| | | return ServerResponse.createBySuccessMsg("用户不存在"); |
| | | if (!MD5Util.verify(password, user.getPassword())) { |
| | | return ServerResponse.createByErrorMsg("密码错误"); |
| | | } |
| | | List<Menu> menus = menuMapper.selectList(new LambdaQueryWrapper<Menu>()); |
| | | if(user.getIsRoot() == 1){ |
| | | String token = JwtUtil.getToken(user); |
| | | Map<String,Object> map = new HashMap<>(); |
| | | map.put("token",token); |
| | | map.put("user",user); |
| | | map.put("menu",menus); |
| | | extracted(account, request); |
| | | RedisUtil.set(user.getId().toString(),token); |
| | | return ServerResponse.createBySuccess(map); |
| | | } |
| | | |
| | | //判断是否锁定 |
| | | if(user.getIsLock() == 1){ |
| | | return ServerResponse.createByErrorMsg("账号已被锁定"); |
| | | } |
| | | |
| | | //判断是否到期 |
| | | if(new java.util.Date().after(user.getEndTime())){ |
| | | return ServerResponse.createByErrorMsg("账号已到期"); |
| | | } |
| | | |
| | | String token = JwtUtil.getToken(user); |
| | | Map<String,Object> map = new HashMap<>(); |
| | | map.put("token",token); |
| | | List<Menu> menuList = menus.stream().filter(f -> f.getIsRoot() == 0).collect(Collectors.toList()); |
| | | map.put("menu",menuList); |
| | | map.put("user",user); |
| | | RedisUtil.set(user.getId().toString(),token); |
| | | |
| | | extracted(account, request); |
| | | return ServerResponse.createBySuccess(map); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | log.error("登录异常:"+e.getMessage()); |
| | | } |
| | | return ServerResponse.createByErrorMsg("系统异常"); |
| | | } |
| | | |
| | | if (!MD5Util.verify(password, user.getPassword())) { |
| | | return ServerResponse.createBySuccessMsg("密码错误"); |
| | | } |
| | | |
| | | //判断是否锁定 |
| | | if(user.getIsLock() == 1){ |
| | | return ServerResponse.createBySuccessMsg("账号已被锁定"); |
| | | } |
| | | |
| | | //判断是否到期 |
| | | if(new java.util.Date().after(user.getEndTime())){ |
| | | return ServerResponse.createBySuccessMsg("账号已到期"); |
| | | } |
| | | |
| | | |
| | | String token = JwtUtil.getToken(user); |
| | | Map<String,String> map = new HashMap<>(); |
| | | map.put("token",token); |
| | | String key = "user_"; |
| | | RedisUtil.set(key+user.getId(),token); |
| | | private void extracted(String account, HttpServletRequest request) { |
| | | String ip = IpAddressUtil.getIpAddress(request); |
| | | String address = null; |
| | | address = IpAddressUtil.getIpPossessionByFile(ip); |
| | | if(null == address){ |
| | | address = IpAddressUtil.getIpAddressByOnline(ip); |
| | | } |
| | | String address = IpAddressUtil.getIpPossessionByFile(ip); |
| | | Log log = new Log(); |
| | | log.setIp(ip); |
| | | log.setAccount(account); |
| | | log.setLoginTime(new java.util.Date()); |
| | | log.setAddress(address); |
| | | logMapper.insert(log); |
| | | |
| | | return ServerResponse.createBySuccess(map); |
| | | } |
| | | |
| | | // 生成指定长度的随机 token |
| | | public static String generateToken() { |
| | | SecureRandom secureRandom = new SecureRandom(); |
| | | byte[] token = new byte[16]; |
| | | secureRandom.nextBytes(token); |
| | | return Base64.getUrlEncoder().withoutPadding().encodeToString(token); |
| | | } |
| | | |
| | | @PostMapping("/saveConfig") |
| | | public ServerResponse saveConfig(@RequestBody SaveConfigVo saveConfigVo) { |
| | | currencyMapper.delete(new LambdaQueryWrapper<ConfigCurrency>().eq(ConfigCurrency::getUserId,saveConfigVo.getUserId())); |
| | | public ServerResponse saveConfig(@RequestBody SaveConfigVo saveConfigVo,HttpServletRequest request) { |
| | | String token = request.getHeader("token"); |
| | | User user = JwtUtil.verify(token); |
| | | saveConfigVo.getCurrencyList().forEach(f->{ |
| | | ConfigCurrency currency = new ConfigCurrency(); |
| | | currency.setUserId(saveConfigVo.getUserId()); |
| | | currency.setUserId(user.getId()); |
| | | currency.setCurrency(f.getCurrency()); |
| | | currency.setBuy(f.getBuy()); |
| | | currency.setSell(f.getSell()); |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| | | String formattedDateTime = now.format(formatter); |
| | | currency.setAddTime(formattedDateTime); |
| | | currencyMapper.insert(currency); |
| | | }); |
| | | List<ConfigCurrency> configCurrencies = currencyMapper.selectList(new LambdaQueryWrapper<ConfigCurrency>().eq(ConfigCurrency::getUserId, saveConfigVo.getUserId())); |
| | | List<ConfigCurrency> configCurrencies = currencyMapper.selectList(new LambdaQueryWrapper<ConfigCurrency>().eq(ConfigCurrency::getUserId, user.getId())); |
| | | String key = "config_"; |
| | | Gson gson = new GsonBuilder().setPrettyPrinting().create(); |
| | | String json = gson.toJson(configCurrencies); |
| | | RedisUtil.set(key+saveConfigVo.getUserId(),json); |
| | | RedisUtil.set(key+user.getId(),json); |
| | | return ServerResponse.createBySuccess(); |
| | | } |
| | | |
| | | @PostMapping("/deleteConfig") |
| | | public ServerResponse deleteConfig(@RequestBody DeleteConfigVo deleteConfigVo) { |
| | | currencyMapper.deleteBatchIds(deleteConfigVo.getConfigId()); |
| | | List<ConfigCurrency> configCurrencies = currencyMapper.selectList(new LambdaQueryWrapper<ConfigCurrency>().eq(ConfigCurrency::getUserId, deleteConfigVo.getUserId())); |
| | | @GetMapping("/deleteConfig") |
| | | public ServerResponse deleteConfig(@RequestParam("id") int id,HttpServletRequest request) { |
| | | String token = request.getHeader("token"); |
| | | User user = JwtUtil.verify(token); |
| | | currencyMapper.deleteById(id); |
| | | List<ConfigCurrency> configCurrencies = currencyMapper.selectList(new LambdaQueryWrapper<ConfigCurrency>().eq(ConfigCurrency::getUserId, user.getId())); |
| | | String key = "config_"; |
| | | Gson gson = new GsonBuilder().setPrettyPrinting().create(); |
| | | String json = gson.toJson(configCurrencies); |
| | | RedisUtil.set(key+deleteConfigVo.getUserId(),json); |
| | | RedisUtil.set(key+user.getId(),json); |
| | | return ServerResponse.createBySuccess(); |
| | | } |
| | | |
| | | @GetMapping("/selectConfig") |
| | | public ServerResponse selectConfig(@RequestParam(value = "currency", required = false) String currency, |
| | | @RequestParam("pageNum") int pageNum, |
| | | @RequestParam("pageSize") int pageSize, |
| | | HttpServletRequest request) { |
| | | String token = request.getHeader("token"); |
| | | User user = JwtUtil.verify(token); |
| | | Page<ConfigCurrency> page = new Page<>(pageNum, pageSize); |
| | | Page<ConfigCurrency> configCurrencyPage = currencyMapper.selectPage(page, new LambdaQueryWrapper<ConfigCurrency>() |
| | | .eq(StringUtils.isNotEmpty(currency), ConfigCurrency::getCurrency, currency).eq(ConfigCurrency::getUserId,user.getId()).orderByDesc(ConfigCurrency::getAddTime)); |
| | | return ServerResponse.createBySuccess(configCurrencyPage); |
| | | |
| | | } |
| | | |
| | | |
| | | @PostMapping("/saveUser") |
| | | public ServerResponse saveUser(User user) { |
| | | public ServerResponse saveUser(@RequestBody User user) { |
| | | long count = userService.count(new LambdaQueryWrapper<User>().eq(User::getAccount, user.getAccount())); |
| | | if(count > 0){ |
| | | return ServerResponse.createByErrorMsg("账号已存在"); |
| | |
| | | @PostMapping("/deleteUser") |
| | | public ServerResponse deleteUser(@RequestParam("id") int id) { |
| | | User byId = userService.getById(id); |
| | | if(null == byId){ |
| | | if(null == byId || byId.getIsRoot() == 1){ |
| | | return ServerResponse.createByErrorMsg("用户不存在"); |
| | | } |
| | | userService.removeById(id); |
| | | return ServerResponse.createBySuccess(); |
| | | } |
| | | |
| | | @PostMapping("/updateUser") |
| | | public ServerResponse deleteUser(User user) { |
| | | User byId = userService.getById(user.getId()); |
| | | if(null == byId){ |
| | | @GetMapping("/selectById") |
| | | public ServerResponse selectById(@RequestParam("id") Integer id) { |
| | | User byId = userService.getById(id); |
| | | if(null == byId || byId.getIsRoot() == 1){ |
| | | return ServerResponse.createByErrorMsg("用户不存在"); |
| | | } |
| | | user.setPassword(MD5Util.encrypt(user.getPassword())); |
| | | return ServerResponse.createBySuccess(byId); |
| | | } |
| | | |
| | | @PostMapping("/updateUser") |
| | | public ServerResponse deleteUser(@RequestBody UpdateUserVo updateUserVo) { |
| | | User user = userService.getById(updateUserVo.getId()); |
| | | if(null == user || user.getIsRoot() == 1){ |
| | | return ServerResponse.createByErrorMsg("用户不存在"); |
| | | } |
| | | user.setAccount(updateUserVo.getAccount()); |
| | | user.setEndTime(updateUserVo.getEndTime()); |
| | | user.setIsLock(updateUserVo.getIsLock()); |
| | | userService.updateById(user); |
| | | return ServerResponse.createBySuccess(); |
| | | } |
| | | |
| | | @PostMapping("/updatePassword") |
| | | public ServerResponse updatePassword(@RequestParam("id") Integer id,@RequestParam("password") String password) { |
| | | User user = userService.getById(id); |
| | | if(null == user || user.getIsRoot() == 1){ |
| | | return ServerResponse.createByErrorMsg("用户不存在"); |
| | | } |
| | | user.setPassword(MD5Util.encrypt(password)); |
| | | userService.updateById(user); |
| | | return ServerResponse.createBySuccess(); |
| | | } |
| | | |
| | | @GetMapping("/selectUser") |
| | | public ServerResponse selectUser(@RequestParam(value = "account", required = false) String account, |
| | | @RequestParam("pageNum") int pageNum, |
| | | @RequestParam("pageSize") int pageSize) { |
| | | Page<User> page = new Page<>(pageNum, pageSize); |
| | | Page<User> pageList = userService.page(page, new LambdaQueryWrapper<User>() |
| | | .like(StringUtils.isNotEmpty(account), User::getAccount, account) |
| | | .eq(User::getIsShow,0) |
| | | .eq(User::getIsRoot,0) |
| | | .orderByDesc(User::getAddTime)); |
| | | return ServerResponse.createBySuccess(pageList); |
| | | } |
| | | |
| | | @GetMapping("/selectLog") |
| | | public ServerResponse selectLog(@RequestParam(value = "account", required = false) String account, |
| | | @RequestParam("pageNum") int pageNum, |
| | | @RequestParam("pageSize") int pageSize) { |
| | | Page<Log> page = new Page<>(pageNum, pageSize); |
| | | Page<Log> pageList = logMapper.selectPage(page, new LambdaQueryWrapper<Log>() |
| | | .like(StringUtils.isNotEmpty(account), Log::getAccount, account) |
| | | .orderByDesc(Log::getLoginTime)); |
| | | return ServerResponse.createBySuccess(pageList); |
| | | } |
| | | |
| | | @GetMapping("/addMark") |
| | | public ServerResponse addMark(@RequestParam(value = "favorites") String favorites, |
| | | HttpServletRequest request) { |
| | | String token = request.getHeader("token"); |
| | | User user = JwtUtil.verify(token); |
| | | |
| | | if (user == null) { |
| | | // 处理用户验证失败的情况,例如返回未授权错误 |
| | | return ServerResponse.createByErrorMsg("标记失败"); |
| | | } |
| | | |
| | | String mark = RedisUtil.get(user.getId() + "_mark"); |
| | | |
| | | if (StringUtils.isNotEmpty(mark)) { |
| | | // 使用 StringJoiner 或者 StringBuilder 来拼接字符串 |
| | | mark += "," + favorites; |
| | | } else { |
| | | mark = favorites; |
| | | } |
| | | |
| | | // 更新 Redis 中的标记 |
| | | RedisUtil.set(user.getId() + "_mark", mark); |
| | | |
| | | // 返回操作成功的响应 |
| | | return ServerResponse.createBySuccess("标记成功"); |
| | | } |
| | | |
| | | @GetMapping("/deleteMark") |
| | | @ResponseBody |
| | | public ServerResponse deleteMark(@RequestParam(value = "favorites") String favorites, |
| | | HttpServletRequest request) { |
| | | String token = request.getHeader("token"); |
| | | User user = JwtUtil.verify(token); |
| | | |
| | | String mark = RedisUtil.get(user.getId() + "_mark"); |
| | | |
| | | if (StringUtils.isNotEmpty(mark)) { |
| | | // 使用逗号分隔符将标记分割成数组 |
| | | String[] marks = mark.split(","); |
| | | |
| | | // 创建一个新的标记列表,用于存储没有要删除的标记 |
| | | List<String> updatedMarks = new ArrayList<>(); |
| | | |
| | | // 遍历现有的标记,将不等于要删除的标记加入新列表 |
| | | for (String m : marks) { |
| | | if (!m.equals(favorites)) { |
| | | updatedMarks.add(m); |
| | | } |
| | | } |
| | | |
| | | // 将新的标记列表转换为逗号分隔的字符串 |
| | | mark = String.join(",", updatedMarks); |
| | | |
| | | // 更新 Redis 中的标记 |
| | | RedisUtil.set(user.getId() + "_mark", mark); |
| | | |
| | | |
| | | } |
| | | // 返回操作成功的响应 |
| | | return ServerResponse.createBySuccess("取消标记成功"); |
| | | } |
| | | |
| | | } |