|
|
|
|
|
package com.sztzjy.linkCommerce.util;
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.http.HttpStatus;
|
|
|
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
|
import com.github.pagehelper.PageInfo;
|
|
|
|
|
|
import com.sztzjy.linkCommerce.config.security.JwtUser;
|
|
|
|
|
|
import com.sztzjy.linkCommerce.config.security.TokenProvider;
|
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class TzApi {
|
|
|
|
|
|
|
|
|
|
|
|
private final static String API_URL = "http://Cloud.sztzjy.com";
|
|
|
|
|
|
|
|
|
|
|
|
private final static String NEW_API_URL = "http://zycloud.sztzjy.com:9090";
|
|
|
|
|
|
// private final static String NEW_API_URL = "http://localhost:9090";
|
|
|
|
|
|
private final static String LOGIN_URL = NEW_API_URL + "/server/account/findByUsernameAndPwd.json";
|
|
|
|
|
|
|
|
|
|
|
|
private final static String GET_CLASS = NEW_API_URL + "/server/account/findClassesBySchoolId.json";
|
|
|
|
|
|
|
|
|
|
|
|
private final static String GET_MAJOR = NEW_API_URL + "/server/account/findMajorsBySchoolId.json";
|
|
|
|
|
|
private final static String GET_CLASS_BY_MAJOR = NEW_API_URL + "/server/account/getClassListByMajorId.json";
|
|
|
|
|
|
private final static String GET_STUDENT_BY_ClALL = NEW_API_URL + "/server/account/getStudentsByClassId.json";
|
|
|
|
|
|
private final static String GET_STUDENT_BY_KEYWORD = NEW_API_URL + "/server/account/getStudentByKeyword.json";
|
|
|
|
|
|
private final static String PAGE_STUDENT_BY_KEYWORD = NEW_API_URL + "/server/account/pagedListStudentByClassId.json";
|
|
|
|
|
|
|
|
|
|
|
|
// 智云接口调用token加入缓存,默认1小时过期
|
|
|
|
|
|
private static CacheProvider cacheProvider = new CacheProvider(3600*12, 6000);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static JwtUser foreignExchangeTradingLogin(String userName, String password,String systemName) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
String requestBody = "username=" + URLEncoder.encode(userName, "UTF8") +
|
|
|
|
|
|
"&password=" + URLEncoder.encode(password, "UTF8")+
|
|
|
|
|
|
"&systemName=" + URLEncoder.encode(systemName, "UTF8");
|
|
|
|
|
|
JSONObject object = HttpUtils.sendPost(
|
|
|
|
|
|
LOGIN_URL,
|
|
|
|
|
|
requestBody,
|
|
|
|
|
|
HttpUtils.CONTENT_TYPE_APPLICATION_X_WWW_FORM_URLENCODED,
|
|
|
|
|
|
null);
|
|
|
|
|
|
|
|
|
|
|
|
if (object == null) {
|
|
|
|
|
|
throw new IllegalArgumentException("登录失败");
|
|
|
|
|
|
}
|
|
|
|
|
|
int code = object.getInt("statusCode");
|
|
|
|
|
|
if (code != HttpStatus.HTTP_OK) {
|
|
|
|
|
|
throw new IllegalArgumentException("登录失败");
|
|
|
|
|
|
}
|
|
|
|
|
|
JSONObject jsonObject = object.getJSONObject("respString");
|
|
|
|
|
|
JSONObject dataObject = jsonObject.getJSONObject("data");
|
|
|
|
|
|
|
|
|
|
|
|
JwtUser user = new JwtUser();
|
|
|
|
|
|
user.setUserId(dataObject.getStr("userid"));
|
|
|
|
|
|
user.setName(dataObject.getStr("name"));
|
|
|
|
|
|
String roleName = dataObject.getStr("rolename");
|
|
|
|
|
|
Integer roleId = roleName.equals("学生") ? 4 : 3;
|
|
|
|
|
|
user.setRoleId(roleId);
|
|
|
|
|
|
user.setSchoolId(dataObject.getStr("schoolid"));
|
|
|
|
|
|
if(roleId==4){
|
|
|
|
|
|
user.setClassId(dataObject.getInt("classid"));
|
|
|
|
|
|
user.setClassName(dataObject.getStr("classname"));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// user.setMajorId(dataObject.getStr("majorid"));
|
|
|
|
|
|
|
|
|
|
|
|
user.setMajorName(dataObject.getStr("majorname"));
|
|
|
|
|
|
user.setSchoolName(dataObject.getStr("schoolname"));
|
|
|
|
|
|
user.setCollegeId(dataObject.getInt("collegeid"));
|
|
|
|
|
|
user.setCollegeName(dataObject.getStr("collegename"));
|
|
|
|
|
|
user.setAuthorizeTime(dataObject.getDate("authorizetime"));
|
|
|
|
|
|
user.setAuthorizeEndTime(dataObject.getDate("authorizeendtime"));
|
|
|
|
|
|
user.setUsername(userName);
|
|
|
|
|
|
user.setPassword(password);
|
|
|
|
|
|
return user;
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static List<Map<String, Object>> GetClassBySchoolIdForForeignExchangeTrading(JwtUser user) {
|
|
|
|
|
|
String token = getToken(user);
|
|
|
|
|
|
String url = GET_CLASS+"?schoolId=" + user.getSchoolId();
|
|
|
|
|
|
return getDataFromApi(url, token);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static List<Map<String, Object>> GetMajorIdForForeignExchangeTrading(JwtUser user) {
|
|
|
|
|
|
String token = getToken(user);
|
|
|
|
|
|
String url = GET_MAJOR+"?schoolId=" + user.getSchoolId();
|
|
|
|
|
|
return getDataFromApi(url, token);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static List<Map<String, Object>> GetClassByMajorIdForForeignExchangeTrading(JwtUser user, String majorId) {
|
|
|
|
|
|
String token = getToken(user);
|
|
|
|
|
|
String url = GET_CLASS_BY_MAJOR + "?majorId=" + majorId;
|
|
|
|
|
|
return getDataFromApi(url, token);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static List<Map<String, Object>> GetStudentInfoByClassIdForForeignExchangeTrading(String classIds, JwtUser user) {
|
|
|
|
|
|
String token = getToken(user);
|
|
|
|
|
|
String url = GET_STUDENT_BY_ClALL + "?classList=" + classIds;
|
|
|
|
|
|
return getStudentsFromApi(url, token);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static List<Map<String, Object>> GetStudentInfoByKeywordForForeignExchangeTrading(JwtUser user, String keyword) throws UnsupportedEncodingException {
|
|
|
|
|
|
String token = getToken(user);
|
|
|
|
|
|
String url = GET_STUDENT_BY_KEYWORD + "?keyword=" + URLEncoder.encode(keyword,"UTF-8") +"&schoolId="+user.getSchoolId();
|
|
|
|
|
|
return getStudentsFromApi(url, token);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static PageInfo<Map<String, Object>> PagedListStudentInfoByClassIdForForeignExchangeTrading(JwtUser user, String classIds, int page, int size) {
|
|
|
|
|
|
String token = getToken(user);
|
|
|
|
|
|
String url = PAGE_STUDENT_BY_KEYWORD + "?classList=" + classIds + "&page=" + page + "&size=" + size;
|
|
|
|
|
|
return getPageDataFromApi(url, token);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static List<Map<String, Object>> getDataFromApi(String url, String token) {
|
|
|
|
|
|
List<Map<String, Object>> dataList = new ArrayList<>();
|
|
|
|
|
|
try {
|
|
|
|
|
|
String responseBody = HttpUtils.sendGet(url, token);
|
|
|
|
|
|
|
|
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
|
JsonNode jsonNode = objectMapper.readTree(responseBody);
|
|
|
|
|
|
|
|
|
|
|
|
JsonNode dataNode = jsonNode.get("data");
|
|
|
|
|
|
|
|
|
|
|
|
if (dataNode.isArray()) {
|
|
|
|
|
|
for (JsonNode node : dataNode) {
|
|
|
|
|
|
Map<String, Object> dataMap = new HashMap<>();
|
|
|
|
|
|
dataMap.put("id", node.get("classid").asText());
|
|
|
|
|
|
dataMap.put("name", node.get("classname").asText());
|
|
|
|
|
|
dataList.add(dataMap);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
throw new IllegalArgumentException("获取数据异常");
|
|
|
|
|
|
}
|
|
|
|
|
|
return dataList;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static List<Map<String, Object>> getStudentsFromApi(String url, String token) {
|
|
|
|
|
|
List<Map<String, Object>> dataList = new ArrayList<>();
|
|
|
|
|
|
try {
|
|
|
|
|
|
String responseBody = HttpUtils.sendGet(url, token);
|
|
|
|
|
|
|
|
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
|
JsonNode jsonNode = objectMapper.readTree(responseBody);
|
|
|
|
|
|
|
|
|
|
|
|
JsonNode dataNode = jsonNode.get("data");
|
|
|
|
|
|
if (dataNode.isArray()) {
|
|
|
|
|
|
for (JsonNode node : dataNode) {
|
|
|
|
|
|
Map<String, Object> dataMap = new HashMap<>();
|
|
|
|
|
|
dataMap.put("userId", node.get("userid").asText());
|
|
|
|
|
|
dataMap.put("studentId", node.get("studentid").asText());
|
|
|
|
|
|
dataMap.put("name", node.get("name").asText());
|
|
|
|
|
|
dataMap.put("className", node.get("classname").asText());
|
|
|
|
|
|
dataMap.put("classId", node.get("classid").asText());
|
|
|
|
|
|
dataMap.put("studentNo", node.get("studentno").asText());
|
|
|
|
|
|
dataMap.put("schoolName", node.get("schoolname").asText());
|
|
|
|
|
|
dataList.add(dataMap);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
throw new IllegalArgumentException("获取数据异常");
|
|
|
|
|
|
}
|
|
|
|
|
|
return dataList;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static PageInfo<Map<String, Object>> getPageDataFromApi(String url, String token) {
|
|
|
|
|
|
PageInfo pageInfo = new PageInfo();
|
|
|
|
|
|
try {
|
|
|
|
|
|
String responseBody = HttpUtils.sendGet(url, token);
|
|
|
|
|
|
|
|
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
|
JsonNode jsonNode = objectMapper.readTree(responseBody);
|
|
|
|
|
|
|
|
|
|
|
|
JsonNode resultNode = jsonNode.get("content");
|
|
|
|
|
|
|
|
|
|
|
|
if (resultNode != null) {
|
|
|
|
|
|
pageInfo.setPages(resultNode.get("totalPage").asInt());
|
|
|
|
|
|
pageInfo.setTotal(resultNode.get("totalRow").asInt());
|
|
|
|
|
|
pageInfo.setPageNum(resultNode.get("pageNumber").asInt());
|
|
|
|
|
|
pageInfo.setPageSize(resultNode.get("pageSize").asInt());
|
|
|
|
|
|
JsonNode retNode = resultNode.get("list");
|
|
|
|
|
|
List<Map<String,Object>> dataList = new ArrayList<>();
|
|
|
|
|
|
if(retNode.isArray()){
|
|
|
|
|
|
for(JsonNode node: retNode){
|
|
|
|
|
|
Map<String, Object> dataMap = new HashMap<>();
|
|
|
|
|
|
dataMap.put("id", node.get("userid").asText());
|
|
|
|
|
|
dataMap.put("name", node.get("name").asText());
|
|
|
|
|
|
dataMap.put("studentNo", node.get("studentno").asText());
|
|
|
|
|
|
dataMap.put("className", node.get("classname").asText());
|
|
|
|
|
|
dataMap.put("majorName", node.get("majorname").asText());
|
|
|
|
|
|
dataMap.put("classId", node.get("classid").asText());
|
|
|
|
|
|
dataList.add(dataMap);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pageInfo.setList(dataList);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
throw new IllegalArgumentException("获取数据异常");
|
|
|
|
|
|
}
|
|
|
|
|
|
return pageInfo;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static String getToken(JwtUser user) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
String access_token;
|
|
|
|
|
|
|
|
|
|
|
|
access_token = (String) cacheProvider.get(user.getUserId() + user.getUsername());
|
|
|
|
|
|
if (StringUtils.hasText(access_token)) {
|
|
|
|
|
|
System.out.println("-------->>> 从缓存中获取 access_token【成功】");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
access_token = TokenProvider.createZHIYUNToken(user);
|
|
|
|
|
|
cacheProvider.put(user.getUserId() + user.getUsername(), access_token);
|
|
|
|
|
|
System.out.println("-------->>> 从远程获取 access_token【成功】");
|
|
|
|
|
|
}
|
|
|
|
|
|
return access_token;
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
throw new IllegalArgumentException(e.getMessage());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
JwtUser user = new JwtUser();
|
|
|
|
|
|
user.setUserId("486");
|
|
|
|
|
|
user.setRoleId(3);
|
|
|
|
|
|
user.setUsername("tzt006");
|
|
|
|
|
|
user.setSchoolId("1");
|
|
|
|
|
|
user.setSchoolName("天择大学");
|
|
|
|
|
|
user.setCollegeId(1);
|
|
|
|
|
|
user.setCollegeName("演示学院");
|
|
|
|
|
|
user.setMajorId(1);
|
|
|
|
|
|
user.setMajorName("产品演示专业");
|
|
|
|
|
|
PagedListStudentInfoByClassIdForForeignExchangeTrading(user, "7", 1, 10);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|