1
0
mirror of https://gitee.com/bookshelfplus/bookshelfplus synced 2025-09-02 23:23:28 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee

后台返回数据统一为CommonReturnType

This commit is contained in:
2022-03-15 13:48:58 +08:00
parent ae26ecc372
commit cbfc277c9c
7 changed files with 62 additions and 13 deletions

View File

@@ -41,8 +41,8 @@ function checkBackendStatus() {
var backendStatus = false;
getRequest("/status/get", {})
.then(function (response) {
console.log("response.data", response.data);
if (response.data.server === "OK") {
console.log("backend response data:", response.data);
if (response.data.status === "success") {
$("#backendStatus").text("后台连接正常");
$("#backendStatus").addClass("info-ok");
} else {
@@ -60,7 +60,7 @@ function checkFrontendStatus() {
var backendStatus = false;
getRequest("../get-frontend-status", {})
.then(function (response) {
console.log("response.data", response.data);
console.log("frontend response data:", response.data);
if (response.data.server === "OK") {
$("#frontendStatus").text("前台连接正常");
$("#frontendStatus").addClass("info-ok");

View File

@@ -70,7 +70,6 @@
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<!--<version>1.3.2</version>-->
<version>2.2.2</version>
</dependency>
@@ -161,6 +160,10 @@
<version>1.5.0-alpha.10.1</version>
<scope>test</scope>
</dependency>
<!--
PageHelper 分页插件
-->
</dependencies>
<build>

View File

@@ -0,0 +1,26 @@
package plus.bookshelf.Common.Response;
import lombok.Data;
@Data
public class CommonReturnType {
// 表明对应请求的返回处理结果 "success" 或 "fail"
private String Status;
// 若 status == "success" 则data内返回前端需要的JSON数据
// 若 status == "fail" 则data内使用通用的错误码格式
private Object Data;
// 定义一个通用的创建方法
public static CommonReturnType create(Object result) {
return CommonReturnType.create(result, CommonReturnTypeStatus.SUCCESS);
}
public static CommonReturnType create(Object result, CommonReturnTypeStatus status) {
CommonReturnType type = new CommonReturnType();
type.setData(result);
type.setStatus(status.toString());
return type;
}
}

View File

@@ -0,0 +1,17 @@
package plus.bookshelf.Common.Response;
public enum CommonReturnTypeStatus {
SUCCESS("success"),
FAILED("failed");
private String str;
private CommonReturnTypeStatus(String str) {
this.str = str;
}
@Override
public String toString() {
return str;
}
}

View File

@@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import plus.bookshelf.Common.Response.CommonReturnType;
import plus.bookshelf.Controller.VO.BookVO;
import plus.bookshelf.Service.Model.BookModel;
import plus.bookshelf.Service.Service.BookService;
@@ -25,14 +26,14 @@ public class BookController extends BaseController {
// @ApiImplicitParam(name = "book", value = "图书详细实体", required = true, dataType = "Book")
@RequestMapping(value = "get", method = {RequestMethod.GET})
@ResponseBody
public BookVO get(@RequestParam(value = "id") Integer id) {
public CommonReturnType get(@RequestParam(value = "id") Integer id) {
if (id == null) {
return null;
}
BookModel bookModel = bookService.getBookById(id);
BookVO bookVO = convertFromModel(bookModel);
return bookVO;
return CommonReturnType.create(bookVO);
}
private BookVO convertFromModel(BookModel bookModel) {

View File

@@ -7,6 +7,7 @@ import io.swagger.annotations.ApiOperation;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import plus.bookshelf.Common.Response.CommonReturnType;
import java.lang.management.ManagementFactory;
import java.math.BigDecimal;
@@ -33,11 +34,11 @@ public class StatusController {
@ApiOperation(value = "系统状态", notes = "获取服务器当前系统负载。SystemLoadAverage返回-1时代表不支持。")
@RequestMapping(value = "get", method = {RequestMethod.GET})
@ResponseBody
public Object get() {
OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("server", "OK");
return hashMap;
public CommonReturnType get() {
// HashMap<String, Object> hashMap = new HashMap<>();
// hashMap.put("server", "OK");
// return CommonReturnType.create(hashMap);
return CommonReturnType.create(null);
}
// @ApiOperation(value = "系统负载", notes = "获取服务器当前系统负载。SystemLoadAverage返回-1时代表不支持。")

View File

@@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import plus.bookshelf.Common.Response.CommonReturnType;
import plus.bookshelf.Controller.VO.UserVO;
import plus.bookshelf.Service.Impl.UserServiceImpl;
import plus.bookshelf.Service.Model.UserModel;
@@ -26,11 +27,11 @@ public class UserController {
@ApiOperation(value = "用户登录",notes = "传入用户名以及密码的MD5值进行登录")
@RequestMapping(value = "login", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
@ResponseBody
public UserVO login(@RequestParam(value = "username") String username,
public CommonReturnType login(@RequestParam(value = "username") String username,
@RequestParam(value = "encryptpwd") String encryptPwd) {
UserModel userModel = userService.userLogin(username, encryptPwd);
UserVO userVO = convertFromService(userModel);
return userVO;
return CommonReturnType.create(userVO);
}
private UserVO convertFromService(UserModel userModel) {