|
@@ -0,0 +1,107 @@
|
|
|
+/**
|
|
|
+* @版权信息 (@copyright Copyright 2017-XXXX JDJR.COM All Right Reserved);
|
|
|
+* @see
|
|
|
+* @author 于海涛 京东金融【技术研发部-证券及营销平台研发部-营销平台研发部】
|
|
|
+* @version 1.0
|
|
|
+* @date 2018年6月7日
|
|
|
+*/
|
|
|
+
|
|
|
+package com.zskk.shop.controller;
|
|
|
+
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.boot.autoconfigure.web.AbstractErrorController;
|
|
|
+import org.springframework.boot.autoconfigure.web.ErrorAttributes;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
+import org.springframework.web.servlet.ModelAndView;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.zskk.shop.exception.ErrorConstant;
|
|
|
+import com.zskk.shop.exception.ZSKKError;
|
|
|
+import com.zskk.shop.exception.ZSKKException;
|
|
|
+import com.zskk.shop.utils.LogUtil;
|
|
|
+
|
|
|
+@Controller
|
|
|
+public class ShopErrorController extends AbstractErrorController {
|
|
|
+ @Value("${zskk.uccenter}")
|
|
|
+ private String uccenter;
|
|
|
+
|
|
|
+ @Value("${zskk.myhost}")
|
|
|
+ private String myhost;
|
|
|
+
|
|
|
+ public ShopErrorController(ErrorAttributes errorAttributes) {
|
|
|
+ super(errorAttributes);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getErrorPath() {
|
|
|
+ return "error";
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping(produces = "text/html")
|
|
|
+ public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
|
|
|
+ Map<String, Object> error = this.getErrorAttributes(request, Boolean.TRUE);
|
|
|
+ String exception = (String)error.get("exception");
|
|
|
+ String message = (String)error.get("message");
|
|
|
+ if (ZSKKException.class.getName().equals(exception)){
|
|
|
+ ZSKKError errorObj = JSON.parseObject(message, ZSKKError.class);
|
|
|
+ if (errorObj.getCode() == ErrorConstant.NO_LOGIN_ERROR.getCode()){
|
|
|
+ return new ModelAndView("redirect:" + uccenter + "/login/authorize?redirect=" + this.getRequestUrl(request));
|
|
|
+ }else{
|
|
|
+ ModelAndView model = new ModelAndView(this.getErrorPath());
|
|
|
+ model.addObject("code", errorObj.getCode());
|
|
|
+ model.addObject("msg", errorObj.getMsg());
|
|
|
+ return model;
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ ModelAndView model = new ModelAndView(this.getErrorPath());
|
|
|
+ model.addObject("code", ErrorConstant.SERVER_ERROR.getCode());
|
|
|
+ model.addObject("msg", ErrorConstant.SERVER_ERROR.getMsg());
|
|
|
+ return model;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping
|
|
|
+ @ResponseBody
|
|
|
+ public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
|
|
|
+ Map<String, Object> error = this.getErrorAttributes(request, Boolean.TRUE);
|
|
|
+ String exception = (String)error.get("exception");
|
|
|
+ String message = (String)error.get("message");
|
|
|
+ Map<String, Object> body = new HashMap<>();
|
|
|
+ if (ZSKKException.class.getName().equals(exception)){
|
|
|
+ ZSKKError errorObj = JSON.parseObject(message, ZSKKError.class);
|
|
|
+ body.put("code", errorObj.getCode());
|
|
|
+ body.put("msg", errorObj.getMsg());
|
|
|
+ }else{
|
|
|
+ body.put("code", ErrorConstant.SERVER_ERROR.getCode());
|
|
|
+ body.put("msg", ErrorConstant.SERVER_ERROR.getMsg());
|
|
|
+ }
|
|
|
+ return new ResponseEntity<Map<String,Object>>(body, HttpStatus.OK);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getRequestUrl(HttpServletRequest request){
|
|
|
+ try{
|
|
|
+ String path = request.getRequestURI();
|
|
|
+ String query = request.getQueryString();
|
|
|
+ String url = myhost + path + (query != null? "?" + query: "");
|
|
|
+ return URLEncoder.encode(url, "UTF-8");
|
|
|
+ }catch (Exception e) {
|
|
|
+ LogUtil.sysError(e.getMessage(), e);
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|