Преглед на файлове

添加api授权和计费

lidongyang преди 5 години
родител
ревизия
a9d60cae39

+ 12 - 22
src/main/java/com/lightinit/hsdataplatform/common/GateWayUtils.java

@@ -1,9 +1,11 @@
 package com.lightinit.hsdataplatform.common;
 
 import com.lightinit.hsdataplatform.dictionary.DicNewGateway;
+import com.lightinit.hsdataplatform.entity.ResourceApiInterfaceWithBLOBs;
 
 import java.util.Date;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 public class GateWayUtils {
@@ -30,30 +32,18 @@ public class GateWayUtils {
         return code ;
     }
 
-    public static String gatewayChargeAdd(String ouyId1, String applicationid, String userId,String chargeType
-            ,String chargeValue, String remark) {
-        String code = null ;
-        Map<String, String> params = new HashMap<>();
-        params.put("routeId", ouyId1);
-        params.put("applicationId", applicationid);
-        params.put("userId", userId);
-        params.put("chargeType", chargeType);
-        params.put("chargeValue", chargeValue);
-        params.put("effectiveTime", new Date().toString());
-        params.put("remark", remark);
-        try {
-            String str = HttpUtil.httpPost(DicNewGateway.CHANGEADD, null, params);
-            Map<String, Object> map1 = HttpUtil.strToMap(str);
-            if (map1.get("code").equals("200")) {
-                code = map1.get("code").toString() ;
-            } else {
-                throw new RuntimeException("新增api鉴权异常:"+map1.get("message"));
+    public static void gatewayChargeAdd(List<ResourceApiInterfaceWithBLOBs> list, String userId, String remark) throws Exception {
+        if (list.size() > 0) {
+            for (ResourceApiInterfaceWithBLOBs resourceApiInterfaceWithBLOBs : list) {
+                Map<String, String> params = new HashMap<>();
+                params.put("routeId", resourceApiInterfaceWithBLOBs.getRouteId());
+                params.put("appId", resourceApiInterfaceWithBLOBs.getAppId());
+                params.put("userId", userId);
+                params.put("remark", remark);
+                HttpUtil.httpPost(SysContants.GatewayPath.GATEWAYURL_AUTHSAVE, null, params);
+                HttpUtil.httpPost(SysContants.GatewayPath.GATEWAYURL_CHARGESAVE, null, params);
             }
-            System.out.println("=================" + map1 + "==========================");
-        } catch (Exception e) {
-            throw new RuntimeException("新增api计费异常!");
         }
-        return code ;
     }
 
 }

+ 1 - 1
src/main/java/com/lightinit/hsdataplatform/controller/admin/AuthorizationController.java

@@ -79,7 +79,7 @@ public class AuthorizationController extends BaseController{
             return getAllFieldInvalidResultState(inputModel, bindingResult);
         }
         LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
-        ResultState<Long> resultState = authorizationService.handle(inputModel,loginUser.getId());
+        ResultState<Long> resultState = authorizationService.handle(inputModel,loginUser);
         return resultState;
     }
 

+ 108 - 106
src/main/java/com/lightinit/hsdataplatform/impl/admin/AuthorizationServiceImpl.java

@@ -10,10 +10,7 @@ import com.lightinit.hsdataplatform.entity.*;
 import com.lightinit.hsdataplatform.mapper.*;
 import com.lightinit.hsdataplatform.model.ResultState;
 import com.lightinit.hsdataplatform.model.ResultStateCode;
-import com.lightinit.hsdataplatform.model.admin.AuthorizationHeableModel;
-import com.lightinit.hsdataplatform.model.admin.AuthorizationListModel;
-import com.lightinit.hsdataplatform.model.admin.AuthorizationSearchModel;
-import com.lightinit.hsdataplatform.model.admin.AuthorizationViewModel;
+import com.lightinit.hsdataplatform.model.admin.*;
 import com.lightinit.hsdataplatform.service.admin.IAccountManageService;
 import com.lightinit.hsdataplatform.service.admin.IAuthorizationService;
 import com.lightinit.hsdataplatform.spring.DynamicDataContextHolder;
@@ -61,6 +58,9 @@ public class AuthorizationServiceImpl implements IAuthorizationService {
     @Autowired
     private PublishInfoMapper publishInfoMapper ;
 
+    @Autowired
+    private ResourceApiInterfaceMapper resourceApiInterfaceMapper ;
+
     @Override
     public List<AuthorizationListModel> queryList(AuthorizationSearchModel inputModel) {
         DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
@@ -81,7 +81,7 @@ public class AuthorizationServiceImpl implements IAuthorizationService {
 
     @Override
     @Transactional
-    public ResultState<Long> handle(AuthorizationHeableModel inputModel,Long followId) {
+    public ResultState<Long> handle(AuthorizationHeableModel inputModel, LoginUser loginUser) {
         DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
         ResultState<Long> resultState = new ResultState<Long>();
         Date createdAt = new Date();
@@ -94,14 +94,15 @@ public class AuthorizationServiceImpl implements IAuthorizationService {
         ResourceAuthExample.Criteria criteria = example.createCriteria() ;
         criteria.andIdEqualTo(inputModel.getId()) ;
         int result0 = resourceAuthMapper.updateByExampleSelective(resourceAuth,example) ;
+
         int result1 = 0;int result2 = 0 ;
         if (inputModel.getAuthorizationState().equals(DicAuthorizationState.AUTHORIZE_OK)){
             PublishSetting publishSetting = publishSettingMapper.selectByPrimaryKey(inputModel.getPublishSettingId()) ;
-
+            PublishInfoWithBLOBs publishInfoWithBLOBs = publishInfoMapper.selectByPrimaryKey(publishSetting.getPublishInfoId());
             //插入order表
             Order order = new Order() ;
             order.setPlatformType(inputModel.getPlatfromType());
-            order.setUserId(followId) ;
+            order.setUserId(loginUser.getId()) ;
             order.setOrderTime(createdAt) ;
             order.setOrderNumber(OrderNumberUtils.generate());
             if (inputModel.getPlatfromType().equals(DicOrderTypeState.OFFLINE)){
@@ -130,25 +131,25 @@ public class AuthorizationServiceImpl implements IAuthorizationService {
             if (result1>0&&result2>0){
                 //=================新路由鉴权和计费==================
                 if (order.getOrderState().equals(DicOrderState.PAID)&&orderGoods.getResourceType().equals(DicResourceType.API)){
-                    String userName = null ;
-                    if (order.getPlatformType().equals(DicPlatformType.OPEN)){
-                        User user = userMapper.selectByPrimaryKey(order.getUserId());
-                        if (user!=null) {
-                            userName = user.getUsername() ;
-                        }
-                    }else {
-                        Admin admin = adminMapper.selectByPrimaryKey(order.getUserId());
-                        if (admin!=null) {
-                            userName =admin.getUsername() ;
-                        }
-                    }
+//                    String userName = null ;
+//                    if (order.getPlatformType().equals(DicPlatformType.OPEN)){
+//                        User user = userMapper.selectByPrimaryKey(loginUser.getId());
+//                        if (user!=null) {
+//                            userName = user.getUsername() ;
+//                        }
+//                    }else {
+//                        Admin admin = adminMapper.selectByPrimaryKey(loginUser.getId());
+//                        if (admin!=null) {
+//                            userName =admin.getUsername() ;
+//                        }
+//                    }
                     try {
-                        PublishInfoWithBLOBs publishInfoWithBLOBs = publishInfoMapper.selectByPrimaryKey(publishSetting.getPublishInfoId());
-                        // 新增网关授权
-                        GateWayUtils.gatewayAuthAdd(publishInfoWithBLOBs.getOutId1(),DicNewGateway.APPLICATIONID,userName,userName+"添加授权") ;
+                        ResourceApiInterfaceExample example1 = new ResourceApiInterfaceExample() ;
+                        ResourceApiInterfaceExample.Criteria criteria1 = example1.createCriteria() ;
+                        criteria1.andApiIdEqualTo(publishInfoWithBLOBs.getResourceId()) ;
+                        List<ResourceApiInterfaceWithBLOBs> resourceApiInterfaceWithBLOBs = resourceApiInterfaceMapper.selectByExampleWithBLOBs(example1);
                         //新增网关计费
-                        GateWayUtils.gatewayChargeAdd(publishInfoWithBLOBs.getOutId1(),DicNewGateway.APPLICATIONID,userName,
-                                DicPublishSettingChargeMethod.FREE,publishSetting.getUseTimesLimit().toString(),userName+"添加计费") ;
+                        GateWayUtils.gatewayChargeAdd(resourceApiInterfaceWithBLOBs,loginUser.getUsername(),loginUser.getUsername()+"添加计费") ;
                     } catch (Exception e) {
                         e.printStackTrace();
                         log.error("同步网关失败!所在类是:AuthorizationServiceImpl,行数是:147");
@@ -161,90 +162,91 @@ public class AuthorizationServiceImpl implements IAuthorizationService {
         if (!(result0>0||(result1>0&&result2>0))){
             resultState.setStateCode(ResultStateCode.INVALID_DATA);
             resultState.setMsg("授权失败");
-        }else {
-
-            //=====================================================
-            //授权权限
-            UserAuthSettingExample example1 = new UserAuthSettingExample() ;
-            UserAuthSettingExample.Criteria criteria1 = example1.createCriteria() ;
-            criteria1.andUserIdEqualTo((long) 60) ;
-            List<UserAuthSetting> userAuthSettings = userAuthSettingMapper.selectByExample(example1);
-            if (userAuthSettings.size()>0){
-                UserAuthSetting userAuthSetting = userAuthSettings.get(0);
-                String authKey = userAuthSetting.getAuthKey() ;
-                Map<String,Object> params = new HashMap<>() ;
-                params.put("Authorization","Bearer "+TokenUntils.getToken()) ;
-                String str1 = null;
-                try {
-                    str1 = HttpUtil.httpGet2(DicPaasApi.EDITUSER+authKey, "Bearer "+TokenUntils.getToken(),null );
-                    Map<String,Object> map1= HttpUtil.strToMap(str1) ;
-                    if ((boolean)map1.get("success")){
-                        LinkedTreeMap linkedTreeMap = (LinkedTreeMap) map1.get("data");
-                        //角色列表
-                        Double id = (Double) linkedTreeMap.get("id");
-                        int newId = id.intValue() ;
-                        String userName = (String) linkedTreeMap.get("username");
-                        ArrayList<Map<String,Object>> arrayList = (ArrayList<Map<String, Object>>) linkedTreeMap.get("roles");
-                        if (arrayList.size()>0){
-                            Map<String, Object> map3 = arrayList.get(0);
-                            //权限列表
-                            List<Map<String,Object>> list2 = (List<Map<String, Object>>) map3.get("authorities");
-                            Map<String,Object> map4 = new HashMap<>() ;
-//                            map4.put("id",inputModel.getOutId1()) ;
-                            map4.put("id",43) ;
-                            list2.add(map4) ;
-                            Map<String,Object> map5 = new HashMap<>() ;
-                            //更新角色
-                            Map<String, String> head = TokenUntils.getHead();
-                            head.put("Content-Type","application/json") ;
-                            map3.put("authorities",list2) ;
-                            Double id2 = (Double) map3.get("id");
-                            String str3 = HttpUtil.httpPut(DicPaasApi.UPDATAROLES+id2.intValue(), head ,map3) ;
-                            Map<String,Object> map10= HttpUtil.strToMap(str3) ;
-                            if ((boolean)map10.get("success")){
-                                arrayList.set(0,map3) ;
-                                linkedTreeMap.put("roles",arrayList) ;
-                                linkedTreeMap.remove("permissions") ;
-                                linkedTreeMap.remove("authorities") ;
-                                String str2 = HttpUtil.httpPut(DicPaasApi.UPDATEUSER+newId, head ,linkedTreeMap) ;
-                            }
-                        }else {
-                            //新建一个角色
-                            Map<String,Object> map8 = new HashMap<>() ;
-                            map8.put("name",userName) ;
-                            map8.put("value",userName) ;
-                            List<Map<String,Object>> list3 = new ArrayList<>() ;
-                            Map<String,Object> map9 = new HashMap<>() ;
-                            map9.put("id",inputModel.getOutId1()) ;
-                            list3.add(map9) ;
-                            map8.put("authorities",list3) ;
-                            Map<String, String> head = TokenUntils.getHead();
-                            head.put("Content-Type","application/json") ;
-                            String str2 = HttpUtil.httpPost(DicPaasApi.ADDROLES, head ,map8) ;
-                            Map<String,Object> map7= HttpUtil.strToMap(str2) ;
-                            if ((boolean)map7.get("success")){
-                                LinkedTreeMap linkedTreeMap1 = (LinkedTreeMap) map7.get("data");
-                                Double id3 = (Double) linkedTreeMap1.get("id");
-                                int newId3 = id3.intValue() ;
-                                List<Map<String,Object>> list4 = new ArrayList<>() ;
-                                Map<String,Object> map11 = new HashMap<>() ;
-                                map11.put("id",newId3) ;
-                                list4.add(map11) ;
-                                linkedTreeMap.put("roles",list4) ;
-                                linkedTreeMap.remove("permissions") ;
-                                linkedTreeMap.remove("authorities") ;
-                                String str3 = HttpUtil.httpPut(DicPaasApi.UPDATEUSER+newId, head ,linkedTreeMap) ;
-                            }
-                        }
-
-                    }
-                } catch (Exception e) {
-                    e.printStackTrace();
-                }
+        }
+//        else {
+//
+//            //=====================================================
+//            //授权权限
+//            UserAuthSettingExample example1 = new UserAuthSettingExample() ;
+//            UserAuthSettingExample.Criteria criteria1 = example1.createCriteria() ;
+//            criteria1.andUserIdEqualTo((long) 60) ;
+//            List<UserAuthSetting> userAuthSettings = userAuthSettingMapper.selectByExample(example1);
+//            if (userAuthSettings.size()>0){
+//                UserAuthSetting userAuthSetting = userAuthSettings.get(0);
+//                String authKey = userAuthSetting.getAuthKey() ;
+//                Map<String,Object> params = new HashMap<>() ;
+//                params.put("Authorization","Bearer "+TokenUntils.getToken()) ;
+//                String str1 = null;
+//                try {
+//                    str1 = HttpUtil.httpGet2(DicPaasApi.EDITUSER+authKey, "Bearer "+TokenUntils.getToken(),null );
+//                    Map<String,Object> map1= HttpUtil.strToMap(str1) ;
+//                    if ((boolean)map1.get("success")){
+//                        LinkedTreeMap linkedTreeMap = (LinkedTreeMap) map1.get("data");
+//                        //角色列表
+//                        Double id = (Double) linkedTreeMap.get("id");
+//                        int newId = id.intValue() ;
+//                        String userName = (String) linkedTreeMap.get("username");
+//                        ArrayList<Map<String,Object>> arrayList = (ArrayList<Map<String, Object>>) linkedTreeMap.get("roles");
+//                        if (arrayList.size()>0){
+//                            Map<String, Object> map3 = arrayList.get(0);
+//                            //权限列表
+//                            List<Map<String,Object>> list2 = (List<Map<String, Object>>) map3.get("authorities");
+//                            Map<String,Object> map4 = new HashMap<>() ;
+////                            map4.put("id",inputModel.getOutId1()) ;
+//                            map4.put("id",43) ;
+//                            list2.add(map4) ;
+//                            Map<String,Object> map5 = new HashMap<>() ;
+//                            //更新角色
+//                            Map<String, String> head = TokenUntils.getHead();
+//                            head.put("Content-Type","application/json") ;
+//                            map3.put("authorities",list2) ;
+//                            Double id2 = (Double) map3.get("id");
+//                            String str3 = HttpUtil.httpPut(DicPaasApi.UPDATAROLES+id2.intValue(), head ,map3) ;
+//                            Map<String,Object> map10= HttpUtil.strToMap(str3) ;
+//                            if ((boolean)map10.get("success")){
+//                                arrayList.set(0,map3) ;
+//                                linkedTreeMap.put("roles",arrayList) ;
+//                                linkedTreeMap.remove("permissions") ;
+//                                linkedTreeMap.remove("authorities") ;
+//                                String str2 = HttpUtil.httpPut(DicPaasApi.UPDATEUSER+newId, head ,linkedTreeMap) ;
+//                            }
+//                        }else {
+//                            //新建一个角色
+//                            Map<String,Object> map8 = new HashMap<>() ;
+//                            map8.put("name",userName) ;
+//                            map8.put("value",userName) ;
+//                            List<Map<String,Object>> list3 = new ArrayList<>() ;
+//                            Map<String,Object> map9 = new HashMap<>() ;
+//                            map9.put("id",inputModel.getOutId1()) ;
+//                            list3.add(map9) ;
+//                            map8.put("authorities",list3) ;
+//                            Map<String, String> head = TokenUntils.getHead();
+//                            head.put("Content-Type","application/json") ;
+//                            String str2 = HttpUtil.httpPost(DicPaasApi.ADDROLES, head ,map8) ;
+//                            Map<String,Object> map7= HttpUtil.strToMap(str2) ;
+//                            if ((boolean)map7.get("success")){
+//                                LinkedTreeMap linkedTreeMap1 = (LinkedTreeMap) map7.get("data");
+//                                Double id3 = (Double) linkedTreeMap1.get("id");
+//                                int newId3 = id3.intValue() ;
+//                                List<Map<String,Object>> list4 = new ArrayList<>() ;
+//                                Map<String,Object> map11 = new HashMap<>() ;
+//                                map11.put("id",newId3) ;
+//                                list4.add(map11) ;
+//                                linkedTreeMap.put("roles",list4) ;
+//                                linkedTreeMap.remove("permissions") ;
+//                                linkedTreeMap.remove("authorities") ;
+//                                String str3 = HttpUtil.httpPut(DicPaasApi.UPDATEUSER+newId, head ,linkedTreeMap) ;
+//                            }
+//                        }
+//
+//                    }
+//                } catch (Exception e) {
+//                    e.printStackTrace();
+//                }
 
-            }
+//            }
             //=====================================================
-        }
+//        }
         return resultState ;
     }
 

+ 2 - 5
src/main/java/com/lightinit/hsdataplatform/service/admin/IAuthorizationService.java

@@ -1,10 +1,7 @@
 package com.lightinit.hsdataplatform.service.admin;
 
 import com.lightinit.hsdataplatform.model.ResultState;
-import com.lightinit.hsdataplatform.model.admin.AuthorizationHeableModel;
-import com.lightinit.hsdataplatform.model.admin.AuthorizationListModel;
-import com.lightinit.hsdataplatform.model.admin.AuthorizationSearchModel;
-import com.lightinit.hsdataplatform.model.admin.AuthorizationViewModel;
+import com.lightinit.hsdataplatform.model.admin.*;
 
 import java.util.List;
 
@@ -15,7 +12,7 @@ public interface IAuthorizationService {
 
     AuthorizationViewModel queryOneView(long id);
 
-    ResultState<Long> handle(AuthorizationHeableModel inputModel,Long followId);
+    ResultState<Long> handle(AuthorizationHeableModel inputModel, LoginUser loginUser);
 
     int isAdminFalg(Long id);
 }