admin
2019-10-22 32f925357776c46bd9e35cf9aad4fdd0f6535d1d
fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserAccountServiceImpl.java
@@ -3,6 +3,7 @@
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;
@@ -15,7 +16,9 @@
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.HttpUtil;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.entity.FileUploadResult;
import org.yeshi.utils.tencentcloud.COSManager;
import com.google.gson.Gson;
@@ -1082,7 +1085,7 @@
      // 通过Code换取信息
      WXAccountInfoDTO wxAccount = Constant.getWXAccount(acceptData.getPlatform(), acceptData.getVersion());
      WeiXinUser weiXinUser = WXLoginUtil.getWeiXinUserWithSavePortrait(code, wxAccount.getAppId(),
      WeiXinUser weiXinUser = WXLoginUtil.getWeiXinUser(code, wxAccount.getAppId(),
            wxAccount.getAppSecret());
      if (weiXinUser == null) {
         throw new UserAccountException(1, "微信帐号授权失败");
@@ -1120,9 +1123,26 @@
               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() + ""));
                     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(weiXinUser.getHeadimgurl());
               userInfo.setPortrait(portrait);
               userInfo.setAppId(appId);
               userInfo.setNickName(weiXinUser.getNickname());
               userInfo.setWxName(weiXinUser.getNickname());
@@ -1170,7 +1190,7 @@
      // 通过Code换取信息
      WXAccountInfoDTO wxAccount = Constant.getWXAccount(acceptData.getPlatform(), acceptData.getVersion());
      WeiXinUser weiXinUser = WXLoginUtil.getWeiXinUserWithSavePortrait(wxCode, wxAccount.getAppId(),
      WeiXinUser weiXinUser = WXLoginUtil.getWeiXinUser(wxCode, wxAccount.getAppId(),
            wxAccount.getAppSecret());
      if (weiXinUser == null) {
         throw new UserAccountException(1, "微信帐号授权失败");
@@ -1184,45 +1204,23 @@
         throw new UserAccountException(Constant.CODE_FORBIDDEN_USER, Constant.FORBIDDEN_USER_REASON_DESC);
      }
      // 采用redis事务防止一个微信号多次注册问题
      String watchKey = StringUtil.Md5("REGISTER:" + weiXinUser.getUnionid());
      Jedis jedis = jedisPool.getResource();
      try {
         jedis.watch(watchKey);
         if (jedis.get(watchKey) != null && Integer.parseInt(jedis.get(watchKey)) > 1)
            throw new UserAccountException(10, "请稍后再试");
         Transaction tran = jedis.multi();
         tran.incr(watchKey);
         List<Object> exec = tran.exec();
         if (exec == null || exec.size() == 0)
            throw new UserAccountException(10, "请稍后再试");
         UserInfo userInfo = userInfoMapper.getEffectiveUserInfoByWXUnionId(weiXinUser.getUnionid());
         if (userInfo == null) {
            redisManager.cacheCommonString(watchKey, JsonUtil.getSimpleGson().toJson(weiXinUser), 60 * 20);
            throw new UserAccountException(101, watchKey);
         }
         LogHelper.test("微信unionID存在:" + weiXinUser.getUnionid());
         // 更新账户登录信息
         updateLonginInfo(userInfo, loginType, request);
         Long uid = userInfo.getId();
         ThreadUtil.run(new Runnable() {
            public void run() {
               // 删除邀请分享图
               spreadUserImgService.deleteImgUrl(uid);
            }
         });
         return userInfo;
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         jedis.del(watchKey);
         jedis.unwatch();
         jedisPool.returnResource(jedis);
      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);
      }
      throw new UserAccountException(10, "请稍后再试");
      // 更新账户登录信息
      updateLonginInfo(userInfo, loginType, request);
      Long uid = userInfo.getId();
      ThreadUtil.run(new Runnable() {
         public void run() {
            // 删除邀请分享图
            spreadUserImgService.deleteImgUrl(uid);
         }
      });
      return userInfo;
   }
   
