BackendLoginController.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package com.fuint.module.backendApi.controller;
  2. import com.fuint.common.dto.AccountInfo;
  3. import com.fuint.common.enums.AdminRoleEnum;
  4. import com.fuint.common.service.*;
  5. import com.fuint.common.util.TokenUtil;
  6. import com.fuint.common.util.TreeUtil;
  7. import com.fuint.common.vo.RouterVo;
  8. import com.fuint.framework.annoation.OperationServiceLog;
  9. import com.fuint.framework.exception.BusinessCheckException;
  10. import com.fuint.module.backendApi.request.LoginRequest;
  11. import com.fuint.common.Constants;
  12. import com.fuint.framework.web.BaseController;
  13. import com.fuint.framework.web.ResponseObject;
  14. import com.fuint.module.backendApi.response.LoginResponse;
  15. import com.fuint.repository.model.TAccount;
  16. import com.fuint.repository.model.TDuty;
  17. import com.fuint.repository.model.TSource;
  18. import com.fuint.utils.StringUtil;
  19. import io.swagger.annotations.Api;
  20. import io.swagger.annotations.ApiOperation;
  21. import org.springframework.web.bind.annotation.*;
  22. import com.fuint.common.domain.TreeNode;
  23. import javax.annotation.Resource;
  24. import javax.servlet.http.HttpServletRequest;
  25. import java.util.*;
  26. /**
  27. * 后台登录接口
  28. *
  29. * Created by FSQ
  30. * CopyRight https://www.fuint.cn
  31. */
  32. @Api(tags="管理端-后台登录相关接口")
  33. @RestController
  34. @RequestMapping("/backendApi/login")
  35. public class BackendLoginController extends BaseController {
  36. /**
  37. * 后台账号接口
  38. * */
  39. @Resource
  40. AccountService accountService;
  41. /**
  42. * 后台菜单接口
  43. * */
  44. @Resource
  45. SourceService sourceService;
  46. /**
  47. * 后台角色接口
  48. * */
  49. @Resource
  50. DutyService dutyService;
  51. /**
  52. * 验证码接口
  53. * */
  54. @Resource
  55. private CaptchaService captchaService;
  56. /**
  57. * 后台登录
  58. * */
  59. @ApiOperation(value = "后台登录")
  60. @RequestMapping(value="/doLogin", method = RequestMethod.POST)
  61. @OperationServiceLog(description = "登录后台系统")
  62. public ResponseObject doLogin(HttpServletRequest request, @RequestBody LoginRequest loginRequest) {
  63. String userAgent = request.getHeader("user-agent");
  64. String accountName = loginRequest.getUsername();
  65. String password = loginRequest.getPassword();
  66. String captchaCode = loginRequest.getCaptchaCode();
  67. String uuid = loginRequest.getUuid();
  68. Boolean captchaVerify = captchaService.checkCodeByUuid(captchaCode, uuid);
  69. if (!captchaVerify) {
  70. return getFailureResult(201,"图形验证码有误");
  71. }
  72. if (StringUtil.isEmpty(accountName)|| StringUtil.isEmpty(password) || StringUtil.isEmpty(captchaCode)) {
  73. return getFailureResult(Constants.HTTP_RESPONSE_CODE_PARAM_ERROR);
  74. } else {
  75. AccountInfo accountInfo = accountService.getAccountByName(loginRequest.getUsername());
  76. if (accountInfo == null) {
  77. return getFailureResult(Constants.HTTP_RESPONSE_CODE_USER_LOGIN_ERROR);
  78. }
  79. TAccount tAccount = accountService.getAccountInfoById(accountInfo.getId());
  80. String myPassword = tAccount.getPassword();
  81. String inputPassword = accountService.getEntryptPassword(password, tAccount.getSalt());
  82. if (!myPassword.equals(inputPassword) || !tAccount.getAccountStatus().toString().equals("1")) {
  83. return getFailureResult(201, "账号或密码有误");
  84. }
  85. String token = TokenUtil.generateToken(userAgent, accountInfo.getId());
  86. accountInfo.setToken(token);
  87. TokenUtil.saveAccountToken(accountInfo);
  88. LoginResponse response = new LoginResponse();
  89. response.setLogin(true);
  90. response.setToken(token);
  91. response.setTokenCreatedTime(new Date());
  92. return getSuccessResult(response);
  93. }
  94. }
  95. /**
  96. * 获取登录信息接口
  97. * */
  98. @ApiOperation(value = "获取登录信息")
  99. @RequestMapping(value = "/getInfo", method = RequestMethod.GET)
  100. public ResponseObject getInfo(HttpServletRequest request) throws BusinessCheckException {
  101. String token = request.getHeader("Access-Token");
  102. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  103. if (accountInfo == null) {
  104. return getFailureResult(401, "登录信息已失效,请重新登录");
  105. }
  106. TAccount tAccount = accountService.getAccountInfoById(accountInfo.getId());
  107. if (accountInfo == null || tAccount == null || !tAccount.getAccountStatus().toString().equals("1")) {
  108. return getFailureResult(Constants.HTTP_RESPONSE_CODE_NOLOGIN);
  109. }
  110. List<Long> roleIds = accountService.getRoleIdsByAccountId(accountInfo.getId());
  111. List<String> roles = new ArrayList<>();
  112. if (roleIds.size() > 0) {
  113. for (int i = 0; i < roleIds.size(); i++) {
  114. TDuty role = dutyService.getRoleById(roleIds.get(i));
  115. for (AdminRoleEnum item : AdminRoleEnum.values()) {
  116. if (role.getDutyType().equals(item.getKey())) {
  117. roles.add(item.getValue());
  118. }
  119. }
  120. }
  121. }
  122. List<TSource> sources = sourceService.getMenuListByUserId(accountInfo.getMerchantId(), accountInfo.getId());
  123. List<String> permissions = new ArrayList<>();
  124. if (sources.size() > 0) {
  125. for (TSource source : sources) {
  126. if (source.getPath() != null) {
  127. String permission = source.getPath().replaceAll("/", ":");
  128. permissions.add(permission);
  129. }
  130. }
  131. }
  132. Map<String, Object> result = new HashMap<>();
  133. result.put("accountInfo", accountInfo);
  134. result.put("roles", roles);
  135. result.put("permissions", permissions);
  136. return getSuccessResult(result);
  137. }
  138. /**
  139. * 获取登录路由菜单接口
  140. *
  141. * @return
  142. */
  143. @ApiOperation(value = "获取登录路由菜单接口")
  144. @RequestMapping(value = "/getRouters", method = RequestMethod.GET)
  145. @CrossOrigin
  146. public ResponseObject getRouters(HttpServletRequest request) throws BusinessCheckException {
  147. String token = request.getHeader("Access-Token");
  148. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  149. if (accountInfo == null) {
  150. return getFailureResult(401, "登录信息已失效,请重新登录");
  151. }
  152. List<TSource> sources = sourceService.getMenuListByUserId(accountInfo.getMerchantId(), accountInfo.getId());
  153. List<TreeNode> trees = new ArrayList<>();
  154. TreeNode treeNode;
  155. for (TSource tSource : sources) {
  156. treeNode = new TreeNode();
  157. treeNode.setName(tSource.getSourceName());
  158. treeNode.setEname(tSource.getEname());
  159. treeNode.setNewIcon(tSource.getNewIcon());
  160. treeNode.setPath(tSource.getPath());
  161. treeNode.setId(tSource.getSourceId());
  162. treeNode.setLevel(tSource.getSourceLevel());
  163. treeNode.setIsMenu(tSource.getIsMenu());
  164. treeNode.setSort((tSource.getSourceStyle() == null || StringUtil.isEmpty(tSource.getSourceStyle())) ? 0 : Integer.parseInt(tSource.getSourceStyle()));
  165. if (tSource.getParentId() != null) {
  166. treeNode.setpId(tSource.getParentId());
  167. }
  168. treeNode.setUrl(tSource.getSourceCode());
  169. treeNode.setIcon(tSource.getIcon());
  170. trees.add(treeNode);
  171. }
  172. List<TreeNode> treeNodes = TreeUtil.sourceTreeNodes(trees);
  173. List<RouterVo> routers = sourceService.buildMenus(treeNodes);
  174. return getSuccessResult(routers);
  175. }
  176. /**
  177. * 退出后台登录
  178. * */
  179. @ApiOperation(value = "退出后台登录")
  180. @RequestMapping(value = "/logout", method = RequestMethod.POST)
  181. @OperationServiceLog(description = "退出后台系统")
  182. public ResponseObject logout(HttpServletRequest request) {
  183. String token = request.getHeader("Access-Token");
  184. if (StringUtil.isEmpty(token)) {
  185. return getFailureResult(Constants.HTTP_RESPONSE_CODE_USER_NOT_EXIST);
  186. }
  187. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  188. if (accountInfo != null) {
  189. TokenUtil.removeToken(token);
  190. }
  191. return getSuccessResult(true);
  192. }
  193. }