| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- package api.controller;
- import api.entity.database.system.Logininfor;
- import api.entity.database.system.Role;
- import api.entity.database.system.User;
- import api.entity.database.system.UserRole;
- import api.entity.input.CodeInput;
- import api.entity.input.LoginInput;
- import api.model.AjaxResult;
- import api.service.system.ILogininforService;
- import api.service.system.IRoleService;
- import api.service.system.IUserRoleService;
- import api.service.system.IUserService;
- import api.util.annotation.Anonymous;
- import api.util.constants.CacheConstants;
- import api.util.helper.*;
- import com.alibaba.fastjson2.JSON;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
- import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.multipart.MultipartFile;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.*;
- import java.util.stream.Collectors;
- @Api(value = "默认", tags = "默认")
- @Slf4j
- @Anonymous
- @RestController
- @RequestMapping()
- public class HomeController extends BaseController {
- @Autowired
- private RedisHelper redisHelper;
- @Autowired
- private IUserService userService;
- @Autowired
- private IUserRoleService userRoleService;
- @Autowired
- private IRoleService roleService;
- @Autowired
- private ILogininforService logininforService;
- @ApiOperation("Hello World")
- @GetMapping("/")
- public AjaxResult Info() {
- return Success("Hello World!", new Date());
- }
- @ApiOperation("导出Excel")
- @PostMapping("/exportExcel")
- public void ExportExcel() {
- List<User> list = userService.getList();
- ExcelHelper<User> excel = new ExcelHelper<>(User.class);
- excel.exportExcel("xlsx", list);
- }
- @ApiOperation("导入Excel")
- @PostMapping("/importExcel")
- public AjaxResult importExcel(@RequestPart MultipartFile file) throws Exception {
- ExcelHelper<User> excel = new ExcelHelper<>(User.class);
- List<User> list = excel.importExcel(file.getInputStream());
- if (list == null) {
- return Error("导入失败");
- }
- return Success("成功", list);
- }
- @ApiOperation("登录")
- @PostMapping("/login")
- public AjaxResult Login(@RequestBody LoginInput input){
- HttpServletRequest request = ServletHelper.getRequest();
- Logininfor logininfor = new Logininfor();
- logininfor.setUserName(input.getUserName());
- logininfor.setIpaddr(ServletHelper.getIpAddr(request));
- logininfor.setBrowser(ServletHelper.getBrowser(request));
- logininfor.setOs(ServletHelper.getOs(request));
- logininfor.setLoginTime(new Date());
- if(!StringHelper.isEmpty(input.getSource())){
- logininfor.setSource(input.getSource());
- }
- else {
- logininfor.setSource("PC");
- }
- if (input == null || StringHelper.isEmpty(input.getUserName()) || StringHelper.isEmpty(input.getPassword()) || StringHelper.isEmpty(input.getCode())) {
- logininfor.setMsg("请输入账号、密码、验证码");
- logininfor.setStatus("1");
- logininforService.insert(logininfor);
- return Error("请输入账号、密码、验证码");
- }
- if (StringHelper.isEmpty(input.getUuid())) {
- logininfor.setMsg("验证码已过期");
- logininfor.setStatus("1");
- logininforService.insert(logininfor);
- return Error("验证码已过期");
- }
- String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + input.getUuid();
- String code = redisHelper.getCache(verifyKey);
- if (StringHelper.isEmpty(code) || !Objects.equals(input.getCode(), code)) {
- logininfor.setMsg("验证码已过期");
- logininfor.setStatus("1");
- logininforService.insert(logininfor);
- return Error("验证码已过期");
- }
- String encrypt = SecretHelper.MD5(input.getPassword());
- LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
- try{
- int usercode= Integer.parseInt( input.getUserName());
- qw.eq(User::getUserName,input.getUserName());
- }catch (Exception e ){
- qw.eq(User::getNickName,input.getUserName());
- }
- qw .eq(User::getPassword, encrypt)
- .eq(User::getDeleteFlag, 0);
- User user = userService.getEntity(qw);
- if (user == null) {
- logininfor.setMsg("账号或者密码错误");
- logininfor.setStatus("1");
- logininforService.insert(logininfor);
- return Error("账号或者密码错误");
- }
- if (Objects.equals(user.getDeleteFlag(), "1")) {
- logininfor.setMsg("账号已禁用"+input.getUserName()+"分机号-" + input.getExtensionPhone());//
- logininfor.setStatus("1");
- logininforService.insert(logininfor);
- return Error("账号已禁用");
- }else if(Objects.equals(user.getDeleteFlag(), "2")){
- LambdaQueryWrapper<Logininfor> loginqw=new LambdaQueryWrapper<>();
- loginqw.eq(Logininfor::getUserName,input.getUserName()).like(Logininfor::getMsg,"锁定账号登录").orderByDesc(Logininfor::getInfoId);
- List<Logininfor> inforList= logininforService.getList(loginqw);
- if(inforList!=null &&inforList.stream().count()>0) {
- if (DateHelper.calcutemin(new Date(), inforList.stream().findFirst().get().getLoginTime()) > 30) {
- LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
- updateWrapper.eq(User::getUserId, user.getUserId()).set(User::getDeleteFlag, 0);
- userService.updateBatch(updateWrapper);
- } else {
- logininfor.setMsg("锁定账号登录:帐号-" + input.getUserName() + ";分机号-" + input.getExtensionPhone());
- logininfor.setStatus("1");
- logininforService.insert(logininfor);
- return Error("此账号已经被锁定,请联系管理员解锁");
- }
- }else {
- logininfor.setMsg("锁定账号登录:帐号-" + input.getUserName() + ";分机号-" + input.getExtensionPhone());
- logininfor.setStatus("1");
- logininforService.insert(logininfor);
- return Error("此账号已经被锁定,请联系管理员解锁");
- }
- }
- user.setLastLoginDate(new Date());
- user.setLoginIp(ServletHelper.getIpAddr(request));
- userService.update(user);
- redisHelper.deleteCache(verifyKey);
- // List<Long> roleids = userRoleService.getList().stream().filter(p -> p.getUserId().equals(user.getUserId()))
- // .map(UserRole::getRoleId).sorted().collect(Collectors.toList());
- Role role=roleService.getEntity(user.getRoleId());
- Map<String, Object> cls = new HashMap<>();
- cls.put("UserId", user.getUserId());
- cls.put("RoleId", role.getRoleId());
- cls.put("RoleCode", role.getRoleCode());
- String token = JwtHelper.createToken(cls);
-
- // Calendar oldCal = Calendar.getInstance();
- // if (user.getUpdatePasswordTime() == null) {
- // oldCal.setTime(user.getCreateTime());
- // } else {
- // oldCal.setTime(user.getUpdatePasswordTime());
- // }
- // oldCal.add(2, 3);
- // Calendar nowCal = Calendar.getInstance();
- // nowCal.setTime(new Date());
- // //判断最后密码修改时间是否在三个月内
- // Boolean resetPassword = oldCal.after(nowCal);
- // Boolean easyPassword = FormatHelper.checkPassword(input.getPassword());
- Map<String, Object> data = new HashMap<>();
- data.put("token", token);
- // data.put("resetPassword", resetPassword);
- // data.put("easyPassword", easyPassword);
- logininfor.setStatus("0");
- logininfor.setMsg("登录成功");
- logininforService.insert(logininfor);
- return Success("登录成功", data);
- }
- @ApiOperation("退出")
- @PostMapping("/logout")
- public AjaxResult LoginOut() {
- return Success("");
- }
- @ApiOperation("代码生成")
- @GetMapping("/getcode")
- public void GetCode(HttpServletResponse response, CodeInput input) {
- Map<String, Object> table = SqlRunner.db().selectOne("select table_name,table_comment from information_schema.tables"
- + " where table_schema = (select database()) AND table_name = {0} ", input.getTableName());
- if (table == null) {
- Map map = new HashMap();
- map.put("state", "error");
- map.put("message", "此表不存在");
- try {
- PrintWriter out = response.getWriter();
- out.write(JSON.toJSONString(map));
- out.flush();
- out.close();
- } catch (IOException ex) {
- throw new RuntimeException(ex);
- }
- return;
- }
- List<Map<String, Object>> columns = SqlRunner.db().selectList("select column_name,data_type,column_type,is_nullable,"
- + "column_key,column_comment from information_schema.columns where table_schema = (select database()) "
- + "and table_name = {0} order by ordinal_position", input.getTableName());
- CodeHelper.GetCode(response, table, columns, input.getPackageName(), input.getControllerName());
- }
- @ApiOperation(value = "客户上传图片")
- @PostMapping("/online/upload/{userId}")
- public AjaxResult uploadFile(MultipartFile file,@PathVariable String userId) {
- String path="files/online/"+ DateHelper.getDate();
- String filePath = FileUploadHelper.uploadImage(path,file);
- return Success("上传成功",filePath);
- }
- }
|