package com.yunhai.bschclassmanage.controller; import com.yunhai.bschclassmanage.model.vo.PageVO; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; @Slf4j public class BaseController { /** * 根据请求参数构建PageVO对象,设置当前页和每页大小,并提供默认值。 * @param page 页码字符串(从1开始) * @param pageSize 每页大小字符串 * @return PageVO对象 */ protected PageVO getPageVO(String page, String pageSize) { int defaultPage = 1; int defaultSize = 10; int maxSize = 100; // 可配置 int current = defaultPage; int size = defaultSize; if (StringUtils.isNumeric(page)) { try { current = Integer.parseInt(page); if (current < 1) { current = defaultPage; } } catch (NumberFormatException ignored) { // 保持默认 } } if (StringUtils.isNumeric(pageSize)) { try { size = Integer.parseInt(pageSize); if (size < 1) { size = defaultSize; } else if (size > maxSize) { size = maxSize; } } catch (NumberFormatException ignored) { // 保持默认 } } PageVO pageVO = new PageVO(); pageVO.setCurrent(current); pageVO.setPageSize(size); return pageVO; } /** * 安全地将字符串转换为Integer,若转换失败返回null。 * @param str 待转换字符串 * @return Integer值或null */ public static Integer getIntByString(String str) { if (str == null || str.trim().isEmpty() || "null".equalsIgnoreCase(str.trim())) { return null; } try { return Integer.parseInt(str.trim()); } catch (NumberFormatException e) { return null; // 静默失败,不抛异常 } } }