From 36681e15e12aaa9135f69260472de65303cdcba3 Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期二, 26 四月 2022 19:10:45 +0800
Subject: [PATCH] 任务优化

---
 app/src/main/java/com/yeshi/makemoney/app/service/impl/user/UserAccountServiceImpl.java |   90 ++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 85 insertions(+), 5 deletions(-)

diff --git a/app/src/main/java/com/yeshi/makemoney/app/service/impl/user/UserAccountServiceImpl.java b/app/src/main/java/com/yeshi/makemoney/app/service/impl/user/UserAccountServiceImpl.java
index 6a3cd2b..a66d6f0 100644
--- a/app/src/main/java/com/yeshi/makemoney/app/service/impl/user/UserAccountServiceImpl.java
+++ b/app/src/main/java/com/yeshi/makemoney/app/service/impl/user/UserAccountServiceImpl.java
@@ -1,11 +1,13 @@
 package com.yeshi.makemoney.app.service.impl.user;
 
 import com.yeshi.makemoney.app.dto.user.LoginInfoDTO;
+import com.yeshi.makemoney.app.entity.SystemEnum;
 import com.yeshi.makemoney.app.entity.config.SystemConfigKey;
 import com.yeshi.makemoney.app.entity.user.UserInfo;
 import com.yeshi.makemoney.app.entity.user.UserLoginRecord;
 import com.yeshi.makemoney.app.entity.user.WXUserInfo;
 import com.yeshi.makemoney.app.exception.user.LoginException;
+import com.yeshi.makemoney.app.exception.user.UserAccountException;
 import com.yeshi.makemoney.app.service.inter.config.SystemConfigService;
 import com.yeshi.makemoney.app.service.inter.user.*;
 import com.yeshi.makemoney.app.service.manager.VerifyCodeManager;
@@ -61,9 +63,9 @@
                     loginInfo.setPhone(mobile);
                 } else {
                     //楠岃瘉鐮佺櫥褰�
-//                    if (!verifyCodeManager.isPhoneCodeRight(loginInfo.getSystem(), loginInfo.getPhone(), loginInfo.getVcode())) {
-//                        throw new LoginException("楠岃瘉鐮侀敊璇�");
-//                    }
+                    if (!verifyCodeManager.isPhoneCodeRight(loginInfo.getSystem(), loginInfo.getPhone(), loginInfo.getVcode())) {
+                        throw new LoginException("楠岃瘉鐮侀敊璇�");
+                    }
                 }
                 //鎵ц鐧诲綍鎿嶄綔
                 user = userInfoService.selectByPhoneAndSystemAndStatus(loginInfo.getPhone(), loginInfo.getSystem(), UserInfo.STATUS_NORMAL);
@@ -99,8 +101,11 @@
                 //淇濆瓨寰俊鐢ㄦ埛淇℃伅
                 WXUserInfo wxUserInfo = null;
                 try {
-                    wxUserInfo = wxUserInfoService.save(WXUserInfo.create(weiXinUser));
+                    WXUserInfo temp = WXUserInfo.create(weiXinUser);
+                    temp.setSystem(loginInfo.getSystem());
+                    wxUserInfo = wxUserInfoService.save(temp);
                 } catch (Exception e) {
+                    e.printStackTrace();
                     throw new LoginException("淇濆瓨寰俊鎺堟潈淇℃伅澶辫触");
                 }
                 wxUserInfo.setSystem(loginInfo.getSystem());
@@ -111,7 +116,11 @@
                     newUser.setWxUser(wxUserInfo);
                     newUser.setSystem(loginInfo.getSystem());
                     newUser.setNickName(wxUserInfo.getNickName());
-                    newUser.setPortrait(wxUserInfo.getHeadImgUrl());
+                    if (StringUtil.isNullOrEmpty(wxUserInfo.getHeadImgUrl())) {
+                        newUser.setPortrait(systemConfigService.getValueCache(loginInfo.getSystem(), SystemConfigKey.defaultPortrait));
+                    } else {
+                        newUser.setPortrait(wxUserInfo.getHeadImgUrl());
+                    }
                     try {
                         userInfoService.add(newUser);
                     } catch (Exception e) {
@@ -157,6 +166,77 @@
         userInfoService.update(update);
     }
 