@@ -1291,7 +1289,7 @@
         throw new UserAccountException(1, "用户不存在");
      }
      WXAccountInfoDTO wxAccount = Constant.getWXAccount(acceptData.getPlatform(), acceptData.getVersion());
      WeiXinUser weiXinUser = WXLoginUtil.getWeiXinUserWithSavePortrait(code, wxAccount.getAppId(),
      WeiXinUser weiXinUser = WXLoginUtil.getWeiXinUser(code, wxAccount.getAppId(),
            wxAccount.getAppSecret());
      if (weiXinUser == null) {
         throw new UserAccountException(2, "微信帐号授权失败");
@@ -1308,7 +1306,7 @@
         if (newUser != null) {
            throw new UserAccountException(4, "该微信号已被其他帐号绑定");
         }
      } else if (wxUnionId.equals(wxUnionIdExist)) {
      } else if (wxUnionId.equalsIgnoreCase(wxUnionIdExist)) {
         throw new UserAccountException(3, "微信帐号一致无需更换");
      }
@@ -1335,7 +1333,20 @@
      String defaultPortrait = Constant.systemCommonConfig.getDefaultPortrait();
      if (!StringUtil.isNullOrEmpty(weiXinUser.getHeadimgurl()) && !StringUtil.isNullOrEmpty(defaultPortrait)
            && defaultPortrait.equals(user.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);
      }
      userInfoMapper.updateByPrimaryKeySelective(updateUserInfo);
@@ -1378,73 +1389,69 @@
      if (weiXinUser == null)
         throw new UserAccountException(1, "微信授权失效,请使用微信重新登录");
      
      String watchKey = StringUtil.Md5("REGISTER:" + weiXinUser.getUnionid());
      Jedis jedis = jedisPool.getResource();
      try {
         jedis.watch(watchKey);
         if (jedis.get(watchKey) != null && Integer.parseInt(jedis.get(watchKey)) > 1)
            throw new UserAccountException(10, "请稍后再试");
         Transaction tran = jedis.multi();
         tran.incr(watchKey);
         List<Object> exec = tran.exec();
         if (exec == null || exec.size() == 0)
            throw new UserAccountException(10, "请稍后再试");
         UserInfo userInfo = userInfoMapper.getEffectiveUserInfoByWXUnionId(weiXinUser.getUnionid());
         // 直接用的微信登录
         if (userInfo != null)
            throw new UserAccountException(10, "该微信号已被占用");
         // 创建新账户
         userInfo = new UserInfo();
         userInfo.setPortrait(weiXinUser.getHeadimgurl());
         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() {
            public void run() {
               try {
                  // 第一次登录时创建用户额外信息
                  userInfoExtraService.createUserInfoExtra(uid);
               } catch (Exception e) {
                  e.printStackTrace();
               }
               // 加入绑定记录
               UserAccountBindingHistory history = new UserAccountBindingHistory();
               history.setContent(phone);
               history.setType(UserAccountBindingHistory.TYPE_PHONE);
               history.setUid(uid);
               history.setFirst(true);
               userAccountBindingHistoryService.addUserAccountBindingHistory(history);
               userInfoModifyRecordService.addModifyRecord(uid, ModifyTypeEnum.bindPhone, phone);
               userInfoModifyRecordService.addModifyRecord(uid, ModifyTypeEnum.bindWeiXin,   weiXinUser.getUnionid());
            }
         });
         // 删除缓存
         redisManager.removeCommonString(key);
         return userInfo;
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         jedis.del(watchKey);
         jedis.unwatch();
         jedisPool.returnResource(jedis);
      UserInfo userInfo = userInfoMapper.getEffectiveUserInfoByWXUnionId(weiXinUser.getUnionid());
      // 直接用的微信登录
      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() + ""));
            if (result != null && !StringUtil.isNullOrEmpty(result.getUrl()))
               portrait = result.getUrl();
         } else {
            portrait = weiXinUser.getHeadimgurl();
         }
      }
      throw new UserAccountException(10, "请稍后再试");
      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);
      Long uid = userInfo.getId();
      ThreadUtil.run(new Runnable() {
         public void run() {
            try {
               // 第一次登录时创建用户额外信息
               userInfoExtraService.createUserInfoExtra(uid);
            } catch (Exception e) {
               e.printStackTrace();
            }
            // 加入绑定记录
            UserAccountBindingHistory history = new UserAccountBindingHistory();
            history.setContent(phone);
            history.setType(UserAccountBindingHistory.TYPE_PHONE);
            history.setUid(uid);
            history.setFirst(true);
            userAccountBindingHistoryService.addUserAccountBindingHistory(history);
            userInfoModifyRecordService.addModifyRecord(uid, ModifyTypeEnum.bindPhone, phone);
            userInfoModifyRecordService.addModifyRecord(uid, ModifyTypeEnum.bindWeiXin,   weiXinUser.getUnionid());
         }
      });
      // 删除缓存
      redisManager.removeCommonString(key);
      return userInfo;
   }