|
|
@ -7,6 +7,7 @@ import cn.hutool.core.lang.Snowflake; |
|
|
|
import cn.hutool.core.util.ObjectUtil; |
|
|
|
import cn.hutool.core.util.RandomUtil; |
|
|
|
import cn.hutool.core.util.StrUtil; |
|
|
|
import com.ccsens.cloudutil.feign.HealthFeignClient; |
|
|
|
import com.ccsens.tall.bean.dto.ProjectDto; |
|
|
|
import com.ccsens.tall.bean.dto.UserDto; |
|
|
|
import com.ccsens.tall.bean.po.*; |
|
|
@ -15,10 +16,13 @@ import com.ccsens.tall.exception.SmsException; |
|
|
|
import com.ccsens.tall.persist.dao.*; |
|
|
|
import com.ccsens.util.*; |
|
|
|
import com.ccsens.util.bean.wx.po.WxOauth2UserInfo; |
|
|
|
import com.ccsens.util.enterprisewx.WeiXinConstant; |
|
|
|
import com.ccsens.util.enterprisewx.vo.WeiXinVo; |
|
|
|
import com.ccsens.util.exception.BaseException; |
|
|
|
import com.ccsens.util.wx.WxGzhUtil; |
|
|
|
import com.ccsens.util.wx.WxXcxUtil; |
|
|
|
import io.swagger.annotations.ApiModelProperty; |
|
|
|
import lombok.Data; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.lang3.RandomStringUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
@ -28,6 +32,7 @@ import org.springframework.transaction.annotation.Transactional; |
|
|
|
import org.xhtmlrenderer.css.parser.property.PrimitivePropertyBuilders; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.regex.Matcher; |
|
|
@ -57,6 +62,8 @@ public class UserService implements IUserService { |
|
|
|
private SysProjectDao sysProjectDao; |
|
|
|
@Autowired |
|
|
|
private UserAttentionDao userAttentionDao; |
|
|
|
@Autowired |
|
|
|
private HealthFeignClient healthFeignClient; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
@ -64,10 +71,10 @@ public class UserService implements IUserService { |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public UserVo.UserSign signin(WebConstant.CLIENT_TYPE clientType, WebConstant.IDENTIFY_TYPE identifyType, |
|
|
|
String identifier, String credential, String clientIp) throws Exception { |
|
|
|
String identifier, String credential, String clientIp,String redirect) throws Exception { |
|
|
|
UserVo.UserSign userSignVo = null; |
|
|
|
//1.登陆
|
|
|
|
userSignVo = __signin(identifyType, identifier, credential); |
|
|
|
userSignVo = __signin(identifyType, identifier, credential, redirect); |
|
|
|
if (ObjectUtil.isNotNull(userSignVo) && ObjectUtil.isNotNull(userSignVo.getAuthId())) { |
|
|
|
//2.添加登陆记录
|
|
|
|
__addSigninRecord(clientType, clientIp, userSignVo.getAuthId()); |
|
|
@ -76,8 +83,10 @@ public class UserService implements IUserService { |
|
|
|
} |
|
|
|
|
|
|
|
private UserVo.UserSign __signin(WebConstant.IDENTIFY_TYPE identifyType, |
|
|
|
String identifier, String credential) throws Exception { |
|
|
|
String identifier, String credential, String redirect) throws Exception { |
|
|
|
switch (identifyType) { |
|
|
|
case WxEnterprise: |
|
|
|
return wxEnterpriseLogin(identifier, credential, redirect); |
|
|
|
case Wxmp: |
|
|
|
credential = credential == null ? "old" : credential; |
|
|
|
return wxmplogin(identifier,credential); |
|
|
@ -100,6 +109,33 @@ public class UserService implements IUserService { |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 企业登录 |
|
|
|
* @param identifier |
|
|
|
* @param credential |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
private UserVo.UserSign wxEnterpriseLogin(String identifier, String credential, String redirect) { |
|
|
|
log.info("企业微信登录:{},{}, {}"); |
|
|
|
String suiteId = WeiXinConstant.SuiteId.getValue(credential); |
|
|
|
if (StrUtil.isBlank(suiteId) || StrUtil.isBlank(identifier)) { |
|
|
|
throw new BaseException(CodeEnum.PARAM_ERROR); |
|
|
|
} |
|
|
|
String suiteAccessToken = healthFeignClient.getSuiteAccessToken(suiteId); |
|
|
|
WeiXinVo.UserInfo userInfo = WeiXinConstant.getUserInfo(identifier, suiteAccessToken); |
|
|
|
log.info("用户信息"); |
|
|
|
if (userInfo == null) { |
|
|
|
throw new BaseException(CodeEnum.PARAM_ERROR); |
|
|
|
} |
|
|
|
WxEnterpriseParam param = new WxEnterpriseParam(); |
|
|
|
param.setRedirect(redirect); |
|
|
|
param.setSuiteAccessToken(suiteAccessToken); |
|
|
|
param.setUserTicket(userInfo.getUser_ticket()); |
|
|
|
UserVo.UserSign userSignVo = getUserSign(userInfo.getUserId(), null, (byte) WebConstant.IDENTIFY_TYPE.WxEnterprise.value, param); |
|
|
|
|
|
|
|
return userSignVo; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 微信网页登陆 |
|
|
|
* |
|
|
@ -348,19 +384,36 @@ public class UserService implements IUserService { |
|
|
|
* 微信小程序 |
|
|
|
*/ |
|
|
|
private UserVo.UserSign wxmplogin(String code,String gameType) throws Exception { |
|
|
|
|
|
|
|
UserVo.UserSign userSignVo = null; |
|
|
|
//0.获取openid
|
|
|
|
WxXcxUtil.WechatUser wechatUser = WxXcxUtil.getUserInfo(code,gameType); |
|
|
|
String openId = wechatUser.openid; |
|
|
|
String unionId = wechatUser.unionid; |
|
|
|
log.info("小程序登录,openid:{} ,unionId:{}", openId, unionId); |
|
|
|
//1.查找对应账户,不存在则注册
|
|
|
|
UserVo.UserSign userSignVo = getUserSign(openId, unionId, (byte) WebConstant.IDENTIFY_TYPE.Wxmp.value,null); |
|
|
|
return userSignVo; |
|
|
|
} |
|
|
|
|
|
|
|
@Data |
|
|
|
private static class WxEnterpriseParam{ |
|
|
|
private String suiteAccessToken; |
|
|
|
private String userTicket; |
|
|
|
private String redirect; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* @param openId |
|
|
|
* @param unionId |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
private UserVo.UserSign getUserSign(String openId, String unionId, |
|
|
|
byte identifyType, WxEnterpriseParam param) { |
|
|
|
UserVo.UserSign userSignVo;//1.查找对应账户,不存在则注册
|
|
|
|
List<SysAuth> authList = null; |
|
|
|
SysAuth theAuth = null; |
|
|
|
if(ObjectUtil.isNotNull(openId)) { |
|
|
|
SysAuthExample authExample = new SysAuthExample(); |
|
|
|
authExample.createCriteria().andIdentifyTypeEqualTo((byte) WebConstant.IDENTIFY_TYPE.Wxmp.value) |
|
|
|
authExample.createCriteria().andIdentifyTypeEqualTo(identifyType) |
|
|
|
.andIdentifierEqualTo(openId); |
|
|
|
authList = authDao.selectByExample(authExample); |
|
|
|
} |
|
|
@ -372,25 +425,31 @@ public class UserService implements IUserService { |
|
|
|
sysAuthList = authDao.selectByExample(sysAuthExample); |
|
|
|
} |
|
|
|
if (CollectionUtil.isNotEmpty(sysAuthList)) { |
|
|
|
// SysAuth sysAuth = sysAuthList.get(0);
|
|
|
|
//添加认证方式
|
|
|
|
theAuth = new SysAuth(); |
|
|
|
theAuth.setId(snowflake.nextId()); |
|
|
|
theAuth.setUserId(sysAuthList.get(0).getUserId()); |
|
|
|
theAuth.setIdentifyType((byte) WebConstant.IDENTIFY_TYPE.Wxmp.value); |
|
|
|
theAuth.setIdentifier(openId); |
|
|
|
theAuth.setCredential(unionId); |
|
|
|
authDao.insertSelective(theAuth); |
|
|
|
if (identifyType == WebConstant.IDENTIFY_TYPE.Wxmp.value) { |
|
|
|
//添加认证方式
|
|
|
|
theAuth = new SysAuth(); |
|
|
|
theAuth.setId(snowflake.nextId()); |
|
|
|
theAuth.setUserId(sysAuthList.get(0).getUserId()); |
|
|
|
theAuth.setIdentifyType(identifyType); |
|
|
|
theAuth.setIdentifier(openId); |
|
|
|
theAuth.setCredential(unionId); |
|
|
|
authDao.insertSelective(theAuth); |
|
|
|
} else { |
|
|
|
theAuth = sysAuthList.get(0); |
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
//新建用户并保存微信信息
|
|
|
|
SysUser user = new SysUser(); |
|
|
|
user.setId(snowflake.nextId()); |
|
|
|
//企业微信获取详细用户信息,并绑定用户信息
|
|
|
|
getUserDetail(identifyType, param, user); |
|
|
|
userDao.insertSelective(user); |
|
|
|
//添加认证方式
|
|
|
|
theAuth = new SysAuth(); |
|
|
|
theAuth.setId(snowflake.nextId()); |
|
|
|
theAuth.setUserId(user.getId()); |
|
|
|
theAuth.setIdentifyType((byte) WebConstant.IDENTIFY_TYPE.Wxmp.value); |
|
|
|
theAuth.setIdentifyType(identifyType); |
|
|
|
theAuth.setIdentifier(openId); |
|
|
|
theAuth.setCredential(unionId); |
|
|
|
authDao.insertSelective(theAuth); |
|
|
@ -416,6 +475,23 @@ public class UserService implements IUserService { |
|
|
|
return userSignVo; |
|
|
|
} |
|
|
|
|
|
|
|
private void getUserDetail(byte identifyType, WxEnterpriseParam param, SysUser user) { |
|
|
|
if (identifyType == WebConstant.IDENTIFY_TYPE.Wxmp.value) { |
|
|
|
WeiXinVo.UserDetail userDetail = WeiXinConstant.getUserDetail(param.getSuiteAccessToken(), param.getUserTicket()); |
|
|
|
if (userDetail != null) { |
|
|
|
user.setNickname(userDetail.getName()); |
|
|
|
user.setGender(userDetail.getGender()); |
|
|
|
user.setAvatarUrl(userDetail.getAvatar()); |
|
|
|
} |
|
|
|
Map<String, Object> map = new HashMap<>(); |
|
|
|
map.put("tallUserId", user.getId()); |
|
|
|
map.put("userid", userDetail.getUserid()); |
|
|
|
String s = RestTemplateUtil.postBody(WeiXinConstant.getBindUrl(param.getRedirect()), map); |
|
|
|
log.info("{}绑定关系结果:{}", s); |
|
|
|
//TODO 绑定失败如何处理
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 微信登陆后天添加用户和认证方式 |
|
|
|
* |
|
|
|