admin
2020-05-19 744594ef1a2f530fc3e86ea9dc48b62247f79420
fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserAccountServiceImpl.java
@@ -18,8 +18,6 @@
import com.google.gson.Gson;
import com.qcloud.cos.model.COSObjectSummary;
import com.qcloud.cos.model.ObjectListing;
import com.yeshi.fanli.dao.mybatis.AccountMessageMapper;
import com.yeshi.fanli.dao.mybatis.UserConnectHistoryMapper;
import com.yeshi.fanli.dao.mybatis.UserInfoMapper;
import com.yeshi.fanli.dto.wx.WXAccountInfoDTO;
import com.yeshi.fanli.entity.accept.AcceptData;
@@ -78,13 +76,7 @@
   private BindingAccountService bindingAccountService;
   @Resource
   private UserConnectHistoryMapper userConnectHistoryMapper;
   @Resource
   private ThreeSaleSerivce threeSaleSerivce;
   @Resource
   private AccountMessageMapper accountMessageMapper;
   @Resource
   private SpreadUserImgService spreadUserImgService;
@@ -149,8 +141,7 @@
            }
            userInfo = getUserInfoByTaoBaoOpenId(appId, tbUserInfo.getOpenid());
            // 判断用户是否被删除
            if (userInfo != null && userInfo.getState() != null
                  && (userInfo.getState() != UserInfo.STATE_NORMAL))
            if (userInfo != null && userInfo.getState() != null && (userInfo.getState() != UserInfo.STATE_NORMAL))
               userInfo = null;
            if (userInfo == null) {// 原先的账号不存在
               userInfo = new UserInfo();
@@ -193,9 +184,7 @@
            userInfo = getUserInfoByWXUnionId(appId, weiXinUser.getUnionid());
            // 判断用户是否被删除
            if (userInfo != null && userInfo.getState() != null
                  && (userInfo.getState() != UserInfo.STATE_NORMAL
                     ))
            if (userInfo != null && userInfo.getState() != null && (userInfo.getState() != UserInfo.STATE_NORMAL))
               userInfo = null;
            // 直接用的微信登录
            if (lastUser == null) {
@@ -332,8 +321,7 @@
            userInfo = getUserInfoByPhone(appId, phone);
            // 判断用户是否被删除
            if (userInfo != null && userInfo.getState() != null
                  && (userInfo.getState() != UserInfo.STATE_NORMAL))
            if (userInfo != null && userInfo.getState() != null && (userInfo.getState() != UserInfo.STATE_NORMAL))
               userInfo = null;
            if (userInfo == null) {// 原先的账号不存在
@@ -459,7 +447,6 @@
      }
   }
   /**
    * 根据系统与unionid寻找用户
    * 
@@ -532,7 +519,7 @@
      // 剔除被删除掉的用户
      for (int i = 0; i < list.size(); i++) {
         if (list.get(i).getState() !=UserInfo.STATE_NORMAL) {
         if (list.get(i).getState() != UserInfo.STATE_NORMAL) {
            list.remove(i);
            i--;
         }
@@ -864,7 +851,6 @@
      String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSLoginCount, phone);
      redisManager.removeCommonString(key);
      // 采用redis事务防止一个手机号多次注册问题
      String watchKey = StringUtil.Md5("REGISTER:" + phone);
      Jedis jedis = jedisPool.getResource();
@@ -973,8 +959,8 @@
                  InputStream asInputStream = HttpUtil.getAsInputStream(weiXinUser.getHeadimgurl());
                  if (asInputStream != null) {
                     FileUploadResult result = COSManager.getInstance().uploadFile(asInputStream,
                           String.format(FilePathEnum.userWXPortrait.getPath() + "%s_%s.jpg", weiXinUser.getUnionid(),
                                 System.currentTimeMillis() + ""));
                           String.format(FilePathEnum.userWXPortrait.getPath() + "%s_%s.jpg",
                                 weiXinUser.getUnionid(), System.currentTimeMillis() + ""));
                     if (result != null && !StringUtil.isNullOrEmpty(result.getUrl()))
                        portrait = result.getUrl();
                  } else {
@@ -1050,7 +1036,8 @@
      UserInfo userInfo = userInfoMapper.getEffectiveUserInfoByWXUnionId(weiXinUser.getUnionid());
      if (userInfo == null) {
         String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.emptyKey, StringUtil.Md5("WXLogin:" + weiXinUser.getUnionid()));
         String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.emptyKey,
               StringUtil.Md5("WXLogin:" + weiXinUser.getUnionid()));
         redisManager.cacheCommonString(key, JsonUtil.getSimpleGson().toJson(weiXinUser), 60 * 20);
         throw new UserAccountException(101, key);
      }
@@ -1123,7 +1110,7 @@
   }
   @Transactional(rollbackFor=UserAccountException.class)
   @Transactional(rollbackFor = UserAccountException.class)
   @Override
   public void bindWeiXin(AcceptData acceptData, Long uid, String code) throws UserAccountException {
      WXAccountInfoDTO wxAccount = Constant.getWXAccount(acceptData.getPlatform(), acceptData.getVersion());
@@ -1138,18 +1125,15 @@
      }
      bindWeiXin(uid, weiXinUser);
   }
   @Transactional(rollbackFor=UserAccountException.class)
   @Transactional(rollbackFor = UserAccountException.class)
   @Override
   public void bindWeiXin(Long uid,WeiXinUser weiXinUser) throws UserAccountException {
   public void bindWeiXin(Long uid, WeiXinUser weiXinUser) throws UserAccountException {
      UserInfo user = userInfoMapper.selectAvailableByPrimaryKey(uid);
      if (user == null) {
         throw new UserAccountException(1, "用户不存在");
      }
      if (weiXinUser == null) {
         throw new UserAccountException(2, "微信帐号授权失败");
      }
@@ -1197,8 +1181,9 @@
         if (asInputStream == null) {
            LogHelper.test("微信头像下载失败: " + weiXinUser.getUnionid() + " " + headimgurl);
         } else {
            FileUploadResult result = COSManager.getInstance().uploadFile(asInputStream, String
                  .format(FilePathEnum.userWXPortrait.getPath() + "%s_%s.jpg", weiXinUser.getUnionid(), System.currentTimeMillis() + ""));
            FileUploadResult result = COSManager.getInstance().uploadFile(asInputStream,
                  String.format(FilePathEnum.userWXPortrait.getPath() + "%s_%s.jpg", weiXinUser.getUnionid(),
                        System.currentTimeMillis() + ""));
            if (result != null && !StringUtil.isNullOrEmpty(result.getUrl())) {
               headimgurl = result.getUrl();
            } else {
@@ -1230,9 +1215,6 @@
   @Override
   public UserInfo bindPhoneToLogin(String phone, String key, String appId, HttpServletRequest request)
         throws UserAccountException {
      UserInfo phoneUser = userInfoMapper.getEffectiveUserInfoByPhone(phone);
      if (phoneUser != null)
         throw new UserAccountException(1, "号码已经被占用");
      // 判断手机号码是否被封禁
      ForbiddenUserIdentifyCode identifyCode1 = forbiddenUserIdentifyCodeService
@@ -1257,8 +1239,9 @@
      if (!StringUtil.isNullOrEmpty(weiXinUser.getHeadimgurl())) {
         InputStream asInputStream = HttpUtil.getAsInputStream(weiXinUser.getHeadimgurl());
         if (asInputStream != null) {
            FileUploadResult result = COSManager.getInstance().uploadFile(asInputStream, String
                  .format(FilePathEnum.userWXPortrait.getPath() +"%s_%s.jpg", weiXinUser.getUnionid(), System.currentTimeMillis() + ""));
            FileUploadResult result = COSManager.getInstance().uploadFile(asInputStream,
                  String.format(FilePathEnum.userWXPortrait.getPath() + "%s_%s.jpg", weiXinUser.getUnionid(),
                        System.currentTimeMillis() + ""));
            if (result != null && !StringUtil.isNullOrEmpty(result.getUrl()))
               portrait = result.getUrl();
         } else {
@@ -1269,21 +1252,37 @@
      if (StringUtil.isNullOrEmpty(portrait))
         portrait = Constant.systemCommonConfig.getDefaultPortrait();
      // 创建新账户
      userInfo = new UserInfo();
      userInfo.setPhone(phone);
      userInfo.setPortrait(portrait);
      userInfo.setAppId(appId);
      userInfo.setNickName(weiXinUser.getNickname());
      userInfo.setWxName(weiXinUser.getNickname());
      userInfo.setWxOpenId(weiXinUser.getOpenid());
      userInfo.setWxUnionId(weiXinUser.getUnionid());
      userInfo.setWxPic(weiXinUser.getHeadimgurl());
      userInfo.setLastLoginTime(System.currentTimeMillis());
      userInfo.setLoginType(2);
      userInfo.setLastLoginIp(request.getRemoteHost());
      userInfo.setState(UserInfo.STATE_NORMAL);
      addUser(userInfo);
      // 是否需要创建新账户
      UserInfo phoneUser = userInfoMapper.getEffectiveUserInfoByPhone(phone);
      if (phoneUser != null) {
         // 绑定微信
         if (StringUtil.isNullOrEmpty(phoneUser.getWxUnionId())) {
            try {
               bindWeiXin(phoneUser.getId(), weiXinUser);
            } catch (Exception e) {
               throw new UserAccountException(1, "微信绑定失败");
            }
         } else {
            throw new UserAccountException(1, "该微信已被绑定");
         }
         userInfo = userInfoMapper.selectAvailableByPrimaryKey(phoneUser.getId());
      } else {
         // 创建新账户
         userInfo = new UserInfo();
         userInfo.setPhone(phone);
         userInfo.setPortrait(portrait);
         userInfo.setAppId(appId);
         userInfo.setNickName(weiXinUser.getNickname());
         userInfo.setWxName(weiXinUser.getNickname());
         userInfo.setWxOpenId(weiXinUser.getOpenid());
         userInfo.setWxUnionId(weiXinUser.getUnionid());
         userInfo.setWxPic(weiXinUser.getHeadimgurl());
         userInfo.setLastLoginTime(System.currentTimeMillis());
         userInfo.setLoginType(2);
         userInfo.setLastLoginIp(request.getRemoteHost());
         userInfo.setState(UserInfo.STATE_NORMAL);
         addUser(userInfo);
      }
      Long uid = userInfo.getId();
      ThreadUtil.run(new Runnable() {
@@ -1435,6 +1434,5 @@
      // 插入记录
      userInfoDeleteRecordService.addDeleteRecord(uid, UserInfo.STATE_DELETE_OUT_OF_DATE, reason);
   }
}