mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-14 10:57:19 +08:00
重构:初步封装后端 CRUD 公共组件(BaseController、BaseService、BaseServiceImpl)
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.common.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 增删改查请求映射器注解
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2023/1/27 9:54
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface CrudRequestMapping {
|
||||
|
||||
/**
|
||||
* 路径映射 URI(等同于:@RequestMapping("/foo1"))
|
||||
*/
|
||||
String value() default "";
|
||||
|
||||
/**
|
||||
* API 列表
|
||||
*/
|
||||
Api[] api() default Api.ALL;
|
||||
|
||||
/**
|
||||
* API 枚举
|
||||
*/
|
||||
enum Api {
|
||||
/**
|
||||
* 所有 API
|
||||
*/
|
||||
ALL,
|
||||
/**
|
||||
* 分页
|
||||
*/
|
||||
PAGE,
|
||||
/**
|
||||
* 列表
|
||||
*/
|
||||
LIST,
|
||||
/**
|
||||
* 详情
|
||||
*/
|
||||
DETAIL,
|
||||
/**
|
||||
* 新增
|
||||
*/
|
||||
CREATE,
|
||||
/**
|
||||
* 修改
|
||||
*/
|
||||
UPDATE,
|
||||
/**
|
||||
* 删除
|
||||
*/
|
||||
DELETE,;
|
||||
}
|
||||
}
|
@@ -0,0 +1,149 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.common.base;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.enums.ParameterIn;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import top.charles7c.cnadmin.common.model.query.PageQuery;
|
||||
import top.charles7c.cnadmin.common.model.vo.PageInfo;
|
||||
import top.charles7c.cnadmin.common.model.vo.R;
|
||||
|
||||
/**
|
||||
* 控制器基类
|
||||
*
|
||||
* @param <S>
|
||||
* 业务接口
|
||||
* @param <V>
|
||||
* 列表信息
|
||||
* @param <D>
|
||||
* 详情信息
|
||||
* @param <Q>
|
||||
* 查询条件
|
||||
* @param <C>
|
||||
* 创建信息
|
||||
* @param <U>
|
||||
* 修改信息
|
||||
* @author Charles7c
|
||||
* @since 2023/1/26 10:45
|
||||
*/
|
||||
@NoArgsConstructor
|
||||
public abstract class BaseController<S extends BaseService<V, D, Q, C, U>, V, D, Q, C, U> {
|
||||
|
||||
@Autowired
|
||||
protected S baseService;
|
||||
|
||||
/**
|
||||
* 分页查询列表
|
||||
*
|
||||
* @param query
|
||||
* 查询条件
|
||||
* @param pageQuery
|
||||
* 分页查询条件
|
||||
* @return 分页列表信息
|
||||
*/
|
||||
@Operation(summary = "分页查询列表")
|
||||
@GetMapping
|
||||
protected R<PageInfo<V>> page(@Validated Q query, @Validated PageQuery pageQuery) {
|
||||
PageInfo<V> pageInfo = baseService.page(query, pageQuery);
|
||||
return R.ok(pageInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询列表
|
||||
*
|
||||
* @param query
|
||||
* 查询条件
|
||||
* @return 列表信息
|
||||
*/
|
||||
@Operation(summary = "查询列表")
|
||||
@GetMapping("/all")
|
||||
protected R<List<V>> list(@Validated Q query) {
|
||||
List<V> list = baseService.list(query);
|
||||
return R.ok(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看详情
|
||||
*
|
||||
* @param id
|
||||
* ID
|
||||
* @return 详情信息
|
||||
*/
|
||||
@Operation(summary = "查看详情")
|
||||
@Parameter(name = "id", description = "ID", in = ParameterIn.PATH)
|
||||
@GetMapping("/{id}")
|
||||
protected R<D> detail(@PathVariable Long id) {
|
||||
D detail = baseService.detail(id);
|
||||
return R.ok(detail);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增
|
||||
*
|
||||
* @param request
|
||||
* 创建信息
|
||||
* @return 自增 ID
|
||||
*/
|
||||
@Operation(summary = "新增")
|
||||
@PostMapping
|
||||
protected R<Long> create(@Validated @RequestBody C request) {
|
||||
Long id = baseService.create(request);
|
||||
return R.ok("新增成功", id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改
|
||||
*
|
||||
* @param id
|
||||
* ID
|
||||
* @param request
|
||||
* 修改信息
|
||||
* @return /
|
||||
*/
|
||||
@Operation(summary = "修改")
|
||||
@Parameter(name = "id", description = "ID", in = ParameterIn.PATH)
|
||||
@PutMapping("/{id}")
|
||||
protected R update(@PathVariable Long id, @Validated @RequestBody U request) {
|
||||
baseService.update(id, request);
|
||||
return R.ok("修改成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*
|
||||
* @param ids
|
||||
* ID 列表
|
||||
* @return /
|
||||
*/
|
||||
@Operation(summary = "删除")
|
||||
@Parameter(name = "ids", description = "ID 列表", in = ParameterIn.PATH)
|
||||
@DeleteMapping("/{ids}")
|
||||
protected R delete(@PathVariable List<Long> ids) {
|
||||
baseService.delete(ids);
|
||||
return R.ok("删除成功");
|
||||
}
|
||||
}
|
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.common.base;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
/**
|
||||
* 详情 VO 基类
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2023/1/26 10:40
|
||||
*/
|
||||
@Data
|
||||
public class BaseDetailVO extends BaseVO {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 创建人
|
||||
*/
|
||||
@JsonIgnore
|
||||
private Long createUser;
|
||||
|
||||
/**
|
||||
* 创建人
|
||||
*/
|
||||
@Schema(description = "创建人")
|
||||
private String createUserString;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@Schema(description = "创建时间")
|
||||
private LocalDateTime createTime;
|
||||
}
|
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.common.model.entity;
|
||||
package top.charles7c.cnadmin.common.base;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.common.base;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import top.charles7c.cnadmin.common.model.query.PageQuery;
|
||||
import top.charles7c.cnadmin.common.model.vo.PageInfo;
|
||||
|
||||
/**
|
||||
* 业务接口基类
|
||||
*
|
||||
* @param <V>
|
||||
* 列表信息
|
||||
* @param <D>
|
||||
* 详情信息
|
||||
* @param <Q>
|
||||
* 查询条件
|
||||
* @param <C>
|
||||
* 创建信息
|
||||
* @param <U>
|
||||
* 修改信息
|
||||
* @author Charles7c
|
||||
* @since 2023/1/26 16:54
|
||||
*/
|
||||
public interface BaseService<V, D, Q, C, U> {
|
||||
|
||||
/**
|
||||
* 分页查询列表
|
||||
*
|
||||
* @param query
|
||||
* 查询条件
|
||||
* @param pageQuery
|
||||
* 分页查询条件
|
||||
* @return 分页列表信息
|
||||
*/
|
||||
default PageInfo<V> page(Q query, PageQuery pageQuery) {
|
||||
return new PageInfo<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询列表
|
||||
*
|
||||
* @param query
|
||||
* 查询条件
|
||||
* @return 列表信息
|
||||
*/
|
||||
default List<V> list(Q query) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看详情
|
||||
*
|
||||
* @param id
|
||||
* ID
|
||||
* @return 详情信息
|
||||
*/
|
||||
default D detail(Long id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增
|
||||
*
|
||||
* @param request
|
||||
* 创建信息
|
||||
* @return 自增 ID
|
||||
*/
|
||||
default Long create(C request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改
|
||||
*
|
||||
* @param id
|
||||
* ID
|
||||
* @param request
|
||||
* 修改信息
|
||||
*/
|
||||
default void update(Long id, U request) {}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*
|
||||
* @param ids
|
||||
* ID 列表
|
||||
*/
|
||||
default void delete(List<Long> ids) {}
|
||||
}
|
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.common.base;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.model.query.PageQuery;
|
||||
import top.charles7c.cnadmin.common.model.vo.PageInfo;
|
||||
import top.charles7c.cnadmin.common.util.helper.QueryHelper;
|
||||
import top.charles7c.cnadmin.common.util.validate.CheckUtils;
|
||||
|
||||
/**
|
||||
* 业务实现基类
|
||||
*
|
||||
* @param <M>
|
||||
* Mapper 接口
|
||||
* @param <T>
|
||||
* 实体类
|
||||
* @author Charles7c
|
||||
* @since 2023/1/26 21:52
|
||||
*/
|
||||
public abstract class BaseServiceImpl<M extends BaseMapper<T>, T, V, D, Q, C, U> implements BaseService<V, D, Q, C, U> {
|
||||
|
||||
@Autowired
|
||||
protected M baseMapper;
|
||||
|
||||
protected Class<T> entityClass = currentEntityClass();
|
||||
protected Class<V> voClass = currentVoClass();
|
||||
protected Class<D> detailVoClass = currentDetailVoClass();
|
||||
|
||||
@Override
|
||||
public PageInfo<V> page(Q query, PageQuery pageQuery) {
|
||||
QueryWrapper<T> queryWrapper = QueryHelper.build(query);
|
||||
IPage<T> page = baseMapper.selectPage(pageQuery.toPage(), queryWrapper);
|
||||
return PageInfo.build(page, voClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<V> list(Q query) {
|
||||
QueryWrapper<T> queryWrapper = QueryHelper.build(query);
|
||||
List<T> entityList = baseMapper.selectList(queryWrapper);
|
||||
return BeanUtil.copyToList(entityList, voClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
public D detail(Long id) {
|
||||
T entity = baseMapper.selectById(id);
|
||||
CheckUtils.throwIfNull(entity, String.format("ID为 [%s] 的记录已不存在", id));
|
||||
return BeanUtil.copyProperties(entity, detailVoClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public abstract Long create(C request);
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void update(Long id, U request) {
|
||||
T entity = BeanUtil.copyProperties(request, entityClass);
|
||||
baseMapper.updateById(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void delete(List<Long> ids) {
|
||||
baseMapper.deleteBatchIds(ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取实体类 Class 对象
|
||||
*
|
||||
* @return 实体类 Class 对象
|
||||
*/
|
||||
protected Class<T> currentEntityClass() {
|
||||
return (Class<T>)ReflectionKit.getSuperClassGenericType(this.getClass(), BaseServiceImpl.class, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取列表信息类 Class 对象
|
||||
*
|
||||
* @return 列表信息类 Class 对象
|
||||
*/
|
||||
protected Class<V> currentVoClass() {
|
||||
return (Class<V>)ReflectionKit.getSuperClassGenericType(this.getClass(), BaseServiceImpl.class, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取详情信息类 Class 对象
|
||||
*
|
||||
* @return 详情信息类 Class 对象
|
||||
*/
|
||||
protected Class<D> currentDetailVoClass() {
|
||||
return (Class<D>)ReflectionKit.getSuperClassGenericType(this.getClass(), BaseServiceImpl.class, 3);
|
||||
}
|
||||
}
|
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.common.base;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
/**
|
||||
* VO 基类
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2023/1/26 10:40
|
||||
*/
|
||||
@Data
|
||||
public class BaseVO implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 修改人
|
||||
*/
|
||||
@JsonIgnore
|
||||
private Long updateUser;
|
||||
|
||||
/**
|
||||
* 修改人
|
||||
*/
|
||||
@Schema(description = "修改人")
|
||||
private String updateUserString;
|
||||
|
||||
/**
|
||||
* 修改时间
|
||||
*/
|
||||
@Schema(description = "修改时间")
|
||||
private LocalDateTime updateTime;
|
||||
}
|
@@ -31,7 +31,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.config.properties.ContinewAdminProperties;
|
||||
import top.charles7c.cnadmin.common.config.properties.ContiNewAdminProperties;
|
||||
|
||||
/**
|
||||
* 接口文档配置
|
||||
@@ -44,7 +44,7 @@ import top.charles7c.cnadmin.common.config.properties.ContinewAdminProperties;
|
||||
@ConditionalOnProperty(name = "springdoc.swagger-ui.enabled", havingValue = "true", matchIfMissing = true)
|
||||
public class SwaggerConfiguration {
|
||||
|
||||
private final ContinewAdminProperties continewAdminProperties;
|
||||
private final ContiNewAdminProperties continewAdminProperties;
|
||||
|
||||
/**
|
||||
* 接口文档配置
|
||||
|
@@ -33,9 +33,11 @@ import org.springframework.web.filter.CorsFilter;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||
|
||||
import top.charles7c.cnadmin.common.config.properties.CorsProperties;
|
||||
import top.charles7c.cnadmin.common.config.properties.LocalStorageProperties;
|
||||
import top.charles7c.cnadmin.common.handler.CrudRequestMappingHandlerMapping;
|
||||
|
||||
/**
|
||||
* Web MVC 配置
|
||||
@@ -108,4 +110,12 @@ public class WebMvcConfiguration implements WebMvcConfigurer {
|
||||
converters.add(0, mappingJackson2HttpMessageConverter);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* CRUD 请求映射器处理器映射器
|
||||
*/
|
||||
@Bean
|
||||
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
|
||||
return new CrudRequestMappingHandlerMapping();
|
||||
}
|
||||
}
|
||||
|
@@ -24,8 +24,8 @@ import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.base.BaseEntity;
|
||||
import top.charles7c.cnadmin.common.exception.ServiceException;
|
||||
import top.charles7c.cnadmin.common.model.entity.BaseEntity;
|
||||
import top.charles7c.cnadmin.common.util.helper.LoginHelper;
|
||||
|
||||
/**
|
||||
|
@@ -37,7 +37,7 @@ import cn.hutool.extra.spring.SpringUtil;
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "continew-admin")
|
||||
public class ContinewAdminProperties {
|
||||
public class ContiNewAdminProperties {
|
||||
|
||||
/**
|
||||
* 名称
|
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.common.config;
|
||||
package top.charles7c.cnadmin.common.config.threadpool;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.Executor;
|
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.common.handler;
|
||||
|
||||
import static top.charles7c.cnadmin.common.annotation.CrudRequestMapping.Api;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.annotation.CrudRequestMapping;
|
||||
import top.charles7c.cnadmin.common.util.ExceptionUtils;
|
||||
|
||||
/**
|
||||
* CRUD 请求映射器处理器映射器
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2023/1/27 10:30
|
||||
*/
|
||||
@NoArgsConstructor
|
||||
public class CrudRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
|
||||
|
||||
@Override
|
||||
protected RequestMappingInfo getMappingForMethod(@NonNull Method method, @NonNull Class<?> handlerType) {
|
||||
RequestMappingInfo requestMappingInfo = super.getMappingForMethod(method, handlerType);
|
||||
if (requestMappingInfo == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 如果没有声明 @CrudRequestMapping 注解,直接返回
|
||||
if (!handlerType.isAnnotationPresent(CrudRequestMapping.class)) {
|
||||
return requestMappingInfo;
|
||||
}
|
||||
|
||||
// 获取 @CrudRequestMapping 注解信息
|
||||
CrudRequestMapping crudRequestMapping = handlerType.getDeclaredAnnotation(CrudRequestMapping.class);
|
||||
// 拼接路径前缀(合并了 @RequestMapping 的部分能力)
|
||||
String pathPrefix = crudRequestMapping.value();
|
||||
if (StrUtil.isNotBlank(pathPrefix)) {
|
||||
requestMappingInfo = RequestMappingInfo.paths(pathPrefix).build().combine(requestMappingInfo);
|
||||
}
|
||||
|
||||
// 过滤 API
|
||||
Api[] apiArr = crudRequestMapping.api();
|
||||
// 如果非本类中定义,且 API 列表中不包含,则忽略
|
||||
Api api = ExceptionUtils.exToNull(() -> Api.valueOf(method.getName().toUpperCase()));
|
||||
if (method.getDeclaringClass() != handlerType && !ArrayUtil.containsAny(apiArr, Api.ALL, api)) {
|
||||
return null;
|
||||
}
|
||||
return requestMappingInfo;
|
||||
}
|
||||
}
|
@@ -27,7 +27,7 @@ import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.config.properties.ContinewAdminProperties;
|
||||
import top.charles7c.cnadmin.common.config.properties.ContiNewAdminProperties;
|
||||
|
||||
import net.dreamlu.mica.ip2region.core.Ip2regionSearcher;
|
||||
import net.dreamlu.mica.ip2region.core.IpInfo;
|
||||
@@ -55,7 +55,7 @@ public class IpUtils {
|
||||
* @return 归属地信息
|
||||
*/
|
||||
public static String getCityInfo(String ip) {
|
||||
if (ContinewAdminProperties.IP_ADDR_LOCAL_PARSE_ENABLED) {
|
||||
if (ContiNewAdminProperties.IP_ADDR_LOCAL_PARSE_ENABLED) {
|
||||
return getLocalCityInfo(ip);
|
||||
} else {
|
||||
return getHttpCityInfo(ip);
|
||||
|
Reference in New Issue
Block a user