UserController.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package com.loan.system.controller.wechat;
  2. import com.loan.system.constant.JwtClaimsConstant;
  3. import com.loan.system.domain.dto.UserLoginDTO;
  4. import com.loan.system.domain.entity.Customer;
  5. import com.loan.system.domain.entity.Role;
  6. import com.loan.system.domain.entity.User;
  7. import com.loan.system.domain.enums.ExceptionEnum;
  8. import com.loan.system.domain.enums.RoleEnum;
  9. import com.loan.system.domain.pojo.Result;
  10. import com.loan.system.domain.vo.CustomerVO;
  11. import com.loan.system.domain.vo.UserLoginVO;
  12. import com.loan.system.exception.DescribeException;
  13. import com.loan.system.properties.JwtProperties;
  14. import com.loan.system.repository.RoleRepository;
  15. import com.loan.system.service.CustomerService;
  16. import com.loan.system.service.UserService;
  17. import com.loan.system.utils.JwtUtil;
  18. import com.loan.system.utils.ResultUtil;
  19. import io.swagger.annotations.Api;
  20. import io.swagger.annotations.ApiOperation;
  21. import org.apache.commons.lang3.ObjectUtils;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.web.bind.annotation.*;
  24. import javax.security.auth.login.LoginException;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. /**
  29. * @author Edwin
  30. * @date 2020/9/2 - 19:11
  31. */
  32. @RestController
  33. @RequestMapping("/wechat")
  34. @Api(tags = "微信用户接口")
  35. public class UserController {//包含内部人员、外部人员
  36. @Autowired
  37. private UserService userService;
  38. @Autowired
  39. private JwtProperties jwtProperties;
  40. @Autowired
  41. private RoleRepository roleRepository;
  42. @Autowired
  43. private CustomerService customerService;
  44. /*
  45. 1.先查询user查看是否为内部人员
  46. 2.若不是内部人员,查询外部人员
  47. 3.外部人员也不存在,进行注册(因为小程序端直接获取手机号,任何用户都可以登录)
  48. */
  49. @PostMapping("/login")
  50. @ApiOperation("微信登陆")
  51. public Result login(@RequestBody UserLoginDTO userLoginDTO){
  52. User user = userService.getUserByMobile(userLoginDTO.getTel());
  53. UserLoginVO userLoginVO = new UserLoginVO();
  54. //为微信用户生成jwt令牌
  55. Map<String ,Object> claims=new HashMap<>();
  56. Long userId = null;
  57. if (!ObjectUtils.isEmpty(user)){
  58. userLoginVO.setUserId(user.getId());
  59. userLoginVO.setRole(user.getRole());
  60. userLoginVO.setOpenid(user.getOpenid());
  61. }else{
  62. CustomerVO customer = customerService.getCustomerByMobile(userLoginDTO.getTel());
  63. if(ObjectUtils.isEmpty(customer)) {
  64. Customer customer1 = new Customer();
  65. customer1.setMobile(userLoginDTO.getTel());
  66. //补充
  67. customerService.addCustomer(customer1);
  68. customer = customerService.getCustomerByMobile(userLoginDTO.getTel());
  69. }
  70. userLoginVO.setUserId(customer.getId());
  71. userLoginVO.setRole(RoleEnum.EXTERNAL.getMsg());
  72. userLoginVO.setOpenid(customer.getOpenid());
  73. }
  74. claims.put(JwtClaimsConstant.USER_ID,userLoginVO.getUserId());
  75. String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
  76. userLoginVO.setToken(token);
  77. return ResultUtil.success("success", userLoginVO);
  78. }
  79. // @PostMapping("/login")
  80. // @ApiOperation("微信登陆")
  81. // public Result login(@RequestBody UserLoginDTO userLoginDTO){
  82. // //微信登录
  83. // User user = userService.wxLogin(userLoginDTO);
  84. //
  85. // //为微信用户生成jwt令牌
  86. // Map<String ,Object> claims=new HashMap<>();
  87. // claims.put(JwtClaimsConstant.USER_ID,user.getId());
  88. // String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
  89. //
  90. // UserLoginVO userLoginVO = new UserLoginVO();
  91. // userLoginVO.setUserId(user.getId());
  92. // userLoginVO.setOpenid(user.getOpenid());
  93. // userLoginVO.setRole(user.getRole());
  94. // userLoginVO.setToken(token);
  95. //
  96. // return ResultUtil.success("success", userLoginVO);
  97. // }
  98. @PostMapping("/role")
  99. @ApiOperation("添加用户角色")
  100. public Result addRole(@RequestParam Long userId, @RequestBody Role role){
  101. roleRepository.save(role);
  102. return ResultUtil.success("success");
  103. }
  104. @GetMapping("/user/info")
  105. @ApiOperation("查询用户信息")
  106. public Result findUserInfo(){
  107. return ResultUtil.success("success");
  108. }
  109. @GetMapping("/customers")
  110. @ApiOperation("查询所有客户")
  111. public Result findAllCustomers(){
  112. return ResultUtil.success("success", customerService.getAllCustomers(false));
  113. }
  114. @GetMapping("/customers/{id}")
  115. @ApiOperation("按id选择客户")
  116. public Result findCustomerById(@PathVariable Long id){
  117. return ResultUtil.success("success", customerService.findByCustomerId( id));
  118. }
  119. @GetMapping("/customers/{key}")
  120. @ApiOperation("按关键字(姓名/手机号)选择客户")
  121. public Result findCustomerByKey(@PathVariable("key") String key){
  122. return ResultUtil.success("success", customerService.getCustomerByKey(key,false));
  123. }
  124. @GetMapping("/users")
  125. @ApiOperation("查询所有用户")
  126. public Result findAllUsers(Boolean isDelete){
  127. return ResultUtil.success("success", userService.getAllUsers(isDelete));
  128. }
  129. @GetMapping("/recommenders")
  130. @ApiOperation("查询所有推荐人")
  131. public Result findAllRecommenders(Boolean isDelete){
  132. return ResultUtil.success("success", userService.getAllRecommenders(isDelete));
  133. }
  134. }