+    @Transactional(rollbackFor = Exception.class)
+    @Override
+    public void bindWX(Long uid, String code) throws UserAccountException {
+        UserInfo user = userInfoService.get(uid);
+
+        if (user == null) {
+            throw new UserAccountException(UserAccountException.CODE_USER_ERROR, "鐢ㄦ埛涓嶅瓨鍦�");
+        }
+
+        SystemEnum system = user.getSystem();
+        String appId = systemConfigService.getValueCache(system, SystemConfigKey.wxAppId);
+        String appSecret = systemConfigService.getValueCache(system, SystemConfigKey.wxAppSecret);
+        WeiXinUser weiXinUser = WXAppLoginUtil.getWeiXinUser(code, appId, appSecret);
+        if (weiXinUser == null) {
+            throw new UserAccountException(UserAccountException.CODE_ERROR_WX_INFO, "寰俊鎺堟潈淇℃伅鑾峰彇澶辫触");
+        }
+        //淇濆瓨寰俊鐢ㄦ埛淇℃伅
+        WXUserInfo wxUserInfo = null;
+        try {
+            WXUserInfo temp = WXUserInfo.create(weiXinUser);
+            temp.setSystem(system);
+            wxUserInfo = wxUserInfoService.save(temp);
+        } catch (Exception e) {
+            throw new UserAccountException(UserAccountException.CODE_ERROR_WX_INFO, "淇濆瓨寰俊鎺堟潈淇℃伅澶辫触");
+        }
+        UserInfo u = userInfoService.selectByWXIdAndSystemAndStatus(wxUserInfo.getId(), system, UserInfo.STATUS_NORMAL);
+        if (u != null) {
+            throw new UserAccountException(UserAccountException.CODE_ALREAD_BIND, "寰俊宸茬粦瀹氬叾浠栬处鍙�");
+        }
+        //缁戝畾鍒扮敤鎴�
+        UserInfo update = new UserInfo();
+        update.setId(uid);
+        update.setWxUser(wxUserInfo);
+        userInfoService.update(update);
+    }
+
+    @Override
+    public void bindPhone(Long uid, String phone, String vcode, String token) throws UserAccountException {
+        UserInfo user = userInfoService.get(uid);
+        if (user == null) {
+            throw new UserAccountException(UserAccountException.CODE_USER_ERROR, "鐢ㄦ埛涓嶅瓨鍦�");
+        }
+        SystemEnum system = user.getSystem();
+        if (!StringUtil.isNullOrEmpty(token)) {
+            //涓�閿櫥褰�
+            //鑾峰彇鐢佃瘽鍙风爜
+            String mobile = AliyunOneKeyLoginUtil.getMobile(token, UUID.randomUUID().toString(), systemConfigService.getValueCache(system, SystemConfigKey.aliyunOneKeyAuthAcessKey), systemConfigService.getValueCache(system, SystemConfigKey.aliyunOneKeyAuthAcessSecret));
+            if (StringUtil.isNullOrEmpty(mobile)) {
+                throw new UserAccountException(UserAccountException.CODE_ERROR_PHONE, "鐢佃瘽鍙风爜鑾峰彇澶辫触");
+            }
+            phone = mobile;
+        } else {
+            //楠岃瘉鐮佺櫥褰�
+            if (!verifyCodeManager.isPhoneCodeRight(system, phone, vcode)) {
+                throw new UserAccountException(UserAccountException.CODE_ERROR_PHONE, "楠岃瘉鐮侀敊璇�");
+            }
+        }
+
+
+        UserInfo u = userInfoService.selectByPhoneAndSystemAndStatus(phone, system, UserInfo.STATUS_NORMAL);
+        if (u != null) {
+            throw new UserAccountException(UserAccountException.CODE_ALREAD_BIND, "鎵嬫満鍙峰凡缁戝畾鍏朵粬璐﹀彿");
+        }
+
+        //缁戝畾鍒扮敤鎴�
+        UserInfo update = new UserInfo();
+        update.setId(uid);
+        update.setPhone(phone);
+        userInfoService.update(update);
+    }
+
 
     /**
      * @return void

--
Gitblit v1.8.0