yujian
2020-05-09 7e7db2fa55a9a3af46d4fd8ede0dee147f101d64
fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserAccountServiceImpl.java
@@ -1,12 +1,6 @@
package com.yeshi.fanli.service.impl.user;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
@@ -24,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;
@@ -34,9 +26,7 @@
import com.yeshi.fanli.entity.bus.user.ForbiddenUserIdentifyCode.ForbiddenUserIdentifyCodeTypeEnum;
import com.yeshi.fanli.entity.bus.user.LoginResult;
import com.yeshi.fanli.entity.bus.user.SMSHistory;
import com.yeshi.fanli.entity.bus.user.ThreeSale;
import com.yeshi.fanli.entity.bus.user.UserAccountBindingHistory;
import com.yeshi.fanli.entity.bus.user.UserConnectHistory;
import com.yeshi.fanli.entity.bus.user.UserExtraTaoBaoInfo;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.bus.user.UserInfoModifyRecord.ModifyTypeEnum;
@@ -57,11 +47,11 @@
import com.yeshi.fanli.service.inter.user.msg.UserAccountMsgNotificationService;
import com.yeshi.fanli.service.inter.user.tb.UserExtraTaoBaoInfoService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.FileUtil;
import com.yeshi.fanli.util.FilePathEnum;
import com.yeshi.fanli.util.RedisKeyEnum;
import com.yeshi.fanli.util.RedisManager;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.ThreadUtil;
import com.yeshi.fanli.util.TimeUtil;
import com.yeshi.fanli.util.factory.msg.MsgAccountDetailFactory;
import com.yeshi.fanli.util.wx.WXLoginUtil;
@@ -86,13 +76,7 @@
   private BindingAccountService bindingAccountService;
   @Resource
   private UserConnectHistoryMapper userConnectHistoryMapper;
   @Resource
   private ThreeSaleSerivce threeSaleSerivce;
   @Resource
   private AccountMessageMapper accountMessageMapper;
   @Resource
   private SpreadUserImgService spreadUserImgService;
@@ -121,9 +105,8 @@
   @Lazy
   @Resource
   private UserInfoModifyRecordService userInfoModifyRecordService;
   @Transactional
   @Transactional(rollbackFor = Exception.class)
   @Override
   public LoginResult login(HttpServletRequest request, AcceptData acceptData, Boolean first, String appId,
         String code, String phone, UserInfo tbUserInfo, boolean wxinstall, int loginType)
