UserController.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package com.ims.eval.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.ims.common.utils.RSAUtils;
  4. import com.ims.eval.entity.dto.result.R;
  5. import com.ims.eval.entity.Myuser;
  6. import com.ims.eval.feign.RemoteServiceBuilder;
  7. import com.ims.eval.service.IUserService;
  8. import io.swagger.annotations.ApiOperation;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.web.bind.annotation.*;
  12. import javax.servlet.http.HttpServletRequest;
  13. import java.security.KeyPair;
  14. import java.util.Base64;
  15. import java.util.List;
  16. import java.util.concurrent.atomic.AtomicBoolean;
  17. /**
  18. * <p>
  19. * 前端控制器
  20. * </p>
  21. *
  22. * @author wang
  23. * @since 2023-03-03
  24. */
  25. @Slf4j
  26. @RestController
  27. @RequestMapping("//user")
  28. public class UserController {
  29. @Autowired
  30. private IUserService userService;
  31. @Autowired
  32. private RemoteServiceBuilder remoteServiceBuilder;
  33. @Autowired
  34. private HttpServletRequest request;
  35. @GetMapping("/publicKey")
  36. public R getPublicKey() throws Exception {
  37. try {
  38. KeyPair keyPair = RSAUtils.generateKeyPair(2048);
  39. String publicKey = Base64.getEncoder().encodeToString(keyPair.getPublic().getEncoded());
  40. return R.ok().data(publicKey);
  41. } catch (Exception e) {
  42. e.printStackTrace();
  43. return R.error("登录失败");
  44. }
  45. }
  46. @GetMapping("/getEncrypt")
  47. public String getEncrypt(String pwd) throws Exception {
  48. KeyPair keyPair = RSAUtils.generateKeyPair(2048);
  49. // 加密
  50. byte[] encryptedData = RSAUtils.encrypt(pwd.getBytes(), keyPair.getPublic());
  51. String str2 = Base64.getEncoder().encodeToString(encryptedData).replaceAll(" ", "+");
  52. return str2;
  53. }
  54. /**
  55. * 更具code获取用户信息
  56. *
  57. * @return
  58. */
  59. @GetMapping(value = "getSysUser")
  60. public JSONObject getSysUser(
  61. @RequestParam(value = "code", required = false) String code) {
  62. JSONObject json = null;
  63. try {
  64. json = remoteServiceBuilder.getGatewayUrl().getSysUser(code);
  65. } catch (Exception e) {
  66. e.printStackTrace();
  67. }
  68. return json;
  69. }
  70. /**
  71. * 根据token获取code
  72. *
  73. * @return
  74. */
  75. @GetMapping(value = "getCodeByToken")
  76. public JSONObject getCodeByToken(
  77. @RequestParam(value = "token", required = false) String token) {
  78. JSONObject json = null;
  79. try {
  80. json = remoteServiceBuilder.getGatewayUrl().getCodeByToken(token);
  81. } catch (Exception e) {
  82. e.printStackTrace();
  83. }
  84. return json;
  85. }
  86. /**
  87. * 登录接口
  88. *
  89. * @param username
  90. * @param password
  91. * @return
  92. */
  93. @PostMapping(value = "bladeAuth")
  94. public R bladeAuth(
  95. @RequestParam(value = "username", required = false) String username,
  96. @RequestParam(value = "password", required = false) String password) {
  97. JSONObject json = null;
  98. try {
  99. // json = userService.getbladeAuth("000000",username,password,"password","all","account");
  100. json = userService.getbladeAuth(username, password);
  101. } catch (Exception e) {
  102. log.error("错误", e);
  103. return R.ok().error("登录失败");
  104. }
  105. return R.ok().data(json);
  106. }
  107. @GetMapping(value = "pageList")
  108. public JSONObject pageList(
  109. @RequestParam(value = "current", required = false) Integer current,
  110. @RequestParam(value = "size", required = false) Integer size,
  111. @RequestParam(value = "orgId", required = false) String orgId,
  112. @RequestParam(value = "idCard", required = false) String idCard,
  113. @RequestParam(value = "mobile", required = false) String mobile,
  114. @RequestParam(value = "no", required = false) String no,
  115. @RequestParam(value = "name", required = false) String name,
  116. @RequestParam(value = "loginName", required = false) String loginName) {
  117. JSONObject json = null;
  118. try {
  119. json = userService.pageList(current, size, orgId, idCard, mobile, no, name, loginName, request);
  120. } catch (Exception e) {
  121. log.error("错误", e);
  122. return null;
  123. }
  124. return json;
  125. }
  126. @GetMapping(value = "/getLoginNameByUserInfo")
  127. public JSONObject getLoginNameByUserInfo(@RequestParam(value = "info") String info) {
  128. JSONObject json = null;
  129. try {
  130. json = userService.getLoginNameByUserInfo(info, request);
  131. } catch (Exception e) {
  132. log.error("错误", e);
  133. return null;
  134. }
  135. return json;
  136. }
  137. /**
  138. * 退出登录
  139. *
  140. * @param token
  141. * @return
  142. */
  143. @PostMapping(value = "removeByToken")
  144. public JSONObject removeByToken(@RequestParam(value = "token", required = true) String token) {
  145. JSONObject json = null;
  146. try {
  147. json = remoteServiceBuilder.getGatewayUrl().removeByToken(token);
  148. } catch (Exception e) {
  149. log.error("错误", e);
  150. }
  151. return json;
  152. }
  153. //@ImsPreAuth("eval:dataDictionary:edit")
  154. @PostMapping(value = "/save")
  155. @ApiOperation(value = "新增(修改)", notes = "新增(修改)")
  156. public R addAll(@RequestBody List<Myuser> user) {
  157. AtomicBoolean b = new AtomicBoolean(false);
  158. user.stream().forEach(i -> {
  159. b.set(userService.saveOrUpdate(i));
  160. });
  161. if (b.get()) {
  162. return R.ok().data(b);
  163. } else {
  164. return R.error().data("保存失败!");
  165. }
  166. }
  167. /**
  168. * 查询所有数据
  169. *
  170. * @param id 主键
  171. * @param orgId 组织id
  172. * @return
  173. */
  174. //@ImsPreAuth("eval:organizationEvaluationRule:view")
  175. @GetMapping(value = "listAll")
  176. public R listAll(
  177. @RequestParam(value = "id", required = false) String id,
  178. @RequestParam(value = "orgId", required = false) String orgId,
  179. @RequestParam(value = "unitId", required = false) String unitId) {
  180. List<Myuser> list = userService.listAll(id, orgId, unitId);
  181. return R.ok().data(list);
  182. }
  183. /**
  184. * 查询所有数据
  185. *
  186. * @param id 主键
  187. * @return
  188. */
  189. //@ImsPreAuth("eval:organizationEvaluationRule:view")
  190. @GetMapping(value = "getId")
  191. public R getByid(
  192. @RequestParam(value = "id", required = false) String id) {
  193. Myuser user = userService.getById(id);
  194. return R.ok().data(user);
  195. }
  196. }