Commit f0110d44e6de17816c4b10345353d1e955a96caa

Authored by baishou
1 parent efe2cf9e
Exists in master

接口统一管理类和code状态类封装完成

springboot/src/main/java/com/sincre/springboot/ApiModel/Page.java 0 → 100644
@@ -0,0 +1,33 @@ @@ -0,0 +1,33 @@
  1 +package com.zjx.springboot02.apimodel;
  2 +
  3 +public class Page {
  4 + public Integer getTotal() {
  5 + return total;
  6 + }
  7 +
  8 + public void setTotal(Integer total) {
  9 + this.total = total;
  10 + }
  11 +
  12 + public Integer getPage() {
  13 + return page;
  14 + }
  15 +
  16 + public void setPage(Integer page) {
  17 + this.page = page;
  18 + }
  19 +
  20 + public Integer getSize() {
  21 + return size;
  22 + }
  23 +
  24 + public void setSize(Integer size) {
  25 + this.size = size;
  26 + }
  27 +
  28 + private Integer total;
  29 + private Integer page;
  30 + private Integer size;
  31 +
  32 +
  33 +}
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 -import com.fasterxml.jackson.annotation.JsonInclude; 3 +/**
  4 + * 萤石接口返回的Model
  5 + * @param <T>
  6 + */
  7 +public class YinShiResResult<T> {
4 8
5 -@JsonInclude(JsonInclude.Include.NON_EMPTY)  
6 -public class YinShiResResult { 9 + private Page page ;
  10 + private T data;
7 11
8 - private YinShiToken data;  
9 -  
10 - public YinShiToken getData() { 12 + public T getData() {
11 return data; 13 return data;
12 } 14 }
13 15
14 - public void setData(YinShiToken data) { 16 + public void setData(T data) {
15 this.data = data; 17 this.data = data;
16 } 18 }
17 19
@@ -31,33 +33,15 @@ public class YinShiResResult { @@ -31,33 +33,15 @@ public class YinShiResResult {
31 this.msg = msg; 33 this.msg = msg;
32 } 34 }
33 35
34 - private String code;  
35 - private String msg;  
36 -  
37 - public class YinShiToken{  
38 -  
39 - private String accessToken;  
40 - /**  
41 - * 精确到毫秒的时间戳  
42 - */  
43 - private Long expireTime;  
44 -  
45 - public String getAccessToken() {  
46 - return accessToken;  
47 - }  
48 -  
49 - public void setAccessToken(String accessToken) {  
50 - this.accessToken = accessToken;  
51 - }  
52 -  
53 - public Long getExpireTime() {  
54 - return expireTime;  
55 - } 36 + public Page getPage() {
  37 + return page;
  38 + }
56 39
57 - public void setExpireTime(Long expireTime) {  
58 - this.expireTime = expireTime;  
59 - } 40 + public void setPage(Page page) {
  41 + this.page = page;
60 } 42 }
  43 + private String code;
  44 + private String msg;
61 } 45 }
62 46
63 47
springboot/src/main/java/com/sincre/springboot/common/ResponseCode.java 0 → 100644
@@ -0,0 +1,29 @@ @@ -0,0 +1,29 @@
  1 +package com.zjx.springboot02.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 @@ @@ -0,0 +1,121 @@
  1 +package com.zjx.springboot02.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 int 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(int 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 int 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(Integer 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,7 +2,10 @@ package com.sincre.springboot.controller;
2 2
3 3
4 import com.alibaba.fastjson.JSON; 4 import com.alibaba.fastjson.JSON;
  5 +import com.sincre.springboot.ApiModel.YinShiResResult;
5 import com.sincre.springboot.common.MD5; 6 import com.sincre.springboot.common.MD5;
  7 +import com.sincre.springboot.common.ResponseCode;
  8 +import com.sincre.springboot.common.ServerResponse;
6 import com.sincre.springboot.utils.ApiHelper; 9 import com.sincre.springboot.utils.ApiHelper;
7 import com.sincre.springboot.utils.CacheHelper; 10 import com.sincre.springboot.utils.CacheHelper;
8 import com.sincre.springboot.utils.ResultUtils; 11 import com.sincre.springboot.utils.ResultUtils;
@@ -85,7 +88,7 @@ public class YinShiController { @@ -85,7 +88,7 @@ public class YinShiController {
85 88
86 @ApiOperation(value = "获取子账号信息列表") 89 @ApiOperation(value = "获取子账号信息列表")
87 @GetMapping("getChildAccountList") 90 @GetMapping("getChildAccountList")
88 - public String getChildAccountList(@RequestParam("pageIndex") Integer pageIndex, @RequestParam("pageSize") Integer pageSize) { 91 + public ServerResponse getChildAccountList(@RequestParam("pageIndex") Integer pageIndex, @RequestParam("pageSize") Integer pageSize) {
89 92
90 String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/list"; 93 String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/list";
91 Map<String, Object> map = new HashMap<>(); 94 Map<String, Object> map = new HashMap<>();
@@ -96,7 +99,9 @@ public class YinShiController { @@ -96,7 +99,9 @@ public class YinShiController {
96 map.put("pageSize", pageSize); 99 map.put("pageSize", pageSize);
97 String result = ApiHelper.doPost(url, new HashMap<String, String>(), map); 100 String result = ApiHelper.doPost(url, new HashMap<String, String>(), map);
98 101
99 - return result; 102 + 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());
100 } 105 }
101 106
102 @ApiOperation(value = "修改当前子账户密码") 107 @ApiOperation(value = "修改当前子账户密码")