|
@@ -6,6 +6,7 @@ import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.crypto.SecureUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.google.common.base.Strings;
|
|
|
import com.zbkj.common.constants.*;
|
|
|
import com.zbkj.common.exception.CrmebException;
|
|
|
import com.zbkj.common.model.user.User;
|
|
@@ -20,6 +21,7 @@ import com.zbkj.common.vo.WeChatMiniAuthorizeVo;
|
|
|
import com.zbkj.common.vo.WeChatOauthToken;
|
|
|
import com.zbkj.front.service.LoginService;
|
|
|
import com.zbkj.service.service.*;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
@@ -38,6 +40,7 @@ import java.util.concurrent.TimeUnit;
|
|
|
* 移动端登录服务类
|
|
|
*/
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class LoginServiceImpl implements LoginService {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(LoginServiceImpl.class);
|
|
@@ -222,6 +225,118 @@ public class LoginServiceImpl implements LoginService {
|
|
|
return loginResponse;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public LoginResponse wechatRegisterBinding(WxBindingPhoneRequest request) {
|
|
|
+ log.debug("注册绑定canshu:{}",request);
|
|
|
+ // 检验
|
|
|
+ WeChatMiniAuthorizeVo response = wechatService.miniAuthCode(request.getCode());
|
|
|
+/* if (response == null || Strings.isNullOrEmpty(response.getOpenId())) {
|
|
|
+ log.debug("获取OpenId失败");
|
|
|
+ throw new CrmebException("获取OpenId失败");
|
|
|
+ }*/
|
|
|
+ log.debug("注册绑定获取的微信数据:{}",response);
|
|
|
+
|
|
|
+ // 进入创建用户绑定手机号流程
|
|
|
+ String value = redisUtil.get(request.getKey());
|
|
|
+ if (StrUtil.isBlank(value)) {
|
|
|
+ throw new CrmebException("用户缓存已过期,请清除缓存重新登录");
|
|
|
+ }
|
|
|
+ RegisterThirdUserRequest registerThirdUserRequest = JSONObject.parseObject(value, RegisterThirdUserRequest.class);
|
|
|
+ log.debug("redis中的数据:{}",registerThirdUserRequest);
|
|
|
+ if (!request.getType().equals(registerThirdUserRequest.getType())) {
|
|
|
+ throw new CrmebException("用户的类型与缓存中的类型不符");
|
|
|
+ }
|
|
|
+
|
|
|
+/* if (!response.getOpenId().equals(registerThirdUserRequest.getOpenId())) {
|
|
|
+ log.debug("判断openId:“{}”不等与“{}”",response.getOpenId(),registerThirdUserRequest.getOpenId());
|
|
|
+ throw new CrmebException("登陆失败OpenId不一致");
|
|
|
+ }*/
|
|
|
+
|
|
|
+
|
|
|
+ boolean isNew = true;
|
|
|
+ User user = userService.getByAccount(registerThirdUserRequest.getOpenId());
|
|
|
+ // 查询是否用对应得token
|
|
|
+ Integer userTokenType = getUserTokenType(request.getType());
|
|
|
+ if (ObjectUtil.isNotNull(user)) {// 历史用户校验
|
|
|
+ if (request.getType().equals(UserConstants.REGISTER_TYPE_WECHAT) && user.getIsWechatPublic()) {
|
|
|
+ throw new CrmebException("该账号已绑定微信公众号");
|
|
|
+ }
|
|
|
+ if (request.getType().equals(UserConstants.REGISTER_TYPE_ROUTINE) && user.getIsWechatRoutine()) {
|
|
|
+ throw new CrmebException("该账号已绑定微信小程序");
|
|
|
+ }
|
|
|
+ if (request.getType().equals(UserConstants.REGISTER_TYPE_ANDROID_WX) && user.getIsWechatAndroid()) {
|
|
|
+ throw new CrmebException("该账号已绑定微信Android");
|
|
|
+ }
|
|
|
+ if (request.getType().equals(UserConstants.REGISTER_TYPE_IOS_WX) && user.getIsWechatIos()) {
|
|
|
+ throw new CrmebException("该账号已绑定微信IOS");
|
|
|
+ }
|
|
|
+ UserToken userToken = userTokenService.getTokenByUserId(user.getId(), userTokenType);
|
|
|
+ if (ObjectUtil.isNotNull(userToken)) {
|
|
|
+ throw new CrmebException("该账号已被注册");
|
|
|
+ }
|
|
|
+ isNew = false;
|
|
|
+ } else {
|
|
|
+ user = new User();
|
|
|
+ user.setRegisterType(registerThirdUserRequest.getType());
|
|
|
+ // 这里手机号是空的
|
|
|
+ user.setPhone(request.getPhone());
|
|
|
+ user.setAccount(registerThirdUserRequest.getOpenId()); // account数据库由32位改成128位了,因为openId长度是128
|
|
|
+ user.setSpreadUid(0);
|
|
|
+ user.setIsEnterpriseUser(1);
|
|
|
+ // 数据中密码长度由原来的32位改成 256,因为openId长度是128,生成后的密码可能比128长
|
|
|
+ user.setPwd(CommonUtil.createPwd(registerThirdUserRequest.getOpenId()));
|
|
|
+ user.setNickname(randomChar(4));
|
|
|
+ user.setAvatar(systemConfigService.getValueByKey(SysConfigConstants.USER_DEFAULT_AVATAR_CONFIG_KEY));
|
|
|
+ user.setSex(0);
|
|
|
+ user.setIsEnterpriseUser(1);
|
|
|
+ user.setAddress("");
|
|
|
+ }
|
|
|
+ switch (request.getType()) {
|
|
|
+ case UserConstants.REGISTER_TYPE_WECHAT:
|
|
|
+ user.setIsWechatPublic(true);
|
|
|
+ break;
|
|
|
+ case UserConstants.REGISTER_TYPE_ROUTINE:
|
|
|
+ user.setIsWechatRoutine(true);
|
|
|
+ break;
|
|
|
+ case UserConstants.REGISTER_TYPE_IOS_WX:
|
|
|
+ user.setIsWechatIos(true);
|
|
|
+ break;
|
|
|
+ case UserConstants.REGISTER_TYPE_ANDROID_WX:
|
|
|
+ user.setIsWechatAndroid(true);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ user.setLastLoginTime(CrmebDateUtil.nowDateTime());
|
|
|
+ User finalUser = user;
|
|
|
+ boolean finalIsNew = isNew;
|
|
|
+ Boolean execute = transactionTemplate.execute(e -> {
|
|
|
+ Integer spreadPid = Optional.ofNullable(registerThirdUserRequest.getSpreadPid()).orElse(0);
|
|
|
+ if (finalIsNew) {// 新用户
|
|
|
+ // 分销绑定
|
|
|
+ if (spreadPid > 0 && userService.checkBingSpread(finalUser, registerThirdUserRequest.getSpreadPid(), "new")) {
|
|
|
+ finalUser.setSpreadUid(registerThirdUserRequest.getSpreadPid());
|
|
|
+ finalUser.setSpreadTime(CrmebDateUtil.nowDateTime());
|
|
|
+ userService.updateSpreadCountByUid(registerThirdUserRequest.getSpreadPid(), Constants.OPERATION_TYPE_ADD);
|
|
|
+ }
|
|
|
+ userService.save(finalUser);
|
|
|
+ } else {
|
|
|
+ userService.updateById(finalUser);
|
|
|
+ if (finalUser.getSpreadUid().equals(0) && spreadPid > 0) {
|
|
|
+ // 绑定推广关系
|
|
|
+ bindSpread(finalUser, spreadPid);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ userTokenService.bind(registerThirdUserRequest.getOpenId(), userTokenType, finalUser.getId());
|
|
|
+ return Boolean.TRUE;
|
|
|
+ });
|
|
|
+ if (!execute) {
|
|
|
+ log.error(StrUtil.format("微信用户注册生成失败,openid = {}, key = {}", registerThirdUserRequest.getOpenId(), request.getKey()));
|
|
|
+ throw new CrmebException(StrUtil.format("微信用户注册生成失败,openid = {}, key = {}", registerThirdUserRequest.getOpenId(), request.getKey()));
|
|
|
+ }
|
|
|
+ LoginResponse loginResponse = getLoginResponse(finalUser);
|
|
|
+ log.debug("返回数据:{}", loginResponse);
|
|
|
+ return loginResponse;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 微信注册绑定手机号
|
|
|
*
|