admin
2019-12-06 cd637932a970e7935b17d41568fe89c92775bccc
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;
@@ -12,7 +13,11 @@
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
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 +194,7 @@
      userInfoExtra.setFirstLoginTime(new Date());
      userInfoExtra.setCreateTime(new Date());
      userInfoExtra.setUpdateTime(new Date());
      userInfoExtra.setActiveTime(new Date());
      userInfoExtraMapper.insertSelective(userInfoExtra);
   }
@@ -399,8 +405,7 @@
      }
      // 获取微信信息
      WeiXinUser weiXinUser = WXLoginUtil.getWeiXinUserWithSavePortrait(code, wxAccount.getAppId(),
            wxAccount.getAppSecret());
      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);
@@ -501,20 +519,163 @@
   }
   @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);
   }
   @Override
   public void updateGoldCoin(Long id, Integer goldCoin) {
      userInfoExtraMapper.updateGoldCoin(id, goldCoin);
   }
   @Override
   public UserInfoExtraVO getInfoExtraVOByUid(Long uid) {
      return userInfoExtraMapper.getInfoExtraVOByUid(uid);
@@ -606,9 +767,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 +787,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 +810,48 @@
      }
      return inviter;
   }
   @Override
   public void updateAutoExtract(Integer days) {
      userInfoExtraMapper.updateAutoExtract(days);
   }
   @Transactional
   @Override
   public void updateInviteCode(String inviteCode, Long uid) throws UserInfoExtraException {
      if (inviteCode.length() >= 6 && inviteCode.length() <= 12) {
         UserInfoExtra extra = getUserInfoExtra(uid);
         if (extra == null) {
            throw new UserInfoExtraException(2, "用户信息不存在");
         }
         if (StringUtil.isNullOrEmpty(extra.getInviteCode())) {
            throw new UserInfoExtraException(3, "邀请码尚未激活");
         }
         if (extra.getInviteCodeState() == null
               || extra.getInviteCodeState() != UserInfoExtra.INVITE_CODE_SATTE_VALID_NO_UPDATE) {
            throw new UserInfoExtraException(4, "只能修改一次");
         }
         if (inviteCode.equalsIgnoreCase(extra.getInviteCode())) {
            throw new UserInfoExtraException(5, "不能为原邀请码");
         }
         UserInfoExtra update = new UserInfoExtra();
         update.setId(extra.getId());
         extra = userInfoExtraMapper.selectByInviteCode(inviteCode);
         if (extra != null) {
            throw new UserInfoExtraException(6, "邀请码已存在");
         }
         update.setInviteCode(inviteCode);
         update.setInviteCodeState(UserInfoExtra.INVITE_CODE_SATTE_VALID_UPDATED);
         update.setUpdateTime(new Date());
         userInfoExtraMapper.updateByPrimaryKeySelective(update);
      } else {
         throw new UserInfoExtraException(1, "邀请码必须为6到12位");
      }
   }
}