Commit 6f6f947d40c316fd57e6d199d990beead95bc3c9

Authored by 陶汉栋
2 parents e041d203 7dc3d7c7
Exists in master

Merge branch 'master' of http://git.shunzhi.net/taohd/javassm

# Conflicts:
#	springboot/src/main/java/com/sincre/springboot/controller/YinShiController.java
springboot/src/main/java/com/sincre/springboot/ApiModel/Page.java 0 → 100644
... ... @@ -0,0 +1,32 @@
  1 +package com.sincre.springboot.ApiModel;
  2 +
  3 +public class Page {
  4 + public Integer getTotal() {
  5 + return total;
  6 + }
  7 + public Integer getPage() {
  8 + return page;
  9 + }
  10 +
  11 + public Integer getSize() {
  12 + return size;
  13 + }
  14 +
  15 + public void setTotal(Integer total) {
  16 + this.total = total;
  17 + }
  18 +
  19 + public void setPage(Integer page) {
  20 + this.page = page;
  21 + }
  22 +
  23 + public void setSize(Integer size) {
  24 + this.size = size;
  25 + }
  26 +
  27 + private Integer total;
  28 + private Integer page;
  29 + private Integer size;
  30 +
  31 +
  32 +}
... ...
springboot/src/main/java/com/sincre/springboot/ApiModel/YinShiResResult.java
1 1 package com.sincre.springboot.ApiModel;
2 2  
3   -import com.fasterxml.jackson.annotation.JsonInclude;
4 3  
5   -@JsonInclude(JsonInclude.Include.NON_EMPTY)
6   -public class YinShiResResult {
  4 +/**
  5 + * 萤石接口返回的Model
  6 + * @param <T>
  7 + */
  8 +public class YinShiResResult<T> {
7 9  
8   - private YinShiToken data;
  10 + private Page page ;
  11 + private T data;
9 12  
10   - public YinShiToken getData() {
  13 + public T getData() {
11 14 return data;
12 15 }
13 16  
14   - public void setData(YinShiToken data) {
  17 + public void setData(T data) {
15 18 this.data = data;
16 19 }
17 20  
18   - public String getCode() {
  21 + public Integer getCode() {
19 22 return code;
20 23 }
21 24  
22   - public void setCode(String code) {
  25 + public void setCode(Integer code) {
23 26 this.code = code;
24 27 }
25 28  
... ... @@ -31,31 +34,15 @@ public class YinShiResResult {
31 34 this.msg = msg;
32 35 }
33 36  
34   - private String code;
35   - private String msg;
36   -}
37   -
38   -class YinShiToken{
39   -
40   - private String accessToken;
41   -
42   - private Long expireTime;
43   -
44   - public String getAccessToken() {
45   - return accessToken;
  37 + public Page getPage() {
  38 + return page;
46 39 }
47 40  
48   - public void setAccessToken(String accessToken) {
49   - this.accessToken = accessToken;
50   - }
51   -
52   - public Long getExpireTime() {
53   - return expireTime;
54   - }
55   -
56   - public void setExpireTime(Long expireTime) {
57   - this.expireTime = expireTime;
  41 + public void setPage(Page page) {
  42 + this.page = page;
58 43 }
  44 + private Integer code;
  45 + private String msg;
  46 +}
59 47  
60 48  
61   -}
... ...
springboot/src/main/java/com/sincre/springboot/ApiPlatform/YinShiServiceConfig.java
... ... @@ -11,6 +11,9 @@ public class YinShiServiceConfig {
11 11 */
12 12 public static String appKey = "3780bdecb44c4b608367ba469d6d52ea";
13 13 public static String appSecret = "35c83d24b5a39b171572f6eae4ded9a7";
  14 + /**
  15 + * 用于管理员获取accessToken
  16 + */
14 17 public static String AccessToken = "";
15 18  
16 19  
... ...
springboot/src/main/java/com/sincre/springboot/common/ResponseCode.java 0 → 100644
... ... @@ -0,0 +1,29 @@
  1 +package com.sincre.springboot.common;
  2 +
  3 +/**
  4 + * Created by Ziv
  5 + */
  6 +public enum ResponseCode {
  7 +
  8 + SUCCESS(200, "SUCCESS"),
  9 + ERROR(500, "ServerInnerERROR");
  10 +
  11 +
  12 + private final int code;
  13 + private final String desc;
  14 +
  15 +
  16 + ResponseCode(int code, String desc) {
  17 + this.code = code;
  18 + this.desc = desc;
  19 + }
  20 +
  21 + public int getCode() {
  22 + return code;
  23 + }
  24 +
  25 + public String getDesc() {
  26 + return desc;
  27 + }
  28 +
  29 +}
... ...
springboot/src/main/java/com/sincre/springboot/common/ServerResponse.java 0 → 100644
... ... @@ -0,0 +1,121 @@
  1 +package com.sincre.springboot.common;
  2 +
  3 +import com.fasterxml.jackson.annotation.JsonIgnore;
  4 +import com.fasterxml.jackson.annotation.JsonInclude;
  5 +
  6 +
  7 +import java.io.Serializable;
  8 +
  9 +/**
  10 + * Created by Ziv
  11 + */
  12 +@JsonInclude(JsonInclude.Include.NON_EMPTY)
  13 +
  14 +public class ServerResponse<T> implements Serializable {
  15 +
  16 + /**
  17 + * 分页时用到的总数量
  18 + */
  19 + private String total;
  20 + /**
  21 + * 操作状态码
  22 + */
  23 + private int code;
  24 + /**
  25 + * 状态信息
  26 + */
  27 + private String msg;
  28 + /**
  29 + * 返回的数据包
  30 + */
  31 + private T data;
  32 +
  33 + private ServerResponse(int code) {
  34 + this.code = code;
  35 + }
  36 +
  37 + private ServerResponse(int code, T data) {
  38 + this.code = code;
  39 + this.data = data;
  40 + }
  41 +
  42 + private ServerResponse(int code, String msg, T data) {
  43 + this.code = code;
  44 + this.msg = msg;
  45 + this.data = data;
  46 + }
  47 +
  48 + private ServerResponse(String total,int code, String msg, T data) {
  49 + this.total = total;
  50 + this.code = code;
  51 + this.msg = msg;
  52 + this.data = data;
  53 + }
  54 + private ServerResponse(int code, String msg) {
  55 + this.code = code;
  56 + this.msg = msg;
  57 + }
  58 +
  59 + @JsonIgnore
  60 + //使之不在json序列化结果当中
  61 + public boolean isSuccess() {
  62 + return this.code == ResponseCode.SUCCESS.getCode();
  63 + }
  64 +
  65 + public String getTotal() {
  66 + return total;
  67 + }
  68 + public int getCode() {
  69 + return code;
  70 + }
  71 +
  72 + public T getData() {
  73 + return data;
  74 + }
  75 +
  76 + public String getMsg() {
  77 + return msg;
  78 + }
  79 +
  80 +
  81 + public static <T> ServerResponse<T> createBySuccess() {
  82 + return new ServerResponse<T>(ResponseCode.SUCCESS.getCode());
  83 + }
  84 +
  85 + /*
  86 + 该方法对应的是private ServerResponse(int code,String msg)的构造方法
  87 + */
  88 + public static <T> ServerResponse<T> createBySuccessMessage(String msg) {
  89 + return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), msg);
  90 + }
  91 +
  92 + /*
  93 + 该方法对应的是private ServerResponse(int code,T data)构造方法
  94 + */
  95 + public static <T> ServerResponse<T> createBySuccess(T data) {
  96 + return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), data);
  97 + }
  98 +
  99 + public static <T> ServerResponse<T> createBySuccess(String msg, T data) {
  100 + return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), msg, data);
  101 + }
  102 +
  103 + public static <T> ServerResponse<T> createBySuccess(String total,String msg, T data) {
  104 + return new ServerResponse<T>(total,ResponseCode.SUCCESS.getCode(), msg, data);
  105 + }
  106 +
  107 +
  108 + public static <T> ServerResponse<T> createByError() {
  109 + return new ServerResponse<T>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDesc());
  110 + }
  111 +
  112 +
  113 + public static <T> ServerResponse<T> createByErrorMessage(String errorMessage) {
  114 + return new ServerResponse<T>(ResponseCode.ERROR.getCode(), errorMessage);
  115 + }
  116 +
  117 + public static <T> ServerResponse<T> createByErrorCodeMessage(int errorCode, String errorMessage) {
  118 + return new ServerResponse<T>(errorCode, errorMessage);
  119 + }
  120 +
  121 +}
