main
周文涛 2 years ago
parent 64cf46450b
commit ccdaccd6ef

@ -6,7 +6,7 @@ package com.example.zxweb.common.constant.enums;
* @author: ZhouWenTao * @author: ZhouWenTao
* @date: 2023/9/8 12:05 * @date: 2023/9/8 12:05
*/ */
public enum IotApiPathsEnum { public enum IotApiEnum {
/** /**
* GET,query * GET,query
@ -84,7 +84,7 @@ public enum IotApiPathsEnum {
* @param path * @param path
* @param text * @param text
*/ */
IotApiPathsEnum(String path, String text) { IotApiEnum(String path, String text) {
this.path = path; this.path = path;
this.text = text; this.text = text;
} }
@ -97,7 +97,7 @@ public enum IotApiPathsEnum {
* @return String * @return String
*/ */
public static String getTextByPath(String path){ public static String getTextByPath(String path){
for (IotApiPathsEnum e : IotApiPathsEnum.values()) { for (IotApiEnum e : IotApiEnum.values()) {
if (path.startsWith(e.getPath())) { if (path.startsWith(e.getPath())) {
return e.getText(); return e.getText();
} }
@ -111,7 +111,7 @@ public enum IotApiPathsEnum {
* @return * @return
*/ */
public static String getPathByText(String text){ public static String getPathByText(String text){
for (IotApiPathsEnum e : IotApiPathsEnum.values()) { for (IotApiEnum e : IotApiEnum.values()) {
if (text.startsWith(e.getText())) { if (text.startsWith(e.getText())) {
return e.getPath(); return e.getPath();
} }

@ -1,10 +1,10 @@
package com.example.zxweb.controller; package com.example.zxweb.controller;
import com.alibaba.fastjson.JSONObject;
import com.example.zxweb.common.api.vo.Result; import com.example.zxweb.common.api.vo.Result;
import com.example.zxweb.common.constant.enums.IotApiPathsEnum; import com.example.zxweb.common.constant.enums.IotApiEnum;
import com.example.zxweb.utils.AssertUtils; import com.example.zxweb.utils.AssertUtils;
import com.example.zxweb.utils.IotUtils; import com.example.zxweb.utils.IotUtils;
import com.example.zxweb.utils.RestUtil;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -31,10 +31,10 @@ public class IotController {
@GetMapping(value = "/devices") @GetMapping(value = "/devices")
public Result<?> devices(@RequestParam(value = "serial",defaultValue = "")String serial,@RequestParam(value = "productId",defaultValue = "")String productId){ public Result<?> devices(@RequestParam(value = "serial",defaultValue = "")String serial,@RequestParam(value = "productId",defaultValue = "")String productId){
AssertUtils.notEmpty(serial,"请输入-[serial]");AssertUtils.notEmpty(productId,"请输入-[productId]"); AssertUtils.notEmpty(serial,"请输入-[serial]");AssertUtils.notEmpty(productId,"请输入-[productId]");
Map<String,Object> queryData=new LinkedHashMap<>(); JSONObject requestBody=new JSONObject();
queryData.put("serial",serial); requestBody.put("serial",serial);
queryData.put("productId",productId); requestBody.put("productId",productId);
//请求数据 //请求数据
return Result.OK(IotUtils.getApi(IotApiPathsEnum.getPathByText("获取单个设备信息"),queryData)); return Result.OK(IotUtils.getApi(IotApiEnum.getPathByText("获取单个设备信息"),requestBody));
} }
} }

@ -1,11 +1,8 @@
package com.example.zxweb.utils; package com.example.zxweb.utils;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.example.zxweb.common.constant.enums.IotApiPathsEnum;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.Map;
/** /**
* @Description ios * @Description ios
* @Author ZhouWenTao * @Author ZhouWenTao
@ -16,7 +13,10 @@ public class IotUtils {
//请求服务地址 //请求服务地址
private static final String API_PREFIX = "https://10.0.10.153:2443/api/iot/v1"; private static final String API_PREFIX = "https://10.0.10.153:2443/api/iot/v1";
public static JSONObject getApi(String apiUrl,Map<String,Object> queryData) { private static final JSONObject headers=JSONObject.parseObject("{\"x-consumer-username\":\"dwVendor\",\"appCode\":\"42142fd0jkbf4515853b7fcec64748f6\"}");
public static JSONObject getApi(String apiUrl,JSONObject queryData) {
StringBuilder sb=new StringBuilder(); StringBuilder sb=new StringBuilder();
int i=0; int i=0;
for (String field : queryData.keySet()) { for (String field : queryData.keySet()) {
@ -25,7 +25,7 @@ public class IotUtils {
sb.append(field).append("=").append(value); sb.append(field).append("=").append(value);
i++; i++;
} }
JSONObject jsonObject = RestUtil.get(API_PREFIX+apiUrl + sb); JSONObject responseBody = RestUtil.get(API_PREFIX+apiUrl,queryData,null,headers);
return jsonObject; return responseBody;
} }
} }

@ -74,28 +74,28 @@ public class RestUtil {
* get * get
*/ */
public static JSONObject get(String url) { public static JSONObject get(String url) {
return getNative(url, null, null).getBody(); return getNative(url, null, null,null).getBody();
} }
/** /**
* get * get
*/ */
public static JSONObject get(String url, JSONObject variables) { public static JSONObject get(String url, JSONObject variables) {
return getNative(url, variables, null).getBody(); return getNative(url, variables, null,null).getBody();
} }
/** /**
* get * get
*/ */
public static JSONObject get(String url, JSONObject variables, JSONObject params) { public static JSONObject get(String url, JSONObject variables, JSONObject params,JSONObject header) {
return getNative(url, variables, params).getBody(); return getNative(url, variables, params,header).getBody();
} }
/** /**
* get ResponseEntity * get ResponseEntity
*/ */
public static ResponseEntity<JSONObject> getNative(String url, JSONObject variables, JSONObject params) { public static ResponseEntity<JSONObject> getNative(String url, JSONObject variables, JSONObject params,JSONObject header) {
return request(url, HttpMethod.GET, variables, params,null); return request(url, HttpMethod.GET, variables, params,header);
} }
/** /**

Loading…
Cancel
Save