Complete core functions #9

Merged
FatttSnake merged 171 commits from FatttSnake into dev 2024-02-23 11:56:35 +08:00
13 changed files with 70 additions and 103 deletions
Showing only changes of commit e1e661988a - Show all commits

View File

@@ -1,48 +1,19 @@
drop table if exists t_user;
create table if not exists t_user
(
id bigint not null primary key,
username varchar(20) not null comment '用户名',
password char(70) not null comment '密码',
locking int not null comment '锁定',
expiration datetime comment '过期时间',
credentials_expiration datetime comment '认证过期时间',
enable int not null comment '启用',
last_login_time datetime comment '上次登录时间',
last_login_ip varchar(128) comment '上次登录 IP',
create_time datetime not null default (utc_timestamp()) comment '创建时间',
update_time datetime not null default (utc_timestamp()) comment '修改时间',
deleted bigint not null default 0,
version int not null default 0,
constraint t_user_unique unique (username, deleted)
) comment '用户表';
insert into t_user (id, username, password, locking, enable) value (0, 'admin',
'$2a$10$3wDGdzTZlC..7eY6u2XM5u78xUQo0z5Sj5yOpneD4QJ0q/TA5TY0S',
0, 1);
drop table if exists t_sys_log;
create table t_sys_log
create table t_sys_log -- 系统日志表
(
id bigint not null,
log_type varchar(50) not null comment '日志类型',
operate_user_id bigint not null comment '操作用户',
operate_time datetime(3) not null default (utc_timestamp()) comment '操作时间',
request_uri varchar(500) default null comment '请求 URI',
request_method varchar(10) default null comment '请求方式',
request_params text comment '请求参数',
request_ip varchar(20) not null comment '请求 IP',
request_server_address varchar(50) not null comment '请求服务器地址',
is_exception char(1) default null comment '是否异常',
exception_info text comment '异常信息',
start_time datetime(3) not null comment '开始时间',
end_time datetime(3) not null comment '结束时间',
execute_time bigint default null comment '执行时间',
user_agent varchar(500) default null comment '用户代理',
primary key (id) using btree,
key idx_sys_log_log_type (log_type) using btree,
key idx_sys_log_operate_user_id (operate_user_id) using btree,
key idx_sys_log_is_exception (is_exception) using btree,
key idx_sys_log_operate_time (operate_time) using btree
) comment '系统日志表';
log_type varchar(50) not null, -- 日志类型
operate_user_id bigint not null, -- 操作用户
operate_time datetime(3) not null default (strftime('%Y-%m-%d %H:%M:%f','now')), -- 操作时间
request_uri varchar(500) default null, -- 请求 URI
request_method varchar(10) default null, -- 请求方式
request_params text, -- 请求参数
request_ip varchar(20) not null, -- 请求 IP
request_server_address varchar(50) not null, -- 请求服务器地址
exception int not null default 0, -- 是否异常
exception_info text, -- 异常信息
start_time datetime(3) not null, -- 开始时间
end_time datetime(3) not null, -- 结束时间
execute_time bigint default null, -- 执行时间
user_agent varchar(500) default null -- 用户代理
);

View File

@@ -153,7 +153,7 @@
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.42.0.0</version>
<version>3.44.1.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>

View File

