package com.iailab.module.data.http.service.impl;
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.iailab.module.data.common.utils.HttpsRequest;
|
import com.iailab.framework.common.page.PageData;
|
import com.iailab.framework.common.service.impl.BaseServiceImpl;
|
import com.iailab.module.data.http.dao.HttpTokenDao;
|
import com.iailab.module.data.http.entity.HttpTokenEntity;
|
import com.iailab.module.data.http.service.HttpTokenService;
|
import org.apache.commons.lang3.StringUtils;
|
import javax.annotation.Resource;
|
import org.springframework.stereotype.Service;
|
|
import java.util.Date;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
@Service
|
public class HttpTokenServiceImpl extends BaseServiceImpl<HttpTokenDao, HttpTokenEntity> implements HttpTokenService {
|
|
@Resource
|
private HttpsRequest httpsRequest;
|
|
@Override
|
public PageData<HttpTokenEntity> page(Map<String, Object> params) {
|
IPage<HttpTokenEntity> page = baseDao.selectPage(
|
getPage(params, "client_id", false),
|
getWrapper(params)
|
);
|
return getPageData(page, HttpTokenEntity.class);
|
}
|
|
private QueryWrapper<HttpTokenEntity> getWrapper(Map<String, Object> params){
|
String clientId = (String)params.get("clientId");
|
QueryWrapper<HttpTokenEntity> wrapper = new QueryWrapper<>();
|
wrapper.eq(StringUtils.isNotBlank(clientId), "client_id", clientId);
|
return wrapper;
|
}
|
|
@Override
|
public HttpTokenEntity getByApiId(String apiId) {
|
QueryWrapper<HttpTokenEntity> wrapper = new QueryWrapper<>();
|
wrapper.eq(StringUtils.isNotBlank(apiId), "api_id", apiId);
|
return baseDao.selectOne(wrapper);
|
}
|
|
|
@Override
|
public void updateToken(String clientId) {
|
Map<String, String> params = new HashMap<>(1);
|
params.put("timeout", "30000");
|
HttpTokenEntity entity = baseDao.selectOne(new QueryWrapper<HttpTokenEntity>().eq("client_id", clientId));
|
Map<String, String> dataJson = new HashMap<>(2);
|
String url = entity.getLoginUrl();
|
String userName = entity.getUsername();
|
String password = entity.getPassword();
|
String prvsetName = entity.getPrvsetName();
|
String projectName = entity.getProjectName();
|
String platform = entity.getPlatform();
|
dataJson.put("username", userName);
|
dataJson.put("password", password);
|
dataJson.put("prvset_name", prvsetName);
|
dataJson.put("project_name", projectName);
|
dataJson.put("platform", platform);
|
//查询token的请求
|
String responseStr = httpsRequest.doPostToken(url, params, JSONObject.toJSONString(dataJson), "utf-8");
|
//插入token和更新时间
|
entity.setToken(responseStr);
|
entity.setUpdateTime(new Date());
|
baseDao.update(entity, new QueryWrapper<HttpTokenEntity>().eq("client_id", clientId));
|
}
|
|
@Override
|
public String queryToken(String clientId) {
|
return baseDao.selectOne(new QueryWrapper<HttpTokenEntity>().eq("client_id", clientId)).getToken();
|
}
|
}
|