Browse Source

完成添加鉴权和计费

lidongyang 5 years ago
parent
commit
dd9b28fe0c

+ 10 - 0
src/main/java/com/lightinit/hsdataplatform/common/GateWayUtils.java

@@ -1,6 +1,7 @@
 package com.lightinit.hsdataplatform.common;
 
 import com.lightinit.hsdataplatform.dictionary.DicNewGateway;
+import com.lightinit.hsdataplatform.entity.Resource;
 import com.lightinit.hsdataplatform.entity.ResourceApiInterfaceWithBLOBs;
 
 import java.util.Date;
@@ -46,4 +47,13 @@ public class GateWayUtils {
         }
     }
 
+    public static void gatewayChargeAddByMic(Resource resource, String userId, String remark) throws Exception {
+        Map<String, String> params = new HashMap<>();
+        params.put("routeId", resource.getOutId1());
+        params.put("appId", resource.getOutId2());
+        params.put("userId", userId);
+        params.put("remark", remark);
+        HttpUtil.httpPost(SysContants.GatewayPath.GATEWAYURL_AUTHSAVE, null, params);
+        HttpUtil.httpPost(SysContants.GatewayPath.GATEWAYURL_CHARGESAVE, null, params);
+    }
 }

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

@@ -93,7 +93,7 @@ public class ResourceMicroappRegisterController extends BaseController{
 
     @RequestMapping(value = "add.action")
     @ResponseBody
-    public ResultState doAdd( HttpSession session, @Valid ResourceDataMicroappDataModel inputModel, BindingResult bindingResult,MultipartFile imgFile){
+    public ResultState doAdd( HttpSession session, @Valid ResourceDataMicroappDataModel inputModel, BindingResult bindingResult,MultipartFile imgFile) throws Exception {
         if (bindingResult.hasErrors()){
             return getAllFieldInvalidResultState(inputModel, bindingResult);
         }

+ 56 - 144
src/main/java/com/lightinit/hsdataplatform/impl/admin/AuthorizationServiceImpl.java

@@ -1,17 +1,13 @@
 package com.lightinit.hsdataplatform.impl.admin;
 
-import com.google.gson.internal.LinkedTreeMap;
 import com.lightinit.hsdataplatform.common.GateWayUtils;
-import com.lightinit.hsdataplatform.common.HttpUtil;
 import com.lightinit.hsdataplatform.common.OrderNumberUtils;
-import com.lightinit.hsdataplatform.common.TokenUntils;
 import com.lightinit.hsdataplatform.dictionary.*;
 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.*;
-import com.lightinit.hsdataplatform.service.admin.IAccountManageService;
 import com.lightinit.hsdataplatform.service.admin.IAuthorizationService;
 import com.lightinit.hsdataplatform.spring.DynamicDataContextHolder;
 import org.slf4j.Logger;
@@ -20,46 +16,46 @@ import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.servlet.ModelAndView;
 
-import javax.lang.model.type.IntersectionType;
-import javax.servlet.http.HttpSession;
-import java.util.*;
+import java.util.Date;
+import java.util.List;
 
 @Service
 public class AuthorizationServiceImpl implements IAuthorizationService {
 
     static Logger log = LoggerFactory.getLogger(AuthorizationServiceImpl.class);
     @Autowired
-    private ResourceAuthMapper resourceAuthMapper ;
+    private ResourceAuthMapper resourceAuthMapper;
 
     @Autowired
-    private PublishSettingMapper publishSettingMapper ;
+    private PublishSettingMapper publishSettingMapper;
 
     @Autowired
-    private OrderMapper orderMapper ;
+    private OrderMapper orderMapper;
 
     @Autowired
-    private OrderGoodsMapper orderGoodsMapper ;
+    private OrderGoodsMapper orderGoodsMapper;
 
     @Autowired
-    private AdminRoleMapper adminRoleMapper ;
+    private AdminRoleMapper adminRoleMapper;
 
     @Autowired
-    private UserAuthSettingMapper userAuthSettingMapper ;
+    private UserAuthSettingMapper userAuthSettingMapper;
 
     @Autowired
-    private UserMapper userMapper ;
+    private UserMapper userMapper;
 
     @Autowired
-    private AdminMapper adminMapper ;
+    private AdminMapper adminMapper;
 
     @Autowired
-    private PublishInfoMapper publishInfoMapper ;
+    private PublishInfoMapper publishInfoMapper;
 
     @Autowired
-    private ResourceApiInterfaceMapper resourceApiInterfaceMapper ;
+    private ResourceApiInterfaceMapper resourceApiInterfaceMapper;
+
+    @Autowired
+    private ResourceMapper resourceMapper;
 
     @Override
     public List<AuthorizationListModel> queryList(AuthorizationSearchModel inputModel) {
@@ -87,167 +83,83 @@ public class AuthorizationServiceImpl implements IAuthorizationService {
         Date createdAt = new Date();
 
         //修改resource_auth表
-        ResourceAuth resourceAuth = new ResourceAuth() ;
+        ResourceAuth resourceAuth = new ResourceAuth();
         resourceAuth.setAuditOpinion(inputModel.getAuditOpinion());
         resourceAuth.setAuthState(inputModel.getAuthorizationState());
-        ResourceAuthExample example = new ResourceAuthExample() ;
-        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()) ;
+        ResourceAuthExample example = new ResourceAuthExample();
+        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());
+            Resource resource = resourceMapper.selectByPrimaryKey(publishInfoWithBLOBs.getResourceId());
             //插入order表
-            Order order = new Order() ;
+            Order order = new Order();
             order.setPlatformType(inputModel.getPlatfromType());
-            order.setUserId(loginUser.getId()) ;
-            order.setOrderTime(createdAt) ;
+            order.setUserId(loginUser.getId());
+            order.setOrderTime(createdAt);
             order.setOrderNumber(OrderNumberUtils.generate());
-            if (inputModel.getPlatfromType().equals(DicOrderTypeState.OFFLINE)){
+            if (inputModel.getPlatfromType().equals(DicOrderTypeState.OFFLINE)) {
                 order.setOrderState(DicOrderState.PAID);
-            }else {
-                if (publishSetting.getChargeMethod().equals(DicPublishSettingChargeMethod.FREE)){
+            } else {
+                if (publishSetting.getChargeMethod().equals(DicPublishSettingChargeMethod.FREE)) {
                     order.setOrderState(DicOrderState.PAID);
-                }else {
+                } else {
                     order.setOrderState(DicOrderState.NOTPAID);
                 }
             }
             order.setOrderAmount(publishSetting.getPackagePrice());
             order.setRemark(publishSetting.getDescription());
             order.setCreatedAt(createdAt);
-            result1 = orderMapper.insert(order) ;
+            result1 = orderMapper.insert(order);
             //插入order_goods表
-            OrderGoods orderGoods = new OrderGoods() ;
-            BeanUtils.copyProperties(publishSetting,orderGoods);
+            OrderGoods orderGoods = new OrderGoods();
+            BeanUtils.copyProperties(publishSetting, orderGoods);
             orderGoods.setOrderId(order.getId());
-            orderGoods.setResourceType(inputModel.getResourceType()) ;
+            orderGoods.setResourceType(inputModel.getResourceType());
             orderGoods.setResourceName(inputModel.getResourceName());
             orderGoods.setPublishSettingId(inputModel.getPublishSettingId());
             orderGoods.setCreatedAt(createdAt);
             orderGoods.setResourceAuthId(inputModel.getId());
-            result2 = orderGoodsMapper.insert(orderGoods) ;
-            if (result1>0&&result2>0){
+            result2 = orderGoodsMapper.insert(orderGoods);
+            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(loginUser.getId());
-//                        if (user!=null) {
-//                            userName = user.getUsername() ;
-//                        }
-//                    }else {
-//                        Admin admin = adminMapper.selectByPrimaryKey(loginUser.getId());
-//                        if (admin!=null) {
-//                            userName =admin.getUsername() ;
-//                        }
-//                    }
+                if (order.getOrderState().equals(DicOrderState.PAID) && orderGoods.getResourceType().equals(DicResourceType.API)) {
                     try {
-                        ResourceApiInterfaceExample example1 = new ResourceApiInterfaceExample() ;
-                        ResourceApiInterfaceExample.Criteria criteria1 = example1.createCriteria() ;
-                        criteria1.andApiIdEqualTo(publishInfoWithBLOBs.getResourceId()) ;
+                        ResourceApiInterfaceExample example1 = new ResourceApiInterfaceExample();
+                        ResourceApiInterfaceExample.Criteria criteria1 = example1.createCriteria();
+                        criteria1.andApiIdEqualTo(publishInfoWithBLOBs.getResourceId());
                         List<ResourceApiInterfaceWithBLOBs> resourceApiInterfaceWithBLOBs = resourceApiInterfaceMapper.selectByExampleWithBLOBs(example1);
                         //新增网关计费
-                        GateWayUtils.gatewayChargeAdd(resourceApiInterfaceWithBLOBs,loginUser.getUsername(),loginUser.getUsername()+"添加计费") ;
+                        GateWayUtils.gatewayChargeAdd(resourceApiInterfaceWithBLOBs, loginUser.getUsername(), loginUser.getUsername() + "添加计费");
+                    } catch (Exception e) {
+                        e.printStackTrace();
+                        log.error("同步网关失败!所在类是:AuthorizationServiceImpl,行数是:147");
+                    }
+                }
+                if (order.getOrderState().equals(DicOrderState.PAID) && orderGoods.getResourceType().equals(DicResourceType.MICROAPP)) {
+                    try {
+                        //新增网关计费
+                        GateWayUtils.gatewayChargeAddByMic(resource, loginUser.getUsername(), loginUser.getUsername() + "添加计费");
                     } catch (Exception e) {
                         e.printStackTrace();
                         log.error("同步网关失败!所在类是:AuthorizationServiceImpl,行数是:147");
                     }
                 }
+
             }
         }
 
 
-        if (!(result0>0||(result1>0&&result2>0))){
+        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();
-//                }
-
-//            }
-            //=====================================================
-//        }
-        return resultState ;
+        return resultState;
     }
 
     @Override

+ 1 - 1
src/main/java/com/lightinit/hsdataplatform/impl/admin/ResourceAPIRegisterServiceImpl.java

@@ -113,7 +113,7 @@ public class ResourceAPIRegisterServiceImpl implements IResourceAPIRegisterServi
                     //2。插入应用
                     Map<String, String> map1 = new HashMap<>();
                     map1.put("appName",addResourceModel.getResourceName()) ;
-                    String str1 = HttpUtil.httpPost(SysContants.GatewayPath.GATEWAYURL_APPSAVE, null, map);
+                    String str1 = HttpUtil.httpPost(SysContants.GatewayPath.GATEWAYURL_APPSAVE, null, map1);
                     //插入resouse_interface表
                     ResourceApiInterfaceWithBLOBs entity3 = new ResourceApiInterfaceWithBLOBs();
                     BeanUtils.copyProperties(addAPIInterfaceModel, entity3);

+ 32 - 44
src/main/java/com/lightinit/hsdataplatform/impl/admin/ResourceMicroappRegisterServiceImpl.java

@@ -1,6 +1,7 @@
 package com.lightinit.hsdataplatform.impl.admin;
 
 import com.lightinit.hsdataplatform.common.HttpUtil;
+import com.lightinit.hsdataplatform.common.SysContants;
 import com.lightinit.hsdataplatform.dictionary.DicPaasApi;
 import com.lightinit.hsdataplatform.dictionary.DicResourceMicroappState;
 import com.lightinit.hsdataplatform.dictionary.DicResourceState;
@@ -50,16 +51,46 @@ public class ResourceMicroappRegisterServiceImpl implements IResourceMicroappReg
     }
 
     @Transactional
-    public ResultState<Long> add(ResourceDataMicroappDataModel inputModel,String userName) {
+    public ResultState<Long> add(ResourceDataMicroappDataModel inputModel,String userName) throws Exception {
         DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
         ResultState<Long> resultState = new ResultState<Long>();
         Date createdAt = new Date();
+        //1。 插入网关
+        Map<String, String> map = new HashMap<>();
+        // 路由名称
+        map.put("routeName", inputModel.getResourceName());
+        String url =  inputModel.getUrl() ;
+        if (StringUtils.isNotEmpty(url)){
+            String newUrl = null ;
+            // 映射路径
+            url.replace("https://", "");
+            if (url.contains("http://")){
+                newUrl =  url.replace("http://", "") ;
+            }
+            if (url.contains("https://")){
+                newUrl =   url.replace("https://", "") ;
+            }
+            int i = newUrl.indexOf("/");
+            String substring = newUrl.substring(i, newUrl.length());
+            // 上下文路径
+            map.put("contextPath", SysContants.GatewayPath.HSDATAGATEWAYAPP+substring);
+            // 映射URL
+            map.put("mappingUrl", url);
+        }
+        String str = HttpUtil.httpPost(SysContants.GatewayPath.GATEWAYURL_ROUTESAVE, null, map);
+        //2。插入应用
+        Map<String, String> map1 = new HashMap<>();
+        map1.put("appName",inputModel.getResourceName()) ;
+        String str1 = HttpUtil.httpPost(SysContants.GatewayPath.GATEWAYURL_APPSAVE, null, map1);
+
 
         Resource entity1 = new Resource();
         BeanUtils.copyProperties(inputModel, entity1);
         entity1.setResourceType(DicResourceType.MICROAPP);
         entity1.setResourceState(StringUtils.isNotEmpty(inputModel.getResourceState()) && inputModel.getResourceState().equals(DicResourceMicroappState.PENDING) ? DicResourceMicroappState.PENDING : DicResourceMicroappState.DRAFT);
         entity1.setCreatedAt(createdAt);
+        entity1.setOutId1(str);
+        entity1.setOutId2(str1);
         int sqlResult1 = resourceMapper.insert(entity1);
 
         ResourceMicroappWithBLOBs entity2 = new ResourceMicroappWithBLOBs();
@@ -71,49 +102,6 @@ public class ResourceMicroappRegisterServiceImpl implements IResourceMicroappReg
         if (!(sqlResult1 > 0 && sqlResult2 > 0)){
             resultState.setStateCode(ResultStateCode.INVALID_DATA);
             resultState.setMsg("数据插入失败");
-        }else {
-            if(inputModel.getResourceState().equals(DicResourceState.PENDING)) {
-                try {
-//                    HashMap<String, String> headers = new HashMap<>();
-//                    headers.put("Content-Type", "application/json");
-//                    HashMap<String, String> json = new HashMap<>();
-//                    json.put("summary", inputModel.getResourceSummary());
-//                    json.put("imageName", inputModel.getImageTags());
-//                    json.put("submitUser",userName) ;
-//                    json.put("name",inputModel.getResourceName()) ;
-//                    json.put("lable",inputModel.getServiceTag()) ;
-//                    json.put("kindId",inputModel.getAppkinds().substring(0,inputModel.getAppkinds().indexOf("."))) ;
-//                    json.put("developerId",inputModel.getDeveloperId().substring(0,inputModel.getDeveloperId().indexOf("."))) ;
-//                    json.put("developers",inputModel.getDevelopers()) ;
-//                    json.put("img","") ;
-//                    json.put("lesseeId","") ;
-//                    json.put("classIfcation",inputModel.getClassIfcation()) ;
-//                    json.put("versionList","1") ;
-//                    json.put("content",inputModel.getContent()) ;
-//                    String message = HttpUtil.httpPost(DicPaasApi.NMSPAPP, headers, json); //同步
-////                    String message = HttpUtil.httpPost("http://121.17.30.198:8097/microservice-gateway/msgs-server/v1/nmspApp", headers, json); //同步
-//                    System.out.println("执行结果"+message);
-//                    Map<String,Object> map1= HttpUtil.strToMap(message);
-//                    if((boolean)map1.get("success")) {
-//                        Map<String,Object> map2= (Map<String, Object>) map1.get("data");
-//                        Double outId = (Double) map2.get("id");
-//                        resourceMapper.insertOutId(outId.intValue(),entity1.getId()) ;//保存应用版本
-//                        Map<String,String> map3 = new HashMap<>() ;
-//                        map3.put("appId",String.valueOf(outId.intValue())) ;
-//                        map3.put("image","") ;
-//                        map3.put("version","0.0.1") ;
-//                        map3.put("submitUser",userName) ;
-//                        map3.put("updateContent","更新内容") ;
-//                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-//                        json.put("releaseDate",sdf.format(createdAt)) ;
-//                        String str1 = HttpUtil.httpPost(DicPaasApi.NMSPAPPVERSION,null,map3) ;
-//                        System.out.println("-------------------"+str1+"------------------------");
-//                    }
-                }catch (Exception e){
-                    resultState.setMsg("数据插入失败");
-                }
-            }
-
         }
         resultState.setData(entity1.getId());
         return resultState;

+ 1 - 1
src/main/java/com/lightinit/hsdataplatform/service/admin/IResourceMicroappRegisterService.java

@@ -11,7 +11,7 @@ import java.util.Map;
 public interface IResourceMicroappRegisterService {
     List<ResourceMicroappRegisterListModel> queryList(ResourceMicroappRegisterSearchModel inputModel);
     long queryCount(ResourceMicroappRegisterSearchModel inputModel);
-    ResultState<Long> add(ResourceDataMicroappDataModel inputModel,String userName);
+    ResultState<Long> add(ResourceDataMicroappDataModel inputModel,String userName) throws Exception;
 
     ResultState<Long> delete(long id);