diff --git a/springboot/src/main/java/com/sincre/springboot/ApiModel/Page.java b/springboot/src/main/java/com/sincre/springboot/ApiModel/Page.java index 3c476cc..dcc0694 100644 --- a/springboot/src/main/java/com/sincre/springboot/ApiModel/Page.java +++ b/springboot/src/main/java/com/sincre/springboot/ApiModel/Page.java @@ -1,26 +1,25 @@ -package com.zjx.springboot02.apimodel; +package com.sincre.springboot.ApiModel; public class Page { public Integer getTotal() { return total; } + public Integer getPage() { + return page; + } - public void setTotal(Integer total) { - this.total = total; + public Integer getSize() { + return size; } - public Integer getPage() { - return page; + public void setTotal(Integer total) { + this.total = total; } public void setPage(Integer page) { this.page = page; } - public Integer getSize() { - return size; - } - public void setSize(Integer size) { this.size = size; } diff --git a/springboot/src/main/java/com/sincre/springboot/ApiModel/YinShiResResult.java b/springboot/src/main/java/com/sincre/springboot/ApiModel/YinShiResResult.java index 40c451e..2a9c56a 100644 --- a/springboot/src/main/java/com/sincre/springboot/ApiModel/YinShiResResult.java +++ b/springboot/src/main/java/com/sincre/springboot/ApiModel/YinShiResResult.java @@ -1,5 +1,6 @@ package com.sincre.springboot.ApiModel; + /** * 萤石接口返回的Model * @param @@ -17,11 +18,11 @@ public class YinShiResResult { this.data = data; } - public String getCode() { + public Integer getCode() { return code; } - public void setCode(String code) { + public void setCode(Integer code) { this.code = code; } @@ -40,7 +41,7 @@ public class YinShiResResult { public void setPage(Page page) { this.page = page; } - private String code; + private Integer code; private String msg; } diff --git a/springboot/src/main/java/com/sincre/springboot/ApiPlatform/YinShiServiceConfig.java b/springboot/src/main/java/com/sincre/springboot/ApiPlatform/YinShiServiceConfig.java index bce0786..911454f 100644 --- a/springboot/src/main/java/com/sincre/springboot/ApiPlatform/YinShiServiceConfig.java +++ b/springboot/src/main/java/com/sincre/springboot/ApiPlatform/YinShiServiceConfig.java @@ -11,6 +11,9 @@ public class YinShiServiceConfig { */ public static String appKey = "3780bdecb44c4b608367ba469d6d52ea"; public static String appSecret = "35c83d24b5a39b171572f6eae4ded9a7"; + /** + * 用于管理员获取accessToken + */ public static String AccessToken = ""; diff --git a/springboot/src/main/java/com/sincre/springboot/common/ResponseCode.java b/springboot/src/main/java/com/sincre/springboot/common/ResponseCode.java index 6c19f94..11a3c2f 100644 --- a/springboot/src/main/java/com/sincre/springboot/common/ResponseCode.java +++ b/springboot/src/main/java/com/sincre/springboot/common/ResponseCode.java @@ -1,4 +1,4 @@ -package com.zjx.springboot02.Common; +package com.sincre.springboot.common; /** * Created by Ziv diff --git a/springboot/src/main/java/com/sincre/springboot/common/ServerResponse.java b/springboot/src/main/java/com/sincre/springboot/common/ServerResponse.java index 14c0723..40c6873 100644 --- a/springboot/src/main/java/com/sincre/springboot/common/ServerResponse.java +++ b/springboot/src/main/java/com/sincre/springboot/common/ServerResponse.java @@ -1,4 +1,4 @@ -package com.zjx.springboot02.Common; +package com.sincre.springboot.common; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonInclude; @@ -16,7 +16,7 @@ public class ServerResponse implements Serializable { /** * 分页时用到的总数量 */ - private int total; + private String total; /** * 操作状态码 */ @@ -45,7 +45,7 @@ public class ServerResponse implements Serializable { this.data = data; } - private ServerResponse(int total,int code, String msg, T data) { + private ServerResponse(String total,int code, String msg, T data) { this.total = total; this.code = code; this.msg = msg; @@ -62,7 +62,7 @@ public class ServerResponse implements Serializable { return this.code == ResponseCode.SUCCESS.getCode(); } - public int getTotal() { + public String getTotal() { return total; } public int getCode() { @@ -100,7 +100,7 @@ public class ServerResponse implements Serializable { return new ServerResponse(ResponseCode.SUCCESS.getCode(), msg, data); } - public static ServerResponse createBySuccess(Integer total,String msg, T data) { + public static ServerResponse createBySuccess(String total,String msg, T data) { return new ServerResponse(total,ResponseCode.SUCCESS.getCode(), msg, data); } diff --git a/springboot/src/main/java/com/sincre/springboot/controller/YinShiController.java b/springboot/src/main/java/com/sincre/springboot/controller/YinShiController.java index ea8772b..8fc877c 100644 --- a/springboot/src/main/java/com/sincre/springboot/controller/YinShiController.java +++ b/springboot/src/main/java/com/sincre/springboot/controller/YinShiController.java @@ -28,27 +28,13 @@ import com.sincre.springboot.ApiPlatform.YinShiServiceConfig; @Api(value = "YinShiController", tags = "萤石对接接口") public class YinShiController { - private static String AccessToken = "at.2scte32926nu6q7j6adhlabg28emicz6-58f6w0596w-1ppubtz-uxh6dnv5x"; - @ApiOperation(value = "用于管理员获取accessToken") @GetMapping("/token") - public String GetYinShiToken() { - String appKey = YinShiServiceConfig.appKey; - String appSecret = YinShiServiceConfig.appSecret; + public ServerResponse GetYinShiToken() { - String url = YinShiServiceConfig.HostUrl + "lapp/token/get"; - Map map = new HashMap<>(); + CacheHelper.GetYinShiToken(); - map.put("appKey", appKey); - map.put("appSecret", appSecret); - String result = ApiHelper.doPost(url, new HashMap(), map); - JSONObject jsonObject = new JSONObject(result); - JSONObject data = jsonObject.optJSONObject("data"); - YinShiServiceConfig.AccessToken = data.optString("accessToken"); - Date date = new Date(); - Date dateFu = new Date(data.optLong("expireTime")); - CacheHelper.putYingshiYunToken(YinShiServiceConfig.AccessToken, (int) ((dateFu.getTime()-date.getTime())/1000)); - return ResultUtils.getInstance().resturnResultYingshi(result); + return ServerResponse.createBySuccessMessage(ResponseCode.SUCCESS.getDesc()); } @ApiOperation(value = "增加子账号") @@ -68,7 +54,7 @@ public class YinShiController { map.put("accountName", accountName); map.put("password", password); String result = ApiHelper.doPost(url, new HashMap(), map); - return ResultUtils.getInstance().resturnResultYingshi(result); + return result;//ResultUtils.getInstance().resturnResultYingshi(result); } @ApiOperation(value = "获取单个子账户信息") @@ -77,7 +63,7 @@ public class YinShiController { String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/get"; Map map = new HashMap<>(); - //子账户密码,LowerCase(MD5(AppKey#密码明文)) + map.put("accessToken", YinShiServiceConfig.AccessToken); map.put("accountId", accountId); map.put("accountName", accountName); @@ -92,16 +78,16 @@ public class YinShiController { String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/list"; Map map = new HashMap<>(); - //子账户密码,LowerCase(MD5(AppKey#密码明文)) map.put("accessToken", YinShiServiceConfig.AccessToken); map.put("pageStart", pageIndex); map.put("pageSize", pageSize); - String result = ApiHelper.doPost(url, new HashMap(), map); + String result = ApiHelper.doPost(url, new HashMap(), map); + System.out.println(result); YinShiResResult yinShiResResult = JSON.parseObject(result,YinShiResResult.class); - System.out.println(yinShiResResult.getPage().getTotal()); - return ServerResponse.createBySuccess(yinShiResResult.getPage().getTotal(), ResponseCode.SUCCESS.getDesc(),yinShiResResult.getData()); + + return ResultUtils.getInstance().resturnResultYingshi(yinShiResResult); } @ApiOperation(value = "修改当前子账户密码") @@ -110,7 +96,6 @@ public class YinShiController { String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/updatePassword"; Map map = new HashMap<>(); - //子账户密码,LowerCase(MD5(AppKey#密码明文)) oldPassword = YinShiServiceConfig.appKey + "#" + oldPassword; newPassword = YinShiServiceConfig.appKey + "#" + newPassword; @@ -202,7 +187,4 @@ public class YinShiController { return result; } - - - } diff --git a/springboot/src/main/java/com/sincre/springboot/utils/CacheHelper.java b/springboot/src/main/java/com/sincre/springboot/utils/CacheHelper.java index d7c4fe6..75b7725 100644 --- a/springboot/src/main/java/com/sincre/springboot/utils/CacheHelper.java +++ b/springboot/src/main/java/com/sincre/springboot/utils/CacheHelper.java @@ -8,6 +8,7 @@ import com.sincre.springboot.ApiPlatform.TuYaCloudService; import com.sincre.springboot.ApiPlatform.YinShiServiceConfig; import com.sincre.springboot.common.EhcacheUtil; import org.apache.commons.lang3.StringUtils; +import org.json.JSONObject; import java.util.HashMap; import java.util.Map; @@ -18,6 +19,7 @@ public class CacheHelper { static String TuYaTokenKey = "TuYa_Token"; static String TuYaRefreshTokenKey = "TuYa_Re_Token"; + private static void setTuYaToken(){ String apiUrl = "/v1.0/token?grant_type=1"; @@ -110,4 +112,23 @@ public class CacheHelper { public static void putYingshiYunToken(String token,int time){ ehcacheUtil.setex(YinShiServiceConfig.accessToken,token,time); } + + /** + * 获得萤石管理权限的Token + */ + public static void GetYinShiToken() { + String appKey = YinShiServiceConfig.appKey; + String appSecret = YinShiServiceConfig.appSecret; + + String url = YinShiServiceConfig.HostUrl + "lapp/token/get"; + Map map = new HashMap<>(); + + map.put("appKey", appKey); + map.put("appSecret", appSecret); + String result = ApiHelper.doPost(url, new HashMap(), map); + JSONObject jsonObject = new JSONObject(result); + JSONObject data = jsonObject.optJSONObject("data"); + YinShiServiceConfig.AccessToken = data.optString("accessToken"); + } + } diff --git a/springboot/src/main/java/com/sincre/springboot/utils/ResultUtils.java b/springboot/src/main/java/com/sincre/springboot/utils/ResultUtils.java index c95880c..f714a90 100644 --- a/springboot/src/main/java/com/sincre/springboot/utils/ResultUtils.java +++ b/springboot/src/main/java/com/sincre/springboot/utils/ResultUtils.java @@ -1,10 +1,11 @@ package com.sincre.springboot.utils; -import com.sincre.springboot.controller.YinShiController; +import com.sincre.springboot.ApiModel.YinShiResResult; +import com.sincre.springboot.common.ResponseCode; +import com.sincre.springboot.common.ServerResponse; import com.sincre.springboot.model.ResultModelObj; import org.json.JSONObject; -import org.springframework.http.codec.json.Jackson2JsonDecoder; /** * 结果返回工具类 @@ -30,26 +31,43 @@ public class ResultUtils { /** * 萤石统一结果回调 - * @param result data为对象的字符串 + * @param yinShiResResult 萤石接口返回对象的字符串 * @return 返回的data可以是对象或者集合,都以字符串形式返回 */ - public String resturnResultYingshi(String result) { - JSONObject jsonObject = new JSONObject(result); - ResultModelObj resultModelObj = new ResultModelObj(); - long code = jsonObject.optLong("code"); - if (code==10002){//accesstoken过期 - new YinShiController().GetYinShiToken(); + public ServerResponse resturnResultYingshi(YinShiResResult yinShiResResult) { +// JSONObject jsonObject = new JSONObject(result); +// ResultModelObj resultModelObj = new ResultModelObj(); +// long code = jsonObject.optLong("code"); +// if (code==10002){//accesstoken过期 +// new YinShiController().GetYinShiToken(); +// try { +// Thread.sleep(1000); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// } +// resultModelObj.setCode(code); +// resultModelObj.setData(jsonObject.optString("data")); +// resultModelObj.setMsg(jsonObject.optString("msg")); +// resultModelObj.setTotal(jsonObject.optInt("page")); +// return resultModelObj.toString(); + + int code = yinShiResResult.getCode(); + System.out.println(yinShiResResult.getPage().getTotal()); + if(code == 200){ + return ServerResponse.createBySuccess(yinShiResResult.getPage().getTotal().toString(), ResponseCode.SUCCESS.getDesc(),yinShiResResult.getData()); + } + else { + if (code==10002){//accessToken过期或异常 + CacheHelper.GetYinShiToken(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } - resultModelObj.setCode(code); - resultModelObj.setData(jsonObject.optString("data")); - resultModelObj.setMsg(jsonObject.optString("msg")); - resultModelObj.setTotal(jsonObject.optInt("page")); - return resultModelObj.toString(); + return ServerResponse.createByErrorMessage(yinShiResResult.getMsg()); + } } private void getYinShiToken(){ -- libgit2 0.21.0