| | |
| | | import com.mzl.flower.constant.Constants; |
| | | import com.mzl.flower.dto.request.CreateWechatUserDTO; |
| | | import com.mzl.flower.dto.request.UserLoginDTO; |
| | | import com.mzl.flower.dto.response.customer.CustomerDTO; |
| | | import com.mzl.flower.dto.response.wx.WxUserVO; |
| | | import com.mzl.flower.entity.system.User; |
| | | import com.mzl.flower.mapper.customer.CustomerMapper; |
| | | import com.mzl.flower.service.customer.CustomerService; |
| | | import com.mzl.flower.service.impl.WechatUserDetailsService; |
| | | import com.mzl.flower.service.login.LoginService; |
| | | import com.mzl.flower.service.system.UserService; |
| | | import com.mzl.flower.service.system.WeChatService; |
| | |
| | | import org.springframework.security.oauth2.common.OAuth2RefreshToken; |
| | | import org.springframework.security.oauth2.provider.token.TokenStore; |
| | | import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; |
| | | import org.springframework.util.ObjectUtils; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.validation.constraints.NotBlank; |
| | | import java.util.Map; |
| | | |
| | | @Slf4j |
| | | @RestController |
| | |
| | | @Autowired |
| | | private UserService userService; |
| | | |
| | | @Autowired |
| | | private CustomerMapper customerMapper; |
| | | |
| | | |
| | | @Autowired |
| | | private TokenStore tokenStore; |
| | |
| | | public static final String TOKEN_KEY = "TOKEN-KEY"; |
| | | |
| | | public static final String SEPARATOR = ":"; |
| | | |
| | | |
| | | @PostMapping("/login/wechat") |
| | | @ApiOperation(value = "微信小程序登录", notes = "微信小程序登录") |
| | |
| | | String openId = session.getOpenid(); |
| | | String sessionKey = session.getSessionKey(); |
| | | String unionId = session.getUnionid(); |
| | | // 判断当前用户账号是否存在 |
| | | dto.setOpenId(openId); |
| | | dto.setSessionKey(sessionKey); |
| | | dto.setUnionId(unionId); |
| | | dto.setUserType(Constants.USER_TYPE.customer.name()); |
| | | |
| | | User existUserByOpenId = userService.getExistUserByOpenId(openId, sessionKey, unionId, Constants.USER_TYPE.customer.name()); |
| | | if(ObjectUtils.isEmpty(existUserByOpenId)){ |
| | | userService.registWxUser(dto); |
| | | } |
| | | |
| | | User user = userService.findUserByOpenId(openId, sessionKey, unionId |
| | | , dto.getImgUrl(), dto.getNickname(),Constants.USER_TYPE.customer.name()); |
| | | String tokenCache = stringCacheClient.get(TOKEN_KEY + SEPARATOR + user.getId()); |
| | |
| | | log.error(e.getMessage(), e); |
| | | throw new BaseException(R.RUNTIME_EXCEPTION.getCode(), "登录错误"); |
| | | } |
| | | } |
| | | |
| | | @PostMapping("/wx/getuserphonenumber") |
| | | @ApiOperation(value = "获取当前授权的手机号") |
| | | public ResponseEntity<ReturnDataDTO> getuserphonenumber(@RequestBody CreateWechatUserDTO dto) throws Exception{ |
| | | return returnData(R.SUCCESS.getCode(), weChatService.getuserphonenumber(dto)); |
| | | } |
| | | |
| | | @PostMapping("/wx/getExistUserByOpenId") |
| | | @ApiOperation(value = "查看当前微信号是否已经绑定了用户") |
| | | public ResponseEntity<ReturnDataDTO> getExistUserByOpenId(@RequestBody CreateWechatUserDTO dto) throws Exception{ |
| | | WxMaJscode2SessionResult session = weChatService.getWxMaSessionInfo(dto.getCode()); |
| | | String openId = session.getOpenid(); |
| | | String sessionKey = session.getSessionKey(); |
| | | String unionId = session.getUnionid(); |
| | | User user=userService.getExistUserByOpenId(openId, sessionKey, unionId,dto.getUserType()); |
| | | return returnData(R.SUCCESS.getCode(), new WxUserVO(openId,sessionKey,unionId,user)); |
| | | } |
| | | |
| | | @GetMapping("/wx/jscode2session") |
| | |
| | | User user = userService.findByTel(username, Constants.USER_TYPE.customer.name()); |
| | | if(user == null){ |
| | | throw new ValidationException("用户不存在"); |
| | | } |
| | | CustomerDTO currentCustomer = customerMapper.getCurrentCustomer(user.getId()); |
| | | if (ObjectUtils.isEmpty(currentCustomer)) { |
| | | throw new ValidationException("用户不存在"); |
| | | } |
| | | if (currentCustomer.getIsEnabled() == false) { |
| | | throw new ValidationException("用户已禁用,请联系管理员"); |
| | | } |
| | | String tokenCache = stringCacheClient.get(TOKEN_KEY + SEPARATOR + user.getId()); |
| | | if (StringUtils.isNotBlank(tokenCache)) { |
| | |
| | | if(user == null){ |
| | | throw new ValidationException("用户不存在"); |
| | | } |
| | | CustomerDTO currentCustomer = customerMapper.getCurrentCustomer(user.getId()); |
| | | if (ObjectUtils.isEmpty(currentCustomer)) { |
| | | throw new ValidationException("用户不存在"); |
| | | } |
| | | if (currentCustomer.getIsEnabled() == false) { |
| | | throw new ValidationException("用户已禁用,请联系管理员"); |
| | | } |
| | | String tokenCache = stringCacheClient.get(TOKEN_KEY + SEPARATOR + user.getId()); |
| | | if (StringUtils.isNotBlank(tokenCache)) { |
| | | //强制删除token,下线 |