diff --git a/springboot/pom.xml b/springboot/pom.xml index 124d510..ec311b3 100644 --- a/springboot/pom.xml +++ b/springboot/pom.xml @@ -77,11 +77,17 @@ 2.0.0 + + org.json + json + 20180813 + + org.apache.commons commons-lang3 - 3.9 + runtime 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 1dbca06..bce0786 100644 --- a/springboot/src/main/java/com/sincre/springboot/ApiPlatform/YinShiServiceConfig.java +++ b/springboot/src/main/java/com/sincre/springboot/ApiPlatform/YinShiServiceConfig.java @@ -4,6 +4,15 @@ public class YinShiServiceConfig { public final static String HostUrl = "https://open.ys7.com/api/"; + public final static String accessToken = "accessToken"; + + /** + * 萤石云appKey和appSecret + */ public static String appKey = "3780bdecb44c4b608367ba469d6d52ea"; - public static String appSecret = "b6399d42a10215242c3d72944abdc8a2"; + public static String appSecret = "35c83d24b5a39b171572f6eae4ded9a7"; + public static String AccessToken = ""; + + + } diff --git a/springboot/src/main/java/com/sincre/springboot/SpringbootApplication.java b/springboot/src/main/java/com/sincre/springboot/SpringbootApplication.java index 4f1e536..9fd95cc 100644 --- a/springboot/src/main/java/com/sincre/springboot/SpringbootApplication.java +++ b/springboot/src/main/java/com/sincre/springboot/SpringbootApplication.java @@ -1,5 +1,6 @@ package com.sincre.springboot; +import com.sincre.springboot.controller.YinShiController; import org.apache.ibatis.annotations.Mapper; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; @@ -9,6 +10,8 @@ import org.springframework.boot.web.servlet.support.SpringBootServletInitializer import javax.swing.*; import java.awt.*; +import java.text.SimpleDateFormat; +import java.util.Date; @MapperScan("com.sincre.springboot.mapper") @SpringBootApplication @@ -17,6 +20,7 @@ public class SpringbootApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(SpringbootApplication.class, args); + new YinShiController().GetYinShiToken();//获取萤石云token } } diff --git a/springboot/src/main/java/com/sincre/springboot/controller/TuYaYunController.java b/springboot/src/main/java/com/sincre/springboot/controller/TuYaYunController.java index 8e38b35..f434486 100644 --- a/springboot/src/main/java/com/sincre/springboot/controller/TuYaYunController.java +++ b/springboot/src/main/java/com/sincre/springboot/controller/TuYaYunController.java @@ -9,6 +9,7 @@ import com.sincre.springboot.ApiModel.UserInfo; import com.sincre.springboot.ApiPlatform.TuYaCloudService; import com.sincre.springboot.utils.ApiHelper; import com.sincre.springboot.utils.CacheHelper; +import com.sincre.springboot.utils.ResultUtils; import io.swagger.annotations.Api; import org.springframework.web.bind.annotation.*; @@ -43,7 +44,6 @@ public class TuYaYunController { } catch(Exception ex){ System.out.println(ex.getMessage()); } - System.out.println(result); return resResult; } 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 e3fbe0b..b181494 100644 --- a/springboot/src/main/java/com/sincre/springboot/controller/YinShiController.java +++ b/springboot/src/main/java/com/sincre/springboot/controller/YinShiController.java @@ -1,15 +1,20 @@ package com.sincre.springboot.controller; +import com.alibaba.fastjson.JSON; import com.sincre.springboot.common.MD5; import com.sincre.springboot.utils.ApiHelper; +import com.sincre.springboot.utils.CacheHelper; +import com.sincre.springboot.utils.ResultUtils; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import org.json.JSONObject; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -17,175 +22,181 @@ import com.sincre.springboot.ApiPlatform.YinShiServiceConfig; @RestController @RequestMapping("/YinShi") -@Api(value = "YinShiController",tags = "萤石对接接口") +@Api(value = "YinShiController", tags = "萤石对接接口") public class YinShiController { - private static String AccessToken = "at.2scte32926nu6q7j6adhlabg28emicz6-58f6w0596w-1ppubtz-uxh6dnv5x"; + @ApiOperation(value = "用于管理员获取accessToken") @GetMapping("/token") - public String GetYinShiToken(){ + public String 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); - - return result; + 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"); + 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); } @ApiOperation(value = "增加子账号") @GetMapping("/addChildAccount") - public String addChildAccount(@RequestParam String accountName, @RequestParam String password){ + public String addChildAccount(@RequestParam String accountName, @RequestParam String password) { - String url =YinShiServiceConfig.HostUrl + "lapp/ram/account/create"; - Map map = new HashMap<>(); + String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/create"; + Map map = new HashMap<>(); //子账户密码,LowerCase(MD5(AppKey#密码明文)) - password = YinShiServiceConfig.appKey+"#"+password; + password = YinShiServiceConfig.appKey + "#" + password; try { password = MD5.md5(password, "").toLowerCase(); - }catch (Exception ex){ + } catch (Exception ex) { ex.getStackTrace(); } - map.put("accessToken",AccessToken); - map.put("accountName",accountName); - map.put("password",password); - String result = ApiHelper.doPost(url,new HashMap(),map); - - return result; + map.put("accessToken", YinShiServiceConfig.AccessToken); + map.put("accountName", accountName); + map.put("password", password); + String result = ApiHelper.doPost(url, new HashMap(), map); + return ResultUtils.getInstance().resturnResultYingshi(result); } @ApiOperation(value = "获取单个子账户信息") @GetMapping("getChildAccount") - public String getChildAccount(@RequestParam String accountId,@RequestParam String accountName){ + public String getChildAccount(@RequestParam String accountId, @RequestParam String accountName) { - String url =YinShiServiceConfig.HostUrl + "lapp/ram/account/get"; - Map map = new HashMap<>(); + 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); + String result = ApiHelper.doPost(url, new HashMap(), map); - map.put("accessToken",AccessToken); - map.put("accountId",accountId); - map.put("accountName",accountName); - String result = ApiHelper.doPost(url,new HashMap(),map); - - return result; + return result; } @ApiOperation(value = "获取子账号信息列表") @GetMapping("getChildAccountList") - public String getChildAccountList(@RequestParam("pageIndex") Integer pageIndex,@RequestParam("pageSize") Integer pageSize){ + public String getChildAccountList(@RequestParam("pageIndex") Integer pageIndex, @RequestParam("pageSize") Integer pageSize) { - String url =YinShiServiceConfig.HostUrl + "lapp/ram/account/list"; - Map map = new HashMap<>(); + String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/list"; + Map map = new HashMap<>(); //子账户密码,LowerCase(MD5(AppKey#密码明文)) - map.put("accessToken",AccessToken); - map.put("pageStart",pageIndex); - map.put("pageSize",pageSize); - String result = ApiHelper.doPost(url,new HashMap(),map); + map.put("accessToken", YinShiServiceConfig.AccessToken); + map.put("pageStart", pageIndex); + map.put("pageSize", pageSize); + String result = ApiHelper.doPost(url, new HashMap(), map); - return result; + return result; } @ApiOperation(value = "修改当前子账户密码") @GetMapping("updateChildPassword") - public String updateChildPassword(@RequestParam("accountId") String accountId,@RequestParam("newPassword") String newPassword,@RequestParam("oldPassword") String oldPassword){ + public String updateChildPassword(@RequestParam("accountId") String accountId, @RequestParam("newPassword") String newPassword, @RequestParam("oldPassword") String oldPassword) { - String url =YinShiServiceConfig.HostUrl + "lapp/ram/account/updatePassword"; - Map map = new HashMap<>(); + String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/updatePassword"; + Map map = new HashMap<>(); //子账户密码,LowerCase(MD5(AppKey#密码明文)) - oldPassword = YinShiServiceConfig.appKey+"#"+oldPassword; - newPassword = YinShiServiceConfig.appKey+"#"+newPassword; + oldPassword = YinShiServiceConfig.appKey + "#" + oldPassword; + newPassword = YinShiServiceConfig.appKey + "#" + newPassword; try { oldPassword = MD5.md5(oldPassword, "").toLowerCase(); newPassword = MD5.md5(newPassword, "").toLowerCase(); - }catch (Exception ex){ + } catch (Exception ex) { ex.getStackTrace(); } - map.put("accessToken",AccessToken); - map.put("accountId",accountId); - map.put("oldPassword",oldPassword); - map.put("newPassword",newPassword); - String result = ApiHelper.doPost(url,new HashMap(),map); + map.put("accessToken", YinShiServiceConfig.AccessToken); + map.put("accountId", accountId); + map.put("oldPassword", oldPassword); + map.put("newPassword", newPassword); + String result = ApiHelper.doPost(url, new HashMap(), map); - return result; + return result; } @ApiOperation(value = "删除指定子账户") @GetMapping("deleteChildAccount") - public String deleteChildAccount(@RequestParam String accountId){ + public String deleteChildAccount(@RequestParam String accountId) { - String url =YinShiServiceConfig.HostUrl + "lapp/ram/account/delete"; - Map map = new HashMap<>(); + String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/delete"; + Map map = new HashMap<>(); - map.put("accessToken",AccessToken); - map.put("accountId",accountId); - String result = ApiHelper.doPost(url,new HashMap(),map); + map.put("accessToken", YinShiServiceConfig.AccessToken); + map.put("accountId", accountId); + String result = ApiHelper.doPost(url, new HashMap(), map); - return result; + return result; } @ApiOperation(value = "设置子账户的授权策略") @GetMapping("policySet") - public String policySet(@RequestParam String accountId,@RequestParam String policy){ + public String policySet(@RequestParam String accountId, @RequestParam String policy) { - String url =YinShiServiceConfig.HostUrl + "lapp/ram/policy/set"; - Map map = new HashMap<>(); + String url = YinShiServiceConfig.HostUrl + "lapp/ram/policy/set"; + Map map = new HashMap<>(); - map.put("accessToken",AccessToken); - map.put("accountId",accountId); - map.put("policy",policy); - String result = ApiHelper.doPost(url,new HashMap(),map); + map.put("accessToken", YinShiServiceConfig.AccessToken); + map.put("accountId", accountId); + map.put("policy", policy); + String result = ApiHelper.doPost(url, new HashMap(), map); - return result; + return result; } @ApiOperation(value = "增加子账户授权策略中的授权语句") @GetMapping("policyAdd") - public String policyAdd(@RequestParam String accountId,@RequestParam String statement){ + public String policyAdd(@RequestParam String accountId, @RequestParam String statement) { - String url =YinShiServiceConfig.HostUrl + "lapp/ram/statement/add"; - Map map = new HashMap<>(); + String url = YinShiServiceConfig.HostUrl + "lapp/ram/statement/add"; + Map map = new HashMap<>(); - map.put("accessToken",AccessToken); - map.put("accountId",accountId); - map.put("statement",statement); - String result = ApiHelper.doPost(url,new HashMap(),map); + map.put("accessToken", YinShiServiceConfig.AccessToken); + map.put("accountId", accountId); + map.put("statement", statement); + String result = ApiHelper.doPost(url, new HashMap(), map); - return result; + return result; } @ApiOperation(value = "删除子账户授权策略中指定设备的所有授权语句") @GetMapping("policyDelete") - public String policyDelete(@RequestParam String accountId,@RequestParam String deviceSerial){ + public String policyDelete(@RequestParam String accountId, @RequestParam String deviceSerial) { - String url =YinShiServiceConfig.HostUrl + "lapp/ram/statement/delete"; - Map map = new HashMap<>(); + String url = YinShiServiceConfig.HostUrl + "lapp/ram/statement/delete"; + Map map = new HashMap<>(); - map.put("accessToken",AccessToken); - map.put("accountId",accountId); - map.put("deviceSerial",deviceSerial); - String result = ApiHelper.doPost(url,new HashMap(),map); + map.put("accessToken", YinShiServiceConfig.AccessToken); + map.put("accountId", accountId); + map.put("deviceSerial", deviceSerial); + String result = ApiHelper.doPost(url, new HashMap(), map); - return result; + return result; } @ApiOperation(value = "获取B模式子账户accessToken") @GetMapping("getChildAccountToken") - public String getChildAccountToken(@RequestParam String accountId){ + public String getChildAccountToken(@RequestParam String accountId) { - String url =YinShiServiceConfig.HostUrl + "lapp/ram/token/get"; - Map map = new HashMap<>(); + String url = YinShiServiceConfig.HostUrl + "lapp/ram/token/get"; + Map map1 = new HashMap<>(); - map.put("accessToken",AccessToken); - map.put("accountId",accountId); - String result = ApiHelper.doPost(url,new HashMap(),map); + map1.put("accessToken", YinShiServiceConfig.AccessToken); + map1.put("accountId", accountId); + String result = ApiHelper.doPost(url, new HashMap(), map1); - return result; + return result; } + + + } diff --git a/springboot/src/main/java/com/sincre/springboot/model/ResultModelObj.java b/springboot/src/main/java/com/sincre/springboot/model/ResultModelObj.java new file mode 100644 index 0000000..f43eb70 --- /dev/null +++ b/springboot/src/main/java/com/sincre/springboot/model/ResultModelObj.java @@ -0,0 +1,64 @@ +package com.sincre.springboot.model; + +/** + * 返回结果对象 + */ +public class ResultModelObj { + + private int total; + + /** + * 错误信息 + */ + private String msg; + + /** + * 错误码 + */ + private long code; + + /** + * 返回结果 + */ + private String data; + + public int getTotal() { + return total; + } + + public void setTotal(int total) { + this.total = total; + } + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + public long getCode() { + return code; + } + + public void setCode(long code) { + this.code = code; + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } + + @Override + public String toString() { + return "{\"msg\":" + msg + + ", \"code\":" + code + + ", \"data\":" + data + + ",\"total\":"+total+"}"; + } +} 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 5e93b06..3b23753 100644 --- a/springboot/src/main/java/com/sincre/springboot/utils/CacheHelper.java +++ b/springboot/src/main/java/com/sincre/springboot/utils/CacheHelper.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.sincre.springboot.ApiModel.TuYaResResult; import com.sincre.springboot.ApiModel.TuYaResTokenObj; import com.sincre.springboot.ApiPlatform.TuYaCloudService; +import com.sincre.springboot.ApiPlatform.YinShiServiceConfig; import com.sincre.springboot.common.EhcacheUtil; import org.apache.commons.lang3.StringUtils; @@ -91,4 +92,23 @@ public class CacheHelper { System.out.println(ex.getMessage()); } } + + + /** + * 获取萤石云token + * @return + */ + public static String getYingshiYunToken(){ + return ehcacheUtil.get(YinShiServiceConfig.accessToken); + } + + /** + * 设置萤石云token + * @param token + * @param time + */ + public static void putYingshiYunToken(String token,int time){ + ehcacheUtil.setex(YinShiServiceConfig.accessToken,token,time); + } + } diff --git a/springboot/src/main/java/com/sincre/springboot/utils/ResultUtils.java b/springboot/src/main/java/com/sincre/springboot/utils/ResultUtils.java new file mode 100644 index 0000000..c95880c --- /dev/null +++ b/springboot/src/main/java/com/sincre/springboot/utils/ResultUtils.java @@ -0,0 +1,80 @@ +package com.sincre.springboot.utils; + + +import com.sincre.springboot.controller.YinShiController; +import com.sincre.springboot.model.ResultModelObj; +import org.json.JSONObject; +import org.springframework.http.codec.json.Jackson2JsonDecoder; + +/** + * 结果返回工具类 + * 成功的code统一为 200,其他的根据接口返回的code返回给客户端 + */ +public class ResultUtils { + + private static ResultUtils resultUtils; + + public ResultUtils() { + + } + + public static ResultUtils getInstance() { + if (null == resultUtils) { + synchronized (ResultUtils.class) { + if (null == resultUtils) resultUtils = new ResultUtils(); + } + } + return resultUtils; + } + + + /** + * 萤石统一结果回调 + * @param result data为对象的字符串 + * @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(); + 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(); + } + + private void getYinShiToken(){ + + } + + /** + * 涂鸦云返回结果 + * @param result + * @return + */ + public String resturnResultTuya(String result) { + JSONObject jsonObject = new JSONObject(result); + boolean success = jsonObject.optBoolean("success"); + ResultModelObj resultModelObj = new ResultModelObj(); + if (success){//成功 + resultModelObj.setCode(200); + resultModelObj.setData(jsonObject.optString("result")); + resultModelObj.setMsg(jsonObject.optString("msg")); + }else {//失败 + resultModelObj.setCode(jsonObject.optLong("code")); + resultModelObj.setData("{}"); + resultModelObj.setMsg(jsonObject.optString("msg")); + } + return resultModelObj.toString(); + } + +} diff --git a/springboot/src/main/resources/mybatis-config.xml b/springboot/src/main/resources/mybatis-config.xml index bd0e469..c735a6a 100644 --- a/springboot/src/main/resources/mybatis-config.xml +++ b/springboot/src/main/resources/mybatis-config.xml @@ -11,5 +11,6 @@ + diff --git a/springboot/src/main/webapp/WEB-INF/jsp/index.jsp b/springboot/src/main/webapp/WEB-INF/jsp/index.jsp deleted file mode 100644 index e69de29..0000000 --- a/springboot/src/main/webapp/WEB-INF/jsp/index.jsp +++ /dev/null diff --git a/springboot/src/main/webapp/WEB-INF/lib/sqljdbc42.jar b/springboot/src/main/webapp/WEB-INF/lib/sqljdbc42.jar deleted file mode 100644 index 82dac34..0000000 Binary files a/springboot/src/main/webapp/WEB-INF/lib/sqljdbc42.jar and /dev/null differ diff --git a/springboot/src/main/webapp/WEB-INF/web.xml b/springboot/src/main/webapp/WEB-INF/web.xml deleted file mode 100644 index 9a127b4..0000000 --- a/springboot/src/main/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - \ No newline at end of file -- libgit2 0.21.0