com.yanyun.auth.service.AuthSystemService Maven / Gradle / Ivy
package com.yanyun.auth.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yanyun.auth.dto.ResultDto;
import com.yanyun.auth.model.SystemAggregation;
import com.yanyun.auth.model.SystemModel;
import com.yanyun.auth.model.SystemUserModel;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
/**
* 定义资源的鉴权调用服务
*/
public class AuthSystemService {
private RestTemplate restTemplate;
private String authUrl;
public AuthSystemService(RestTemplate restTemplate, String authUrl) {
this.restTemplate = restTemplate;
this.authUrl = authUrl;
}
/**
* 注册系统API
* @param systemModel 系统模型
* @return
*/
public ResultDto registerSystem(SystemModel systemModel){
//封装请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity httpEntity = new HttpEntity(systemModel,headers);
//发送post请求
ResultDto resultDto = restTemplate.postForObject(authUrl + "/system", httpEntity, ResultDto.class);
if (resultDto!=null && resultDto.getCode()==200){
//如果正确返回,那么组装对象返回
SystemAggregation systemAggregation = new SystemAggregation();
String jsonStr = JSON.toJSONString(resultDto.getData());
JSONObject aggregationObject = JSON.parseObject(jsonStr);
String realPassword = aggregationObject.getString("realPassword");
SystemModel system = aggregationObject.getObject("systemClient",SystemModel.class);
SystemUserModel userModel = aggregationObject.getObject("systemsUser",SystemUserModel.class);
userModel.setPassword(realPassword);
systemAggregation.setSystemModel(system);
systemAggregation.setSystemUserModel(userModel);
return ResultDto.success(systemAggregation);
}
return resultDto;
}
/**
* 跟新系统API
* @param systemModel 待更新对象模型
* @return
*/
public ResultDto updateSystem(SystemModel systemModel){
//封装请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity httpEntity = new HttpEntity(systemModel,headers);
//发送post请求
ResponseEntity exchange = restTemplate.exchange(authUrl + "/system", HttpMethod.PUT, httpEntity, ResultDto.class);
ResultDto resultDto = exchange.getBody();
if(resultDto!=null && resultDto.getCode()==200){
String systemJSONStr = JSON.toJSONString(resultDto.getData());
SystemModel system = JSON.parseObject(systemJSONStr, SystemModel.class);
return ResultDto.success(system);
}
return resultDto;
}
/**
* 删除系统API
* @param clientId
* @return
*/
public ResultDto deleteSystem(String clientId){
ResponseEntity exchange = restTemplate.exchange(authUrl + "/system/" + clientId, HttpMethod.DELETE, null, ResultDto.class);
return exchange.getBody();
}
/**
* 按照clientId查询系统信息
* @param clientId
* @return
*/
public ResultDto querySystemModelByClientId(String clientId){
ResultDto resultDto = restTemplate.getForObject(authUrl + "/system/" + clientId, ResultDto.class);
if (resultDto!=null && resultDto.getCode()==200){
//做数据转换
String systemJSONStr = JSON.toJSONString(resultDto.getData());
SystemModel systemModel = JSON.parseObject(systemJSONStr, SystemModel.class);
return resultDto.success(systemModel);
}
return resultDto;
}
}