Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
FLIPPED-AURORA
gin-vue-admin
提交
f5369c79
G
gin-vue-admin
项目概览
FLIPPED-AURORA
/
gin-vue-admin
大约 1 年 前同步成功
通知
333
Star
18155
Fork
5506
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gin-vue-admin
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f5369c79
编写于
9月 23, 2020
作者:
Sliver_Horn
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
完善ErrorToEmail中间件
上级
db122950
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
69 addition
and
13 deletion
+69
-13
server/config.yaml
server/config.yaml
+1
-1
server/middleware/email.go
server/middleware/email.go
+55
-0
server/middleware/operation.go
server/middleware/operation.go
+0
-12
server/service/sys_user.go
server/service/sys_user.go
+13
-0
未找到文件。
server/config.yaml
浏览文件 @
f5369c79
...
...
@@ -77,7 +77,7 @@ system:
addr
:
8888
db-type
:
"
mysql"
# support mysql/postgresql/sqlite/sqlserver
need-init-data
:
false
error-to-email
:
tru
e
error-to-email
:
fals
e
# captcha configuration
captcha
:
...
...
server/middleware/email.go
0 → 100644
浏览文件 @
f5369c79
package
middleware
import
(
"gin-vue-admin/global"
"gin-vue-admin/model"
"gin-vue-admin/model/request"
"gin-vue-admin/service"
"gin-vue-admin/utils"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"io/ioutil"
"strconv"
"time"
)
func
ErrorToEmail
()
gin
.
HandlerFunc
{
return
func
(
c
*
gin
.
Context
)
{
var
username
string
if
claims
,
ok
:=
c
.
Get
(
"claims"
);
ok
{
waitUse
:=
claims
.
(
*
request
.
CustomClaims
)
username
=
waitUse
.
Username
}
else
{
id
,
_
:=
strconv
.
Atoi
(
c
.
Request
.
Header
.
Get
(
"x-user-id"
))
err
,
user
:=
service
.
FindUserById
(
id
)
if
err
!=
nil
{
username
=
"Unknown"
}
username
=
user
.
Username
}
body
,
_
:=
ioutil
.
ReadAll
(
c
.
Request
.
Body
)
record
:=
model
.
SysOperationRecord
{
Ip
:
c
.
ClientIP
(),
Method
:
c
.
Request
.
Method
,
Path
:
c
.
Request
.
URL
.
Path
,
Agent
:
c
.
Request
.
UserAgent
(),
Body
:
string
(
body
),
}
now
:=
time
.
Now
()
c
.
Next
()
latency
:=
time
.
Now
()
.
Sub
(
now
)
status
:=
c
.
Writer
.
Status
()
record
.
ErrorMessage
=
c
.
Errors
.
ByType
(
gin
.
ErrorTypePrivate
)
.
String
()
str
:=
"接收到的请求为"
+
record
.
Body
+
"
\n
"
+
"请求方式为"
+
record
.
Path
+
"
\n
"
+
"报错信息如下"
+
record
.
ErrorMessage
+
"
\n
"
+
"耗时"
+
latency
.
String
()
+
"
\n
"
if
global
.
GVA_CONFIG
.
System
.
ErrorToEmail
{
if
status
!=
200
{
subject
:=
username
+
""
+
record
.
Ip
+
"调用了"
+
record
.
Path
+
"报错了"
if
err
:=
utils
.
ErrorToEmail
(
subject
,
str
);
err
!=
nil
{
global
.
GVA_LOG
.
Error
(
"ErrorToEmail Failed, err:"
,
zap
.
Any
(
"err"
,
err
))
}
}
}
}
}
server/middleware/operation.go
浏览文件 @
f5369c79
...
...
@@ -2,12 +2,10 @@ package middleware
import
(
"bytes"
"encoding/json"
"gin-vue-admin/global"
"gin-vue-admin/model"
"gin-vue-admin/model/request"
"gin-vue-admin/service"
"gin-vue-admin/utils"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"io/ioutil"
...
...
@@ -62,16 +60,6 @@ func OperationRecord() gin.HandlerFunc {
record
.
Latency
=
latency
record
.
Resp
=
writer
.
body
.
String
()
if
global
.
GVA_CONFIG
.
System
.
ErrorToEmail
{
if
record
.
Status
!=
200
{
subject
:=
record
.
Ip
+
"调用了"
+
record
.
Path
+
"报错了"
body
,
_
:=
json
.
Marshal
(
record
)
if
err
:=
utils
.
ErrorToEmail
(
subject
,
string
(
body
));
err
!=
nil
{
global
.
GVA_LOG
.
Error
(
"ErrorToEmail Failed, err:"
,
zap
.
Any
(
"err"
,
err
))
}
}
}
if
err
:=
service
.
CreateSysOperationRecord
(
record
);
err
!=
nil
{
global
.
GVA_LOG
.
Error
(
"create operation record error:"
,
zap
.
Any
(
"err"
,
err
))
}
...
...
server/service/sys_user.go
浏览文件 @
f5369c79
...
...
@@ -112,3 +112,16 @@ func SetUserInfo(reqUser model.SysUser) (err error, user model.SysUser) {
err
=
global
.
GVA_DB
.
Updates
(
&
reqUser
)
.
Error
return
err
,
reqUser
}
// @title FindUserById
// @description Get user information by id, 通过id获取用户信息
// @auth (2020/04/05 20:22)
// @param id int
// @return err error
// @return user *model.SysUser
func
FindUserById
(
id
int
)
(
err
error
,
user
*
model
.
SysUser
)
{
var
u
model
.
SysUser
err
=
global
.
GVA_DB
.
Where
(
"`id` = ?"
,
id
)
.
First
(
&
u
)
.
Error
return
err
,
&
u
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录