错误消息机制问题重新处理

This commit is contained in:
Chopper
2021-05-18 15:40:26 +08:00
parent 09cb489dc9
commit 0528a1e1ca
73 changed files with 309 additions and 266 deletions

View File

@@ -259,6 +259,7 @@ public enum ResultCode {
* OSS
*/
OSS_NOT_EXIST(80201,"OSS未配置"),
OSS_EXCEPTION(80202,"文件上传失败,请稍后重试"),
/**
* 验证码
@@ -266,7 +267,17 @@ public enum ResultCode {
VERIFICATION_SEND_SUCCESS(80301,"短信验证码,发送成功"),
VERIFICATION_ERROR(80302,"验证失败"),
VERIFICATION_SMS_ERROR(80303,"短信验证码错误,请重新校验"),
VERIFICATION_SMS_EXPIRED_ERROR(80304,"验证码已失效,请重新校验")
VERIFICATION_SMS_EXPIRED_ERROR(80304,"验证码已失效,请重新校验"),
/**
* 配置错误
*/
ALIPAY_NOT_SETTING(80401,"支付宝支付未配置"),
ALIPAY_EXCEPTION(80402,"支付宝支付错误,请稍后重试"),
ALIPAY_PARAMS_EXCEPTION(80403,"支付宝参数异常"),
WECHAT_PAY_NOT_SETTING(80402,"微信支付未配置"),
;
private final Integer code;

View File

@@ -1,7 +1,6 @@
package cn.lili.common.exception;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
@@ -23,7 +22,7 @@ import java.util.List;
*/
@RestControllerAdvice
@Slf4j
public class GlobalControllerExceptionHandler {
public class GlobalControllerExceptionHandler {
/**
* 如果超过长度,则前后段交互体验不佳,使用默认错误消息
@@ -44,9 +43,9 @@ public class GlobalControllerExceptionHandler {
//如果是自定义异常,则获取异常,返回自定义错误消息
if (e instanceof ServiceException) {
ResultCode resultCode=((ServiceException) e).getResultCode();
ResultCode resultCode = ((ServiceException) e).getResultCode();
if (resultCode != null) {
return ResultUtil.error(resultCode.code(), resultCode.message());
throw new ServiceException(resultCode);
}
}
@@ -55,7 +54,7 @@ public class GlobalControllerExceptionHandler {
if (e != null && e.getMessage() != null && e.getMessage().length() < MAX_LENGTH) {
errorMsg = e.getMessage();
}
return ResultUtil.error(400, errorMsg);
throw new ServiceException(ResultCode.ERROR, errorMsg);
}
@ExceptionHandler(RuntimeException.class)
@@ -64,7 +63,7 @@ public class GlobalControllerExceptionHandler {
log.error("全局异常[RuntimeException]:", e);
return ResultUtil.error(400, "服务器异常,请稍后重试");
throw new ServiceException(ResultCode.ERROR, "服务器异常,请稍后重试");
}
// /**
@@ -101,9 +100,9 @@ public class GlobalControllerExceptionHandler {
BindException exception = (BindException) e;
List<FieldError> fieldErrors = exception.getBindingResult().getFieldErrors();
for (FieldError error : fieldErrors) {
return ResultUtil.error(400,error.getDefaultMessage());
throw new ServiceException(ResultCode.ERROR, error.getDefaultMessage());
}
return ResultUtil.error(ResultCode.ERROR);
throw new ServiceException(ResultCode.ERROR);
}
}

View File

@@ -27,4 +27,9 @@ public class ServiceException extends RuntimeException {
this.resultCode = resultCode;
}
public ServiceException(ResultCode resultCode, String message) {
this.resultCode = resultCode;
this.msg = message;
}
}

View File

@@ -17,10 +17,6 @@ public class ResultUtil<T> {
* 正常响应
*/
private static final Integer SUCCESS = 200;
/**
* 业务异常
*/
private static final Integer ERROR = 400;
/**
@@ -44,30 +40,6 @@ public class ResultUtil<T> {
return this.resultMessage;
}
/**
* 服务器异常 追加状态码
*
* @param resultCode 返回码
*/
public ResultMessage<T> setErrorMsg(ResultCode resultCode) {
this.resultMessage.setSuccess(false);
this.resultMessage.setMessage(resultCode.message());
this.resultMessage.setCode(resultCode.code());
return this.resultMessage;
}
/**
* 服务器异常 追加状态码
* @param code 状态码
* @param msg 返回消息
*/
public ResultMessage<T> setErrorMsg(Integer code, String msg) {
this.resultMessage.setSuccess(false);
this.resultMessage.setMessage(msg);
this.resultMessage.setCode(code);
return this.resultMessage;
}
/**
* 返回成功消息
@@ -95,20 +67,4 @@ public class ResultUtil<T> {
public static <T> ResultMessage<T> success(ResultCode resultCode) {
return new ResultUtil<T>().setSuccessMsg(resultCode);
}
/**
* 返回失败
* @param resultCode 返回状态码
*/
public static <T> ResultMessage<T> error(ResultCode resultCode) {
return new ResultUtil<T>().setErrorMsg(resultCode);
}
/**
* 返回失败
* @param code 状态码
* @param msg 返回消息
*/
public static <T> ResultMessage<T> error(Integer code, String msg) {
return new ResultUtil<T>().setErrorMsg(code, msg);
}
}

View File

@@ -60,7 +60,7 @@ public class VueCodeGenerator {
try {
result = generateClassData(path);
} catch (Exception e) {
return ResultUtil.error(ResultCode.ERROR);
throw new ServiceException(ResultCode.ERROR);
}
return ResultUtil.data(result);
}

View File

@@ -3,6 +3,7 @@ package cn.lili.modules.connect.util;
import cn.hutool.json.JSONUtil;
import cn.lili.common.cache.Cache;
import cn.lili.common.cache.CachePrefix;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.token.Token;
import cn.lili.common.utils.ResultUtil;
@@ -27,7 +28,6 @@ import cn.lili.modules.system.entity.dto.connect.dto.QQConnectSettingItem;
import cn.lili.modules.system.entity.dto.connect.dto.WechatConnectSettingItem;
import cn.lili.modules.system.entity.enums.SettingEnum;
import cn.lili.modules.system.service.SettingService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -83,12 +83,12 @@ public class ConnectUtil {
token = connectService.unionLoginCallback(type, authUser, callback.getState());
resultMessage = ResultUtil.data(token);
} catch (ServiceException e) {
resultMessage = ResultUtil.error(400,e.getMessage());
throw new ServiceException(ResultCode.ERROR, e.getMessage());
}
}
//否则录入响应结果,等待前端获取信息
else {
resultMessage = ResultUtil.error(400,response.getMsg());
throw new ServiceException(ResultCode.ERROR, response.getMsg());
}
//缓存写入登录结果300秒有效
cache.put(CachePrefix.CONNECT_RESULT.getPrefix() + callback.getCode(), resultMessage, 300L);

View File

@@ -118,7 +118,7 @@ public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> impleme
@Override
public Token usernameLogin(String username, String password) {
Member member = this.findByUsername(username);
Member member = this.findMember(username);
//判断用户是否存在
if (member == null || !member.getDisabled()) {
throw new ServiceException(ResultCode.USER_NOT_EXIST);
@@ -134,7 +134,7 @@ public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> impleme
@Override
public Token usernameStoreLogin(String username, String password) {
Member member = this.findByUsername(username);
Member member = this.findMember(username);
//判断用户是否存在
if (member == null || !member.getDisabled()) {
throw new ServiceException(ResultCode.USER_NOT_EXIST);
@@ -156,6 +156,18 @@ public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> impleme
return storeTokenGenerate.createToken(member.getUsername(), false);
}
/**
* 传递手机号或者用户名
*
* @param userName
* @return
*/
private Member findMember(String userName) {
QueryWrapper<Member> queryWrapper = new QueryWrapper();
queryWrapper.eq("username", userName).or().eq("mobile", userName);
return memberMapper.selectOne(queryWrapper);
}
@Override
public Token autoRegister(ConnectAuthUser authUser) {
@@ -347,7 +359,7 @@ public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> impleme
queryWrapper.like(StringUtils.isNotBlank(memberSearchVO.getMobile()), "mobile", memberSearchVO.getMobile());
//按照会员状态查询
queryWrapper.eq(StringUtils.isNotBlank(memberSearchVO.getDisabled()), "disabled",
memberSearchVO.getDisabled().equals(SwitchEnum.OPEN.name())?1:0);
memberSearchVO.getDisabled().equals(SwitchEnum.OPEN.name()) ? 1 : 0);
queryWrapper.orderByDesc("create_time");
return this.page(PageUtil.initPage(page), queryWrapper);
}

View File

@@ -71,7 +71,7 @@ public class AliPayPlugin implements Payment {
CashierParam cashierParam = cashierSupport.cashierParam(payParam);
//请求订单编号
String outTradeNo = SnowFlake.getIdStr();
//准备支付参数
AlipayTradeWapPayModel payModel = new AlipayTradeWapPayModel();
payModel.setBody(cashierParam.getTitle());
payModel.setSubject(cashierParam.getDetail());
@@ -86,7 +86,8 @@ public class AliPayPlugin implements Payment {
AliPayRequest.wapPay(response, payModel, callbackUrl(apiProperties.getBuyer(), PaymentMethodEnum.ALIPAY),
notifyUrl(apiProperties.getBuyer(), PaymentMethodEnum.ALIPAY));
} catch (Exception e) {
e.printStackTrace();
log.error("H5支付异常", e);
throw new ServiceException(ResultCode.ALIPAY_EXCEPTION);
}
return null;
}
@@ -121,8 +122,11 @@ public class AliPayPlugin implements Payment {
String orderInfo = AliPayRequest.appPayToResponse(payModel, notifyUrl(apiProperties.getBuyer(), PaymentMethodEnum.ALIPAY)).getBody();
return ResultUtil.data(orderInfo);
} catch (AlipayApiException e) {
e.printStackTrace();
return ResultUtil.error(ResultCode.PAY_ERROR);
log.error("支付宝支付异常:", e);
throw new ServiceException(ResultCode.ALIPAY_EXCEPTION);
} catch (Exception e) {
log.error("支付业务异常:", e);
throw new ServiceException(ResultCode.PAY_ERROR);
}
}
@@ -151,9 +155,9 @@ public class AliPayPlugin implements Payment {
JSONObject jsonObject = JSONObject.parseObject(resultStr);
return ResultUtil.data(jsonObject.getJSONObject("alipay_trade_precreate_response").getString("qr_code"));
} catch (Exception e) {
e.printStackTrace();
log.error("支付业务异常:", e);
throw new ServiceException(ResultCode.PAY_ERROR);
}
return null;
}
@@ -169,6 +173,7 @@ public class AliPayPlugin implements Payment {
model.setRefundAmount(refundLog.getTotalAmount() + "");
model.setRefundReason(refundLog.getRefundReason());
model.setOutRequestNo(refundLog.getOutOrderNo());
//交互退款
try {
AlipayTradeRefundResponse alipayTradeRefundResponse = AliPayApi.tradeRefundToResponse(model);
log.error("支付宝退款,参数:{},支付宝响应:{}", JSONUtil.toJsonStr(model), JSONUtil.toJsonStr(alipayTradeRefundResponse));
@@ -180,7 +185,8 @@ public class AliPayPlugin implements Payment {
}
refundLogService.save(refundLog);
} catch (Exception e) {
e.printStackTrace();
log.error("支付退款异常:", e);
throw new ServiceException(ResultCode.PAY_ERROR);
}
}
@@ -192,9 +198,11 @@ public class AliPayPlugin implements Payment {
if (StringUtils.isNotEmpty(refundLog.getPaymentReceivableNo())) {
model.setTradeNo(refundLog.getPaymentReceivableNo());
} else {
throw new ServiceException(ResultCode.ERROR);
log.error("退款时,支付参数为空导致异常:{}", refundLog);
throw new ServiceException(ResultCode.ALIPAY_PARAMS_EXCEPTION);
}
try {
//与阿里进行交互
AlipayTradeCancelResponse alipayTradeCancelResponse = AliPayApi.tradeCancelToResponse(model);
if (alipayTradeCancelResponse.isSuccess()) {
refundLog.setIsRefund(true);
@@ -204,7 +212,7 @@ public class AliPayPlugin implements Payment {
}
refundLogService.save(refundLog);
} catch (Exception e) {
e.printStackTrace();
log.error("支付宝退款异常",e);
}
}
@@ -256,8 +264,7 @@ public class AliPayPlugin implements Payment {
log.info("支付回调通知:支付失败-参数:{}", map);
}
} catch (AlipayApiException e) {
e.printStackTrace();
throw new ServiceException("支付回调通知异常");
log.error("支付回调通知异常", e);
}
}
@@ -272,7 +279,7 @@ public class AliPayPlugin implements Payment {
if (setting != null) {
return JSONUtil.toBean(setting.getSettingValue(), AlipayPaymentSetting.class);
}
throw new ServiceException("支付未配置");
throw new ServiceException(ResultCode.ALIPAY_NOT_SETTING);
}

