| | |
| | | import com.oying.modules.security.service.UserDetailsServiceImpl; |
| | | import com.oying.modules.security.service.WeiXinService; |
| | | import com.oying.modules.security.service.dto.AuthUserDto; |
| | | import com.oying.modules.security.service.dto.AuthUserPhoneDto; |
| | | import com.oying.modules.security.service.dto.AuthUserWeixinDto; |
| | | import com.oying.modules.security.service.dto.JwtUserDto; |
| | | import com.oying.modules.system.domain.DictDetail; |
| | |
| | | if (StringUtils.isBlank(code)) { |
| | | throw new BadRequestException("验证码不存在或已过期"); |
| | | } |
| | | if (StringUtils.isBlank(authUser.getCode()) || !authUser.getCode().equalsIgnoreCase(code)) { |
| | | if (!authUser.getCode().equalsIgnoreCase(code)) { |
| | | throw new BadRequestException("验证码错误"); |
| | | } |
| | | // 获取用户信息 |
| | |
| | | return ResponseEntity.ok(R.success(authInfo)); |
| | | } |
| | | |
| | | @Log("微信授权登录") |
| | | @ApiOperation("微信授权登录") |
| | | @Log("短信验证码登录") |
| | | @ApiOperation("短信验证码登录") |
| | | @AnonymousPostMapping(value = "/login/phone") |
| | | public ResponseEntity<Object> loginPhone(@Validated @RequestBody AuthUserPhoneDto authUser, HttpServletRequest request) { |
| | | // 查询验证码 |
| | | String code = redisUtils.get(authUser.getUuid(), String.class); |
| | | // 清除验证码 |
| | | redisUtils.del(authUser.getUuid()); |
| | | if (StringUtils.isBlank(code)) { |
| | | throw new BadRequestException("验证码不存在或已过期"); |
| | | } |
| | | if (!authUser.getCode().equalsIgnoreCase(code)) { |
| | | throw new BadRequestException("验证码错误"); |
| | | } |
| | | // 获取用户信息 |
| | | JwtUserDto jwtUser = userDetailsService.loadUserByUsername(authUser.getUsername()); |
| | | Authentication authentication = new UsernamePasswordAuthenticationToken(jwtUser, null, jwtUser.getAuthorities()); |
| | | SecurityContextHolder.getContext().setAuthentication(authentication); |
| | | // 生成令牌 |
| | | String token = tokenProvider.createToken(jwtUser); |
| | | // 返回 token 与 用户信息 |
| | | Map<String, Object> authInfo = new HashMap<String, Object>(2) {{ |
| | | put("token", properties.getTokenStartWith() + token); |
| | | put("user", jwtUser); |
| | | }}; |
| | | if (loginProperties.isSingleLogin()) { |
| | | // 踢掉之前已经登录的token |
| | | onlineUserService.kickOutForUsername(authUser.getUsername()); |
| | | } |
| | | // 保存在线信息 |
| | | onlineUserService.save(jwtUser, token, request); |
| | | // 返回登录信息 |
| | | return ResponseEntity.ok(R.success(authInfo)); |
| | | } |
| | | |
| | | public static final String OLD = "OLD"; |
| | | public static final String NEW = "NEW"; |
| | | |
| | | @Log("小程序:微信授权登录") |
| | | @ApiOperation("小程序:微信授权登录") |
| | | @AnonymousPostMapping(value = "/login/weixin") |
| | | public ResponseEntity<Object> loginWeixin(@Validated @RequestBody AuthUserWeixinDto authUser, HttpServletRequest request) throws Exception { |
| | | JSONObject jsonObject; |
| | | switch (authUser.getCode()) { |
| | | case "OLD": |
| | | switch (authUser.getType()) { |
| | | case OLD: |
| | | jsonObject = weiXinService.code2Session(authUser.getCode()); |
| | | String openid = jsonObject.getString("openid"); |
| | | User userDto = userService.findByOpenid(openid); |
| | | if (userDto == null) { |
| | | Map<String, Object> authInfo = new HashMap<String, Object>(2) {{ |
| | | put("token", openid); |
| | | put("member", null); |
| | | }}; |
| | | return ResponseEntity.ok(authInfo); |
| | | return ResponseEntity.ok(R.success(openid)); |
| | | } |
| | | authUser.setUsername(userDto.getUsername()); |
| | | break; |
| | | case "NEW": |
| | | case NEW: |
| | | jsonObject = weiXinService.getPhoneNumber(authUser.getCode()); |
| | | String phone = jsonObject.getJSONObject("phone_info").getString("purePhoneNumber"); |
| | | User user1 = userService.findByName(phone); |
| | |
| | | //创建用户 |
| | | User user = new User(); |
| | | user.setUsername(phone); |
| | | user.setNickName("LYHD-" + phone); |
| | | user.setNickName("OYING-" + phone); |
| | | user.setUserType(ConstantsKey.BUYER); |
| | | user.setPhone(phone); |
| | | user.setGender("男"); |
| | | user.setEnabled(true); |
| | | getRole(user); |
| | | user.setPassword(passwordEncoder.encode(phone.substring(phone.length() - 6))); |
| | |
| | | Set<Role> roles = new HashSet<>(); |
| | | Role role = new Role(); |
| | | DictDetail detail = dictDetailService.getDictByName(ConstantsKey.USER_TYPE_BUYER).get(0); |
| | | role.setId(Long.valueOf(detail.getLabel())); |
| | | role.setId(Long.valueOf(detail.getValue())); |
| | | roles.add(role); |
| | | user1.setRoles(roles); |
| | | } |