|
@@ -1,9 +1,12 @@
|
|
package com.stellvoy.user.service.impl;
|
|
package com.stellvoy.user.service.impl;
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.stellvoy.api.client.auth.AuthClient;
|
|
import com.stellvoy.api.client.auth.AuthClient;
|
|
import com.stellvoy.api.client.task.TaskClient;
|
|
import com.stellvoy.api.client.task.TaskClient;
|
|
|
|
+import com.stellvoy.api.client.user.UserClient;
|
|
import com.stellvoy.api.dto.task.NewTaskDTO;
|
|
import com.stellvoy.api.dto.task.NewTaskDTO;
|
|
import com.stellvoy.api.dto.user.LoginFormDTO;
|
|
import com.stellvoy.api.dto.user.LoginFormDTO;
|
|
import com.stellvoy.common.domain.dto.LoginUserDTO;
|
|
import com.stellvoy.common.domain.dto.LoginUserDTO;
|
|
@@ -21,17 +24,23 @@ import com.stellvoy.user.service.ICodeService;
|
|
import com.stellvoy.user.service.IUserService;
|
|
import com.stellvoy.user.service.IUserService;
|
|
import com.stellvoy.user.service.UserTaskService;
|
|
import com.stellvoy.user.service.UserTaskService;
|
|
import com.stellvoy.user.utils.AliyunIdCardVerifier;
|
|
import com.stellvoy.user.utils.AliyunIdCardVerifier;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
-import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
|
+import org.springframework.http.converter.StringHttpMessageConverter;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
|
+import java.net.URLEncoder;
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalDateTime;
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.HashMap;
|
|
|
|
-import java.util.List;
|
|
|
|
-import java.util.Map;
|
|
|
|
|
|
+import java.util.*;
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import static com.stellvoy.user.constants.UserErrorInfo.Msg.*;
|
|
import static com.stellvoy.user.constants.UserErrorInfo.Msg.*;
|
|
@@ -43,6 +52,7 @@ import static com.stellvoy.user.constants.UserErrorInfo.Msg.*;
|
|
* </p>
|
|
* </p>
|
|
*/
|
|
*/
|
|
@Service
|
|
@Service
|
|
|
|
+@Slf4j
|
|
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
|
|
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
@@ -55,6 +65,15 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
|
|
private UserTaskService userTaskService;
|
|
private UserTaskService userTaskService;
|
|
@Autowired
|
|
@Autowired
|
|
private TaskClient taskClient;
|
|
private TaskClient taskClient;
|
|
|
|
+ @Autowired
|
|
|
|
+ private AuthClient authClient;
|
|
|
|
+ @Value("${wechat.appid}")
|
|
|
|
+ private String appId;
|
|
|
|
+
|
|
|
|
+ @Value("${wechat.appsecret}")
|
|
|
|
+ private String appSecret;
|
|
|
|
+ private final RestTemplate restTemplate = new RestTemplate();
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public LoginUserDTO queryUserDetail(LoginFormDTO loginDTO, boolean isStaff) {
|
|
public LoginUserDTO queryUserDetail(LoginFormDTO loginDTO, boolean isStaff) {
|
|
// 1.判断登录方式
|
|
// 1.判断登录方式
|
|
@@ -68,6 +87,11 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
|
|
if (type == 2) {
|
|
if (type == 2) {
|
|
user = loginByVerifyCode(loginDTO.getCellPhone(), loginDTO.getPassword());
|
|
user = loginByVerifyCode(loginDTO.getCellPhone(), loginDTO.getPassword());
|
|
}
|
|
}
|
|
|
|
+ // 微信登录
|
|
|
|
+ if (type == 3) {
|
|
|
|
+ int userId = Integer.parseInt(loginDTO.getUsername());
|
|
|
|
+ user = lambdaQuery().eq(User::getId, userId).one();
|
|
|
|
+ }
|
|
// 4.错误的登录方式
|
|
// 4.错误的登录方式
|
|
if (user == null) {
|
|
if (user == null) {
|
|
throw new BadRequestException(ILLEGAL_LOGIN_TYPE);
|
|
throw new BadRequestException(ILLEGAL_LOGIN_TYPE);
|
|
@@ -99,6 +123,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
|
|
user.setCreater(UserContext.getUser());
|
|
user.setCreater(UserContext.getUser());
|
|
user.setUpdater(UserContext.getUser());
|
|
user.setUpdater(UserContext.getUser());
|
|
user.setUsername(dto.getCellPhone());
|
|
user.setUsername(dto.getCellPhone());
|
|
|
|
+ user.setNickName(dto.getCellPhone());
|
|
user.setImage("https://suppliermanage.oss-cn-shanghai.aliyuncs.com/gig/img/woman.png");
|
|
user.setImage("https://suppliermanage.oss-cn-shanghai.aliyuncs.com/gig/img/woman.png");
|
|
save(user);
|
|
save(user);
|
|
return user;
|
|
return user;
|
|
@@ -124,6 +149,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
|
|
}
|
|
}
|
|
lambdaUpdate()
|
|
lambdaUpdate()
|
|
.eq(User::getId, userId)
|
|
.eq(User::getId, userId)
|
|
|
|
+ .set(User::getNickName, dto.getName())
|
|
.set(User::getIdentityCardId, dto.getIdentityCardId())
|
|
.set(User::getIdentityCardId, dto.getIdentityCardId())
|
|
.set(User::getSex, sex)
|
|
.set(User::getSex, sex)
|
|
.set(User::getImage, image)
|
|
.set(User::getImage, image)
|
|
@@ -232,6 +258,107 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
|
|
return userTaskDTO;
|
|
return userTaskDTO;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 生成微信登录二维码
|
|
|
|
+ */
|
|
|
|
+ public WeChatDTO wechatLogin() throws UnsupportedEncodingException {
|
|
|
|
+ WeChatDTO weChatDTO = new WeChatDTO();
|
|
|
|
+ String redirectUri = "https://labelx.stellvoy.com/us/users/wechat/code";
|
|
|
|
+ String encodedRedirectUri = URLEncoder.encode(redirectUri, StandardCharsets.UTF_8.toString());
|
|
|
|
+ String state = UUID.randomUUID().toString();
|
|
|
|
+ weChatDTO.setRedirectUri(encodedRedirectUri);
|
|
|
|
+ weChatDTO.setState(state);
|
|
|
|
+ weChatDTO.setAppid(appId);
|
|
|
|
+ stringRedisTemplate.opsForValue().set("weChatCode:" + state, state, 5, TimeUnit.MINUTES);
|
|
|
|
+ return weChatDTO;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 处理微信回调,接收 code
|
|
|
|
+ */
|
|
|
|
+ public void wechatCallback(String code, String state) {
|
|
|
|
+ // 从Redis中获取state
|
|
|
|
+ String storedCode = stringRedisTemplate.opsForValue().get("weChatCode:" + state);
|
|
|
|
+ // state校验
|
|
|
|
+ if (storedCode == null || !storedCode.equals(state)) {
|
|
|
|
+ throw new RuntimeException("state错误或已过期");
|
|
|
|
+ }
|
|
|
|
+ // 使用 code 获取 access_token 和 openid
|
|
|
|
+ String accessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appId +
|
|
|
|
+ "&secret=" + appSecret +
|
|
|
|
+ "&code=" + code +
|
|
|
|
+ "&grant_type=authorization_code";
|
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
|
+ restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8));
|
|
|
|
+ ResponseEntity<String> accessTokenResponse = restTemplate.getForEntity(accessTokenUrl, String.class);
|
|
|
|
+ JSONObject accessTokenJson = JSON.parseObject(accessTokenResponse.getBody());
|
|
|
|
+ String accessToken = accessTokenJson.getString("access_token");
|
|
|
|
+ String openid = accessTokenJson.getString("openid");
|
|
|
|
+ // 使用 access_token 和 openid 获取用户信息
|
|
|
|
+ String userInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken +
|
|
|
|
+ "&openid=" + openid +
|
|
|
|
+ "&lang=zh_CN";
|
|
|
|
+ ResponseEntity<String> userInfoResponse = restTemplate.getForEntity(userInfoUrl, String.class);
|
|
|
|
+ JSONObject userInfoJson = JSON.parseObject(userInfoResponse.getBody());
|
|
|
|
+
|
|
|
|
+ // 处理用户信息,例如登录或注册用户
|
|
|
|
+ String nickname = userInfoJson.getString("nickname");
|
|
|
|
+ String headimgurl = userInfoJson.getString("headimgurl");
|
|
|
|
+ User user = lambdaQuery().eq(User::getOpenId, openid).one();
|
|
|
|
+ if(user == null){
|
|
|
|
+ user = new User();
|
|
|
|
+ user.setNickName(nickname);
|
|
|
|
+ user.setImage(headimgurl);
|
|
|
|
+ user.setOpenId(openid);
|
|
|
|
+ user.setPassword("123456");
|
|
|
|
+ save(user);
|
|
|
|
+ user = lambdaQuery().eq(User::getOpenId, openid).one();
|
|
|
|
+ }
|
|
|
|
+ stringRedisTemplate.opsForValue().set("weChatCode:" + state, openid, 5, TimeUnit.MINUTES);
|
|
|
|
+ stringRedisTemplate.opsForValue().set("userId:" + state, user.getId().toString(), 5, TimeUnit.MINUTES);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 处理前端轮询
|
|
|
|
+ */
|
|
|
|
+ public CheckLoginDTO checkLoginStatus(String state) {
|
|
|
|
+ // 从Redis中获取state
|
|
|
|
+ String storedCode = stringRedisTemplate.opsForValue().get("weChatCode:" + state);
|
|
|
|
+ // state校验
|
|
|
|
+ if (storedCode == null) {
|
|
|
|
+ throw new RuntimeException("state错误或已失效");
|
|
|
|
+ }
|
|
|
|
+ User one = lambdaQuery().eq(User::getOpenId, storedCode).one();
|
|
|
|
+ if (one == null) {
|
|
|
|
+ throw new RuntimeException("用户未登录");
|
|
|
|
+ }
|
|
|
|
+ LoginFormDTO loginDTO = new LoginFormDTO();
|
|
|
|
+ loginDTO.setType(3);
|
|
|
|
+ loginDTO.setPassword("123456");
|
|
|
|
+ String userId = stringRedisTemplate.opsForValue().get("userId:" + state);
|
|
|
|
+ loginDTO.setUsername(userId);
|
|
|
|
+ String token = authClient.loginByPw(loginDTO);
|
|
|
|
+ CheckLoginDTO checkLoginDTO = new CheckLoginDTO();
|
|
|
|
+ checkLoginDTO.setToken(token);
|
|
|
|
+ checkLoginDTO.setUserId(one.getId());
|
|
|
|
+ if(one.getCellPhone()==null){
|
|
|
|
+ checkLoginDTO.setCellPhone("未绑定手机号");
|
|
|
|
+ }
|
|
|
|
+ checkLoginDTO.setCellPhone(one.getCellPhone());
|
|
|
|
+ return checkLoginDTO;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 验证验证码
|
|
|
|
+ */
|
|
|
|
+ public void weChatVerifyCode(WeChatVerifyCodeDTO dto, Long userId) {
|
|
|
|
+ codeService.verifyCode(dto.getPhone(), dto.getCode());
|
|
|
|
+ lambdaUpdate().eq(User::getId, userId)
|
|
|
|
+ .set(User::getCellPhone, dto.getPhone())
|
|
|
|
+ .set(User::getUsername, dto.getPhone())
|
|
|
|
+ .update();
|
|
|
|
+ }
|
|
|
|
+
|
|
public User loginByPw(LoginFormDTO loginDTO) {
|
|
public User loginByPw(LoginFormDTO loginDTO) {
|
|
// 1.数据校验
|
|
// 1.数据校验
|
|
String cellPhone = loginDTO.getCellPhone();
|
|
String cellPhone = loginDTO.getCellPhone();
|
|
@@ -247,7 +374,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
|
|
if (user.getStatus() == 0) {
|
|
if (user.getStatus() == 0) {
|
|
throw new ForbiddenException(USER_FROZEN);
|
|
throw new ForbiddenException(USER_FROZEN);
|
|
}
|
|
}
|
|
- // 4.校验密码
|
|
|
|
|
|
+ //4.校验密码
|
|
// if (!passwordEncoder.matches(loginDTO.getPassword(), user.getPassword())) {
|
|
// if (!passwordEncoder.matches(loginDTO.getPassword(), user.getPassword())) {
|
|
// throw new BadRequestException(INVALID_UN_OR_PW);
|
|
// throw new BadRequestException(INVALID_UN_OR_PW);
|
|
// }
|
|
// }
|