View File

@@ -138,7 +138,7 @@ public class WechatPlugin implements Payment {
return ResultUtil.data(JSONUtil.toJsonStr(response.getBody()));
} catch (Exception e) {
e.printStackTrace();
return ResultUtil.error(ResultCode.PAY_ERROR);
throw new ServiceException(ResultCode.PAY_ERROR);
}
}
@@ -206,10 +206,10 @@ public class WechatPlugin implements Payment {
return ResultUtil.data(map);
}
log.error("微信支付参数验证错误,请及时处理");
return ResultUtil.error(ResultCode.PAY_ERROR);
throw new ServiceException(ResultCode.PAY_ERROR);
} catch (Exception e) {
log.error("支付异常", e);
return ResultUtil.error(ResultCode.PAY_ERROR);
throw new ServiceException(ResultCode.PAY_ERROR);
}
}
@@ -269,10 +269,10 @@ public class WechatPlugin implements Payment {
return ResultUtil.data(map);
}
log.error("微信支付参数验证错误,请及时处理");
return ResultUtil.error(ResultCode.PAY_ERROR);
throw new ServiceException(ResultCode.PAY_ERROR);
} catch (Exception e) {
log.error("支付异常", e);
return ResultUtil.error(ResultCode.PAY_ERROR);
throw new ServiceException(ResultCode.PAY_ERROR);
}
}
@@ -324,14 +324,14 @@ public class WechatPlugin implements Payment {
return ResultUtil.data(new JSONObject(response.getBody()).getStr("code_url"));
} else {
log.error("微信支付参数验证错误,请及时处理");
return ResultUtil.error(ResultCode.PAY_ERROR);
throw new ServiceException(ResultCode.PAY_ERROR);
}
} catch (ServiceException e) {
log.error("支付异常", e);
return ResultUtil.error(ResultCode.PAY_ERROR);
throw new ServiceException(ResultCode.PAY_ERROR);
} catch (Exception e) {
log.error("支付异常", e);
return ResultUtil.error(ResultCode.PAY_ERROR);
throw new ServiceException(ResultCode.PAY_ERROR);
}
}
@@ -407,10 +407,10 @@ public class WechatPlugin implements Payment {
return ResultUtil.data(map);
}
log.error("微信支付参数验证错误,请及时处理");
return ResultUtil.error(ResultCode.PAY_ERROR);
throw new ServiceException(ResultCode.PAY_ERROR);
} catch (Exception e) {
log.error("支付异常", e);
return ResultUtil.error(ResultCode.PAY_ERROR);
throw new ServiceException(ResultCode.PAY_ERROR);
}
}