@@ -159,8 +142,7 @@
            userInfo = getUserInfoByTaoBaoOpenId(appId, tbUserInfo.getOpenid());
            // 判断用户是否被删除
            if (userInfo != null && userInfo.getState() != null
                  && (userInfo.getState() == UserInfo.STATE_DELETE_OUT_OF_DATE
                        || userInfo.getState() == UserInfo.STATE_DELETE))
                  && (userInfo.getState() != UserInfo.STATE_NORMAL))
               userInfo = null;
            if (userInfo == null) {// 原先的账号不存在
               userInfo = new UserInfo();
@@ -204,8 +186,8 @@
            userInfo = getUserInfoByWXUnionId(appId, weiXinUser.getUnionid());
            // 判断用户是否被删除
            if (userInfo != null && userInfo.getState() != null
                  && (userInfo.getState() == UserInfo.STATE_DELETE_OUT_OF_DATE
                        || userInfo.getState() == UserInfo.STATE_DELETE))
                  && (userInfo.getState() != UserInfo.STATE_NORMAL
                     ))
               userInfo = null;
            // 直接用的微信登录
            if (lastUser == null) {
@@ -223,6 +205,8 @@
                  updateUserInfo.setLastLoginTime(System.currentTimeMillis());
                  updateUserInfo.setLoginType(loginType);
                  updateUserInfo.setLastLoginIp(request.getRemoteHost());
                  // 更新openId
                  updateUserInfo.setOpenid(weiXinUser.getOpenid());
                  userInfoMapper.updateByPrimaryKeySelective(updateUserInfo);
                  // 删除邀请图片
@@ -341,8 +325,7 @@
            userInfo = getUserInfoByPhone(appId, phone);
            // 判断用户是否被删除
            if (userInfo != null && userInfo.getState() != null
                  && (userInfo.getState() == UserInfo.STATE_DELETE_OUT_OF_DATE
                        || userInfo.getState() == UserInfo.STATE_DELETE))
                  && (userInfo.getState() != UserInfo.STATE_NORMAL))
               userInfo = null;
            if (userInfo == null) {// 原先的账号不存在
@@ -379,7 +362,7 @@
      }
   }
   @Transactional
   @Transactional(rollbackFor = Exception.class)
   @Override
   public LoginResult loginNoInstallWX(AcceptData acceptData, String appId, String code, String phone,
         UserInfo tbUserInfo, int loginType) throws UserAccountException {
@@ -431,8 +414,7 @@
      }
      // 判断用户是否被删除
      if (userInfo != null && userInfo.getState() != null && (userInfo.getState() == UserInfo.STATE_DELETE_OUT_OF_DATE
            || userInfo.getState() == UserInfo.STATE_DELETE))
      if (userInfo != null && userInfo.getState() != null && (userInfo.getState() != UserInfo.STATE_NORMAL))
         userInfo = null;
      if (userInfo != null) {
@@ -469,112 +451,6 @@
      }
   }
   @Override
   public void connectUsers(HttpSession session) throws UserAccountException {
      UserInfo mainUser = (UserInfo) session.getAttribute("WEIXIN_USERINFO");
      UserInfo lessUser = (UserInfo) session.getAttribute("LAST_LOGIN_USER");
      int lessUserLoginType = lessUser.getLoginType();
      if (mainUser == null || lessUser == null || mainUser.getId() == null || lessUser.getId() == null)
         throw new UserAccountException(30001, "缺少打通账号");
      mainUser = userInfoMapper.selectByPrimaryKeyForUpdate(mainUser.getId());
      lessUser = userInfoMapper.selectByPrimaryKeyForUpdate(lessUser.getId());
      session.removeAttribute("WEIXIN_USERINFO");
      session.removeAttribute("LAST_LOGIN_USER");
      if (mainUser == null || lessUser == null)
         throw new UserAccountException(30002, "缺少打通账号");
      // 判断是否有绑定相同类型的账号
      if (lessUserLoginType == 1) {// 次账号以淘宝登录
         if (!StringUtil.isNullOrEmpty(mainUser.getPhone()) && !StringUtil.isNullOrEmpty(lessUser.getPhone())
               && !lessUser.getPhone().equalsIgnoreCase(mainUser.getPhone()))
            throw new UserAccountException(30003, "请先解除其中一个账号的手机绑定");
      } else if (lessUserLoginType == 3) {// 次账号以手机号登录
         if (!StringUtil.isNullOrEmpty(mainUser.getOpenid()) && !StringUtil.isNullOrEmpty(lessUser.getOpenid())
               && !lessUser.getOpenid().equalsIgnoreCase(mainUser.getOpenid()))
            throw new UserAccountException(30003, "请先解除其中一个账号的淘宝绑定");
      }
      connectUsers(mainUser, lessUser);
   }
   @Transactional
   @Override
   public void connectUsers(UserInfo mainUser, UserInfo lessUser) throws UserAccountException {
      mainUser = userInfoMapper.selectByPrimaryKeyForUpdate(mainUser.getId());
      lessUser = userInfoMapper.selectByPrimaryKeyForUpdate(lessUser.getId());
      if (!StringUtil.isNullOrEmpty(mainUser.getOpenid()) && !StringUtil.isNullOrEmpty(lessUser.getOpenid()))
         throw new UserAccountException(9, "两个账号都绑定了淘宝,请解绑其中一个");
      if (!StringUtil.isNullOrEmpty(mainUser.getPhone()) && !StringUtil.isNullOrEmpty(lessUser.getPhone()))
         throw new UserAccountException(9, "两个账号都绑定了手机号码,请解绑其中一个");
      if (lessUser.getMyHongBao().compareTo(new BigDecimal("10")) >= 0
            && mainUser.getMyHongBao().compareTo(new BigDecimal("10")) > 0)
         throw new UserAccountException(10, String.format("账户ID:%s 的资金超过10元,为保证资金安全请联系客服打通", lessUser.getId() + ""));
      // 判定是否绑定了同一支付宝
      BindingAccount mainUserAccount = bindingAccountService.getBindingAccountByUidAndType(mainUser.getId(),
            BindingAccount.TYPE_ALIPAY);
      BindingAccount lessUserAccount = bindingAccountService.getBindingAccountByUidAndType(lessUser.getId(),
            BindingAccount.TYPE_ALIPAY);
      // 有不同的支付宝账号
      boolean hasDiffrentAlipayAccount = false;
      if (mainUserAccount != null && lessUserAccount != null) {
         if (!mainUserAccount.getAccount().equalsIgnoreCase(lessUserAccount.getAccount()))
            hasDiffrentAlipayAccount = true;
      }
      if (hasDiffrentAlipayAccount)
         throw new UserAccountException(10, "两个账户都绑定了支付宝,请先解绑其中一个");
      // 判定是否有重叠的账号
      UserInfo updateUser = new UserInfo(mainUser.getId());
      if (!StringUtil.isNullOrEmpty(lessUser.getOpenid())) {
         updateUser.setOpenid(lessUser.getOpenid());
         updateUser.setTbName(lessUser.getTbName());
         updateUser.setTbPic(lessUser.getTbPic());
      }
      if (!StringUtil.isNullOrEmpty(lessUser.getPhone())) {
         updateUser.setPhone(lessUser.getPhone());
      }
      // 备份原来的用户信息
      String logUrl = backupUserImportantInfo(lessUser.getId());
      // 添加打通记录
      UserConnectHistory userConnectHistory = new UserConnectHistory();
      userConnectHistory.setCreateTime(new Date());
      userConnectHistory.setLessUser(lessUser);
      userConnectHistory.setMainUser(mainUser);
      userConnectHistory.setLogUrl(logUrl);
      userConnectHistoryMapper.insertSelective(userConnectHistory);
      userInfoMapper.updateByPrimaryKeySelective(updateUser);
      // 删除原来的账号
      UserInfo updateLessUser = new UserInfo(lessUser.getId());
      updateLessUser.setState(UserInfo.STATE_DELETE);
      updateLessUser.setStateDesc("账号被打通删除");
      userInfoMapper.updateByPrimaryKeySelective(updateLessUser);
      // 融合业务
      userInfoMapper.connectUser(mainUser.getId(), lessUser.getId());
      // 查询是否有多余的新人红包
      int subCount = 0;
      int addCount = 0;
      userAccountMsgNotificationService.connectSuccess(mainUser.getId(), lessUser.getId());
      userInfoDeleteRecordService.addDeleteRecord(lessUser.getId(), UserInfo.STATE_DELETE, "删除:账号被打通删除");
   }
   /**
    * 根据系统与unionid寻找用户
@@ -593,8 +469,7 @@
      List<UserInfo> list = userInfoMapper.listByAppIdAndWXUnionId(appId, unionId);
      // 剔除被删除掉的用户
      for (int i = 0; i < list.size(); i++) {
         if (list.get(i).getState() == UserInfo.STATE_DELETE
               || list.get(i).getState() == UserInfo.STATE_DELETE_OUT_OF_DATE) {
         if (list.get(i).getState() == UserInfo.STATE_NORMAL) {
            list.remove(i);
            i--;
         }
@@ -621,8 +496,7 @@
      // 剔除被删除掉的用户
      for (int i = 0; i < list.size(); i++) {
         if (list.get(i).getState() == UserInfo.STATE_DELETE
               || list.get(i).getState() == UserInfo.STATE_DELETE_OUT_OF_DATE) {
         if (list.get(i).getState() != UserInfo.STATE_NORMAL) {
            list.remove(i);
            i--;
         }
@@ -650,8 +524,7 @@
      // 剔除被删除掉的用户
      for (int i = 0; i < list.size(); i++) {
         if (list.get(i).getState() == UserInfo.STATE_DELETE
               || list.get(i).getState() == UserInfo.STATE_DELETE_OUT_OF_DATE) {
         if (list.get(i).getState() !=UserInfo.STATE_NORMAL) {
            list.remove(i);
            i--;
         }
@@ -669,6 +542,7 @@
      userInfoMapper.updateByPrimaryKeySelective(userInfo);
   }
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void register(UserInfo userInfo) throws UserAccountException {
      if (!StringUtil.isNullOrEmpty(userInfo.getPhone())) {
@@ -692,6 +566,7 @@
      addUser(userInfo);
   }
   @Transactional
   @Override
   public void addUser(UserInfo user) {
@@ -724,12 +599,12 @@
         e.printStackTrace();
      }
      // 添加账号绑定历史
      if (!StringUtil.isNullOrEmpty(user.getPhone()))
         userInfoModifyRecordService.addModifyRecord(uid, ModifyTypeEnum.bindPhone, user.getPhone());
      if (!StringUtil.isNullOrEmpty(user.getWxUnionId()))
         userInfoModifyRecordService.addModifyRecord(uid, ModifyTypeEnum.bindWeiXin, user.getWxUnionId());
   }
   @Override
@@ -885,46 +760,6 @@
   }
   @Override
   public String backupUserImportantInfo(Long uid) {
      String logPath = String.format(FileUtil.getCacheDir() + "/benfen_%s_" + uid + ".log",
            TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyyMMddHHmmss"));
      // 备份用户信息
      UserInfo user = userInfoMapper.selectAvailableByPrimaryKey(uid);
      // TODO 备份资金明细
      // 备份邀请关系
      List<ThreeSale> threeSaleList = new ArrayList<>();
      FileWriter fw = null;
      try {
         // 设置为:True,表示写入的时候追加数据
         fw = new FileWriter(new File(logPath), true);
         fw.write("#UserInfo" + "\r\n");
         fw.write(JsonUtil.getSimpleGsonWithDate().toJson(user) + "\r\n");
         fw.write("#ThreeSale" + "\r\n");
         fw.write(JsonUtil.getSimpleGsonWithDate().toJson(threeSaleList) + "\r\n");
         fw.close();
         String logUrl = COSManager.getInstance()
               .uploadFile(new File(logPath), "beifen/" + new File(logPath).getName()).getUrl();
         return logUrl;
      } catch (IOException e) {
         e.printStackTrace();
      } finally {
         if (new File(logPath).exists())
            new File(logPath).delete();
      }
      return null;
   }
   @Override
   public void clearUserPortrait(Long uid) {
      if (uid == null)
         return;
@@ -972,7 +807,7 @@
      return user.getPortrait();
   }
   @Transactional
   @Transactional(rollbackFor = Exception.class)
   @Override
   public UserInfo loginPhone(HttpServletRequest request, int loginType, String vcode, String phone, String appId)
         throws UserAccountException {
@@ -1018,7 +853,9 @@
      // 清空限制
      redisManager.clearSMSFrequencyLimit(phone, SMSHistory.TYPE_LOGIN);
      // 清理次数
      redisManager.removeCommonString("sendMSNLoginCount" + phone);
      String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSLoginCount, phone);
      redisManager.removeCommonString(key);
      // 采用redis事务防止一个手机号多次注册问题
      String watchKey = StringUtil.Md5("REGISTER:" + phone);
@@ -1067,12 +904,12 @@
      } finally {
         jedis.del(watchKey);
         jedis.unwatch();
         jedisPool.returnResource(jedis);
         jedis.close();
      }
      throw new UserAccountException(10, "请稍后再试");
   }
   @Transactional
   @Transactional(rollbackFor = Exception.class)
   @Override
   public UserInfo loginWinXin(HttpServletRequest request, AcceptData acceptData, int loginType, String code,
         String appId) throws UserAccountException {
@@ -1085,8 +922,7 @@
      // 通过Code换取信息
      WXAccountInfoDTO wxAccount = Constant.getWXAccount(acceptData.getPlatform(), acceptData.getVersion());
      WeiXinUser weiXinUser = WXLoginUtil.getWeiXinUser(code, wxAccount.getAppId(),
            wxAccount.getAppSecret());
      WeiXinUser weiXinUser = WXLoginUtil.getWeiXinUser(code, wxAccount.getAppId(), wxAccount.getAppSecret());
      if (weiXinUser == null) {
         throw new UserAccountException(1, "微信帐号授权失败");
      }
@@ -1123,23 +959,24 @@
               spreadUserImgService.deleteImgUrl(userInfo.getId());
            } else {
               LogHelper.test("微信unionID不存在:" + weiXinUser.getUnionid());
               String portrait = null;
               if (!StringUtil.isNullOrEmpty(weiXinUser.getHeadimgurl())) {
                  InputStream asInputStream = HttpUtil.getAsInputStream(weiXinUser.getHeadimgurl());
                  if (asInputStream != null) {
                     FileUploadResult result = COSManager.getInstance().uploadFile(asInputStream,
                           String.format("/portrait/wx/%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 {
                     portrait = weiXinUser.getHeadimgurl();
                  }
               }
               if (StringUtil.isNullOrEmpty(portrait))
                  portrait = Constant.systemCommonConfig.getDefaultPortrait();
               // 创建新账户
               userInfo = new UserInfo();
               userInfo.setPortrait(portrait);
@@ -1172,15 +1009,15 @@
      } finally {
         jedis.del(watchKey);
         jedis.unwatch();
         jedisPool.returnResource(jedis);
         jedis.close();
      }
      throw new UserAccountException(10, "请稍后再试");
   }
   @Transactional
   @Transactional(rollbackFor = Exception.class)
   @Override
   public UserInfo loginWeiXinNew(HttpServletRequest request, AcceptData acceptData, int loginType, String wxCode, String appId) throws UserAccountException {
   public UserInfo loginWeiXinNew(HttpServletRequest request, AcceptData acceptData, int loginType, String wxCode,
         String appId) throws UserAccountException {
      // 日志信息
      JSONObject logInfo = new JSONObject();
      logInfo.put("appId", appId);
@@ -1190,8 +1027,7 @@
      // 通过Code换取信息
      WXAccountInfoDTO wxAccount = Constant.getWXAccount(acceptData.getPlatform(), acceptData.getVersion());
      WeiXinUser weiXinUser = WXLoginUtil.getWeiXinUser(wxCode, wxAccount.getAppId(),
            wxAccount.getAppSecret());
      WeiXinUser weiXinUser = WXLoginUtil.getWeiXinUser(wxCode, wxAccount.getAppId(), wxAccount.getAppSecret());
      if (weiXinUser == null) {
         throw new UserAccountException(1, "微信帐号授权失败");
      }
@@ -1204,12 +1040,11 @@
         throw new UserAccountException(Constant.CODE_FORBIDDEN_USER, Constant.FORBIDDEN_USER_REASON_DESC);
      }
      UserInfo userInfo = userInfoMapper.getEffectiveUserInfoByWXUnionId(weiXinUser.getUnionid());
      if (userInfo == null) {
         String longinKey = StringUtil.Md5("WXLogin:" + weiXinUser.getUnionid());
         redisManager.cacheCommonString(longinKey, JsonUtil.getSimpleGson().toJson(weiXinUser), 60 * 20);
         throw new UserAccountException(101, longinKey);
         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);
      }
      // 更新账户登录信息
      updateLonginInfo(userInfo, loginType, request);
@@ -1223,8 +1058,6 @@
      return userInfo;
   }
   /**
    * 更新账户登录信息
    * 
@@ -1282,15 +1115,33 @@
   }
   @Transactional(rollbackFor=UserAccountException.class)
   @Override
   public void bindWeiXin(AcceptData acceptData, Long uid, String code) throws UserAccountException {
      WXAccountInfoDTO wxAccount = Constant.getWXAccount(acceptData.getPlatform(), acceptData.getVersion());
      WeiXinUser weiXinUser = WXLoginUtil.getWeiXinUser(code, wxAccount.getAppId(), wxAccount.getAppSecret());
      if (weiXinUser == null) {
         throw new UserAccountException(2, "微信帐号授权失败");
      }
      String wxUnionId = weiXinUser.getUnionid();
      if (StringUtil.isNullOrEmpty(wxUnionId)) {
         throw new UserAccountException(2, "微信帐号授权失败");
      }
      bindWeiXin(uid, weiXinUser);
   }
   @Transactional(rollbackFor=UserAccountException.class)
   @Override
   public void bindWeiXin(Long uid,WeiXinUser weiXinUser) throws UserAccountException {
      UserInfo user = userInfoMapper.selectAvailableByPrimaryKey(uid);
      if (user == null) {
         throw new UserAccountException(1, "用户不存在");
      }
      WXAccountInfoDTO wxAccount = Constant.getWXAccount(acceptData.getPlatform(), acceptData.getVersion());
      WeiXinUser weiXinUser = WXLoginUtil.getWeiXinUser(code, wxAccount.getAppId(),
            wxAccount.getAppSecret());
      if (weiXinUser == null) {
         throw new UserAccountException(2, "微信帐号授权失败");
      }
@@ -1336,14 +1187,14 @@
         String headimgurl = weiXinUser.getHeadimgurl();
         InputStream asInputStream = HttpUtil.getAsInputStream(headimgurl);
         if (asInputStream == null) {
            LogHelper.test("微信头像下载失败: " +weiXinUser.getUnionid()+ " " + headimgurl);
            LogHelper.test("微信头像下载失败: " + weiXinUser.getUnionid() + " " + headimgurl);
         } else {
            FileUploadResult result = COSManager.getInstance().uploadFile(asInputStream,
                  String.format("/portrait/wx/%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 {
               LogHelper.test("微信头像上传失败: " +weiXinUser.getUnionid()+ " " + headimgurl);
               LogHelper.test("微信头像上传失败: " + weiXinUser.getUnionid() + " " + headimgurl);
            }
         }
         updateUserInfo.setPortrait(headimgurl);
@@ -1367,49 +1218,49 @@
      userInfoModifyRecordService.addModifyRecord(uid, ModifyTypeEnum.bindWeiXin, wxUnionId);
   }
   @Override
   public UserInfo bindPhoneToLogin(String phone, String key, String appId, HttpServletRequest request) throws UserAccountException {
   public UserInfo bindPhoneToLogin(String phone, String key, String appId, HttpServletRequest request)
         throws UserAccountException {
      UserInfo phoneUser = userInfoMapper.getEffectiveUserInfoByPhone(phone);
      if (phoneUser != null)
      if (phoneUser != null)
         throw new UserAccountException(1, "号码已经被占用");
      // 判断手机号码是否被封禁
      ForbiddenUserIdentifyCode identifyCode1 = forbiddenUserIdentifyCodeService
            .listByTypeAndIdentifyCode(ForbiddenUserIdentifyCodeTypeEnum.phone, phone);
      if (identifyCode1 != null && identifyCode1.getEffective() != null && identifyCode1.getEffective())
         throw new UserAccountException(Constant.CODE_FORBIDDEN_USER, Constant.FORBIDDEN_USER_REASON_DESC);
      String value = redisManager.getCommonString(key);
      if (StringUtil.isNullOrEmpty(value))
         throw new UserAccountException(1, "微信授权失效,请使用微信重新登录");
      WeiXinUser weiXinUser = JsonUtil.getSimpleGson().fromJson(value, WeiXinUser.class);
      if (weiXinUser == null)
         throw new UserAccountException(1, "微信授权失效,请使用微信重新登录");
      UserInfo userInfo = userInfoMapper.getEffectiveUserInfoByWXUnionId(weiXinUser.getUnionid());
      // 直接用的微信登录
      if (userInfo != null)
      if (userInfo != null)
         throw new UserAccountException(10, "该微信号已被占用");
      String portrait = null;
      if (!StringUtil.isNullOrEmpty(weiXinUser.getHeadimgurl())) {
         InputStream asInputStream = HttpUtil.getAsInputStream(weiXinUser.getHeadimgurl());
         if (asInputStream != null) {
            FileUploadResult result = COSManager.getInstance().uploadFile(asInputStream,
                  String.format("/portrait/wx/%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 {
            portrait = weiXinUser.getHeadimgurl();
         }
      }
      if (StringUtil.isNullOrEmpty(portrait))
         portrait = Constant.systemCommonConfig.getDefaultPortrait();
      // 创建新账户
      userInfo = new UserInfo();
      userInfo.setPhone(phone);
@@ -1425,7 +1276,7 @@
      userInfo.setLastLoginIp(request.getRemoteHost());
      userInfo.setState(UserInfo.STATE_NORMAL);
      addUser(userInfo);
      Long uid = userInfo.getId();
      ThreadUtil.run(new Runnable() {
         public void run() {
@@ -1435,7 +1286,7 @@
            } catch (Exception e) {
               e.printStackTrace();
            }
            // 加入绑定记录
            UserAccountBindingHistory history = new UserAccountBindingHistory();
            history.setContent(phone);
@@ -1445,16 +1296,14 @@
            userAccountBindingHistoryService.addUserAccountBindingHistory(history);
            userInfoModifyRecordService.addModifyRecord(uid, ModifyTypeEnum.bindPhone, phone);
            userInfoModifyRecordService.addModifyRecord(uid, ModifyTypeEnum.bindWeiXin,   weiXinUser.getUnionid());
            userInfoModifyRecordService.addModifyRecord(uid, ModifyTypeEnum.bindWeiXin, weiXinUser.getUnionid());
         }
      });
      // 删除缓存
      redisManager.removeCommonString(key);
      return userInfo;
   }
   @Override
   public void forbiddenUserAll(Long uid, String reason) {
@@ -1578,4 +1427,6 @@
      // 插入记录
      userInfoDeleteRecordService.addDeleteRecord(uid, UserInfo.STATE_DELETE_OUT_OF_DATE, reason);
   }
}