Commit 7dc3d7c7448729ca77028e8c8db9a12bd1709072
1 parent
f0110d44
Exists in
master
接口返回数据的处理demo 获取子账号信息列表接口
Showing
8 changed files
with
83 additions
and
59 deletions
Show diff stats
springboot/src/main/java/com/sincre/springboot/ApiModel/Page.java
| 1 | -package com.zjx.springboot02.apimodel; | 1 | +package com.sincre.springboot.ApiModel; |
| 2 | 2 | ||
| 3 | public class Page { | 3 | public class Page { |
| 4 | public Integer getTotal() { | 4 | public Integer getTotal() { |
| 5 | return total; | 5 | return total; |
| 6 | } | 6 | } |
| 7 | + public Integer getPage() { | ||
| 8 | + return page; | ||
| 9 | + } | ||
| 7 | 10 | ||
| 8 | - public void setTotal(Integer total) { | ||
| 9 | - this.total = total; | 11 | + public Integer getSize() { |
| 12 | + return size; | ||
| 10 | } | 13 | } |
| 11 | 14 | ||
| 12 | - public Integer getPage() { | ||
| 13 | - return page; | 15 | + public void setTotal(Integer total) { |
| 16 | + this.total = total; | ||
| 14 | } | 17 | } |
| 15 | 18 | ||
| 16 | public void setPage(Integer page) { | 19 | public void setPage(Integer page) { |
| 17 | this.page = page; | 20 | this.page = page; |
| 18 | } | 21 | } |
| 19 | 22 | ||
| 20 | - public Integer getSize() { | ||
| 21 | - return size; | ||
| 22 | - } | ||
| 23 | - | ||
| 24 | public void setSize(Integer size) { | 23 | public void setSize(Integer size) { |
| 25 | this.size = size; | 24 | this.size = size; |
| 26 | } | 25 | } |
springboot/src/main/java/com/sincre/springboot/ApiModel/YinShiResResult.java
| 1 | package com.sincre.springboot.ApiModel; | 1 | package com.sincre.springboot.ApiModel; |
| 2 | 2 | ||
| 3 | + | ||
| 3 | /** | 4 | /** |
| 4 | * 萤石接口返回的Model | 5 | * 萤石接口返回的Model |
| 5 | * @param <T> | 6 | * @param <T> |
| @@ -17,11 +18,11 @@ public class YinShiResResult<T> { | @@ -17,11 +18,11 @@ public class YinShiResResult<T> { | ||
| 17 | this.data = data; | 18 | this.data = data; |
| 18 | } | 19 | } |
| 19 | 20 | ||
| 20 | - public String getCode() { | 21 | + public Integer getCode() { |
| 21 | return code; | 22 | return code; |
| 22 | } | 23 | } |
| 23 | 24 | ||
| 24 | - public void setCode(String code) { | 25 | + public void setCode(Integer code) { |
| 25 | this.code = code; | 26 | this.code = code; |
| 26 | } | 27 | } |
| 27 | 28 | ||
| @@ -40,7 +41,7 @@ public class YinShiResResult<T> { | @@ -40,7 +41,7 @@ public class YinShiResResult<T> { | ||
| 40 | public void setPage(Page page) { | 41 | public void setPage(Page page) { |
| 41 | this.page = page; | 42 | this.page = page; |
| 42 | } | 43 | } |
| 43 | - private String code; | 44 | + private Integer code; |
| 44 | private String msg; | 45 | private String msg; |
| 45 | } | 46 | } |
| 46 | 47 |
springboot/src/main/java/com/sincre/springboot/ApiPlatform/YinShiServiceConfig.java
| @@ -11,6 +11,9 @@ public class YinShiServiceConfig { | @@ -11,6 +11,9 @@ public class YinShiServiceConfig { | ||
| 11 | */ | 11 | */ |
| 12 | public static String appKey = "3780bdecb44c4b608367ba469d6d52ea"; | 12 | public static String appKey = "3780bdecb44c4b608367ba469d6d52ea"; |
| 13 | public static String appSecret = "35c83d24b5a39b171572f6eae4ded9a7"; | 13 | public static String appSecret = "35c83d24b5a39b171572f6eae4ded9a7"; |
| 14 | + /** | ||
| 15 | + * 用于管理员获取accessToken | ||
| 16 | + */ | ||
| 14 | public static String AccessToken = ""; | 17 | public static String AccessToken = ""; |
| 15 | 18 | ||
| 16 | 19 |
springboot/src/main/java/com/sincre/springboot/common/ResponseCode.java
springboot/src/main/java/com/sincre/springboot/common/ServerResponse.java
| 1 | -package com.zjx.springboot02.Common; | 1 | +package com.sincre.springboot.common; |
| 2 | 2 | ||
| 3 | import com.fasterxml.jackson.annotation.JsonIgnore; | 3 | import com.fasterxml.jackson.annotation.JsonIgnore; |
| 4 | import com.fasterxml.jackson.annotation.JsonInclude; | 4 | import com.fasterxml.jackson.annotation.JsonInclude; |
| @@ -16,7 +16,7 @@ public class ServerResponse<T> implements Serializable { | @@ -16,7 +16,7 @@ public class ServerResponse<T> implements Serializable { | ||
| 16 | /** | 16 | /** |
| 17 | * 分页时用到的总数量 | 17 | * 分页时用到的总数量 |
| 18 | */ | 18 | */ |
| 19 | - private int total; | 19 | + private String total; |
| 20 | /** | 20 | /** |
| 21 | * 操作状态码 | 21 | * 操作状态码 |
| 22 | */ | 22 | */ |
| @@ -45,7 +45,7 @@ public class ServerResponse<T> implements Serializable { | @@ -45,7 +45,7 @@ public class ServerResponse<T> implements Serializable { | ||
| 45 | this.data = data; | 45 | this.data = data; |
| 46 | } | 46 | } |
| 47 | 47 | ||
| 48 | - private ServerResponse(int total,int code, String msg, T data) { | 48 | + private ServerResponse(String total,int code, String msg, T data) { |
| 49 | this.total = total; | 49 | this.total = total; |
| 50 | this.code = code; | 50 | this.code = code; |
| 51 | this.msg = msg; | 51 | this.msg = msg; |
| @@ -62,7 +62,7 @@ public class ServerResponse<T> implements Serializable { | @@ -62,7 +62,7 @@ public class ServerResponse<T> implements Serializable { | ||
| 62 | return this.code == ResponseCode.SUCCESS.getCode(); | 62 | return this.code == ResponseCode.SUCCESS.getCode(); |
| 63 | } | 63 | } |
| 64 | 64 | ||
| 65 | - public int getTotal() { | 65 | + public String getTotal() { |
| 66 | return total; | 66 | return total; |
| 67 | } | 67 | } |
| 68 | public int getCode() { | 68 | public int getCode() { |
| @@ -100,7 +100,7 @@ public class ServerResponse<T> implements Serializable { | @@ -100,7 +100,7 @@ public class ServerResponse<T> implements Serializable { | ||
| 100 | return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), msg, data); | 100 | return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), msg, data); |
| 101 | } | 101 | } |
| 102 | 102 | ||
| 103 | - public static <T> ServerResponse<T> createBySuccess(Integer total,String msg, T data) { | 103 | + public static <T> ServerResponse<T> createBySuccess(String total,String msg, T data) { |
| 104 | return new ServerResponse<T>(total,ResponseCode.SUCCESS.getCode(), msg, data); | 104 | return new ServerResponse<T>(total,ResponseCode.SUCCESS.getCode(), msg, data); |
| 105 | } | 105 | } |
| 106 | 106 |
springboot/src/main/java/com/sincre/springboot/controller/YinShiController.java
| @@ -28,27 +28,13 @@ import com.sincre.springboot.ApiPlatform.YinShiServiceConfig; | @@ -28,27 +28,13 @@ import com.sincre.springboot.ApiPlatform.YinShiServiceConfig; | ||
| 28 | @Api(value = "YinShiController", tags = "萤石对接接口") | 28 | @Api(value = "YinShiController", tags = "萤石对接接口") |
| 29 | public class YinShiController { | 29 | public class YinShiController { |
| 30 | 30 | ||
| 31 | - private static String AccessToken = "at.2scte32926nu6q7j6adhlabg28emicz6-58f6w0596w-1ppubtz-uxh6dnv5x"; | ||
| 32 | - | ||
| 33 | @ApiOperation(value = "用于管理员获取accessToken") | 31 | @ApiOperation(value = "用于管理员获取accessToken") |
| 34 | @GetMapping("/token") | 32 | @GetMapping("/token") |
| 35 | - public String GetYinShiToken() { | ||
| 36 | - String appKey = YinShiServiceConfig.appKey; | ||
| 37 | - String appSecret = YinShiServiceConfig.appSecret; | 33 | + public ServerResponse GetYinShiToken() { |
| 38 | 34 | ||
| 39 | - String url = YinShiServiceConfig.HostUrl + "lapp/token/get"; | ||
| 40 | - Map<String, Object> map = new HashMap<>(); | 35 | + CacheHelper.GetYinShiToken(); |
| 41 | 36 | ||
| 42 | - map.put("appKey", appKey); | ||
| 43 | - map.put("appSecret", appSecret); | ||
| 44 | - String result = ApiHelper.doPost(url, new HashMap<String, String>(), map); | ||
| 45 | - JSONObject jsonObject = new JSONObject(result); | ||
| 46 | - JSONObject data = jsonObject.optJSONObject("data"); | ||
| 47 | - YinShiServiceConfig.AccessToken = data.optString("accessToken"); | ||
| 48 | - Date date = new Date(); | ||
| 49 | - Date dateFu = new Date(data.optLong("expireTime")); | ||
| 50 | - CacheHelper.putYingshiYunToken(YinShiServiceConfig.AccessToken, (int) ((dateFu.getTime()-date.getTime())/1000)); | ||
| 51 | - return ResultUtils.getInstance().resturnResultYingshi(result); | 37 | + return ServerResponse.createBySuccessMessage(ResponseCode.SUCCESS.getDesc()); |
| 52 | } | 38 | } |
| 53 | 39 | ||
| 54 | @ApiOperation(value = "增加子账号") | 40 | @ApiOperation(value = "增加子账号") |
| @@ -68,7 +54,7 @@ public class YinShiController { | @@ -68,7 +54,7 @@ public class YinShiController { | ||
| 68 | map.put("accountName", accountName); | 54 | map.put("accountName", accountName); |
| 69 | map.put("password", password); | 55 | map.put("password", password); |
| 70 | String result = ApiHelper.doPost(url, new HashMap<String, String>(), map); | 56 | String result = ApiHelper.doPost(url, new HashMap<String, String>(), map); |
| 71 | - return ResultUtils.getInstance().resturnResultYingshi(result); | 57 | + return result;//ResultUtils.getInstance().resturnResultYingshi(result); |
| 72 | } | 58 | } |
| 73 | 59 | ||
| 74 | @ApiOperation(value = "获取单个子账户信息") | 60 | @ApiOperation(value = "获取单个子账户信息") |
| @@ -77,7 +63,7 @@ public class YinShiController { | @@ -77,7 +63,7 @@ public class YinShiController { | ||
| 77 | 63 | ||
| 78 | String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/get"; | 64 | String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/get"; |
| 79 | Map<String, Object> map = new HashMap<>(); | 65 | Map<String, Object> map = new HashMap<>(); |
| 80 | - //子账户密码,LowerCase(MD5(AppKey#密码明文)) | 66 | + |
| 81 | map.put("accessToken", YinShiServiceConfig.AccessToken); | 67 | map.put("accessToken", YinShiServiceConfig.AccessToken); |
| 82 | map.put("accountId", accountId); | 68 | map.put("accountId", accountId); |
| 83 | map.put("accountName", accountName); | 69 | map.put("accountName", accountName); |
| @@ -92,16 +78,16 @@ public class YinShiController { | @@ -92,16 +78,16 @@ public class YinShiController { | ||
| 92 | 78 | ||
| 93 | String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/list"; | 79 | String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/list"; |
| 94 | Map<String, Object> map = new HashMap<>(); | 80 | Map<String, Object> map = new HashMap<>(); |
| 95 | - //子账户密码,LowerCase(MD5(AppKey#密码明文)) | ||
| 96 | 81 | ||
| 97 | map.put("accessToken", YinShiServiceConfig.AccessToken); | 82 | map.put("accessToken", YinShiServiceConfig.AccessToken); |
| 98 | map.put("pageStart", pageIndex); | 83 | map.put("pageStart", pageIndex); |
| 99 | map.put("pageSize", pageSize); | 84 | map.put("pageSize", pageSize); |
| 100 | - String result = ApiHelper.doPost(url, new HashMap<String, String>(), map); | 85 | + String result = ApiHelper.doPost(url, new HashMap<String,String>(), map); |
| 101 | 86 | ||
| 87 | + System.out.println(result); | ||
| 102 | YinShiResResult yinShiResResult = JSON.parseObject(result,YinShiResResult.class); | 88 | YinShiResResult yinShiResResult = JSON.parseObject(result,YinShiResResult.class); |
| 103 | - System.out.println(yinShiResResult.getPage().getTotal()); | ||
| 104 | - return ServerResponse.createBySuccess(yinShiResResult.getPage().getTotal(), ResponseCode.SUCCESS.getDesc(),yinShiResResult.getData()); | 89 | + |
| 90 | + return ResultUtils.getInstance().resturnResultYingshi(yinShiResResult); | ||
| 105 | } | 91 | } |
| 106 | 92 | ||
| 107 | @ApiOperation(value = "修改当前子账户密码") | 93 | @ApiOperation(value = "修改当前子账户密码") |
| @@ -110,7 +96,6 @@ public class YinShiController { | @@ -110,7 +96,6 @@ public class YinShiController { | ||
| 110 | 96 | ||
| 111 | String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/updatePassword"; | 97 | String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/updatePassword"; |
| 112 | Map<String, Object> map = new HashMap<>(); | 98 | Map<String, Object> map = new HashMap<>(); |
| 113 | - //子账户密码,LowerCase(MD5(AppKey#密码明文)) | ||
| 114 | 99 | ||
| 115 | oldPassword = YinShiServiceConfig.appKey + "#" + oldPassword; | 100 | oldPassword = YinShiServiceConfig.appKey + "#" + oldPassword; |
| 116 | newPassword = YinShiServiceConfig.appKey + "#" + newPassword; | 101 | newPassword = YinShiServiceConfig.appKey + "#" + newPassword; |
| @@ -202,7 +187,4 @@ public class YinShiController { | @@ -202,7 +187,4 @@ public class YinShiController { | ||
| 202 | 187 | ||
| 203 | return result; | 188 | return result; |
| 204 | } | 189 | } |
| 205 | - | ||
| 206 | - | ||
| 207 | - | ||
| 208 | } | 190 | } |
springboot/src/main/java/com/sincre/springboot/utils/CacheHelper.java
| @@ -8,6 +8,7 @@ import com.sincre.springboot.ApiPlatform.TuYaCloudService; | @@ -8,6 +8,7 @@ import com.sincre.springboot.ApiPlatform.TuYaCloudService; | ||
| 8 | import com.sincre.springboot.ApiPlatform.YinShiServiceConfig; | 8 | import com.sincre.springboot.ApiPlatform.YinShiServiceConfig; |
| 9 | import com.sincre.springboot.common.EhcacheUtil; | 9 | import com.sincre.springboot.common.EhcacheUtil; |
| 10 | import org.apache.commons.lang3.StringUtils; | 10 | import org.apache.commons.lang3.StringUtils; |
| 11 | +import org.json.JSONObject; | ||
| 11 | 12 | ||
| 12 | import java.util.HashMap; | 13 | import java.util.HashMap; |
| 13 | import java.util.Map; | 14 | import java.util.Map; |
| @@ -18,6 +19,7 @@ public class CacheHelper { | @@ -18,6 +19,7 @@ public class CacheHelper { | ||
| 18 | static String TuYaTokenKey = "TuYa_Token"; | 19 | static String TuYaTokenKey = "TuYa_Token"; |
| 19 | static String TuYaRefreshTokenKey = "TuYa_Re_Token"; | 20 | static String TuYaRefreshTokenKey = "TuYa_Re_Token"; |
| 20 | 21 | ||
| 22 | + | ||
| 21 | private static void setTuYaToken(){ | 23 | private static void setTuYaToken(){ |
| 22 | 24 | ||
| 23 | String apiUrl = "/v1.0/token?grant_type=1"; | 25 | String apiUrl = "/v1.0/token?grant_type=1"; |
| @@ -110,4 +112,23 @@ public class CacheHelper { | @@ -110,4 +112,23 @@ public class CacheHelper { | ||
| 110 | public static void putYingshiYunToken(String token,int time){ | 112 | public static void putYingshiYunToken(String token,int time){ |
| 111 | ehcacheUtil.setex(YinShiServiceConfig.accessToken,token,time); | 113 | ehcacheUtil.setex(YinShiServiceConfig.accessToken,token,time); |
| 112 | } | 114 | } |
| 115 | + | ||
| 116 | + /** | ||
| 117 | + * 获得萤石管理权限的Token | ||
| 118 | + */ | ||
| 119 | + public static void GetYinShiToken() { | ||
| 120 | + String appKey = YinShiServiceConfig.appKey; | ||
| 121 | + String appSecret = YinShiServiceConfig.appSecret; | ||
| 122 | + | ||
| 123 | + String url = YinShiServiceConfig.HostUrl + "lapp/token/get"; | ||
| 124 | + Map<String, Object> map = new HashMap<>(); | ||
| 125 | + | ||
| 126 | + map.put("appKey", appKey); | ||
| 127 | + map.put("appSecret", appSecret); | ||
| 128 | + String result = ApiHelper.doPost(url, new HashMap<String, String>(), map); | ||
| 129 | + JSONObject jsonObject = new JSONObject(result); | ||
| 130 | + JSONObject data = jsonObject.optJSONObject("data"); | ||
| 131 | + YinShiServiceConfig.AccessToken = data.optString("accessToken"); | ||
| 132 | + } | ||
| 133 | + | ||
| 113 | } | 134 | } |
springboot/src/main/java/com/sincre/springboot/utils/ResultUtils.java
| 1 | package com.sincre.springboot.utils; | 1 | package com.sincre.springboot.utils; |
| 2 | 2 | ||
| 3 | 3 | ||
| 4 | -import com.sincre.springboot.controller.YinShiController; | 4 | +import com.sincre.springboot.ApiModel.YinShiResResult; |
| 5 | +import com.sincre.springboot.common.ResponseCode; | ||
| 6 | +import com.sincre.springboot.common.ServerResponse; | ||
| 5 | import com.sincre.springboot.model.ResultModelObj; | 7 | import com.sincre.springboot.model.ResultModelObj; |
| 6 | import org.json.JSONObject; | 8 | import org.json.JSONObject; |
| 7 | -import org.springframework.http.codec.json.Jackson2JsonDecoder; | ||
| 8 | 9 | ||
| 9 | /** | 10 | /** |
| 10 | * 结果返回工具类 | 11 | * 结果返回工具类 |
| @@ -30,26 +31,43 @@ public class ResultUtils { | @@ -30,26 +31,43 @@ public class ResultUtils { | ||
| 30 | 31 | ||
| 31 | /** | 32 | /** |
| 32 | * 萤石统一结果回调 | 33 | * 萤石统一结果回调 |
| 33 | - * @param result data为对象的字符串 | 34 | + * @param yinShiResResult 萤石接口返回对象的字符串 |
| 34 | * @return 返回的data可以是对象或者集合,都以字符串形式返回 | 35 | * @return 返回的data可以是对象或者集合,都以字符串形式返回 |
| 35 | */ | 36 | */ |
| 36 | - public String resturnResultYingshi(String result) { | ||
| 37 | - JSONObject jsonObject = new JSONObject(result); | ||
| 38 | - ResultModelObj resultModelObj = new ResultModelObj(); | ||
| 39 | - long code = jsonObject.optLong("code"); | ||
| 40 | - if (code==10002){//accesstoken过期 | ||
| 41 | - new YinShiController().GetYinShiToken(); | 37 | + public ServerResponse resturnResultYingshi(YinShiResResult yinShiResResult) { |
| 38 | +// JSONObject jsonObject = new JSONObject(result); | ||
| 39 | +// ResultModelObj resultModelObj = new ResultModelObj(); | ||
| 40 | +// long code = jsonObject.optLong("code"); | ||
| 41 | +// if (code==10002){//accesstoken过期 | ||
| 42 | +// new YinShiController().GetYinShiToken(); | ||
| 43 | +// try { | ||
| 44 | +// Thread.sleep(1000); | ||
| 45 | +// } catch (InterruptedException e) { | ||
| 46 | +// e.printStackTrace(); | ||
| 47 | +// } | ||
| 48 | +// } | ||
| 49 | +// resultModelObj.setCode(code); | ||
| 50 | +// resultModelObj.setData(jsonObject.optString("data")); | ||
| 51 | +// resultModelObj.setMsg(jsonObject.optString("msg")); | ||
| 52 | +// resultModelObj.setTotal(jsonObject.optInt("page")); | ||
| 53 | +// return resultModelObj.toString(); | ||
| 54 | + | ||
| 55 | + int code = yinShiResResult.getCode(); | ||
| 56 | + System.out.println(yinShiResResult.getPage().getTotal()); | ||
| 57 | + if(code == 200){ | ||
| 58 | + return ServerResponse.createBySuccess(yinShiResResult.getPage().getTotal().toString(), ResponseCode.SUCCESS.getDesc(),yinShiResResult.getData()); | ||
| 59 | + } | ||
| 60 | + else { | ||
| 61 | + if (code==10002){//accessToken过期或异常 | ||
| 62 | + CacheHelper.GetYinShiToken(); | ||
| 42 | try { | 63 | try { |
| 43 | Thread.sleep(1000); | 64 | Thread.sleep(1000); |
| 44 | } catch (InterruptedException e) { | 65 | } catch (InterruptedException e) { |
| 45 | e.printStackTrace(); | 66 | e.printStackTrace(); |
| 46 | } | 67 | } |
| 47 | } | 68 | } |
| 48 | - resultModelObj.setCode(code); | ||
| 49 | - resultModelObj.setData(jsonObject.optString("data")); | ||
| 50 | - resultModelObj.setMsg(jsonObject.optString("msg")); | ||
| 51 | - resultModelObj.setTotal(jsonObject.optInt("page")); | ||
| 52 | - return resultModelObj.toString(); | 69 | + return ServerResponse.createByErrorMessage(yinShiResResult.getMsg()); |
| 70 | + } | ||
| 53 | } | 71 | } |
| 54 | 72 | ||
| 55 | private void getYinShiToken(){ | 73 | private void getYinShiToken(){ |