View File

@@ -82,7 +82,7 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService {
if (CharSequenceUtil.isNotEmpty(searchDTO.getKeyword())) {
cache.incrementScore(HotWordsRedisKeyEnum.SEARCH_HOT_WORD.name(), searchDTO.getKeyword());
}
NativeSearchQueryBuilder searchQueryBuilder = createSearchQueryBuilder(searchDTO, pageVo, false);
NativeSearchQueryBuilder searchQueryBuilder = createSearchQueryBuilder(searchDTO, pageVo, true);
NativeSearchQuery searchQuery = searchQueryBuilder.build();
log.info("searchGoods DSL:{}", searchQuery.getQuery());
@@ -260,10 +260,14 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService {
//分页
nativeSearchQueryBuilder.withPageable(pageable);
}
//查询参数非空判定
if (searchDTO != null) {
//过滤条件
BoolQueryBuilder filterBuilder = QueryBuilders.boolQuery();
//查询条件
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
//对查询条件进行处理
this.commonSearch(filterBuilder, queryBuilder, searchDTO, isAggregation);
// 未上架的商品不显示
@@ -279,6 +283,7 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService {
this.keywordSearch(filterBuilder, queryBuilder, searchDTO.getKeyword(), isAggregation);
}
//如果是聚合查询
if (isAggregation) {
nativeSearchQueryBuilder.withQuery(filterBuilder);
} else {
@@ -297,23 +302,36 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService {
return nativeSearchQueryBuilder;
}
/**
* 查询属性处理
* @param filterBuilder
* @param queryBuilder
* @param searchDTO
* @param isAggregation
*/
private void commonSearch(BoolQueryBuilder filterBuilder, BoolQueryBuilder queryBuilder, EsGoodsSearchDTO searchDTO, boolean isAggregation) {
//品牌判定
if (CharSequenceUtil.isNotEmpty(searchDTO.getBrandId())) {
String[] brands = searchDTO.getBrandId().split("@");
filterBuilder.must(QueryBuilders.termsQuery("brandId", brands));
}
//规格项判定
if (searchDTO.getNameIds() != null && !searchDTO.getNameIds().isEmpty()) {
filterBuilder.must(QueryBuilders.nestedQuery(ATTR_PATH, QueryBuilders.termsQuery("attrList.nameId", searchDTO.getNameIds()), ScoreMode.None));
}
//分类判定
if (CharSequenceUtil.isNotEmpty(searchDTO.getCategoryId())) {
filterBuilder.must(QueryBuilders.wildcardQuery("categoryPath", "*" + searchDTO.getCategoryId() + "*"));
}
//店铺分类判定
if (CharSequenceUtil.isNotEmpty(searchDTO.getStoreCatId())) {
filterBuilder.must(QueryBuilders.wildcardQuery("storeCategoryPath", "*" + searchDTO.getStoreCatId() + "*"));
}
//店铺判定
if (CharSequenceUtil.isNotEmpty(searchDTO.getStoreId())) {
filterBuilder.filter(QueryBuilders.termQuery("storeId", searchDTO.getStoreId()));
}
//属性判定
if (CharSequenceUtil.isNotEmpty(searchDTO.getProp())) {
String[] props = searchDTO.getProp().split("@");
List<String> nameList = new ArrayList<>();
@@ -340,6 +358,7 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService {
searchDTO.getNotShowCol().put(ATTR_NAME_KEY, nameList);
searchDTO.getNotShowCol().put(ATTR_VALUE_KEY, valueList);
}
//价格区间判定
if (CharSequenceUtil.isNotEmpty(searchDTO.getPrice())) {
String[] prices = searchDTO.getPrice().split("_");
if(prices.length==0){
@@ -355,20 +374,33 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService {
}
}
/**
* 关键字查询处理
* @param filterBuilder
* @param queryBuilder
* @param keyword
* @param isAggregation
*/
private void keywordSearch(BoolQueryBuilder filterBuilder, BoolQueryBuilder queryBuilder, String keyword, boolean isAggregation) {
List<FunctionScoreQueryBuilder.FilterFunctionBuilder> filterFunctionBuilders = new ArrayList<>();
//商品名字匹配
filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.wildcardQuery("goodsName", "*" + keyword + "*"),
ScoreFunctionBuilders.weightFactorFunction(10)));
//属性匹配
filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.nestedQuery(ATTR_PATH, QueryBuilders.wildcardQuery(ATTR_VALUE, "*" + keyword + "*"), ScoreMode.None),
ScoreFunctionBuilders.weightFactorFunction(8)));
FunctionScoreQueryBuilder.FilterFunctionBuilder[] builders = new FunctionScoreQueryBuilder.FilterFunctionBuilder[filterFunctionBuilders.size()];
filterFunctionBuilders.toArray(builders);
FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(builders)
.scoreMode(FunctionScoreQuery.ScoreMode.SUM)
.setMinScore(2);
//聚合搜索则将结果放入过滤条件
if (isAggregation) {
filterBuilder.must(functionScoreQueryBuilder);
} else {
}
//否则放入查询条件
else {
queryBuilder.must(functionScoreQueryBuilder);
}
}