| | |
| | | package org.example.controller; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.google.gson.Gson; |
| | | import com.google.gson.GsonBuilder; |
| | | 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.pojo.vo.DeleteConfigVo; |
| | | import org.example.pojo.vo.SaveConfigVo; |
| | | 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.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.Base64; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * @program: demo |
| | |
| | | @Autowired |
| | | private UserServiceImpl userService; |
| | | |
| | | @Autowired |
| | | private LogMapper logMapper; |
| | | |
| | | @PostMapping("/login") |
| | | public ServerResponse saveUser(@RequestParam("account") String account |
| | | , @RequestParam("password") String password, HttpServletRequest request) { |
| | | |
| | | 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.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); |
| | | 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); |
| | | } |
| | | |
| | | // 生成指定长度的随机 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(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.deleteBatchIds(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("/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(); |