Skip to content

Commit 0244975

Browse files
committed
add error log func
1 parent 6f78c8f commit 0244975

File tree

14 files changed

+297
-99
lines changed

14 files changed

+297
-99
lines changed

.eslintrc.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ module.exports = {
4747
'no-spaced-func': 2, //函数调用时 函数名与()之间不能有空格
4848
'no-this-before-super': 0, //在调用super()之前不能使用this或super
4949
'no-undef': 2, //不能有未定义的变量
50-
'no-use-before-define': 1, //未定义前不能使用
50+
//'no-use-before-define': 1, //未定义前不能使用
5151
'jsx-quotes': [2, 'prefer-double'], //强制在JSX属性(jsx-quotes)中一致使用双引号
5252
'react/display-name': 0, //防止在React组件定义中丢失displayName
5353
'react/forbid-prop-types': [2, { forbid: ['any'] }], //禁止某些propTypes
@@ -62,7 +62,7 @@ module.exports = {
6262
'react/jsx-no-literals': 0, //防止使用未包装的JSX字符串
6363
'react/jsx-no-undef': 1, //在JSX中禁止未声明的变量
6464
'react/jsx-pascal-case': 0, //为用户定义的JSX组件强制使用PascalCase
65-
'react/jsx-sort-props': 2, //强化props按字母排序
65+
//'react/jsx-sort-props': 2, //强化props按字母排序
6666
'react/jsx-uses-react': 1, //防止反应被错误地标记为未使用
6767
'react/jsx-uses-vars': 2, //防止在JSX中使用的变量被错误地标记为未使用
6868
'react/no-danger': 0, //防止使用危险的JSX属性

README.md

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,6 @@ ts版本分支:开发中
2828
git clone https://github.com/jzfai/react-admin-template.git
2929

3030

31-
#!/bin/sh
32-
33-
git filter-branch --env-filter '
34-
OLD_EMAIL="kuanghua@ruixiude.com"
35-
CORRECT_NAME="kuanghua"
36-
CORRECT_EMAIL="869653722@qq.com"
37-
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
38-
then
39-
export GIT_COMMITTER_NAME="$CORRECT_NAME"
40-
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
41-
fi
42-
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
43-
then
44-
export GIT_AUTHOR_NAME="$CORRECT_NAME"
45-
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
46-
fi
47-
' --tag-name-filter cat -- --branches --tags
48-
4931
进入项目目录
5032
cd react-admin-template
5133

@@ -68,6 +50,6 @@ yarn run dev
6850
#### 如果需要实时交流的可以加入wx群(有vue3+ts视频教程)
6951

7052

71-
![http://8.135.1.141/file/images/wx-groud.png](http://8.135.1.141/file/images/wx-groud1.png)
53+
![http://8.135.1.141/file/images/wx-groud.png](http://8.135.1.141/file/images/wx-groud.png)
7254

7355
大家的支持是我前进的动力 欢迎加入一起开发

package.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"dependencies": {
1313
"antd": "^4.8.2",
1414
"echarts": "4.2.1",
15+
"events": "^3.3.0",
1516
"js-cookie": "2.2.0",
1617
"moment-mini": "^2.22.1",
1718
"path": "^0.12.7",
@@ -46,25 +47,26 @@
4647
"@types/sortablejs": "^1.10.6",
4748
"@types/vuedraggable": "^2.24.0",
4849
"@typescript-eslint/eslint-plugin": "^4.33.0",
50+
"@typescript-eslint/parser": "^4.33.0",
4951
"@vitejs/plugin-legacy": "^1.5.2",
5052
"@vitejs/plugin-react-refresh": "^1.3.6",
5153
"babel-eslint": "^10.1.0",
5254
"eslint": "^7.32.0",
55+
"eslint-config-prettier": "^8.3.0",
5356
"eslint-plugin-import": "^2.22.1",
5457
"eslint-plugin-prettier": "^4.0.0",
5558
"eslint-plugin-react": "^7.5.1",
56-
"eslint-config-prettier": "^8.3.0",
57-
"prettier": "^2.4.1",
59+
"husky": "^7.0.2",
5860
"less": "^4.1.1",
5961
"less-loader": "^10.0.1",
62+
"mockjs": "^1.1.0",
63+
"prettier": "^2.4.1",
6064
"sass": "^1.32.12",
6165
"scss": "^0.2.4",
62-
"typescript": "^4.3.2",
63-
"husky": "^7.0.2",
64-
"mockjs": "^1.1.0",
6566
"svg-sprite-loader": "4.1.3",
66-
"vite-plugin-svg-icons": "^1.0.4",
67+
"typescript": "^4.3.2",
68+
"vite": "^2.6.5",
6769
"vite-plugin-mock": "^2.9.6",
68-
"vite": "^2.6.5"
70+
"vite-plugin-svg-icons": "^1.0.4"
6971
}
7072
}

src/index.jsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ import './styles/index.scss'
99
//svg-icon
1010
import 'virtual:svg-icons-register'
1111

12+
//error log collect
13+
import '@/views/error-log/errorLogCollect'
14+
1215
ReactDOM.render(<App />, document.getElementById('root'))

src/mockProdServer.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { createProdMockServer } from 'vite-plugin-mock/es/createProdMockServer'
2-
// 逐一导入您的mock.ts文件
3-
// 如果使用vite.mock.config.ts,只需直接导入文件
4-
// 可以使用 import.meta.glob功能来进行全部导入
5-
import test from '../mock/test'
2+
3+
//using import.meta.glob auto import prefer !!!
4+
const modulesFiles = import.meta.globEager('../mock/*.ts')
5+
const modules = []
6+
for (const path in modulesFiles) {
7+
modules.push(...modulesFiles[path].default)
8+
}
69

710
export function setupProdMockServer() {
8-
createProdMockServer([...test])
11+
createProdMockServer(modules)
912
}

src/router/config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ export const asyncRouters = [
3333
}
3434
]
3535
},
36+
{
37+
path: '/error-log',
38+
children: [
39+
{
40+
path: 'log',
41+
component: () => import('@/views/error-log/index'),
42+
name: 'ErrorLog',
43+
meta: { title: 'Error Log', icon: 'bug' }
44+
}
45+
]
46+
},
3647
{
3748
path: '/nested',
3849
redirect: '/nested/menu1-1',

src/utils/eventBus.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { EventEmitter } from 'events'
2+
export default new EventEmitter()
3+
4+
//监听
5+
// import Bus from './eventBus'
6+
//
7+
// Bus.addListener('changeSiblingsData', (msg) => {
8+
// this.setState({
9+
// bus: msg,
10+
// });
11+
// console.log(msg);
12+
// });
13+
//触发:
14+
// import Bus from './eventBus'
15+
//
16+
// Bus.emit('changeSiblingsData', msg);

src/views/error-log/ErrorLog.jsx

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/* react redux */
2+
// eslint-disable-next-line no-use-before-define
3+
import React, { Fragment } from 'react'
4+
import bus from '@/utils/eventBus'
5+
import axiosReq from '@/utils/axiosReq.js'
6+
import { connect } from 'react-redux'
7+
// import { useSelector, useDispatch } from 'react-redux'
8+
import { Button, message, Popconfirm, Space, Table, Tag } from 'antd'
9+
function ErrorLog(props) {
10+
// const [uState, setState] = React.useState({ count: 111 })
11+
//redux相关
12+
// const countNum = useSelector((state) => state.count.countNum)
13+
// const dispatch = useDispatch()
14+
// function testChangeState() {
15+
// setState({
16+
// ...uState,
17+
// count: uState.count + 1000
18+
// })
19+
// }
20+
21+
const errlogTest = () => {
22+
throw new Error('error test')
23+
}
24+
25+
/*删除*/
26+
let deleteByIdReq = (id) => {
27+
return axiosReq({
28+
url: '/ty-user/errorCollection/deleteById',
29+
data: { id: id },
30+
isParams: true,
31+
method: 'delete',
32+
bfLoading: true
33+
})
34+
}
35+
const tableDelClick = (row) => {
36+
deleteByIdReq(row.id).then(() => {
37+
selectPageReq()
38+
message.success(`【${row.pageUrl}】删除成功`).then()
39+
})
40+
}
41+
const columns = [
42+
{
43+
title: '错误日志',
44+
dataIndex: 'errorLog',
45+
key: 'errorLog'
46+
},
47+
{
48+
title: '页面路径',
49+
dataIndex: 'pageUrl',
50+
key: 'pageUrl'
51+
},
52+
{
53+
title: '创建时间',
54+
dataIndex: 'createTime',
55+
key: 'createTime'
56+
},
57+
{
58+
title: 'Action',
59+
key: 'action',
60+
render: (text, record) => (
61+
<Space size="middle" key={record}>
62+
<Button type="primary" size="small" onClick={() => tableDelClick(record)}>
63+
删除
64+
</Button>
65+
</Space>
66+
)
67+
}
68+
]
69+
const [tableData, setTableData] = React.useState([])
70+
71+
React.useEffect(() => {
72+
//类似于ComponentDidMount
73+
selectPageReq()
74+
//监听页面报错然后触发更新
75+
bus.on('reloadErrorPage', () => {
76+
selectPageReq()
77+
})
78+
return (state) => {
79+
//类似于ComponentUnMount
80+
console.log('useEffectreturn')
81+
}
82+
}, [])
83+
let selectPageReq = () => {
84+
let reqConfig = {
85+
url: '/ty-user/errorCollection/selectPage',
86+
method: 'get',
87+
isParams: true,
88+
isAlertErrorMsg: false
89+
}
90+
axiosReq(reqConfig).then((resData) => {
91+
setTableData(resData.data?.records)
92+
})
93+
}
94+
const [rowIdArr, setRowIdArr] = React.useState([])
95+
const tableOnChange = (pagination, filters, sorter, extra) => {
96+
console.log(pagination, filters, sorter, extra)
97+
}
98+
const onSelectChange = (selectedRowKeys) => {
99+
setRowIdArr(selectedRowKeys)
100+
}
101+
const rowSelection = {
102+
type: 'checkbox',
103+
onChange: onSelectChange
104+
}
105+
const multiDelBtnClick = () => {
106+
if (rowIdArr.length === 0) {
107+
message.warning('表格选项不能为空', 'warning').then()
108+
return
109+
}
110+
Popconfirm({ title: '确认' })
111+
axiosReq({
112+
url: `/ty-user/errorCollection/deleteBatchIds`,
113+
data: rowIdArr,
114+
method: 'DELETE',
115+
bfLoading: true
116+
}).then(() => {
117+
selectPageReq()
118+
message.success('删除成功').then()
119+
})
120+
}
121+
return (
122+
<Fragment>
123+
<div className="mb-1">
124+
<Button type="primary" onClick={errlogTest}>
125+
error test
126+
</Button>
127+
<Button className="ml-1" type="primary" onClick={multiDelBtnClick}>
128+
删除
129+
</Button>
130+
</div>
131+
<Table
132+
rowSelection={rowSelection}
133+
bordered
134+
columns={columns}
135+
dataSource={tableData}
136+
rowKey="id"
137+
onChange={(pagination, filters, sorter, extra) => {
138+
tableOnChange(pagination, filters, sorter, extra)
139+
}}
140+
/>
141+
</Fragment>
142+
)
143+
}
144+
145+
//配置使用redux
146+
export default connect((state) => ({
147+
countNum: state.count.countNum
148+
}))(ErrorLog)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import request from '@/utils/axiosReq'
2+
import bus from '@/utils/eventBus'
3+
import setting from '@/settings'
4+
5+
const checkNeed = () => {
6+
const env = import.meta.env.VITE_APP_ENV
7+
let { errorLog } = setting
8+
if (typeof errorLog === 'string') {
9+
return env === errorLog
10+
}
11+
if (errorLog instanceof Array) {
12+
return errorLog.includes(env)
13+
}
14+
return false
15+
}
16+
const errLogReq = (message) => {
17+
request({
18+
url: '/ty-user/errorCollection/insert',
19+
data: { pageUrl: window.location.href, errorLog: message },
20+
method: 'post',
21+
bfLoading: false,
22+
isAlertErrorMsg: true
23+
}).then(() => {
24+
bus.emit('reloadErrorPage')
25+
})
26+
}
27+
if (checkNeed()) {
28+
window.onerror = function (msg, url, lineNo, columnNo, error) {
29+
const string = msg.toLowerCase()
30+
const substring = 'script error'
31+
if (string.indexOf(substring) > -1) {
32+
errLogReq('Script Error: See Browser Console for Detail')
33+
} else {
34+
const message = [
35+
'Message: ' + msg,
36+
'URL: ' + url,
37+
'Line: ' + lineNo,
38+
'Column: ' + columnNo,
39+
'Error object: ' + JSON.stringify(error)
40+
].join(' - ')
41+
errLogReq(message)
42+
}
43+
return false
44+
}
45+
}

src/views/error-log/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import component from './ErrorLog'
2+
export default component

0 commit comments

Comments
 (0)