Commit d0a8e3fc authored by Quxl's avatar Quxl

优化消息提醒

parent 8543f53a
......@@ -14,21 +14,29 @@ import com.egolm.common.bean.Rjx;
@Component
public class ExceptionHandler implements HandlerExceptionResolver {
private Logger logger = Logger.getLogger(ExceptionHandler.class);
private static final Logger logger = Logger.getLogger(ExceptionHandler.class);
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
logger.error("", ex);
boolean displayInLog = true;
ModelAndView mav = new ModelAndView(new MappingJackson2JsonView());
try {
response.setContentType("application/json");
if (ex instanceof XException) {
mav.addAllObjects(((XException) ex).getRjx());
Rjx rjx = ((XException) ex).getRjx();
mav.addAllObjects(rjx);
if(rjx.getCode() == 200) {
displayInLog = false;
}
} else {
mav.addAllObjects(Rjx.jsonErr().setMessage("未处理异常[" + ex.getClass().getName() + ":" + ex.getMessage() + "]"));
}
} catch (Exception e) {
mav.addAllObjects(Rjx.jsonErr().setMessage("异常处理失败"));
} finally {
if(displayInLog) {
logger.error("", ex);
}
}
return mav;
}
......
......@@ -34,10 +34,12 @@ public class XException extends RuntimeException {
return rjx;
}
public static void assertNotBlank(Object obj, String message) {
public static void assertNotBlank(String message, Object... objs) {
for(Object obj : objs) {
if(obj == null || obj.toString().trim().equals("")) {
throw new XException((message == null || message.trim().equals("")) ? "对象不能为空" : message);
}
}
}
}
......@@ -7,6 +7,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.egolm.common.bean.Rjx;
import com.egolm.film.common.Messages;
import com.egolm.film.config.XException;
import com.egolm.film.login.service.AdminTokenService;
import com.egolm.film.model.LoginToken;
......@@ -22,13 +23,15 @@ public class AdminLoginController {
@Autowired
private AdminTokenService tokenService;
@Autowired
private Messages messages;
@ResponseBody
@PostMapping("login")
@ApiOperation("登陆")
public Object login(String username, String password) {
XException.assertNotBlank(username, "用户名不能为空");
XException.assertNotBlank(password, "密码不能为空");
XException.assertNotBlank(messages.get("sys.err.user_pwd_null"), username, password);
if(tokenService.isLogin()) {
throw new XException("用户已登录", 200);
} else {
......@@ -36,7 +39,7 @@ public class AdminLoginController {
if(token != null) {
return Rjx.jsonOk().setData(token);
} else {
throw new XException("用户名或密码错误");
throw new XException(messages.get("sys.err.user_or_pwd"));
}
}
}
......
......@@ -7,6 +7,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.egolm.common.bean.Rjx;
import com.egolm.film.common.Messages;
import com.egolm.film.config.XException;
import com.egolm.film.login.service.MemberTokenService;
import com.egolm.film.model.LoginToken;
......@@ -19,6 +20,9 @@ import io.swagger.annotations.ApiOperation;
@RequestMapping("member")
public class MemberLoginController {
@Autowired
private Messages messages;
@Autowired
private MemberTokenService tokenService;
......@@ -26,14 +30,15 @@ public class MemberLoginController {
@PostMapping("login")
@ApiOperation("登陆")
public Object login(String username, String password) {
XException.assertNotBlank(messages.get("sys.err.user_pwd_null"), username, password);
if(tokenService.isLogin()) {
throw new XException("用户已登录", 200);
throw new XException("", 200);
} else {
LoginToken token = tokenService.doLogin(username, password);
if(token != null) {
return Rjx.jsonOk().setData(token);
} else {
throw new XException("用户名或密码错误");
throw new XException(messages.get("sys.err.user_or_pwd"));
}
}
}
......
......@@ -7,6 +7,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.egolm.common.bean.Rjx;
import com.egolm.film.common.Messages;
import com.egolm.film.config.XException;
import com.egolm.film.login.service.UserTokenService;
import com.egolm.film.model.LoginToken;
......@@ -22,18 +23,22 @@ public class UserLoginController {
@Autowired
private UserTokenService tokenService;
@Autowired
private Messages messages;
@ResponseBody
@PostMapping("login")
@ApiOperation("登陆")
public Object login(String username, String password) {
XException.assertNotBlank(messages.get("sys.err.user_pwd_null"), username, password);
if(tokenService.isLogin()) {
throw new XException("用户已登录", 200);
throw new XException("", 200);
} else {
LoginToken token = tokenService.doLogin(username, password);
if(token != null) {
return Rjx.jsonOk().setData(token);
} else {
throw new XException("用户名或密码错误");
throw new XException(messages.get("sys.err.user_or_pwd"));
}
}
}
......
......@@ -46,12 +46,12 @@ public class MemberTokenServiceImpl implements MemberTokenService {
throw new XException(messages.get("sys.err"));
}
} else if(list.size() == 0) {
throw new XException(messages.get("sys.err.user_or_pwd"));
throw new XException(messages.get("sys.err.user_pwd_err"));
} else {
throw new XException(messages.get("sys.err"));
}
} else {
throw new XException(messages.get("sys.err.user_or_pwd"));
throw new XException(messages.get("sys.err.user_pwd_err"));
}
}
......
......@@ -3,7 +3,8 @@ email.getPassword.content={0}, \u60A8\u597D, \u60A8\u7684\u65B0\u5BC6\u7801\u662
sys.err=\u7CFB\u7EDF\u9519\u8BEF
sys.err.user_disabled=\u7528\u6237\u5DF2\u7ECF\u7981\u7528
sys.err.user_or_pwd=\u7528\u6237\u6216\u5BC6\u7801\u9519\u8BEF
sys.err.user_pwd_err=\u7528\u6237\u6216\u5BC6\u7801\u9519\u8BEF
sys.err.user_pwd_null=\u7528\u6237\u540D\u548C\u5BC6\u7801\u4E0D\u80FD\u4E3A\u7A7A
msg.hello=\u60A8\u597D
msg.your_pwd=\u60A8\u7684\u5BC6\u7801\u662F
\ No newline at end of file
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