package com.qianwen.smartman.modules.auth.granter;
|
|
import java.util.Objects;
|
import java.util.Optional;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import org.springframework.stereotype.Component;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.qianwen.core.log.exception.ServiceException;
|
import com.qianwen.core.social.utils.SocialUtil;
|
import com.qianwen.core.tool.utils.BeanUtil;
|
import com.qianwen.core.tool.utils.Func;
|
import com.qianwen.core.tool.utils.WebUtil;
|
import com.qianwen.smartman.modules.auth.provider.ITokenGranter;
|
import com.qianwen.smartman.modules.auth.provider.TokenParameter;
|
import com.qianwen.smartman.modules.auth.utils.TokenUtil;
|
import com.qianwen.smartman.modules.cps.entity.Employee;
|
import com.qianwen.smartman.modules.cps.service.IEmployeeService;
|
import com.qianwen.smartman.modules.sync.constant.QyWechatConstant;
|
import com.qianwen.smartman.modules.system.entity.UserInfo;
|
import com.qianwen.smartman.modules.system.entity.UserOauth;
|
import com.qianwen.smartman.modules.system.service.IUserOauthService;
|
|
import me.zhyd.oauth.enums.AuthUserGender;
|
import me.zhyd.oauth.model.AuthCallback;
|
import me.zhyd.oauth.model.AuthResponse;
|
import me.zhyd.oauth.model.AuthUser;
|
import me.zhyd.oauth.request.AuthRequest;
|
|
@Component
|
public class SocialTokenGranter implements ITokenGranter {
|
public static final String GRANT_TYPE = "social";
|
private static final Integer AUTH_SUCCESS_CODE = 2000;
|
private final IUserOauthService userOauthService;
|
private final IEmployeeService employeeService;
|
private final EmployeeTokenGranter employeeTokenGranter;
|
|
|
public SocialTokenGranter(IUserOauthService userOauthService, IEmployeeService employeeService, EmployeeTokenGranter employeeTokenGranter) {
|
this.userOauthService = userOauthService;
|
this.employeeService = employeeService;
|
this.employeeTokenGranter = employeeTokenGranter;
|
}
|
|
@Override // org.springblade.modules.auth.provider.ITokenGranter
|
public UserInfo grant(TokenParameter tokenParameter) {
|
HttpServletRequest request = WebUtil.getRequest();
|
String tenantId = Func.toStr(request.getHeader(TokenUtil.TENANT_HEADER_KEY), "000000");
|
String sourceParameter = request.getParameter("source");
|
String code = request.getParameter("code");
|
String state = request.getParameter("state");
|
AuthRequest authRequest = SocialUtil.getAuthRequest(sourceParameter);
|
AuthCallback authCallback = new AuthCallback();
|
authCallback.setCode(code);
|
authCallback.setState(state);
|
AuthResponse authResponse = authRequest.login(authCallback);
|
if (authResponse.getCode() == AUTH_SUCCESS_CODE.intValue()) {
|
AuthUser authUser = (AuthUser) authResponse.getData();
|
UserOauth userOauth = (UserOauth) Objects.requireNonNull(BeanUtil.copy(authUser, UserOauth.class));
|
userOauth.setSource(authUser.getSource());
|
userOauth.setTenantId(tenantId);
|
userOauth.setUuid(authUser.getUuid());
|
userOauth.setEmail(authUser.getEmail());
|
userOauth.setAvatar(authUser.getAvatar());
|
/*
|
userOauth.setGender((String) Optional.ofNullable(authUser.getGender()).map((v0) -> {
|
return v0.getCode();
|
}).orElse(null));
|
*/
|
userOauth.setGender(Optional.<AuthUserGender>ofNullable(authUser.getGender()).map(AuthUserGender::getCode).orElse(null));
|
return buildUserInfo(userOauth, authUser);
|
}
|
throw new ServiceException(authResponse.getMsg());
|
}
|
|
@Transactional
|
protected UserInfo buildUserInfo(UserOauth userOauth, AuthUser authUser) {
|
/*
|
UserOauth uo = (UserOauth) this.userOauthService.getOne((Wrapper) ((LambdaQueryWrapper) Wrappers.query().lambda().eq((v0) -> {
|
return v0.getUuid();
|
}, userOauth.getUuid())).eq((v0) -> {
|
return v0.getSource();
|
}, userOauth.getSource()));
|
*/
|
UserOauth uo = this.userOauthService.getOne(Wrappers.<UserOauth>query().lambda().eq(UserOauth::getUuid, userOauth.getUuid()).eq(UserOauth::getSource, userOauth.getSource()));
|
|
UserInfo userInfo = new UserInfo();
|
if (Func.isNotEmpty(uo) && Func.isNotEmpty(uo.getUserId())) {
|
userOauth.setId(uo.getId());
|
this.userOauthService.updateById(userOauth);
|
Employee employee = this.employeeService.getById(uo.getUserId());
|
Optional.ofNullable(authUser.getRawUserInfo()).ifPresent(rawUserInfo -> {
|
if (userOauth.getSource().equals(QyWechatConstant.QY_SOURCE)) {
|
String mobile = rawUserInfo.getString("mobile");
|
employee.setTel(mobile);
|
employee.setAvatar(authUser.getAvatar());
|
employee.setEmail(authUser.getEmail());
|
this.employeeService.updateById(employee);
|
}
|
});
|
userInfo.setUser(this.employeeTokenGranter.buildUser(employee));
|
userInfo.setOauthId(Func.toStr(uo.getId()));
|
} else {
|
if (Func.isEmpty(uo)) {
|
this.userOauthService.save(userOauth);
|
userInfo.setOauthId(Func.toStr(userOauth.getId()));
|
} else {
|
userOauth.setId(uo.getId());
|
this.userOauthService.updateById(userOauth);
|
userInfo.setOauthId(Func.toStr(uo.getId()));
|
}
|
userInfo.setUser(null);
|
}
|
return userInfo;
|
}
|
}
|