| | |
| | | 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.LoginOut; |
| | | import org.example.pojo.User; |
| | | 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.MD5Util; |
| | | import org.example.util.*; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RequestParam; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.security.SecureRandom; |
| | | import java.sql.Date; |
| | | import java.time.LocalDate; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * @program: demo |
| | |
| | | **/ |
| | | @RestController |
| | | @RequestMapping("/user") |
| | | @CrossOrigin(origins = "*") |
| | | @Slf4j |
| | | public class UserController { |
| | | |
| | | @Autowired |
| | |
| | | @Autowired |
| | | private UserServiceImpl userService; |
| | | |
| | | @Autowired |
| | | private LogMapper logMapper; |
| | | |
| | | @PostMapping("/login") |
| | | @ResponseBody |
| | | public ServerResponse saveUser(@RequestParam("account") String account |
| | | , @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.createBySuccessMsg("用户不存在"); |
| | | } |
| | | |
| | | if (!MD5Util.verify(password, user.getPassword())) { |
| | | return ServerResponse.createBySuccessMsg("密码错误"); |
| | | } |
| | | |
| | | if(user.getIsRoot() == 1){ |
| | | String token = JwtUtil.getToken(user); |
| | | Map<String,Object> map = new HashMap<>(); |
| | | map.put("token",token); |
| | | String loginJson = "[\n" + |
| | | " {\n" + |
| | | " \"title\":\"行情比对\",\n" + |
| | | " \"name\":\"index\",\n" + |
| | | " \"path\":\"/index\",\n" + |
| | | " \"icon\":\"el-icon-data-analysis\",\n" + |
| | | " \"component\":\"comparison\"\n" + |
| | | " },\n" + |
| | | " {\n" + |
| | | " \"title\":\"用户管理\",\n" + |
| | | " \"name\":\"user\",\n" + |
| | | " \"path\":\"/user\",\n" + |
| | | " \"icon\":\"el-icon-user\",\n" + |
| | | " \"component\": \"user\"\n" + |
| | | " }\n" + |
| | | "]"; |
| | | ObjectMapper objectMapper = new ObjectMapper(); |
| | | |
| | | List<LoginOut> loginOuts = objectMapper.readValue(loginJson, new TypeReference<List<LoginOut>>() {}); |
| | | map.put("menu",loginOuts); |
| | | return ServerResponse.createBySuccess(map); |
| | | } |
| | | |
| | | //判断是否锁定 |
| | | 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,Object> map = new HashMap<>(); |
| | | map.put("token",token); |
| | | String loginJson = "[\n" + |
| | | " {\n" + |
| | | " \"title\":\"行情比对\",\n" + |
| | | " \"name\":\"index\",\n" + |
| | | " \"path\":\"/index\",\n" + |
| | | " \"icon\":\"el-icon-data-analysis\",\n" + |
| | | " \"component\":\"comparison\"\n" + |
| | | " }\n" + |
| | | "]"; |
| | | ObjectMapper objectMapper = new ObjectMapper(); |
| | | List<LoginOut> loginOuts = objectMapper.readValue(loginJson, new TypeReference<List<LoginOut>>() {}); |
| | | map.put("menu",loginOuts); |
| | | |
| | | String key = "user_"; |
| | | RedisUtil.set(key+user.getId(),token); |
| | | String ip = IpAddressUtil.getIpAddress(request); |
| | | String address = null; |
| | | address = IpAddressUtil.getIpPossessionByFile(ip); |
| | | if(null == address){ |
| | | address = IpAddressUtil.getIpAddressByOnline(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); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | log.error("登录异常:"+e.getMessage()); |
| | | } |
| | | return ServerResponse.createBySuccessMsg("系统异常"); |
| | | } |
| | | |
| | | @PostMapping("/saveConfig") |
| | | public ServerResponse saveConfig(SaveConfigVo saveConfigVo) { |
| | | public ServerResponse saveConfig(@RequestBody SaveConfigVo saveConfigVo) { |
| | | // currencyMapper.delete(new LambdaQueryWrapper<ConfigCurrency>().eq(ConfigCurrency::getUserId,saveConfigVo.getUserId())); |
| | | saveConfigVo.getCurrencyList().forEach(f->{ |
| | | ConfigCurrency currency = new ConfigCurrency(); |
| | | currency.setUserId(saveConfigVo.getUserId()); |
| | |
| | | currency.setSell(f.getSell()); |
| | | currencyMapper.insert(currency); |
| | | }); |
| | | List<ConfigCurrency> configCurrencies = currencyMapper.selectList(new LambdaQueryWrapper<ConfigCurrency>().eq(ConfigCurrency::getUserId, saveConfigVo.getUserId())); |
| | | String key = "config_"; |
| | | Gson gson = new GsonBuilder().setPrettyPrinting().create(); |
| | | String json = gson.toJson(configCurrencies); |
| | | RedisUtil.set(key+saveConfigVo.getUserId(),json); |
| | | return ServerResponse.createBySuccess(); |
| | | } |
| | | |
| | | @PostMapping("/deleteConfig") |
| | | public ServerResponse deleteConfig(@RequestBody DeleteConfigVo deleteConfigVo) { |
| | | currencyMapper.delete(new LambdaQueryWrapper<ConfigCurrency>() |
| | | .eq(ConfigCurrency::getUserId,deleteConfigVo.getUserId()) |
| | | .in(ConfigCurrency::getId,deleteConfigVo.getConfigId())); |
| | | List<ConfigCurrency> configCurrencies = currencyMapper.selectList(new LambdaQueryWrapper<ConfigCurrency>().eq(ConfigCurrency::getUserId, deleteConfigVo.getUserId())); |
| | | String key = "config_"; |
| | | Gson gson = new GsonBuilder().setPrettyPrinting().create(); |
| | | String json = gson.toJson(configCurrencies); |
| | | RedisUtil.set(key+deleteConfigVo.getUserId(),json); |
| | | return ServerResponse.createBySuccess(); |
| | | } |
| | | |
| | | @PostMapping("/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)); |
| | | return ServerResponse.createBySuccess(configCurrencyPage); |
| | | |
| | | } |
| | | |
| | | @PostMapping("/saveUser") |
| | | public ServerResponse saveUser(User user) { |
| | | long count = userService.count(new LambdaQueryWrapper<User>().eq(User::getAccount, user.getAccount())); |
| | | if(count > 0){ |
| | | return ServerResponse.createByErrorMsg("账号已存在"); |
| | | } |
| | | user.setAddTime(Date.valueOf(LocalDate.now())); |
| | | user.setPassword(MD5Util.encrypt(user.getPassword())); |
| | | userService.save(user); |
| | | return ServerResponse.createBySuccess(); |
| | |
| | | @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); |
| | |
| | | } |
| | | |
| | | @PostMapping("/updateUser") |
| | | public ServerResponse deleteUser(User user) { |
| | | User byId = userService.getById(user.getId()); |
| | | if(null == byId){ |
| | | public ServerResponse deleteUser(UpdateUserVo updateUserVo) { |
| | | User user = userService.getById(updateUserVo.getId()); |
| | | if(null == user || user.getIsRoot() == 1){ |
| | | return ServerResponse.createByErrorMsg("用户不存在"); |
| | | } |
| | | user.setPassword(MD5Util.encrypt(user.getPassword())); |
| | | user.setAccount(updateUserVo.getAccount()); |
| | | user.setPassword(MD5Util.encrypt(updateUserVo.getPassword())); |
| | | user.setEndTime(updateUserVo.getEndTime()); |
| | | user.setIsLock(updateUserVo.getIsLock()); |
| | | 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>() |
| | | .eq(StringUtils.isNotEmpty(account), User::getAccount, account) |
| | | .eq(User::getIsShow,0) |
| | | .eq(User::getIsRoot,0) |
| | | .orderByDesc(User::getAddTime)); |
| | | return ServerResponse.createBySuccess(pageList); |
| | | } |
| | | } |