RopManager.java 4.27 KB
package com.gsc4cloud.agentmanage.manager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gsc4cloud.agentmanage.config.RopApiConfig;
import com.gsc4cloud.agentmanage.utils.GLRopApiUtils;
import com.gsc4cloud.agentmanage.utils.ThreadLocalCache;
import com.gsc4cloud.agentmanage.response.rop.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

/**
 * @Description todo
 * @Author yelq
 * @Date 2025/7/28-19:24
 * @Version 1.0
 */
@Component
public class RopManager {

    @Autowired
    private RopApiConfig ropApiConfig;

    public void saveLoginInfo(String sessionId) throws Exception {
        if (ropApiConfig.getContextPath() == null || ropApiConfig.getContextPath().isEmpty())
            throw new Exception("缺少必要的rop配置");
        if ("rop-dj-car-boutique".equals(ropApiConfig.getContextPath())){
            ropDjCarBoutiqueHandler(sessionId);
            return;
        }
        if ("rop-dj-smartcarlife".equals(ropApiConfig.getContextPath())){
            ropDjSmartCarLifeHandler(sessionId);
            return;
        }

    }

    private void ropDjSmartCarLifeHandler(String sessionId) throws Exception {
        //请求地址,区分环境
        String url = ropApiConfig.getUrl();
        String appKey = ropApiConfig.getAppKey();
        String secret = ropApiConfig.getSecret();
        //项目名称
        String contextPath = ropApiConfig.getContextPath();
        // 默认拼装路径
        url = url.concat(contextPath).concat("/router");
        MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>(4);
        // 版本号,必填,默认1.0.0
        params.add("v", "1.0.0");
        // 接口名称,必填
        params.add("method", ropApiConfig.getMethod());
        // 参数,根据接口添加
        params.add("sessionId", sessionId);
        // post请求
        RopResponse<Object> post = GLRopApiUtils.post(url, params, RopResponse.class, appKey, secret);
        if (post.getData() != null) {
            String data = JSON.toJSONString(post.getData());
            // json字符串转换对象
            UserInfoResponse loginObj = JSONObject.parseObject(data, UserInfoResponse.class);
            if (loginObj != null && loginObj.getLoginAccount() != null ) {
                LoginAccountResponse loginUserInfo = loginObj.getLoginAccount();
                // 存储到当前线程
                ThreadLocalCache.setLoginAccountInfo(loginUserInfo);
            }

        } else {
            throw new Exception("用户信息获取失败: " + post.getMessage());
        }
    }

    private void ropDjCarBoutiqueHandler(String sessionId) throws Exception {
        //请求地址,区分环境
        String url = ropApiConfig.getUrl();
        String appKey = ropApiConfig.getAppKey();
        String secret = ropApiConfig.getSecret();
        //项目名称
        String contextPath = ropApiConfig.getContextPath();
        // 默认拼装路径
        url = url.concat(contextPath).concat("/router");
        MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>(4);
        // 版本号,必填,默认1.0.0
        params.add("v", "1.0.0");
        // 接口名称,必填
        params.add("method", ropApiConfig.getMethod());
        // 参数,根据接口添加
        params.add("sessionId", sessionId);
        // post请求
        RopResponse<Object> post = GLRopApiUtils.post(url, params, RopResponse.class, appKey, secret);
        if (post.getData() != null) {
            String data = JSON.toJSONString(post.getData());
            // json字符串转换对象
            LoginObjResponse loginObj = JSONObject.parseObject(data, LoginObjResponse.class);
            if (loginObj != null && loginObj.getLoginUserInfoList() != null && !loginObj.getLoginUserInfoList().isEmpty()) {
                LoginUserInfoResponse loginUserInfo = loginObj.getLoginUserInfoList().get(0);
                // 存储到当前线程
                ThreadLocalCache.setLoginInfo(loginUserInfo);
            }

        } else {
            throw new Exception("用户信息获取失败: " + post.getMessage());
        }
    }
}