@gingkoo/base-server 0.0.3-alpha.1 → 0.0.3-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/backend/config/index.js +4 -0
- package/backend/routers/app.js +51 -11
- package/backend/utils/http.js +117 -0
- package/package.json +1 -1
package/backend/config/index.js
CHANGED
package/backend/routers/app.js
CHANGED
|
@@ -7,6 +7,7 @@ const { userlog } = require('../../backend/common/logger');
|
|
|
7
7
|
const jwt = require('../utils/jwt');
|
|
8
8
|
const { checkLogin, setTokenCookies } = require('../common/middleware/auth');
|
|
9
9
|
const { join, readFileContent } = require('../utils/path');
|
|
10
|
+
const api = require('../utils/http');
|
|
10
11
|
const userService = require('../common/services/user');
|
|
11
12
|
const generalConfig = require('../common/services/generalConfig');
|
|
12
13
|
const emailService = require('../common/services/email');
|
|
@@ -25,26 +26,65 @@ router.post('/login', async (req, res) => {
|
|
|
25
26
|
res.sendErr('请输入用户名');
|
|
26
27
|
return;
|
|
27
28
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (!userinfo?.USER_ID) {
|
|
31
|
-
res.sendErr('用户不存在');
|
|
29
|
+
if (!password) {
|
|
30
|
+
res.sendErr('请输入密码');
|
|
32
31
|
return;
|
|
33
32
|
}
|
|
34
33
|
|
|
35
|
-
|
|
36
|
-
if (
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
34
|
+
let userid = null;
|
|
35
|
+
if (config?.oauth?.account_login_url) {
|
|
36
|
+
// 统一账号登录
|
|
37
|
+
let redirect_uri = config.oauth.account_login_url.format({
|
|
38
|
+
username: userId,
|
|
39
|
+
password,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
let resp = await api.get({
|
|
43
|
+
url: redirect_uri,
|
|
44
|
+
headers: {
|
|
45
|
+
'Content-Type': 'application/json',
|
|
46
|
+
Authorization: `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImFwcGlkIjoiOThndmRscm8ifSwiZXhwIjoyMDQ2NzYwNTM5LCJpYXQiOjE3MzE0MDA1Mzl9.EHZi_kvZw7CGGkQ7BoGirdRtkT6nG0pgFBCuF9wXw6I`,
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
// {
|
|
51
|
+
// "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJjb2RlbWFnaWMiLCJleHAiOjE3MzQwNjI2NDMsImlhdCI6MTczNDA1NTQ0MywiaXNzIjoiaWRtIiwic3ViIjoieGlsaW4uZ2FvIn0.6cZQwz2sawEFT5vaMtU_eoGx0CBBllgWhDBSNknmEZM",
|
|
52
|
+
// "expires_in": 7200,
|
|
53
|
+
// "token_type": "Bearer",
|
|
54
|
+
// "user_id": "xilin.gao"
|
|
55
|
+
// }
|
|
56
|
+
|
|
57
|
+
if (resp.isError) {
|
|
58
|
+
return res.sendErr('账号或密码错误');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let { access_token, expires_in, token_type, user_id } = resp;
|
|
62
|
+
userid = user_id;
|
|
63
|
+
} else {
|
|
64
|
+
let userinfo = await userService.getUserInfo(userId);
|
|
65
|
+
if (!userinfo?.USER_ID) {
|
|
66
|
+
res.sendErr('用户不存在');
|
|
67
|
+
return;
|
|
40
68
|
}
|
|
69
|
+
|
|
70
|
+
// 判断密码
|
|
71
|
+
if (userinfo.PASSWD) {
|
|
72
|
+
let flag = await bcrypt.compare(password, userinfo.PASSWD);
|
|
73
|
+
if (!flag) {
|
|
74
|
+
return res.sendErr('密码错误');
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
userid = userinfo?.USER_ID;
|
|
41
78
|
}
|
|
42
79
|
|
|
80
|
+
if (!userid) {
|
|
81
|
+
return res.sendErr('用户不存在');
|
|
82
|
+
}
|
|
43
83
|
let token = jwt.generateToken({
|
|
44
|
-
userid
|
|
84
|
+
userid,
|
|
45
85
|
});
|
|
46
86
|
|
|
47
|
-
userlog.info(`用户:${
|
|
87
|
+
userlog.info(`用户:${userid} 登录成功,token: ${token}`);
|
|
48
88
|
|
|
49
89
|
setTokenCookies(res, token);
|
|
50
90
|
res.sendOk({
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* axios封装
|
|
3
|
+
* 请求拦截、相应拦截、错误统一处理
|
|
4
|
+
*/
|
|
5
|
+
const axios = require('axios');
|
|
6
|
+
const QS = require('qs');
|
|
7
|
+
|
|
8
|
+
const SuccessCode = 'OK'; // 业务code 成功的状态码
|
|
9
|
+
|
|
10
|
+
axios.defaults.timeout = 60000; //请求超时时间
|
|
11
|
+
axios.defaults.withCredentials = false;
|
|
12
|
+
|
|
13
|
+
const HttpCodeMessage = {
|
|
14
|
+
200: '服务器成功返回请求的数据。',
|
|
15
|
+
201: '新建或修改数据成功。',
|
|
16
|
+
202: '一个请求已经进入后台排队(异步任务)。',
|
|
17
|
+
204: '删除数据成功。',
|
|
18
|
+
400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。',
|
|
19
|
+
401: '用户没有权限(令牌、用户名、密码错误)。',
|
|
20
|
+
403: '用户得到授权,但是访问是被禁止的。',
|
|
21
|
+
404: '发出的请求针对的是不存在的记录,服务器没有进行操作。',
|
|
22
|
+
406: '请求的格式不可得。',
|
|
23
|
+
410: '请求的资源被永久删除,且不会再得到的。',
|
|
24
|
+
422: '当创建一个对象时,发生一个验证错误。',
|
|
25
|
+
500: '服务器发生错误,请检查服务器。',
|
|
26
|
+
502: '网关错误。',
|
|
27
|
+
503: '服务不可用,服务器暂时过载或维护。',
|
|
28
|
+
504: '网关超时。',
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// 请求拦截器
|
|
32
|
+
axios.interceptors.request.use(
|
|
33
|
+
(options) => {
|
|
34
|
+
options.headers['Content-Type'] = options.headers['Content-Type']
|
|
35
|
+
? options.headers['Content-Type']
|
|
36
|
+
: 'application/json';
|
|
37
|
+
|
|
38
|
+
return options;
|
|
39
|
+
},
|
|
40
|
+
(error) => {
|
|
41
|
+
return Promise.reject(error);
|
|
42
|
+
},
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
// 响应拦截器
|
|
46
|
+
axios.interceptors.response.use(
|
|
47
|
+
(response) => {
|
|
48
|
+
return response;
|
|
49
|
+
},
|
|
50
|
+
(error) => {
|
|
51
|
+
let errorRes = error.response || {};
|
|
52
|
+
let { status, statusText = '' } = errorRes;
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
data: {
|
|
56
|
+
isError: true,
|
|
57
|
+
code: status,
|
|
58
|
+
message: HttpCodeMessage[status] || statusText,
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
},
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
const sendRequest = ({ url, method, params, headers, timeout }) => {
|
|
65
|
+
let options = {
|
|
66
|
+
url,
|
|
67
|
+
method,
|
|
68
|
+
paramsSerializer: function (params) {
|
|
69
|
+
return QS.stringify(params);
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
if (method == 'GET') {
|
|
74
|
+
options.params = params;
|
|
75
|
+
} else {
|
|
76
|
+
options.data = params;
|
|
77
|
+
}
|
|
78
|
+
if (headers) {
|
|
79
|
+
options.headers = headers;
|
|
80
|
+
}
|
|
81
|
+
if (timeout) {
|
|
82
|
+
options.timeout = timeout;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return new Promise((resolve, reject) => {
|
|
86
|
+
axios
|
|
87
|
+
.request(options)
|
|
88
|
+
.then((result) => {
|
|
89
|
+
resolve(result.data);
|
|
90
|
+
})
|
|
91
|
+
.catch((error) => {
|
|
92
|
+
return resolve(null);
|
|
93
|
+
})
|
|
94
|
+
.finally(() => {});
|
|
95
|
+
});
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
class BaseRequest {
|
|
99
|
+
get(options) {
|
|
100
|
+
options.method = 'GET';
|
|
101
|
+
return sendRequest(options);
|
|
102
|
+
}
|
|
103
|
+
post(options) {
|
|
104
|
+
options.method = 'POST';
|
|
105
|
+
return sendRequest(options);
|
|
106
|
+
}
|
|
107
|
+
put(options) {
|
|
108
|
+
options.method = 'PUT';
|
|
109
|
+
return sendRequest(options);
|
|
110
|
+
}
|
|
111
|
+
del(options) {
|
|
112
|
+
options.method = 'DELETE';
|
|
113
|
+
return sendRequest(options);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
module.exports = new BaseRequest();
|