... ...
springboot/src/main/java/com/sincre/springboot/controller/YinShiController.java
... ... @@ -2,7 +2,10 @@ package com.sincre.springboot.controller;
2 2  
3 3  
4 4 import com.alibaba.fastjson.JSON;
  5 +import com.sincre.springboot.ApiModel.YinShiResResult;
5 6 import com.sincre.springboot.common.MD5;
  7 +import com.sincre.springboot.common.ResponseCode;
  8 +import com.sincre.springboot.common.ServerResponse;
6 9 import com.sincre.springboot.utils.ApiHelper;
7 10 import com.sincre.springboot.utils.CacheHelper;
8 11 import com.sincre.springboot.utils.ResultUtils;
... ... @@ -25,26 +28,13 @@ import com.sincre.springboot.ApiPlatform.YinShiServiceConfig;
25 28 @Api(value = "YinShiController", tags = "萤石对接接口")
26 29 public class YinShiController {
27 30  
28   -
29 31 @ApiOperation(value = "用于管理员获取accessToken")
30 32 @GetMapping("/token")
31   - public String GetYinShiToken() {
32   - String appKey = YinShiServiceConfig.appKey;
33   - String appSecret = YinShiServiceConfig.appSecret;
  33 + public ServerResponse GetYinShiToken() {
34 34  
35   - String url = YinShiServiceConfig.HostUrl + "lapp/token/get";
36   - Map<String, Object> map = new HashMap<>();
  35 + CacheHelper.GetYinShiToken();
37 36  
38   - map.put("appKey", appKey);
39   - map.put("appSecret", appSecret);
40   - String result = ApiHelper.doPost(url, new HashMap<String, String>(), map);
41   - JSONObject jsonObject = new JSONObject(result);
42   - JSONObject data = jsonObject.optJSONObject("data");
43   - YinShiServiceConfig.AccessToken = data.optString("accessToken");
44   - Date date = new Date();
45   - Date dateFu = new Date(data.optLong("expireTime"));
46   - CacheHelper.putYingshiYunToken(YinShiServiceConfig.AccessToken, (int) ((dateFu.getTime()-date.getTime())/1000));
47   - return ResultUtils.getInstance().resturnResultYingshi(result);
  37 + return ServerResponse.createBySuccessMessage(ResponseCode.SUCCESS.getDesc());
48 38 }
49 39  
50 40 @ApiOperation(value = "增加子账号")
... ... @@ -64,7 +54,7 @@ public class YinShiController {
64 54 map.put("accountName", accountName);
65 55 map.put("password", password);
66 56 String result = ApiHelper.doPost(url, new HashMap<String, String>(), map);
67   - return ResultUtils.getInstance().resturnResultYingshi(result);
  57 + return result;//ResultUtils.getInstance().resturnResultYingshi(result);
68 58 }
69 59  
70 60 @ApiOperation(value = "获取单个子账户信息")
... ... @@ -73,7 +63,7 @@ public class YinShiController {
73 63  
74 64 String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/get";
75 65 Map<String, Object> map = new HashMap<>();
76   - //子账户密码,LowerCase(MD5(AppKey#密码明文))
  66 +
77 67 map.put("accessToken", YinShiServiceConfig.AccessToken);
78 68 map.put("accountId", accountId);
79 69 map.put("accountName", accountName);
... ... @@ -84,18 +74,20 @@ public class YinShiController {
84 74  
85 75 @ApiOperation(value = "获取子账号信息列表")
86 76 @GetMapping("getChildAccountList")
87   - public String getChildAccountList(@RequestParam("pageIndex") Integer pageIndex, @RequestParam("pageSize") Integer pageSize) {
  77 + public ServerResponse getChildAccountList(@RequestParam("pageIndex") Integer pageIndex, @RequestParam("pageSize") Integer pageSize) {
88 78  
89 79 String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/list";
90 80 Map<String, Object> map = new HashMap<>();
91   - //子账户密码,LowerCase(MD5(AppKey#密码明文))
92 81  
93 82 map.put("accessToken", YinShiServiceConfig.AccessToken);
94 83 map.put("pageStart", pageIndex);
95 84 map.put("pageSize", pageSize);
96   - String result = ApiHelper.doPost(url, new HashMap<String, String>(), map);
  85 + String result = ApiHelper.doPost(url, new HashMap<String,String>(), map);
97 86  
98   - return result;
  87 + System.out.println(result);
  88 + YinShiResResult yinShiResResult = JSON.parseObject(result,YinShiResResult.class);
  89 +
  90 + return ResultUtils.getInstance().resturnResultYingshi(yinShiResResult);
99 91 }
100 92  
101 93 @ApiOperation(value = "修改当前子账户密码")
... ... @@ -104,7 +96,6 @@ public class YinShiController {
104 96  
105 97 String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/updatePassword";
106 98 Map<String, Object> map = new HashMap<>();
107   - //子账户密码,LowerCase(MD5(AppKey#密码明文))
108 99  
109 100 oldPassword = YinShiServiceConfig.appKey + "#" + oldPassword;
110 101 newPassword = YinShiServiceConfig.appKey + "#" + newPassword;
... ... @@ -189,12 +180,11 @@ public class YinShiController {
189 180  
190 181 String url = YinShiServiceConfig.HostUrl + "lapp/ram/token/get";
191 182 Map<String, Object> map1 = new HashMap<>();
  183 +
192 184 map1.put("accessToken", YinShiServiceConfig.AccessToken);
193 185 map1.put("accountId", accountId);
194 186 String result = ApiHelper.doPost(url, new HashMap<String, String>(), map1);
  187 +
195 188 return result;
196 189 }
197   -
198   -
199   -
200 190 }
... ...
springboot/src/main/java/com/sincre/springboot/utils/CacheHelper.java
... ... @@ -8,6 +8,7 @@ import com.sincre.springboot.ApiPlatform.TuYaCloudService;
8 8 import com.sincre.springboot.ApiPlatform.YinShiServiceConfig;
9 9 import com.sincre.springboot.common.EhcacheUtil;
10 10 import org.apache.commons.lang3.StringUtils;
  11 +import org.json.JSONObject;
11 12  
12 13 import java.util.HashMap;
13 14 import java.util.Map;
... ... @@ -17,6 +18,8 @@ public class CacheHelper {
17 18 static EhcacheUtil ehcacheUtil = new EhcacheUtil();
18 19 static String TuYaTokenKey = "TuYa_Token";
19 20 static String TuYaRefreshTokenKey = "TuYa_Re_Token";
  21 +
  22 +
20 23 private static void setTuYaToken(){
21 24  
22 25 String apiUrl = "/v1.0/token?grant_type=1";
... ... @@ -93,7 +96,6 @@ public class CacheHelper {
93 96 }
94 97 }
95 98  
96   -
97 99 /**
98 100 * 获取萤石云token
99 101 * @return
... ... @@ -111,4 +113,22 @@ public class CacheHelper {
111 113 ehcacheUtil.setex(YinShiServiceConfig.accessToken,token,time);
112 114 }
113 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 +
114 134 }
... ...
springboot/src/main/java/com/sincre/springboot/utils/ResultUtils.java
1 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 7 import com.sincre.springboot.model.ResultModelObj;
6 8 import org.json.JSONObject;
7   -import org.springframework.http.codec.json.Jackson2JsonDecoder;
8 9  
9 10 /**
10 11 * 结果返回工具类
... ... @@ -30,34 +31,52 @@ public class ResultUtils {
30 31  
31 32 /**
32 33 * 萤石统一结果回调
33   - * @param result data为对象的字符串
  34 + *
  35 + * @param yinShiResResult 萤石接口返回对象的字符串
34 36 * @return 返回的data可以是对象或者集合,都以字符串形式返回
35 37 */
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();
42   - try {
43   - Thread.sleep(1000);
44   - } catch (InterruptedException e) {
45   - e.printStackTrace();
  38 + public ServerResponse resturnResultYingshi(YinShiResResult yinShiResResult) {
  39 +// JSONObject jsonObject = new JSONObject(result);
  40 +// ResultModelObj resultModelObj = new ResultModelObj();
  41 +// long code = jsonObject.optLong("code");
  42 +// if (code==10002){//accesstoken过期
  43 +// new YinShiController().GetYinShiToken();
  44 +// try {
  45 +// Thread.sleep(1000);
  46 +// } catch (InterruptedException e) {
  47 +// e.printStackTrace();
  48 +// }
  49 +// }
  50 +// resultModelObj.setCode(code);
  51 +// resultModelObj.setData(jsonObject.optString("data"));
  52 +// resultModelObj.setMsg(jsonObject.optString("msg"));
  53 +// resultModelObj.setTotal(jsonObject.optInt("page"));
  54 +// return resultModelObj.toString();
  55 +
  56 + int code = yinShiResResult.getCode();
  57 + System.out.println(yinShiResResult.getPage().getTotal());
  58 + if (code == 200) {
  59 + return ServerResponse.createBySuccess(yinShiResResult.getPage().getTotal().toString(), ResponseCode.SUCCESS.getDesc(), yinShiResResult.getData());
  60 + } else {
  61 + if (code == 10002) {//accessToken过期或异常
  62 + CacheHelper.GetYinShiToken();
  63 + try {
  64 + Thread.sleep(1000);
  65 + } catch (InterruptedException e) {
  66 + e.printStackTrace();
  67 + }
46 68 }
  69 + return ServerResponse.createByErrorMessage(yinShiResResult.getMsg());
47 70 }
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();
53 71 }
54 72  
55   - private void getYinShiToken(){
  73 + private void getYinShiToken() {
56 74  
57 75 }
58 76  
59 77 /**
60 78 * 涂鸦云返回结果
  79 + *
61 80 * @param result
62 81 * @return
63 82 */
... ... @@ -65,11 +84,11 @@ public class ResultUtils {
65 84 JSONObject jsonObject = new JSONObject(result);
66 85 boolean success = jsonObject.optBoolean("success");
67 86 ResultModelObj resultModelObj = new ResultModelObj();
68   - if (success){//成功
  87 + if (success) {//成功
69 88 resultModelObj.setCode(200);
70 89 resultModelObj.setData(jsonObject.optString("result"));
71 90 resultModelObj.setMsg(jsonObject.optString("msg"));
72   - }else {//失败
  91 + } else {//失败
73 92 resultModelObj.setCode(jsonObject.optLong("code"));
74 93 resultModelObj.setData("{}");
75 94 resultModelObj.setMsg(jsonObject.optString("msg"));
... ...