|
|
@ -1,16 +1,20 @@ |
|
|
|
package com.storeroom.service.impl; |
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.storeroom.exception.BaseException; |
|
|
|
import com.storeroom.service.ApiService; |
|
|
|
import com.storeroom.service.dto.DeviceAllDto; |
|
|
|
import com.storeroom.utils.FastjsonUtils; |
|
|
|
import com.storeroom.utils.HttpUtils; |
|
|
|
import lombok.SneakyThrows; |
|
|
|
import org.apache.http.HttpResponse; |
|
|
|
import org.apache.http.util.EntityUtils; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.util.ObjectUtils; |
|
|
|
|
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.*; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
|
|
@Service |
|
|
@ -22,14 +26,14 @@ public class ApiServiceImpl implements ApiService { |
|
|
|
@Override |
|
|
|
public String getToken() { |
|
|
|
|
|
|
|
//根据code 换取access token |
|
|
|
//获取第三方接口token |
|
|
|
HashMap<String, String> map = new HashMap<>(); |
|
|
|
map.put("Content-type", "application/json"); |
|
|
|
HttpResponse response = HttpUtils.doGet("http://jiton.8800.org:800", "/Api/Third/GetToken", "GET", map, null); |
|
|
|
if (response.getStatusLine().getStatusCode() == 200) { |
|
|
|
String s = EntityUtils.toString(response.getEntity()); |
|
|
|
Map<String, Object> dateMap = FastjsonUtils.toJavaMap(s); |
|
|
|
dateMap.forEach((k, v) -> { |
|
|
|
Map<String, Object> dataMap = FastjsonUtils.toJavaMap(s); |
|
|
|
dataMap.forEach((k, v) -> { |
|
|
|
if (k.equals("Data")) { |
|
|
|
Map<String, Object> Data = FastjsonUtils.toJavaMap(v.toString()); |
|
|
|
access_token = Data.get("access_token").toString(); |
|
|
@ -41,4 +45,43 @@ public class ApiServiceImpl implements ApiService { |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
@SneakyThrows |
|
|
|
@Override |
|
|
|
public List<DeviceAllDto> getDeviceAll() { |
|
|
|
HashMap<String, String> map = new HashMap<>(); |
|
|
|
HashMap<String, String> body = new HashMap<>(); |
|
|
|
String token = getToken(); |
|
|
|
map.put("Content-type", "application/json"); |
|
|
|
map.put("Authorization", "Bearer" + token + ""); |
|
|
|
body.put("keys", ""); |
|
|
|
String s = FastjsonUtils.toJSONString(body); |
|
|
|
HttpResponse response = HttpUtils.doPost("http://jiton.8800.org:800", "/Api/Third/GetDeviceList", "POST", map, null, s); |
|
|
|
if (response.getStatusLine().getStatusCode() == 200) { |
|
|
|
List<DeviceAllDto> deviceAllDtoList = new ArrayList<>(); |
|
|
|
String a = EntityUtils.toString(response.getEntity()); |
|
|
|
Map<String, Object> dataMap = FastjsonUtils.toJavaMap(a); |
|
|
|
dataMap.forEach((k, v) -> { |
|
|
|
if (k.equals("Data")) { |
|
|
|
if (!ObjectUtils.isEmpty(v)) { |
|
|
|
JSONArray jsonArray = (JSONArray) v; |
|
|
|
for (int i = 0; i < jsonArray.size(); i++) { |
|
|
|
DeviceAllDto deviceAllDto = new DeviceAllDto(); |
|
|
|
JSONObject jsonObject = jsonArray.getJSONObject(i); |
|
|
|
deviceAllDto.setDeviceId(jsonObject.get("device_id").toString()); |
|
|
|
deviceAllDto.setDeviceName(jsonObject.get("device_name").toString()); |
|
|
|
deviceAllDto.setCategoryName(jsonObject.get("category_name").toString()); |
|
|
|
deviceAllDto.setAreaName(jsonObject.get("area_name").toString()); |
|
|
|
deviceAllDtoList.add(deviceAllDto); |
|
|
|
} |
|
|
|
} else { |
|
|
|
throw new BaseException("没有数据"); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
return deviceAllDtoList; |
|
|
|
} else { |
|
|
|
throw new BaseException("访问失败" + response.getStatusLine().getStatusCode() + ""); |
|
|
|
} |
|
|
|
} |
|
|
|
} |