feat(商城): 代码优化

master
wayn 1 year ago
parent 31d266845b
commit 424306f776

@ -5,9 +5,11 @@ import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlParserUtils;
import com.wayn.common.constant.Constants;
import com.wayn.common.util.ServletUtils;
import com.wayn.common.util.http.HttpUtil;
import com.wayn.common.util.sql.SqlUtil;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@ -52,36 +54,35 @@ public class BaseController {
/**
*
*
* @param <T>
* @return
*/
protected <T> Page<T> getPage() {
//设置通用分页
try {
Integer pageNumber = ServletUtils.getParameterToInt(Constants.PAGE_NUMBER, "1");
Integer pageSize = ServletUtils.getParameterToInt(Constants.PAGE_SIZE, "10");
String sortName = ServletUtils.getParameter(Constants.SORT_NAME);
String sortOrder = ServletUtils.getParameter(Constants.SORT_ORDER);
Page<T> tPage = new Page<>(pageNumber, pageSize);
if (StringUtils.isNotEmpty(sortName)) {
String[] split = sortName.split(",");
for (String s : split) {
OrderItem orderItem = new OrderItem();
orderItem.setColumn(s.replaceAll("[A-Z]", "_$0").toLowerCase());
orderItem.setAsc(sortOrder == null || !sortOrder.startsWith(Constants.ORDER_DESC));
tPage.addOrder(orderItem);
}
// 设置通用分页
Integer pageNumber = ServletUtils.getParameterToInt(Constants.PAGE_NUMBER, "1");
Integer pageSize = ServletUtils.getParameterToInt(Constants.PAGE_SIZE, "10");
String sortName = ServletUtils.getParameter(Constants.SORT_NAME);
String sortOrder = ServletUtils.getParameter(Constants.SORT_ORDER);
Page<T> tPage = new Page<>(pageNumber, pageSize);
if (StringUtils.isNotEmpty(sortName)) {
sortName = SqlUtil.escapeOrderBySql(sortName);
sortOrder = SqlUtil.escapeOrderBySql(sortOrder);
String[] split = sortName.split(",");
for (String s : split) {
OrderItem orderItem = new OrderItem();
orderItem.setColumn(s.replaceAll("[A-Z]", "_$0").toLowerCase());
orderItem.setAsc(sortOrder == null || !sortOrder.startsWith(Constants.ORDER_DESC));
tPage.addOrder(orderItem);
}
return tPage;
} catch (Exception e) {
// log.error(e.getMessage(), e);
return getPage(1, 10);
}
return tPage;
}
/**
*
*
* @param pageNumber
* @param <T>
* @return
@ -95,8 +96,8 @@ public class BaseController {
*
* </p>
*
* @param pageNumber
* @param pageSize
* @param pageNumber
* @param pageSize
* @param <T>
* @return
*/
@ -147,7 +148,6 @@ public class BaseController {
* @param object
* @param format
* @return json
*
*/
protected String toJson(Object object, String format) {
if (format == null) {

@ -0,0 +1,25 @@
package com.wayn.common.convert;
import com.wayn.common.config.WaynConfig;
import com.wayn.common.core.domain.shop.vo.MallConfigResponseVO;
/**
* @author: waynaqua
* @date: 2023/11/13 23:10
*/
public class MallConfigConvert {
public static MallConfigResponseVO convertMallConfig() {
return MallConfigResponseVO.builder()
.freightLimit(WaynConfig.getFreightLimit())
.freightPrice(WaynConfig.getFreightPrice())
.mobileUrl(WaynConfig.getMobileUrl())
.email(WaynConfig.getEmail())
.name(WaynConfig.getName())
.unpaidOrderCancelDelayTime(WaynConfig.getUnpaidOrderCancelDelayTime())
.version(WaynConfig.getVersion())
.uploadDir(WaynConfig.getUploadDir())
.build();
}
}

@ -19,7 +19,7 @@ public class HomeIndexResponseVO implements Serializable {
private static final long serialVersionUID = -14732478530341760L;
private List<Banner> bannerList;
private List<Diamond> categoryList;
private List<Diamond> diamondList;
private List<Goods> newGoodsList;
private List<Goods> hotGoodsList;
}

@ -0,0 +1,60 @@
package com.wayn.common.core.domain.shop.vo;
import lombok.Builder;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
/**
* @author: waynaqua
* @date: 2023/11/13 23:08
*/
@Data
@Builder
public class MallConfigResponseVO implements Serializable {
@Serial
private static final long serialVersionUID = -4620022749379145372L;
/**
*
*/
private String uploadDir;
/**
*
*/
private String name;
/**
*
*/
private String version;
/**
*
*/
private String email;
/**
*
*/
private String adminUrl;
/**
*
*/
private String mobileUrl;
/**
*
*/
private Integer unpaidOrderCancelDelayTime;
/**
*
*/
private BigDecimal freightLimit;
/**
*
*/
private BigDecimal freightPrice;
}

@ -0,0 +1,20 @@
package com.wayn.common.core.domain.shop.vo;
import com.wayn.common.core.domain.shop.Goods;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.util.List;
/**
* @author: waynaqua
* @date: 2023/11/13 23:32
*/
@Data
public class RecommonGoodsResponseVO implements Serializable {
@Serial
private static final long serialVersionUID = 6261274861901027930L;
private List<Goods> data;
}

@ -0,0 +1,59 @@
package com.wayn.common.util.sql;
import cn.hutool.core.exceptions.UtilException;
import org.apache.commons.lang3.StringUtils;
/**
* sql
*/
public class SqlUtil {
/**
* sql
*/
public static String SQL_REGEX = "and |extractvalue|updatexml|exec |insert |select |delete |update |drop |count |chr |mid |master |truncate |char |declare |or |+|user()";
/**
* 线
*/
public static String SQL_PATTERN = "[a-zA-Z0-9_\\ \\,\\.]+";
/**
* orderBy
*/
private static final int ORDER_BY_MAX_LENGTH = 500;
/**
*
*/
public static String escapeOrderBySql(String value) {
if (StringUtils.isNotEmpty(value) && !isValidOrderBySql(value)) {
throw new UtilException("参数不符合规范,不能进行查询");
}
if (StringUtils.length(value) > ORDER_BY_MAX_LENGTH) {
throw new UtilException("参数已超过最大限制,不能进行查询");
}
return value;
}
/**
* order by
*/
public static boolean isValidOrderBySql(String value) {
return value.matches(SQL_PATTERN);
}
/**
* SQL
*/
public static void filterKeyword(String value) {
if (StringUtils.isEmpty(value)) {
return;
}
String[] sqlKeywords = StringUtils.split(SQL_REGEX, "\\|");
for (String sqlKeyword : sqlKeywords) {
if (StringUtils.indexOfIgnoreCase(value, sqlKeyword) > -1) {
throw new UtilException("参数存在SQL注入风险");
}
}
}
}

@ -7,7 +7,6 @@ import com.wayn.common.util.R;
import com.wayn.mobile.api.service.IHomeService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@ -35,7 +34,7 @@ public class HomeController extends BaseController {
*/
@GetMapping("mallConfig")
public R mallConfig() {
return iHomeService.mallConfig();
return R.success(iHomeService.mallConfig());
}
/**
@ -43,10 +42,10 @@ public class HomeController extends BaseController {
*
* @return R
*/
@GetMapping("goodsList")
public R getGoodsList() {
@GetMapping("recommonGoodsList")
public R recommonGoodsList() {
Page<Goods> page = getPage();
return iHomeService.listGoodsPage(page);
return R.success(iHomeService.listGoodsPage(page));
}
}

@ -3,8 +3,12 @@ package com.wayn.mobile.api.service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wayn.common.core.domain.shop.Goods;
import com.wayn.common.core.domain.shop.vo.HomeIndexResponseVO;
import com.wayn.common.core.domain.shop.vo.MallConfigResponseVO;
import com.wayn.common.core.domain.shop.vo.RecommonGoodsResponseVO;
import com.wayn.common.util.R;
import java.util.List;
public interface IHomeService {
/**
@ -22,12 +26,12 @@ public interface IHomeService {
* @param page
* @return r
*/
R listGoodsPage(Page<Goods> page);
RecommonGoodsResponseVO listGoodsPage(Page<Goods> page);
/**
*
*
* @return r
*/
R mallConfig();
MallConfigResponseVO mallConfig();
}

@ -3,15 +3,16 @@ package com.wayn.mobile.api.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wayn.common.config.WaynConfig;
import com.wayn.common.convert.MallConfigConvert;
import com.wayn.common.core.domain.shop.Banner;
import com.wayn.common.core.domain.shop.Diamond;
import com.wayn.common.core.domain.shop.Goods;
import com.wayn.common.core.domain.shop.vo.HomeIndexResponseVO;
import com.wayn.common.core.domain.shop.vo.MallConfigResponseVO;
import com.wayn.common.core.domain.shop.vo.RecommonGoodsResponseVO;
import com.wayn.common.core.service.shop.IBannerService;
import com.wayn.common.core.service.shop.IDiamondService;
import com.wayn.common.core.service.shop.IGoodsService;
import com.wayn.common.util.R;
import com.wayn.mobile.api.service.IHomeService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@ -47,7 +48,7 @@ public class IHomeServiceImpl implements IHomeService {
.thenAccept(responseVO::setBannerList);
CompletableFuture<Void> f2 = CompletableFuture.supplyAsync(
() -> iDiamondService.list(Wrappers.lambdaQuery(Diamond.class).orderByAsc(Diamond::getSort).last("limit 10")), commonThreadPoolTaskExecutor)
.thenAccept(responseVO::setCategoryList);
.thenAccept(responseVO::setDiamondList);
CompletableFuture<Void> f3 = CompletableFuture.supplyAsync(
() -> iGoodsService.selectHomeIndexGoods(Goods.builder().isNew(true).build()), commonThreadPoolTaskExecutor)
.thenAccept(responseVO::setNewGoodsList);
@ -67,14 +68,17 @@ public class IHomeServiceImpl implements IHomeService {
}
@Override
public R listGoodsPage(Page<Goods> page) {
public RecommonGoodsResponseVO listGoodsPage(Page<Goods> page) {
RecommonGoodsResponseVO responseVO = new RecommonGoodsResponseVO();
IPage<Goods> goodsIPage = iGoodsService.listPage(page, new Goods());
return R.success().add("data", goodsIPage.getRecords());
responseVO.setData(goodsIPage.getRecords());
return responseVO;
}
@Override
public R mallConfig() {
return R.success().add("freightLimit", WaynConfig.getFreightLimit());
public MallConfigResponseVO mallConfig() {
return MallConfigConvert.convertMallConfig();
}
}

Loading…
Cancel
Save