UserController.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.dto.WeChatLoginDTO;
  5. import com.loan.system.domain.entity.Customer;
  6. import com.loan.system.domain.entity.User;
  7. import com.loan.system.domain.pojo.Result;
  8. import com.loan.system.domain.vo.CustomerLoginVO;
  9. import com.loan.system.domain.vo.UserLoginVO;
  10. import com.loan.system.properties.JwtProperties;
  11. import com.loan.system.service.CustomerService;
  12. import com.loan.system.service.Impl.UserServiceImpl;
  13. import com.loan.system.service.UserService;
  14. import com.loan.system.service.WxService;
  15. import com.loan.system.utils.JwtUtil;
  16. import com.loan.system.utils.ResultUtil;
  17. import io.swagger.annotations.Api;
  18. import io.swagger.annotations.ApiOperation;
  19. import org.apache.commons.lang3.ObjectUtils;
  20. import org.slf4j.Logger;
  21. import org.slf4j.LoggerFactory;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.web.bind.annotation.*;
  24. import java.time.LocalDateTime;
  25. import java.time.format.DateTimeFormatter;
  26. import java.util.Date;
  27. import java.util.HashMap;
  28. import java.util.Map;
  29. /**
  30. * @author Edwin
  31. * @date 2020/9/2 - 19:11
  32. */
  33. @RestController
  34. @RequestMapping("/WeChat")
  35. @Api(tags = "微信用户接口")
  36. public class UserController {
  37. @Autowired
  38. private UserService userService;
  39. @Autowired
  40. private JwtProperties jwtProperties;
  41. @Autowired
  42. private WxService wxService;
  43. @Autowired
  44. private CustomerService customerService;
  45. private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
  46. @GetMapping("/get_sessionId")
  47. @ApiOperation("获取微信openid")
  48. public Result get_sessionId(String code){
  49. return userService.get_sessionId(code);
  50. }
  51. /**
  52. * 微信登陆 区分用户还是客户 如果都不是就注册成客户
  53. * @param wxAuth
  54. * @return
  55. */
  56. // @PostMapping("/authlogin")
  57. // @ApiOperation("微信授权登陆获取openid")
  58. // public Result authLogin(@RequestBody WXAuth wxAuth){
  59. // try {
  60. // String json=wxService.wxDecrypt(wxAuth.getEncryptedData(),wxAuth.getIv(),wxAuth.getSessionKey());
  61. // JSONObject jsonObject = JSON.parseObject(json);
  62. // String openid=jsonObject.getString("openId");
  63. // log.info("获取到的用户openid:{}",openid);
  64. // if(openid!=null) {
  65. // return ResultUtil.success("success", openid);
  66. // }
  67. //// userLoginDTO.setTel(phoneNumber);
  68. // } catch (Exception e) {
  69. // e.printStackTrace();
  70. // log.error("微信登陆异常:{}",e.getMessage());
  71. // return ResultUtil.error(ExceptionEnum.WECHAT_LOGIN_ERROR);
  72. // }
  73. // return ResultUtil.error(ExceptionEnum.WECHAT_LOGIN_ERROR);
  74. // }
  75. /**
  76. * 客户登陆
  77. * @param customer
  78. * @return
  79. */
  80. public Result customer_login(Customer customer){
  81. customer=customerService.findBymobileAndIsDelete(customer.getMobile());
  82. if (ObjectUtils.isEmpty(customer))
  83. throw new IllegalArgumentException("Customer object is null");
  84. //为微信用户生成jwt令牌
  85. Map<String ,Object> claims=new HashMap<>();
  86. //用openid还是id?
  87. //claims.put(JwtClaimsConstant.CUSTOMER_ID,customer.getOpenid());
  88. claims.put(JwtClaimsConstant.USER_ID,customer.getId());
  89. String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
  90. log.info("手机号码:{}",customer.getMobile());
  91. CustomerLoginVO customerLoginVO = CustomerLoginVO.builder()
  92. .id(customer.getId())
  93. .openid(customer.getOpenid())
  94. .token(token)
  95. .name(customer.getName())
  96. .is_customer(1)
  97. .build();
  98. return ResultUtil.success("success", customerLoginVO);
  99. }
  100. /**
  101. * 用户登陆
  102. * @param userLoginDTO
  103. * @return
  104. */
  105. public Result user_login(UserLoginDTO userLoginDTO){
  106. User user= userService.findByPhoneNumberAndIsDelete(userLoginDTO.getTel());
  107. if (ObjectUtils.isEmpty(user))
  108. throw new IllegalArgumentException("User object is null");
  109. //为微信用户生成jwt令牌
  110. Map<String ,Object> claims=new HashMap<>();
  111. claims.put(JwtClaimsConstant.USER_ID,user.getId());
  112. String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
  113. log.info("手机号码:{}",userLoginDTO.getTel());
  114. log.info("用户ID:{}",user.getId());
  115. UserLoginVO userLoginVO = UserLoginVO.builder()
  116. .id(user.getId())
  117. .openid(user.getOpenid())
  118. .token(token)
  119. .is_customer(0)
  120. .username(user.getUsername())
  121. .build();
  122. return ResultUtil.success("success", userLoginVO);
  123. }
  124. /**
  125. * 注册客户
  126. * @param customer
  127. * @return
  128. */
  129. public Result register(Customer customer){
  130. customer.setCreateTime(LocalDateTime.now()
  131. .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
  132. customer.setUpdateTime(LocalDateTime.now()
  133. .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
  134. customerService.createCustomer(customer);
  135. return this.customer_login(customer);
  136. }
  137. @PostMapping("/login_wechat")
  138. @ApiOperation("微信手机号登陆")
  139. public Result login(@RequestBody WeChatLoginDTO weChatLoginDTO){
  140. String access_token=wxService.getAccessToken();
  141. String phoneNumber=wxService.getUserPhoneNumber(access_token,weChatLoginDTO.getCode());
  142. boolean user_is_exist = userService.existsByMobileAndIsDelete(phoneNumber);
  143. boolean customer_is_exist=customerService.existsBymobileAndIsDelete(phoneNumber);
  144. log.info("code:{}",phoneNumber);
  145. log.info("用户是否存在:{}",user_is_exist);
  146. if(!customer_is_exist&&!user_is_exist){
  147. Customer customer = new Customer();
  148. customer.setMobile(phoneNumber);
  149. customer.setOpenid(weChatLoginDTO.getOpenid());
  150. return this.register(customer);
  151. }else if(customer_is_exist){ //存在客户
  152. Customer customer=customerService.findBymobileAndIsDelete(phoneNumber);
  153. return this.customer_login(customer);
  154. }else { //存在用户
  155. UserLoginDTO userLoginDTO = new UserLoginDTO();
  156. userLoginDTO.setTel(phoneNumber);
  157. return this.user_login(userLoginDTO);
  158. }
  159. }
  160. @PostMapping("/login")
  161. @ApiOperation("账号登陆")
  162. public Result login(@RequestBody UserLoginDTO userLoginDTO){
  163. String phoneNumber=userLoginDTO.getTel();
  164. boolean user_is_exist = userService.existsByMobileAndIsDelete(phoneNumber);
  165. boolean customer_is_exist=customerService.existsBymobileAndIsDelete(phoneNumber);
  166. if(!customer_is_exist&&!user_is_exist){
  167. Customer customer = new Customer();
  168. customer.setMobile(phoneNumber);
  169. customer.setOpenid(userLoginDTO.getOpenid());
  170. return this.register(customer);
  171. }else if(customer_is_exist){
  172. Customer customer=customerService.findBymobileAndIsDelete(phoneNumber);
  173. return this.customer_login(customer);
  174. }else{
  175. return this.user_login(userLoginDTO);
  176. }
  177. }
  178. }