Commit 3d0dfa88 authored by zengtianlai3's avatar zengtianlai3

完善状态码

parent c3a5aa34
package iot.sixiang.license.consts;
/**
* Created by m33 on 2022/6/15 9:43
*/
public enum ResultCode {
SUCCESS(200, "操作成功"),
SERVER_EXCEPTION(400, "服务异常"),
UNAUTHORIZED(401, "暂未登录或token已经过期"),
VALIDATE_FAILED(402, "参数检验失败"),
FAILED(403, "操作失败");
private long code;
private String msg;
private ResultCode(long code, String msg) {
this.code = code;
this.msg = msg;
}
public long getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
......@@ -58,7 +58,7 @@ public class ApplyController {
if (res) {
return BaseResult.success();
} else {
return BaseResult.fail();
return BaseResult.failed();
}
}
......
......@@ -57,7 +57,7 @@ public class DeviceController {
if (res) {
return BaseResult.success();
} else {
return BaseResult.fail();
return BaseResult.failed();
}
}
......
......@@ -60,7 +60,7 @@ public class LoginController {
return ResResult.success().record(loginVo);
}
}
return ResResult.fail().msg("用户名或密码错误");
return ResResult.failed().msg("用户名或密码错误");
}
@ApiOperation(value = "注销接口", notes = "注销接口")
......@@ -72,7 +72,7 @@ public class LoginController {
@GetMapping("fail")
@ApiIgnore
public ResResult fail(HttpServletRequest request) {
return ResResult.fail().msg(request.getAttribute("msg").toString());
public BaseResult fail(HttpServletRequest request) {
return BaseResult.unauthorized();
}
}
......@@ -107,7 +107,7 @@ public class OperateController {
if (res) {
return BaseResult.success();
} else {
return BaseResult.fail();
return BaseResult.failed();
}
}
......@@ -148,7 +148,7 @@ public class OperateController {
if (res) {
return BaseResult.success();
} else {
return BaseResult.fail();
return BaseResult.failed();
}
}
......@@ -165,7 +165,7 @@ public class OperateController {
if (res) {
return BaseResult.success();
} else {
return BaseResult.fail();
return BaseResult.failed();
}
}
......@@ -188,7 +188,7 @@ public class OperateController {
if (res) {
return BaseResult.success();
} else {
return BaseResult.fail();
return BaseResult.failed();
}
}
......
......@@ -58,7 +58,7 @@ public class UserController {
if (res) {
return BaseResult.success();
} else {
return BaseResult.fail();
return BaseResult.failed();
}
}
......@@ -75,7 +75,7 @@ public class UserController {
if (res) {
return BaseResult.success();
} else {
return BaseResult.fail();
return BaseResult.failed();
}
}
......@@ -98,7 +98,7 @@ public class UserController {
if (res) {
return BaseResult.success();
} else {
return BaseResult.fail();
return BaseResult.failed();
}
}
......
......@@ -16,7 +16,7 @@ public class GlobalExceptionHandler {
@ResponseBody //为了返回数据
public BaseResult error(Exception e){
e.printStackTrace();
return BaseResult.fail().msg("执行了全局的异常类...");
return BaseResult.serverException();
}
//自定义异常
......@@ -24,6 +24,6 @@ public class GlobalExceptionHandler {
@ResponseBody//为了返回数据
public BaseResult error(IotLicenseException e){
e.printStackTrace();
return BaseResult.fail().msg(e.getMsg()).code(e.getCode());
return BaseResult.failed().msg(e.getMsg()).code(e.getCode());
}
}
......@@ -12,7 +12,7 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor //生成无参构造
public class IotLicenseException extends RuntimeException {
private Integer code;
private long code;
private String msg;//异常信息
}
package iot.sixiang.license.model;
import iot.sixiang.license.consts.ResultCode;
//@Data
public class BaseResult {
private int code;
private long code;
private String msg;
public BaseResult(int code, String reason) {
public BaseResult(long code, String msg) {
super();
this.code = code;
this.msg = reason;
this.msg = msg;
}
public BaseResult() {
......@@ -16,21 +18,53 @@ public class BaseResult {
// TODO Auto-generated constructor stub
}
/**
* code = 200
* msg = 操作成功
* @return
*/
public static BaseResult success() {
BaseResult respResult = new BaseResult();
respResult.setCode(200);
respResult.setMsg("success");
return respResult;
return new BaseResult(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMsg());
}
public static BaseResult fail() {
BaseResult respResult = new BaseResult();
respResult.setCode(400);
respResult.setMsg("fail");
return respResult;
/**
* code = 400
* msg = 服务异常
* @return
*/
public static BaseResult serverException() {
return new BaseResult(ResultCode.SERVER_EXCEPTION.getCode(),ResultCode.SERVER_EXCEPTION.getMsg());
}
/**
* code = 401
* msg = 暂未登录或token已经过期
* @return
*/
public static BaseResult unauthorized() {
return new BaseResult(ResultCode.UNAUTHORIZED.getCode(),ResultCode.UNAUTHORIZED.getMsg());
}
/**
* code = 402
* msg = 参数校验失败
* @return
*/
public static BaseResult validate_failed() {
return new BaseResult(ResultCode.VALIDATE_FAILED.getCode(),ResultCode.VALIDATE_FAILED.getMsg());
}
/**
* code = 403
* msg = 操作失败(数据库增删改查等失败)
* @return
*/
public static BaseResult failed() {
return new BaseResult(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMsg());
}
public BaseResult code(int code) {
public BaseResult code(long code) {
this.code = code;
return this;
}
......@@ -41,11 +75,11 @@ public class BaseResult {
return this;
}
public int getCode() {
public long getCode() {
return code;
}
public void setCode(int code) {
public void setCode(long code) {
this.code = code;
}
......
......@@ -24,6 +24,7 @@ public class PageResult<T> {
this.record = record;
}
public PageResult() {
super();
// TODO Auto-generated constructor stub
......
package iot.sixiang.license.model;
import iot.sixiang.license.consts.ResultCode;
import lombok.Data;
/**
......@@ -12,25 +13,63 @@ import lombok.Data;
*/
@Data
public class ResResult<T> {
private int code;
private long code;
private String msg;
private T record;
public ResResult(long code, String msg) {
super();
this.code = code;
this.msg = msg;
}
/**
* code = 200
* msg = 操作成功
* @return
*/
public static ResResult success() {
ResResult respResult = new ResResult();
respResult.setCode(200);
respResult.setMsg("success");
return respResult;
return new ResResult(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMsg());
}
/**
* code = 400
* msg = 服务异常
* @return
*/
public static ResResult serverException() {
return new ResResult(ResultCode.SERVER_EXCEPTION.getCode(),ResultCode.SERVER_EXCEPTION.getMsg());
}
public static ResResult fail() {
ResResult respResult = new ResResult();
respResult.setCode(400);
respResult.setMsg("fail");
return respResult;
/**
* code = 401
* msg = 暂未登录或token已经过期
* @return
*/
public static ResResult unauthorized() {
return new ResResult(ResultCode.UNAUTHORIZED.getCode(),ResultCode.UNAUTHORIZED.getMsg());
}
/**
* code = 402
* msg = 参数校验失败
* @return
*/
public static ResResult validate_failed() {
return new ResResult(ResultCode.VALIDATE_FAILED.getCode(),ResultCode.VALIDATE_FAILED.getMsg());
}
/**
* code = 403
* msg = 操作失败(数据库增删改查等失败)
* @return
*/
public static ResResult failed() {
return new ResResult(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMsg());
}
public ResResult code(int code) {
public ResResult code(long code) {
this.code = code;
return this;
}
......
package iot.sixiang.license.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import iot.sixiang.license.consts.ResultCode;
import iot.sixiang.license.entity.AlarmRead;
import iot.sixiang.license.handler.IotLicenseException;
import iot.sixiang.license.mapper.AlarmMapper;
......@@ -31,7 +32,7 @@ public class AlarmReadServiceImpl extends ServiceImpl<AlarmReadMapper, AlarmRead
@Override
public boolean readAlarm(int userId) {
if (userId == 0) {
throw new IotLicenseException(400, "用户Id不能为空");
throw new IotLicenseException(ResultCode.VALIDATE_FAILED.getCode(),ResultCode.VALIDATE_FAILED.getMsg());
}
List<AlarmVo> alarmList = alarmMapper.getAlarmList(userId);
for (AlarmVo alarm: alarmList) {
......
package iot.sixiang.license.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import iot.sixiang.license.consts.ResultCode;
import iot.sixiang.license.entity.Alarm;
import iot.sixiang.license.handler.IotLicenseException;
import iot.sixiang.license.mapper.AlarmMapper;
......@@ -24,7 +25,7 @@ public class AlarmServiceImpl extends ServiceImpl<AlarmMapper, Alarm> implements
@Override
public List<AlarmVo> getAlarmList(int userId) {
if (userId == 0) {
throw new IotLicenseException(400, "用户Id不能为空");
throw new IotLicenseException(ResultCode.VALIDATE_FAILED.getCode(),ResultCode.VALIDATE_FAILED.getMsg());
}
return alarmMapper.getAlarmList(userId);
}
......
......@@ -2,6 +2,7 @@ package iot.sixiang.license.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import iot.sixiang.license.auth.AuthManager;
import iot.sixiang.license.consts.ResultCode;
import iot.sixiang.license.entity.Apply;
import iot.sixiang.license.handler.IotLicenseException;
import iot.sixiang.license.mapper.ApplyMapper;
......@@ -33,18 +34,13 @@ public class ApplyServiceImpl extends ServiceImpl<ApplyMapper, Apply> implements
ApplyMapper applyMapper;
@Override
public boolean addApply(String appName, String appKey, int userId) {
if(StringUtils.isEmpty(appName)) {
throw new IotLicenseException(400,"应用名不能为空");
}
if(StringUtils.isEmpty(appKey)) {
throw new IotLicenseException(400,"应用key不能为空");
}
if(userId == 0) {
throw new IotLicenseException(400,"用户Id不能为空");
if(StringUtils.isEmpty(appName) || StringUtils.isEmpty(appKey)
|| userId == 0) {
throw new IotLicenseException(ResultCode.VALIDATE_FAILED.getCode(),ResultCode.VALIDATE_FAILED.getMsg());
}
Apply res = applyMapper.getApplyByAppName(appName);
if (res != null) {
throw new IotLicenseException(400,"应用名已存在");
throw new IotLicenseException(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMsg());
}
boolean re = applyMapper.addApply(appName, appKey, userId);
if (!re) {
......@@ -57,13 +53,9 @@ public class ApplyServiceImpl extends ServiceImpl<ApplyMapper, Apply> implements
@Override
public PageInfoModel<AppVo> getAppList(int pageNo, int pageSize, String appName) {
if(pageNo == 0) {
throw new IotLicenseException(400,"pageNo不能为空");
if(pageNo == 0 || pageSize == 0) {
throw new IotLicenseException(ResultCode.VALIDATE_FAILED.getCode(),ResultCode.VALIDATE_FAILED.getMsg());
}
if(pageSize == 0) {
throw new IotLicenseException(400, "pageSize不能为空");
}
List<AppVo> records = applyMapper.getAppList(appName);
records = records.stream().sorted(Comparator.comparing(AppVo::getAppId)).collect(Collectors.toList());
List<AppVo> result = new ArrayList<>();
......
package iot.sixiang.license.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import iot.sixiang.license.consts.ResultCode;
import iot.sixiang.license.device.DeviceManager;
import iot.sixiang.license.entity.Device;
import iot.sixiang.license.handler.IotLicenseException;
......@@ -34,11 +35,8 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
@Override
public PageInfoModel<DeviceVo> getDeviceList(int pageNo, int pageSize, String appName, String userName) {
if(pageNo == 0) {
throw new IotLicenseException(400,"pageNo不能为空");
}
if(pageSize == 0) {
throw new IotLicenseException(400, "pageSize不能为空");
if(pageNo == 0 || pageSize == 0) {
throw new IotLicenseException(ResultCode.VALIDATE_FAILED.getCode(),ResultCode.VALIDATE_FAILED.getMsg());
}
List<DeviceVo> deviceTypes = deviceMapper.getDeviceList(appName,userName);
deviceTypes = deviceTypes.stream().sorted(Comparator.comparing(DeviceVo::getDeviceId)).collect(Collectors.toList());
......@@ -55,11 +53,8 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
@Override
public boolean addDevice(int appId, int count) {
if (appId == 0) {
throw new IotLicenseException(400, "应用Id不能为空");
}
if (count == 0) {
throw new IotLicenseException(400, "count不能为零或空");
if(appId == 0 || count == 0) {
throw new IotLicenseException(ResultCode.VALIDATE_FAILED.getCode(),ResultCode.VALIDATE_FAILED.getMsg());
}
for (int i = 0; i < count; i++) {
String sn = CommonUtil.genRandomNum(18);
......
package iot.sixiang.license.service.impl;
import iot.sixiang.license.consts.ResultCode;
import iot.sixiang.license.handler.IotLicenseException;
import iot.sixiang.license.mapper.ResourceMapper;
import iot.sixiang.license.model.vo.ResourceVo;
......@@ -20,8 +21,8 @@ public class ResourceServiceImpl implements ResourceService {
@Override
public List<ResourceVo> getResource(int userId) {
if (userId == 0) {
throw new IotLicenseException(400, "用户Id不能为空");
if(userId == 0) {
throw new IotLicenseException(ResultCode.VALIDATE_FAILED.getCode(),ResultCode.VALIDATE_FAILED.getMsg());
}
List<ResourceVo> record = resourceMapper.getResource(userId);
return record;
......
......@@ -2,11 +2,12 @@ package iot.sixiang.license.service.impl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import iot.sixiang.license.consts.ResultCode;
import iot.sixiang.license.entity.Server;
import iot.sixiang.license.handler.IotLicenseException;
import iot.sixiang.license.mapper.ServerMapper;
import iot.sixiang.license.service.ServerService;
import iot.sixiang.license.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
......@@ -14,7 +15,7 @@ import java.util.List;
/**
* <p>
* 服务实现类
* 服务实现类
* </p>
*
* @author m33
......@@ -28,33 +29,33 @@ public class ServerServiceImpl extends ServiceImpl<ServerMapper, Server> impleme
@Override
public List<Server> getServerList(int pageNo, int pageSize) {
Page<Server> page = new Page<>(pageNo,pageSize);
baseMapper.selectPage(page,null);
Page<Server> page = new Page<>(pageNo, pageSize);
baseMapper.selectPage(page, null);
List<Server> records = page.getRecords();
return records;
}
@Override
public boolean deleteServer(String serverIp) {
if (JsonUtil.isNull(serverIp)) {
return false;
if (StringUtils.isEmpty(serverIp)) {
throw new IotLicenseException(ResultCode.VALIDATE_FAILED.getCode(), ResultCode.VALIDATE_FAILED.getMsg());
}
return serverMapper.deleteServer(serverIp);
}
@Override
public boolean addServer(String serverIp, int port) {
if (JsonUtil.isNull(serverIp) || JsonUtil.isNull(String.valueOf(port))) {
return false;
if (StringUtils.isEmpty(serverIp) || port == 0) {
throw new IotLicenseException(ResultCode.VALIDATE_FAILED.getCode(), ResultCode.VALIDATE_FAILED.getMsg());
}
return serverMapper.addServer(serverIp,port);
return serverMapper.addServer(serverIp, port);
}
@Override
public boolean updateServer(String serverIp, int port) {
if (JsonUtil.isNull(serverIp) || JsonUtil.isNull(String.valueOf(port))) {
return false;
if (StringUtils.isEmpty(serverIp) || port == 0) {
throw new IotLicenseException(ResultCode.VALIDATE_FAILED.getCode(), ResultCode.VALIDATE_FAILED.getMsg());
}
return serverMapper.updateServer(serverIp,port);
return serverMapper.updateServer(serverIp, port);
}
}
package iot.sixiang.license.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import iot.sixiang.license.consts.ResultCode;
import iot.sixiang.license.entity.SysOperLog;
import iot.sixiang.license.handler.IotLicenseException;
import iot.sixiang.license.mapper.SysOperLogMapper;
......@@ -37,11 +38,8 @@ public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOper
@Override
public PageInfoModel<SysOperLog> getOperLogList(int pageNo, int pageSize) {
if(pageNo == 0) {
throw new IotLicenseException(400,"pageNo不能为空");
}
if(pageSize == 0) {
throw new IotLicenseException(400, "pageSize不能为空");
if(pageNo == 0 || pageSize == 0) {
throw new IotLicenseException(ResultCode.VALIDATE_FAILED.getCode(),ResultCode.VALIDATE_FAILED.getMsg());
}
List<SysOperLog> records = sysOperLogMapper.getOperLogList();
records = records.stream().sorted(Comparator.comparing(SysOperLog::getId)).collect(Collectors.toList());
......
......@@ -2,13 +2,13 @@ package iot.sixiang.license.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import iot.sixiang.license.consts.ResultCode;
import iot.sixiang.license.entity.User;
import iot.sixiang.license.handler.IotLicenseException;
import iot.sixiang.license.mapper.UserMapper;
import iot.sixiang.license.model.PageInfoModel;
import iot.sixiang.license.model.vo.UserVo;
import iot.sixiang.license.service.UserService;
import iot.sixiang.license.util.JsonUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
......@@ -34,11 +34,8 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
@Override
public PageInfoModel<UserVo> getUserList(int pageNo, int pageSize,String userName, String company) {
if(pageNo == 0) {
throw new IotLicenseException(400,"pageNo不能为空");
}
if(pageSize == 0) {
throw new IotLicenseException(400, "pageSize不能为空");
if(pageNo == 0 || pageSize == 0) {
throw new IotLicenseException(ResultCode.VALIDATE_FAILED.getCode(),ResultCode.VALIDATE_FAILED.getMsg());
}
List<UserVo> records = userMapper.getUserList(userName,company);
records = records.stream().sorted(Comparator.comparing(UserVo::getUserId)).collect(Collectors.toList());
......@@ -55,31 +52,28 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
@Override
public boolean deleteUser(int userId) {
if (JsonUtil.isNull(String.valueOf(userId))) {
return false;
if(userId == 0) {
throw new IotLicenseException(ResultCode.VALIDATE_FAILED.getCode(),ResultCode.VALIDATE_FAILED.getMsg());
}
return userMapper.deleteUser(userId);
}
@Override
public boolean addUser(String userName, String company, String password) {
if(StringUtils.isEmpty(userName)) {
throw new IotLicenseException(400,"用户名不能为空");
}
if(StringUtils.isEmpty(company)) {
throw new IotLicenseException(400,"公司名不能为空");
if(StringUtils.isEmpty(userName) || StringUtils.isEmpty(company)) {
throw new IotLicenseException(ResultCode.VALIDATE_FAILED.getCode(),ResultCode.VALIDATE_FAILED.getMsg());
}
User res = userMapper.getUserByUserName(userName);
if (res != null) {
throw new IotLicenseException(400,"用户名已存在");
throw new IotLicenseException(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMsg());
}
return userMapper.addUser(userName,company,password);
}
@Override
public boolean updateUser(int userId, String password) {
if (JsonUtil.isNull(String.valueOf(userId)) || JsonUtil.isNull(password)) {
return false;
if(userId == 0 || StringUtils.isEmpty(password)) {
throw new IotLicenseException(ResultCode.VALIDATE_FAILED.getCode(),ResultCode.VALIDATE_FAILED.getMsg());
}
return userMapper.updateUser(userId,password);
}
......
package iot.sixiang.license.util;
/**
* Created by m33 on 2022/6/8 9:57
*/
public class JsonUtil {
public static boolean isNull(String str) {
if(str == null || str.isEmpty()) {
return true;
}
return false;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment