Explorar o código

修复获取登录信息异常处理

bobo %!s(int64=4) %!d(string=hai) anos
pai
achega
3707c6f1d1

+ 19 - 13
carbon-h5/carbon-h5-service/src/main/java/com/hcloud/microserver/h5/service/impl/CustomerServiceImpl.java

@@ -1,5 +1,7 @@
 package com.hcloud.microserver.h5.service.impl;
 
+import com.hcloud.microserver.commoncore.base.ResultCode;
+import com.hcloud.microserver.commoncore.exception.ApiException;
 import com.hcloud.microserver.h5.bo.CustomerDetails;
 import com.hcloud.microserver.h5.dao.CustomerCompanyInfoMapper;
 import com.hcloud.microserver.h5.dao.CustomerInfoMapper;
@@ -40,13 +42,13 @@ public class CustomerServiceImpl implements CustomerService {
 
     @Override
     public CustomerInfoForm getByUserId(String accountStr) {
-        if (StringUtils.isEmpty(accountStr)){
+        if (StringUtils.isEmpty(accountStr)) {
             return null;
         }
         CustomerInfoForm customer = customerCacheService.getCustomer(accountStr);
-        if (customer==null){
+        if (customer == null) {
             CustomerInfo customerInfo = customerInfoMapper.searchCustomerByAccount(accountStr);
-            if (customerInfo != null){
+            if (customerInfo != null) {
                 String guid = customerInfo.getGuid();
                 customer = customerInfoService.getCustomerInfoById(guid);
             }
@@ -58,7 +60,7 @@ public class CustomerServiceImpl implements CustomerService {
     @Override
     public CustomerDetails loadUserByUserId(String accountStr) {
         CustomerInfoForm byUserId = getByUserId(accountStr);
-        if (byUserId == null){
+        if (byUserId == null) {
             return null;
         }
         return new CustomerDetails(byUserId);
@@ -68,10 +70,14 @@ public class CustomerServiceImpl implements CustomerService {
     public CustomerInfoForm getCurrentMember() {
         SecurityContext ctx = SecurityContextHolder.getContext();
         Authentication auth = ctx.getAuthentication();
-        if(org.springframework.util.StringUtils.isEmpty(auth)){
-            throw new RuntimeException("获取登陆用户失败!");
+
+        CustomerDetails memberDetails = null;
+        try {
+            memberDetails = (CustomerDetails) auth.getPrincipal();
+
+        } catch (Exception e) {
+            throw new ApiException(ResultCode.UNAUTHORIZED);
         }
-        CustomerDetails memberDetails = (CustomerDetails) auth.getPrincipal();
         return memberDetails.getCustomerInfo();
     }
 
@@ -79,21 +85,21 @@ public class CustomerServiceImpl implements CustomerService {
     public String login(LoginForm loginForm) {
         String accountStr = loginForm.getLoginStr();
         String pwd = loginForm.getPwd();
-        if (StringUtils.isEmpty(accountStr)||StringUtils.isEmpty(pwd)){
+        if (StringUtils.isEmpty(accountStr) || StringUtils.isEmpty(pwd)) {
             Asserts.fail("登录失败");
         }
         CustomerDetails customerInfo = loadUserByUserId(accountStr);
 
-        if (customerInfo == null){
+        if (customerInfo == null) {
             Asserts.fail("登录失败");
         }
         Integer isCompany = customerInfo.getCustomerInfo().getIsCompany();
-        if (isCompany != 1){
+        if (isCompany != 1) {
             Asserts.fail("非企业用户不能进行企业登录");
         }
         String s = Md5Util.toMD5(pwd);
 
-        if (!s.equals(customerInfo.getCustomerInfo().getPasswd())){
+        if (!s.equals(customerInfo.getCustomerInfo().getPasswd())) {
             Asserts.fail("账号或密码错误");
         }
         UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(customerInfo, null, customerInfo.getAuthorities());
@@ -102,15 +108,15 @@ public class CustomerServiceImpl implements CustomerService {
         customerCacheService.setToken(token, accountStr);
         return token;
     }
+
     @Autowired
     private CustomerService customerService;
 
-    public void logout(){
+    public void logout() {
         CustomerInfoForm customerInfo = customerService.getCurrentMember();
         String account = customerInfo.getAccount();
         customerCacheService.delToken(account);
     }
 
 
-
 }