Commit bcf27cb3 by zhaoqingwei

hotfix:#投放管理切换数据源

parent 09df89f4
package com.ruoyi.adsdesk.business.operation;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.adsdesk.param.SimulateLoginParam;
import com.ruoyi.adsdesk.vo.AdsUserVo;
import com.ruoyi.adsdesk.vo.ExpandFieldsVo;
import com.ruoyi.adsdesk.vo.UserResponseDto;
import com.ruoyi.common.constant.StrConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder;
import org.apache.logging.log4j.util.Strings;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.net.URI;
import java.util.List;
/**
* 模拟登录业务
* @author Xingbz
* 2022-08-12
*/
@Service
public class AdsSimulateLoginBusiness {
public String querySimulateLoginAuthToken(SimulateLoginParam loginParam) {
RestTemplate restTemplate = new RestTemplate();
JSONObject param = new JSONObject();
param.put("secret","20230000");
param.put("userId",loginParam.getUserId());
param.put("userName",loginParam.getUserNick());
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<?> requestEntity = new HttpEntity<>(param, headers);
String url = "http://test-api2.adsdesk.cn/adsdesk/api/open_api/system/authorize";
String responseStr = restTemplate.exchange(URI.create(url), HttpMethod.POST, requestEntity, String.class).getBody();
responseStr = clearResponseStr(responseStr);
if(Strings.isEmpty(responseStr)){
return null;
}
JSONObject jsonObject = JSON.parseObject(responseStr);
int code = (int) jsonObject.get("code");
JSONArray data = (JSONArray) jsonObject.get("data");
JSONObject first = (JSONObject) data.get(0);
String token = (String) first.get("value");
return token;
}
private String clearResponseStr(String responseStr) {
if (ObjectUtil.isNull(responseStr)) {
return null;
}
// 双引号包括的字符串
if (StrUtil.startWith(responseStr, StrConstants.DOUBLE_QUOTES) && StrUtil.endWith(responseStr, StrConstants.DOUBLE_QUOTES)) {
int placeholderCount = StrUtil.count(responseStr, "\\\"");
// 类似于"{\"en\":\"zhangsan\",\"cn\":\"张三\"}"转义之后的json字符串
if (placeholderCount > 0 && placeholderCount % 2 == 0) {
return StrUtil.replace(StrUtil.removeSuffix(StrUtil.removePrefix(responseStr, StrConstants.DOUBLE_QUOTES), StrConstants.DOUBLE_QUOTES), "\\\"", StrConstants.DOUBLE_QUOTES);
}
}
return responseStr;
}
}
package com.ruoyi.adsdesk.controller.operation;
import com.ruoyi.adsdesk.business.operation.AdsSimulateLoginBusiness;
import com.ruoyi.adsdesk.param.SimulateLoginParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* 模拟登录控制
* @author Xingbz
* 2022-08-12
*/
@RestController
@RequestMapping("/adsdesk/simulate_login")
public class AdsSimulateLoginController {
@Resource
private AdsSimulateLoginBusiness simulateLoginBusiness;
//@Log(title = "模拟登录", businessType = BusinessType.ROOT)
//@PreAuthorize("@ss.hasPermi('operation:simulateLogin:query')")
@PostMapping("/get")
public String querySimulateLoginAuthToken(@RequestBody SimulateLoginParam param) {
return simulateLoginBusiness.querySimulateLoginAuthToken(param);
}
}
package com.ruoyi.adsdesk.param;
import lombok.Data;
/**
* 模拟登录请求参数
* @author Xingbz
* 2022-08-12
*/
@Data
public class SimulateLoginParam {
/**
* 用户ID
*/
private String userId;
private String userName;
private String userNick;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment