admin
2019-11-20 8b2191df2f7d94aa299bd43dcbe97c94e5a61bbd
fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserInfoExtraServiceImpl.java
@@ -1,5 +1,6 @@
package com.yeshi.fanli.service.impl.user;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@@ -13,6 +14,9 @@
import org.springframework.stereotype.Service;
import org.yeshi.utils.DateUtil;
import org.yeshi.utils.HttpUtil;
import org.yeshi.utils.entity.FileUploadResult;
import org.yeshi.utils.tencentcloud.COSManager;
import com.yeshi.fanli.dao.mybatis.user.UserInfoExtraMapper;
import com.yeshi.fanli.dao.mybatis.user.UserRankRecordMapper;
@@ -189,6 +193,7 @@
      userInfoExtra.setFirstLoginTime(new Date());
      userInfoExtra.setCreateTime(new Date());
      userInfoExtra.setUpdateTime(new Date());
      userInfoExtra.setActiveTime(new Date());
      userInfoExtraMapper.insertSelective(userInfoExtra);
   }
@@ -399,7 +404,7 @@
      }
      // 获取微信信息
      WeiXinUser weiXinUser = WXLoginUtil.getWeiXinUserWithSavePortrait(code, wxAccount.getAppId(),
      WeiXinUser weiXinUser = WXLoginUtil.getWeiXinUser(code, wxAccount.getAppId(),
            wxAccount.getAppSecret());
      if (weiXinUser == null) {
         throw new UserInfoExtraException(1, "微信授权失败");
@@ -438,7 +443,20 @@
         String defaultPortrait = Constant.systemCommonConfig.getDefaultPortrait();
         if (!StringUtil.isNullOrEmpty(weiXinUser.getHeadimgurl()) && !StringUtil.isNullOrEmpty(defaultPortrait)
               && defaultPortrait.equals(invitee.getPortrait())) {
            updateUserInfo.setPortrait(weiXinUser.getHeadimgurl());
            String headimgurl = weiXinUser.getHeadimgurl();
            InputStream asInputStream = HttpUtil.getAsInputStream(headimgurl);
            if (asInputStream == null) {
               LogHelper.test("微信头像下载失败: " +weiXinUser.getUnionid()+ " " + headimgurl);
            } else {
               FileUploadResult result = COSManager.getInstance().uploadFile(asInputStream,
                     String.format("/portrait/wx/%s_%s.jpg", weiXinUser.getUnionid(), System.currentTimeMillis() + ""));
               if (result != null && !StringUtil.isNullOrEmpty(result.getUrl())) {
                  headimgurl = result.getUrl();
               } else {
                  LogHelper.test("微信头像上传失败: " +weiXinUser.getUnionid()+ " " + headimgurl);
               }
            }
            updateUserInfo.setPortrait(headimgurl);
         }
         userInfoService.updateByPrimaryKeySelective(updateUserInfo);
@@ -500,11 +518,158 @@
      }
   }
   @Override
   public void activeInviteWX(Long uid, WeiXinUser weiXinUser) throws UserInfoExtraException {
      if (uid == null || weiXinUser == null) {
         throw new UserInfoExtraException(1, "激活信息不完整");
      }
      // 用户信息
      UserInfo invitee = userInfoService.selectByPKey(uid);
      if (invitee == null) {
         throw new UserInfoExtraException(1, "用户不存在");
      }
      // 用户额外信息
      UserInfoExtra extra = userInfoExtraMapper.getInfoExtraByUid(uid);
      if (extra != null) {
         String inviteCodeHas = extra.getInviteCode();
         if (inviteCodeHas != null && inviteCodeHas.trim().length() > 0) {
            throw new UserInfoExtraException(1, "已经激活, 无需再次激活");
         }
      }
      String wxUnionId = weiXinUser.getUnionid();
      if (wxUnionId == null || wxUnionId.trim().length() == 0) {
         throw new UserInfoExtraException(1, "微信授权失败");
      }
      // 验证数据
      String wxUnionIdExist = invitee.getWxUnionId();
      if (StringUtil.isNullOrEmpty(wxUnionIdExist)) {
         UserInfo newUser = userInfoService.getEffectiveUserInfoByWXUnionId(weiXinUser.getUnionid());
         if (newUser != null) {
            throw new UserInfoExtraException(1, "此微信已被其他帐号绑定");
         }
         // 自动绑定微信
         UserInfo updateUserInfo = new UserInfo(uid);
         updateUserInfo.setWxOpenId(weiXinUser.getOpenid());
         updateUserInfo.setWxUnionId(weiXinUser.getUnionid());
         updateUserInfo.setWxName(weiXinUser.getNickname());
         updateUserInfo.setWxPic(weiXinUser.getHeadimgurl());
         // 将默认昵称替换成微信昵称
         String defaultNickName = Constant.systemCommonConfig.getDefaultNickName();
         if (!StringUtil.isNullOrEmpty(defaultNickName) && !StringUtil.isNullOrEmpty(weiXinUser.getNickname())) {
            defaultNickName = defaultNickName + uid;
            if ((defaultNickName.equals(invitee.getNickName()) || "返利券".equals(invitee.getNickName())
                  || invitee.getNickName().startsWith(Constant.systemCommonConfig.getDefaultNickName()))) {
               updateUserInfo.setNickName(weiXinUser.getNickname());
            }
         }
         // 将默认头像替换成微信头像
         String defaultPortrait = Constant.systemCommonConfig.getDefaultPortrait();
         if (!StringUtil.isNullOrEmpty(weiXinUser.getHeadimgurl()) && !StringUtil.isNullOrEmpty(defaultPortrait)
               && defaultPortrait.equals(invitee.getPortrait())) {
            String headimgurl = weiXinUser.getHeadimgurl();
            InputStream asInputStream = HttpUtil.getAsInputStream(headimgurl);
            if (asInputStream == null) {
               LogHelper.test("微信头像下载失败: " +weiXinUser.getUnionid()+ " " + headimgurl);
            } else {
               FileUploadResult result = COSManager.getInstance().uploadFile(asInputStream,
                     String.format("/portrait/wx/%s_%s.jpg", weiXinUser.getUnionid(), System.currentTimeMillis() + ""));
               if (result != null && !StringUtil.isNullOrEmpty(result.getUrl())) {
                  headimgurl = result.getUrl();
               } else {
                  LogHelper.test("微信头像上传失败: " +weiXinUser.getUnionid()+ " " + headimgurl);
               }
            }
            updateUserInfo.setPortrait(headimgurl);
         }
         userInfoService.updateByPrimaryKeySelective(updateUserInfo);
      } else if (!wxUnionId.equals(wxUnionIdExist)) {
         throw new UserInfoExtraException(1, "绑定微信与激活微信不一致");
      }
      // 邀请人ID -1.5.3新版
      Long inviterId = userInviteRecordService.getNewestInviterId(wxUnionId);
      // 兼容1.5.3 之前版本
      ThreeSale threeSale = threeSaleSerivce.getByWorkerId(uid);
      if (inviterId == null && threeSale != null) {
         Long bossId = threeSale.getBoss().getId();
         UserInfoExtra inviterExtra = userInfoExtraMapper.getInfoExtraByUid(bossId);
         if (inviterExtra != null && inviterExtra.getInviteCode() == null) {
            // 更新邀请码
            UserInfoExtra inviterInfoExtra = new UserInfoExtra();
            inviterInfoExtra.setId(inviterExtra.getId());
            inviterInfoExtra.setInviteCode(UserUtil.getInviteCode(bossId));
            userInfoExtraMapper.updateByPrimaryKeySelective(inviterInfoExtra);
         }
      }
      // 没有被邀请过
      if (inviterId == null && threeSale == null) {
         throw new UserInfoExtraException(1, "没有对应的邀请关系");
      }
      // 绑定关系
      try {
         threeSaleSerivce.bindRelationshipByWX(invitee, inviterId, threeSale);
      } catch (ThreeSaleException e) {
         try {
            LogHelper.errorDetailInfo(e);
         } catch (Exception e1) {
            e1.printStackTrace();
         }
         throw new UserInfoExtraException(1, "关系绑定时失败");
      }
      // 邀请码有效、生成邀请码
      String inviteCode = UserUtil.getInviteCode(uid);
      if (inviteCode == null || inviteCode.trim().length() == 0) {
         throw new UserInfoExtraException(1, "激活码生成失败");
      }
      // 保存邀请码
      UserInfoExtra userInfoExtra = new UserInfoExtra();
      userInfoExtra.setUserInfo(invitee);
      userInfoExtra.setInviteCode(inviteCode);
      userInfoExtra.setUpdateTime(new Date());
      if (extra != null) {
         userInfoExtra.setId(extra.getId());
         userInfoExtraMapper.updateByPrimaryKeySelective(userInfoExtra);
      } else {
         userInfoExtra.setCreateTime(new Date());
         userInfoExtraMapper.insertSelective(userInfoExtra);
      }
   }
   @Override
   public void updateActiveTime(Long uid, Date date) {
      UserInfoExtra userInfoExtra = new UserInfoExtra();
      userInfoExtra.setUserInfo(new UserInfo(uid));
      userInfoExtra.setActiveTime(date);
      userInfoExtraMapper.updateInfoExtraByUid(userInfoExtra);
   }
   @Override
   public UserInfoExtra getUserInfoExtra(Long uid) {
      return userInfoExtraMapper.getInfoExtraByUid(uid);
   }
   @Override
   public UserInfoExtra getUserALLInfo(Long uid) {
      return userInfoExtraMapper.gerUserRank(uid);
   }
   @Override
   public UserInfoExtra getByUidForUpdate(Long uid) {
      return userInfoExtraMapper.getByUidForUpdate(uid);
@@ -606,9 +771,9 @@
   }
   @Override
   public UserInfo getInviterInfo(WXAccountInfoDTO wxAccount, Long uid, String code) throws UserInfoExtraException {
      if (code == null) {
         throw new UserInfoExtraException(1, "code信息不完整");
   public UserInfo getInviterInfo(Long uid, String wxUnionId) throws UserInfoExtraException {
      if (wxUnionId == null) {
         throw new UserInfoExtraException(1, "unionId信息不完整");
      }
      // 用户信息
@@ -626,20 +791,10 @@
         }
      }
      // 获取微信信息
      WeiXinUser weiXinUser = WXLoginUtil.getWeiXinUser(code, wxAccount.getAppId(), wxAccount.getAppSecret());
      if (weiXinUser == null) {
         throw new UserInfoExtraException(1, "微信授权失败");
      }
      String wxUnionId = weiXinUser.getUnionid();
      if (wxUnionId == null || wxUnionId.trim().length() == 0) {
         throw new UserInfoExtraException(1, "微信授权失败");
      }
      // 验证数据
      String wxUnionIdExist = invitee.getWxUnionId();
      if (StringUtil.isNullOrEmpty(wxUnionIdExist)) {
         UserInfo newUser = userInfoService.getEffectiveUserInfoByWXUnionId(weiXinUser.getUnionid());
         UserInfo newUser = userInfoService.getEffectiveUserInfoByWXUnionId(wxUnionId);
         if (newUser != null) {
            throw new UserInfoExtraException(1, "此微信已被其他帐号绑定");
         }
@@ -659,4 +814,9 @@
      }
      return inviter;
   }
   @Override
   public void updateAutoExtract(Integer days) {
      userInfoExtraMapper.updateAutoExtract(days);
   }
}