鹤壁政务服务热线

HomeController.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package api.controller;
  2. import api.entity.database.system.Logininfor;
  3. import api.entity.database.system.Role;
  4. import api.entity.database.system.User;
  5. import api.entity.database.system.UserRole;
  6. import api.entity.input.CodeInput;
  7. import api.entity.input.LoginInput;
  8. import api.model.AjaxResult;
  9. import api.service.system.ILogininforService;
  10. import api.service.system.IRoleService;
  11. import api.service.system.IUserRoleService;
  12. import api.service.system.IUserService;
  13. import api.util.annotation.Anonymous;
  14. import api.util.constants.CacheConstants;
  15. import api.util.helper.*;
  16. import com.alibaba.fastjson2.JSON;
  17. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  18. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  19. import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
  20. import io.swagger.annotations.Api;
  21. import io.swagger.annotations.ApiOperation;
  22. import lombok.extern.slf4j.Slf4j;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.web.bind.annotation.*;
  25. import org.springframework.web.multipart.MultipartFile;
  26. import javax.servlet.http.HttpServletRequest;
  27. import javax.servlet.http.HttpServletResponse;
  28. import java.io.IOException;
  29. import java.io.PrintWriter;
  30. import java.util.*;
  31. import java.util.stream.Collectors;
  32. @Api(value = "默认", tags = "默认")
  33. @Slf4j
  34. @Anonymous
  35. @RestController
  36. @RequestMapping()
  37. public class HomeController extends BaseController {
  38. @Autowired
  39. private RedisHelper redisHelper;
  40. @Autowired
  41. private IUserService userService;
  42. @Autowired
  43. private IUserRoleService userRoleService;
  44. @Autowired
  45. private IRoleService roleService;
  46. @Autowired
  47. private ILogininforService logininforService;
  48. @ApiOperation("Hello World")
  49. @GetMapping("/")
  50. public AjaxResult Info() {
  51. return Success("Hello World!", new Date());
  52. }
  53. @ApiOperation("导出Excel")
  54. @PostMapping("/exportExcel")
  55. public void ExportExcel() {
  56. List<User> list = userService.getList();
  57. ExcelHelper<User> excel = new ExcelHelper<>(User.class);
  58. excel.exportExcel("xlsx", list);
  59. }
  60. @ApiOperation("导入Excel")
  61. @PostMapping("/importExcel")
  62. public AjaxResult importExcel(@RequestPart MultipartFile file) throws Exception {
  63. ExcelHelper<User> excel = new ExcelHelper<>(User.class);
  64. List<User> list = excel.importExcel(file.getInputStream());
  65. if (list == null) {
  66. return Error("导入失败");
  67. }
  68. return Success("成功", list);
  69. }
  70. @ApiOperation("登录")
  71. @PostMapping("/login")
  72. public AjaxResult Login(@RequestBody LoginInput input){
  73. HttpServletRequest request = ServletHelper.getRequest();
  74. Logininfor logininfor = new Logininfor();
  75. logininfor.setUserName(input.getUserName());
  76. logininfor.setIpaddr(ServletHelper.getIpAddr(request));
  77. logininfor.setBrowser(ServletHelper.getBrowser(request));
  78. logininfor.setOs(ServletHelper.getOs(request));
  79. logininfor.setLoginTime(new Date());
  80. if(!StringHelper.isEmpty(input.getSource())){
  81. logininfor.setSource(input.getSource());
  82. }
  83. else {
  84. logininfor.setSource("PC");
  85. }
  86. if (input == null || StringHelper.isEmpty(input.getUserName()) || StringHelper.isEmpty(input.getPassword()) || StringHelper.isEmpty(input.getCode())) {
  87. logininfor.setMsg("请输入账号、密码、验证码");
  88. logininfor.setStatus("1");
  89. logininforService.insert(logininfor);
  90. return Error("请输入账号、密码、验证码");
  91. }
  92. if (StringHelper.isEmpty(input.getUuid())) {
  93. logininfor.setMsg("验证码已过期");
  94. logininfor.setStatus("1");
  95. logininforService.insert(logininfor);
  96. return Error("验证码已过期");
  97. }
  98. String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + input.getUuid();
  99. String code = redisHelper.getCache(verifyKey);
  100. if (StringHelper.isEmpty(code) || !Objects.equals(input.getCode(), code)) {
  101. logininfor.setMsg("验证码已过期");
  102. logininfor.setStatus("1");
  103. logininforService.insert(logininfor);
  104. return Error("验证码已过期");
  105. }
  106. String encrypt = SecretHelper.MD5(input.getPassword());
  107. LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
  108. try{
  109. int usercode= Integer.parseInt( input.getUserName());
  110. qw.eq(User::getUserName,input.getUserName());
  111. }catch (Exception e ){
  112. qw.eq(User::getNickName,input.getUserName());
  113. }
  114. qw .eq(User::getPassword, encrypt)
  115. .eq(User::getDeleteFlag, 0);
  116. User user = userService.getEntity(qw);
  117. if (user == null) {
  118. logininfor.setMsg("账号或者密码错误");
  119. logininfor.setStatus("1");
  120. logininforService.insert(logininfor);
  121. return Error("账号或者密码错误");
  122. }
  123. if (Objects.equals(user.getDeleteFlag(), "1")) {
  124. logininfor.setMsg("账号已禁用"+input.getUserName()+"分机号-" + input.getExtensionPhone());//
  125. logininfor.setStatus("1");
  126. logininforService.insert(logininfor);
  127. return Error("账号已禁用");
  128. }else if(Objects.equals(user.getDeleteFlag(), "2")){
  129. LambdaQueryWrapper<Logininfor> loginqw=new LambdaQueryWrapper<>();
  130. loginqw.eq(Logininfor::getUserName,input.getUserName()).like(Logininfor::getMsg,"锁定账号登录").orderByDesc(Logininfor::getInfoId);
  131. List<Logininfor> inforList= logininforService.getList(loginqw);
  132. if(inforList!=null &&inforList.stream().count()>0) {
  133. if (DateHelper.calcutemin(new Date(), inforList.stream().findFirst().get().getLoginTime()) > 30) {
  134. LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
  135. updateWrapper.eq(User::getUserId, user.getUserId()).set(User::getDeleteFlag, 0);
  136. userService.updateBatch(updateWrapper);
  137. } else {
  138. logininfor.setMsg("锁定账号登录:帐号-" + input.getUserName() + ";分机号-" + input.getExtensionPhone());
  139. logininfor.setStatus("1");
  140. logininforService.insert(logininfor);
  141. return Error("此账号已经被锁定,请联系管理员解锁");
  142. }
  143. }else {
  144. logininfor.setMsg("锁定账号登录:帐号-" + input.getUserName() + ";分机号-" + input.getExtensionPhone());
  145. logininfor.setStatus("1");
  146. logininforService.insert(logininfor);
  147. return Error("此账号已经被锁定,请联系管理员解锁");
  148. }
  149. }
  150. user.setLastLoginDate(new Date());
  151. user.setLoginIp(ServletHelper.getIpAddr(request));
  152. userService.update(user);
  153. redisHelper.deleteCache(verifyKey);
  154. // List<Long> roleids = userRoleService.getList().stream().filter(p -> p.getUserId().equals(user.getUserId()))
  155. // .map(UserRole::getRoleId).sorted().collect(Collectors.toList());
  156. Role role=roleService.getEntity(user.getRoleId());
  157. Map<String, Object> cls = new HashMap<>();
  158. cls.put("UserId", user.getUserId());
  159. cls.put("RoleId", role.getRoleId());
  160. cls.put("RoleCode", role.getRoleCode());
  161. String token = JwtHelper.createToken(cls);
  162. // Calendar oldCal = Calendar.getInstance();
  163. // if (user.getUpdatePasswordTime() == null) {
  164. // oldCal.setTime(user.getCreateTime());
  165. // } else {
  166. // oldCal.setTime(user.getUpdatePasswordTime());
  167. // }
  168. // oldCal.add(2, 3);
  169. // Calendar nowCal = Calendar.getInstance();
  170. // nowCal.setTime(new Date());
  171. // //判断最后密码修改时间是否在三个月内
  172. // Boolean resetPassword = oldCal.after(nowCal);
  173. // Boolean easyPassword = FormatHelper.checkPassword(input.getPassword());
  174. Map<String, Object> data = new HashMap<>();
  175. data.put("token", token);
  176. // data.put("resetPassword", resetPassword);
  177. // data.put("easyPassword", easyPassword);
  178. logininfor.setStatus("0");
  179. logininfor.setMsg("登录成功");
  180. logininforService.insert(logininfor);
  181. return Success("登录成功", data);
  182. }
  183. @ApiOperation("退出")
  184. @PostMapping("/logout")
  185. public AjaxResult LoginOut() {
  186. return Success("");
  187. }
  188. @ApiOperation("代码生成")
  189. @GetMapping("/getcode")
  190. public void GetCode(HttpServletResponse response, CodeInput input) {
  191. Map<String, Object> table = SqlRunner.db().selectOne("select table_name,table_comment from information_schema.tables"
  192. + " where table_schema = (select database()) AND table_name = {0} ", input.getTableName());
  193. if (table == null) {
  194. Map map = new HashMap();
  195. map.put("state", "error");
  196. map.put("message", "此表不存在");
  197. try {
  198. PrintWriter out = response.getWriter();
  199. out.write(JSON.toJSONString(map));
  200. out.flush();
  201. out.close();
  202. } catch (IOException ex) {
  203. throw new RuntimeException(ex);
  204. }
  205. return;
  206. }
  207. List<Map<String, Object>> columns = SqlRunner.db().selectList("select column_name,data_type,column_type,is_nullable,"
  208. + "column_key,column_comment from information_schema.columns where table_schema = (select database()) "
  209. + "and table_name = {0} order by ordinal_position", input.getTableName());
  210. CodeHelper.GetCode(response, table, columns, input.getPackageName(), input.getControllerName());
  211. }
  212. @ApiOperation(value = "客户上传图片")
  213. @PostMapping("/online/upload/{userId}")
  214. public AjaxResult uploadFile(MultipartFile file,@PathVariable String userId) {
  215. String path="files/online/"+ DateHelper.getDate();
  216. String filePath = FileUploadHelper.uploadImage(path,file);
  217. return Success("上传成功",filePath);
  218. }
  219. }