@@ -34,6 +34,26 @@ fun main(args: Array<String>) {
}
}
if (!File("data/db").isDirectory) {
if (!File("data/db").mkdir()) {
logger.error("Can not create directory 'data/db', please try again later.")
return
}
}
if (!File("data/db/sqlite.db").isFile || File("data/db/sqlite.db").inputStream()
.use { it.readNBytes(15).toString(Charsets.UTF_8) != "SQLite format 3" }
) {
logger.warn("The 'data/db/sqlite.db' database is lost or damaged, recreating...")
if (File("data/db/sqlite.db").exists() && !File("data/db/sqlite.db").delete()) {
logger.error("Can not recreate database 'data/db/sqlite.db', please try again later.")
}
FatWebApiApplication::class.java.getResourceAsStream("/db/sqlite.db")?.let {
File("data/db/sqlite.db").writeBytes(it.readAllBytes())
}
}
if (File("application-config.yml").exists() || File("data/application-config.yml").exists()) {
runApplication<FatWebApiApplication>(*args)
} else {

View File

@@ -7,7 +7,6 @@ import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import top.fatweb.api.converter.system.SysLogConverter
import top.fatweb.api.entity.common.ResponseCode
import top.fatweb.api.entity.common.ResponseResult
import top.fatweb.api.param.system.SysLogGetParam
@@ -44,9 +43,7 @@ class SysLogController(
@PreAuthorize("hasAnyAuthority('system:log:query:all')")
fun get(@Valid sysLogGetParam: SysLogGetParam?): ResponseResult<PageVo<SysLogVo>> {
return ResponseResult.success(
ResponseCode.DATABASE_SELECT_SUCCESS, data = SysLogConverter.sysLogPageToSysLogPageVo(
sysLogService.getPage(sysLogGetParam)
)
ResponseCode.DATABASE_SELECT_SUCCESS, data = sysLogService.getPage(sysLogGetParam)
)
}
}

View File

@@ -23,7 +23,6 @@ interface SysLogMapper : BaseMapper<SysLog> {
* @param logType List of log types
* @param requestMethod List of request methods
* @param searchRequestUrl Request URL to search for
* @param searchRegex Use regex
* @param searchStartTime Start time to search for
* @param searchEndTime end time to search for
* @return System log in page
@@ -38,7 +37,6 @@ interface SysLogMapper : BaseMapper<SysLog> {
logType: List<String>?,
requestMethod: List<String>?,
searchRequestUrl: String?,
searchRegex: Boolean,
searchStartTime: LocalDateTime?,
searchEndTime: LocalDateTime?
): IPage<SysLog>

View File

@@ -51,8 +51,10 @@ data class SysLogGetParam(
* @author FatttSnake, fatttsnake@gmail.com
* @since 1.0.0
*/
/*
@Schema(description = "查询使用正则表达式", allowableValues = ["true", "false"], defaultValue = "false")
val searchRegex: Boolean = false,
*/
/**
* Start time to search for

View File

@@ -1,5 +1,6 @@
package top.fatweb.api.service.permission.impl
import com.baomidou.dynamic.datasource.annotation.DS
import com.baomidou.mybatisplus.core.metadata.OrderItem
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.kotlin.KtUpdateWrapper
@@ -47,6 +48,7 @@ import java.time.ZoneOffset
* @see User
* @see IUserService
*/
@DS("master")
@Service
class UserServiceImpl(
private val passwordEncoder: PasswordEncoder,

View File

@@ -1,9 +1,10 @@
package top.fatweb.api.service.system
import com.baomidou.mybatisplus.core.metadata.IPage
import com.baomidou.mybatisplus.extension.service.IService
import top.fatweb.api.entity.system.SysLog
import top.fatweb.api.param.system.SysLogGetParam
import top.fatweb.api.vo.PageVo
import top.fatweb.api.vo.system.SysLogVo
/**
* System log service interface
@@ -22,8 +23,8 @@ interface ISysLogService : IService<SysLog> {
* @author FatttSnake, fatttsnake@gmail.com
* @since 1.0.0
* @see SysLogGetParam
* @see IPage
* @see SysLog
* @see PageVo
* @see SysLogVo
*/
fun getPage(sysLogGetParam: SysLogGetParam?): IPage<SysLog>
fun getPage(sysLogGetParam: SysLogGetParam?): PageVo<SysLogVo>
}

View File

@@ -1,15 +1,19 @@
package top.fatweb.api.service.system.impl
import com.baomidou.mybatisplus.core.metadata.IPage
import com.baomidou.dynamic.datasource.annotation.DS
import com.baomidou.mybatisplus.core.metadata.OrderItem
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import org.springframework.stereotype.Service
import top.fatweb.api.converter.system.SysLogConverter
import top.fatweb.api.entity.system.SysLog
import top.fatweb.api.mapper.system.SysLogMapper
import top.fatweb.api.param.system.SysLogGetParam
import top.fatweb.api.service.permission.IUserService
import top.fatweb.api.service.system.ISysLogService
import top.fatweb.api.util.PageUtil
import top.fatweb.api.vo.PageVo
import top.fatweb.api.vo.system.SysLogVo
/**
* System log service implement
@@ -21,21 +25,29 @@ import top.fatweb.api.util.PageUtil
* @see SysLog
* @see ISysLogService
*/
@DS("sqlite")
@Service
class SysLogServiceImpl : ServiceImpl<SysLogMapper, SysLog>(), ISysLogService {
override fun getPage(sysLogGetParam: SysLogGetParam?): IPage<SysLog> {
class SysLogServiceImpl(
private val userService: IUserService
) : ServiceImpl<SysLogMapper, SysLog>(), ISysLogService {
override fun getPage(sysLogGetParam: SysLogGetParam?): PageVo<SysLogVo> {
val sysLogPage = Page<SysLog>(sysLogGetParam?.currentPage ?: 1, sysLogGetParam?.pageSize ?: 20)
PageUtil.setPageSort(sysLogGetParam, sysLogPage, OrderItem.desc("start_time"))
return baseMapper.selectPage(
val sysLogIPage = baseMapper.selectPage(
sysLogPage,
sysLogGetParam?.logType?.split(","),
sysLogGetParam?.requestMethod?.split(","),
sysLogGetParam?.searchRequestUrl,
sysLogGetParam?.searchRegex ?: false,
sysLogGetParam?.searchStartTime,
sysLogGetParam?.searchEndTime
)
sysLogIPage.records.forEach {
it.operateUsername =
it.operateUserId?.let { it1 -> userService.getOne(it1)?.username }
}
return SysLogConverter.sysLogPageToSysLogPageVo(sysLogIPage)
}
}

View File

@@ -14,10 +14,10 @@ spring:
master:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
local:
sqlite:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: org.sqlite.JDBC
url: jdbc:sqlite::resource:db/sqlite.db
url: jdbc:sqlite:data/db/sqlite.db?date_string_format=yyyy-MM-dd'T'HH:mm:ss.SSS
jackson:

View File

@@ -1,24 +0,0 @@
drop table if exists t_sys_log;
create table t_sys_log
(
id bigint not null,
log_type varchar(50) not null comment '日志类型',
operate_user_id bigint not null comment '操作用户',
operate_time datetime(3) not null default (utc_timestamp()) comment '操作时间',
request_uri varchar(500) default null comment '请求 URI',
request_method varchar(10) default null comment '请求方式',
request_params text comment '请求参数',
request_ip varchar(20) not null comment '请求 IP',
request_server_address varchar(50) not null comment '请求服务器地址',
exception int not null default 0 comment '是否异常',
exception_info text comment '异常信息',
start_time datetime(3) not null comment '开始时间',
end_time datetime(3) not null comment '结束时间',
execute_time bigint default null comment '执行时间',
user_agent varchar(500) default null comment '用户代理',
primary key (id) using btree,
key idx_sys_log_log_type (log_type) using btree,
key idx_sys_log_operate_user_id (operate_user_id) using btree,
key idx_sys_log_exception (exception) using btree,
key idx_sys_log_operate_time (operate_time) using btree
) comment '系统日志表';

Binary file not shown.

View File

@@ -16,10 +16,8 @@
t_sys_log.start_time as sys_log_start_time,
t_sys_log.end_time as sys_log_end_time,
t_sys_log.execute_time as sys_log_execute_time,
t_sys_log.user_agent as sys_log_user_agent,
t_user.username as sys_log_operate_username
t_sys_log.user_agent as sys_log_user_agent
from t_sys_log
left join t_user on t_user.id = t_sys_log.operate_user_id
<where>
<foreach collection="logType" item="item" index="index" open="and t_sys_log.log_type in (" separator=","
close=")" nullable="true">
@@ -30,17 +28,9 @@
#{item}
</foreach>
<if test="searchRequestUrl != null">
<choose>
<when test="searchRegex == true">
and concat_ws('?', concat(t_sys_log.request_server_address, t_sys_log.request_uri),
if(length(t_sys_log.request_params) != 0, t_sys_log.request_params, null)) regexp #{searchRequestUrl}
</when>
<otherwise>
and concat_ws('?', concat(t_sys_log.request_server_address, t_sys_log.request_uri),
if(length(t_sys_log.request_params) != 0, t_sys_log.request_params, null)) like concat('%',
#{searchRequestUrl}, '%')
</otherwise>
</choose>
and t_sys_log.request_server_address || t_sys_log.request_uri ||
case when length(t_sys_log.request_params) != 0 then '?' || t_sys_log.request_params else '' end like
'%'||'mail'||'%'
</if>
<if test="searchStartTime != null and searchEndTime != null">
and t_sys_log.start_time between #{searchStartTime} and #{searchEndTime}
@@ -64,7 +54,5 @@
<result property="endTime" column="sys_log_end_time"/>
<result property="executeTime" column="sys_log_execute_time"/>
<result property="userAgent" column="sys_log_user_agent"/>
<result property="operateUsername" column="sys_log_operate_username"/>
</resultMap>
</mapper>