@shun-js/user 0.2.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 qiaowenbin<uikoo9@qq.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/app.js ADDED
@@ -0,0 +1,31 @@
1
+ // config
2
+ const { parseServerConfig } = require('@shun-js/shun-config');
3
+
4
+ // init
5
+ (async () => {
6
+ // config
7
+ const config = await parseServerConfig(process.argv);
8
+ if (!config) {
9
+ console.log('read server config fail');
10
+ return;
11
+ }
12
+
13
+ // options
14
+ const options = {};
15
+
16
+ // options mysql
17
+ options.config = config;
18
+ options.mysql = require('qiao-mysql');
19
+
20
+ // options.redis
21
+ options.redis = require('qiao-redis');
22
+ options.redisOptions = config.redisOptions;
23
+
24
+ // options log
25
+ options.log = require('qiao-log');
26
+ options.logOptions = require('./server/log-options.js')();
27
+
28
+ // start
29
+ const app = await require('qiao-z')(options);
30
+ app.listen(config.port);
31
+ })();
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@shun-js/user",
3
+ "version": "0.2.2",
4
+ "description": "user",
5
+ "keywords": [
6
+ "shun.js",
7
+ "user"
8
+ ],
9
+ "license": "MIT",
10
+ "author": "uikoo9 <uikoo9@qq.com>",
11
+ "homepage": "https://github.com/uikoo9/shun-js",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/uikoo9/shun-js.git"
15
+ },
16
+ "bugs": {
17
+ "url": "https://github.com/uikoo9/shun-js/issues"
18
+ },
19
+ "files": [
20
+ "server",
21
+ "app.js"
22
+ ],
23
+ "dependencies": {
24
+ "@shun-js/shun-config": "^0.0.9",
25
+ "@shun-js/shun-service": "^0.1.0",
26
+ "qiao-encode": "^5.0.0",
27
+ "qiao-log": "^5.1.9",
28
+ "qiao-mysql": "^5.1.9",
29
+ "qiao-redis": "^5.7.8",
30
+ "qiao-z": "^5.7.7"
31
+ },
32
+ "publishConfig": {
33
+ "access": "public",
34
+ "registry": "https://registry.npmjs.org/"
35
+ },
36
+ "gitHead": "99ab9ca840190a2bf5b418c5910e5b7055711a37"
37
+ }
@@ -0,0 +1,17 @@
1
+ // service
2
+ const service = require('../service/UserItemService.js');
3
+
4
+ /**
5
+ * controller
6
+ */
7
+ module.exports = (app) => {
8
+ // user login
9
+ app.post('/user/login', (req, res) => {
10
+ service.userLogin(req, res);
11
+ });
12
+
13
+ // user check
14
+ app.post('/user/check', (req, res) => {
15
+ service.userCheck(req, res);
16
+ });
17
+ };
@@ -0,0 +1,30 @@
1
+ /**
2
+ * log options
3
+ * @returns
4
+ */
5
+ module.exports = () => {
6
+ // log options
7
+ const logLevel = 'debug';
8
+ const logPattern = 'yyyy-MM-dd-hh';
9
+ const logPath = require('path').resolve(__dirname, '../logs/qiao-z.log');
10
+
11
+ return {
12
+ appenders: {
13
+ stdout: {
14
+ type: 'stdout',
15
+ },
16
+ datefile: {
17
+ type: 'dateFile',
18
+ pattern: logPattern,
19
+ filename: logPath,
20
+ keepFileExt: true,
21
+ },
22
+ },
23
+ categories: {
24
+ default: {
25
+ level: logLevel,
26
+ appenders: ['stdout', 'datefile'],
27
+ },
28
+ },
29
+ };
30
+ };
@@ -0,0 +1,89 @@
1
+ // qiao
2
+ const { uuid, AESEncrypt } = require('qiao-encode');
3
+
4
+ // sql
5
+ const sql = require('../sql/UserItemSQL.js');
6
+
7
+ /**
8
+ * getUserItemByName
9
+ * @param {*} req
10
+ * @param {*} res
11
+ * @param {*} mobile
12
+ * @returns
13
+ */
14
+ exports.getUserItemByName = async (req, res, mobile) => {
15
+ const methodName = 'getUserItemByName';
16
+
17
+ // get user item
18
+ try {
19
+ return await req.db.query(sql.getUserItemByName, [mobile]);
20
+ } catch (error) {
21
+ const msg = '获取用户信息失败!';
22
+ req.logger.error(methodName, msg, error.name, error.message);
23
+ res.jsonFail(msg);
24
+ }
25
+ };
26
+
27
+ /**
28
+ * addUserItem
29
+ * @param {*} req
30
+ * @param {*} res
31
+ * @param {*} mobile
32
+ * @returns
33
+ */
34
+ exports.addUserItem = async (req, res, mobile) => {
35
+ const methodName = 'addUserItem';
36
+
37
+ // add user item
38
+ try {
39
+ // pwd
40
+ const password = uuid();
41
+ const encryptPassword = AESEncrypt(password, global.QZ_CONFIG.encryptKey);
42
+ req.logger.info(methodName, 'password', password);
43
+ req.logger.info(methodName, 'encryptPassword', encryptPassword);
44
+
45
+ // add user
46
+ const addUserItemRes = await req.db.query(sql.addUserItem, [mobile, encryptPassword]);
47
+
48
+ // userItem
49
+ const userItem = {
50
+ id: addUserItemRes.insertId,
51
+ usertoken: AESEncrypt(mobile + encryptPassword, global.QZ_CONFIG.encryptKey),
52
+ usermobile: mobile,
53
+ };
54
+ req.logger.info(methodName, 'userItem', userItem);
55
+
56
+ // r
57
+ return userItem;
58
+ } catch (error) {
59
+ const msg = '添加用户失败!';
60
+ req.logger.error(methodName, msg, error.name, error.message);
61
+ res.jsonFail(msg);
62
+ }
63
+ };
64
+
65
+ /**
66
+ * getUserItemById
67
+ * @param {*} req
68
+ * @param {*} res
69
+ * @param {*} id
70
+ * @returns
71
+ */
72
+ exports.getUserItemById = async (req, res, id) => {
73
+ const methodName = 'getUserItemById';
74
+
75
+ // get user item
76
+ try {
77
+ const getUserItemByIdRes = await req.db.query(sql.getUserItemById, [id]);
78
+ if (getUserItemByIdRes.length !== 1) {
79
+ res.jsonFail('缺少用户信息!');
80
+ return;
81
+ }
82
+
83
+ return getUserItemByIdRes[0];
84
+ } catch (error) {
85
+ const msg = '获取用户信息失败!';
86
+ req.logger.error(methodName, msg, error.name, error.message);
87
+ res.jsonFail(msg);
88
+ }
89
+ };
@@ -0,0 +1,146 @@
1
+ // qiao
2
+ const { AESEncrypt } = require('qiao-encode');
3
+
4
+ // model
5
+ const { getUserItemByName, addUserItem, getUserItemById } = require('../model/UserItemModel.js');
6
+
7
+ // feishu
8
+ const { feishuBot } = require('@shun-js/shun-service');
9
+
10
+ /**
11
+ * userLogin
12
+ * @param {*} req
13
+ * @param {*} res
14
+ * @returns
15
+ */
16
+ exports.userLogin = async (req, res) => {
17
+ const methodName = 'userLogin';
18
+
19
+ // const
20
+ const mobile = req.body.mobile;
21
+ const code = req.body.code;
22
+ if (!mobile) {
23
+ const msg = 'need mobile';
24
+ req.logger.warn(methodName, msg, req.body);
25
+ res.jsonFail(msg);
26
+ return;
27
+ }
28
+ if (!code) {
29
+ const msg = 'need code';
30
+ req.logger.warn(methodName, msg, req.body);
31
+ res.jsonFail(msg);
32
+ return;
33
+ }
34
+
35
+ // user code
36
+ const cacheKey = `code-${mobile}`;
37
+ const cacheCode = await req.redis.get(cacheKey);
38
+ req.logger.info(methodName, 'cacheKey', cacheKey);
39
+ req.logger.info(methodName, 'cacheCode', cacheCode);
40
+ req.logger.info(methodName, 'code', code);
41
+ if (cacheCode === code) {
42
+ await req.redis.del(cacheKey);
43
+ } else {
44
+ const msg = '验证码错误,请重新填写';
45
+ req.logger.error(methodName, msg, mobile, code);
46
+ res.jsonFail(msg);
47
+ return;
48
+ }
49
+
50
+ // user item
51
+ const getUserItemRes = await getUserItemByName(req, res, mobile);
52
+ if (!getUserItemRes) return;
53
+
54
+ // reg
55
+ if (getUserItemRes.length !== 1) {
56
+ // add user
57
+ const addUserItemRes = await addUserItem(req, res, mobile);
58
+ if (!addUserItemRes) return;
59
+
60
+ // feishu
61
+ const finalMsg = `【提醒】success - ${methodName} - new user reg - ${addUserItemRes.id}`;
62
+ const feishuBotRes = await feishuBot({
63
+ url: global.QZ_CONFIG.feishu.url,
64
+ feishuUrl: global.QZ_CONFIG.feishu.feishuUrl,
65
+ feishuMsg: finalMsg,
66
+ });
67
+ req.logger.warn(methodName, 'feishuBotRes', feishuBotRes);
68
+
69
+ // r
70
+ res.jsonSuccess('登录成功!', addUserItemRes);
71
+ return;
72
+ }
73
+
74
+ // login
75
+ const user = getUserItemRes[0];
76
+ const userItem = {
77
+ id: user.id,
78
+ usertoken: AESEncrypt(mobile + user.user_item_password, global.QZ_CONFIG.encryptKey),
79
+ usermobile: mobile,
80
+ };
81
+
82
+ // feishu
83
+ const finalMsg = `【提醒】success - ${methodName} - user login - ${userItem.id}`;
84
+ const feishuBotRes = await feishuBot({
85
+ url: global.QZ_CONFIG.feishu.url,
86
+ feishuUrl: global.QZ_CONFIG.feishu.feishuUrl,
87
+ feishuMsg: finalMsg,
88
+ });
89
+ req.logger.warn(methodName, 'feishuBotRes', feishuBotRes);
90
+
91
+ // r
92
+ res.jsonSuccess('登录成功!', userItem);
93
+ };
94
+
95
+ /**
96
+ * userCheck
97
+ * @param {*} req
98
+ * @param {*} res
99
+ * @returns
100
+ */
101
+ exports.userCheck = async (req, res) => {
102
+ const methodName = 'userCheck';
103
+
104
+ // const
105
+ const paths = JSON.parse(req.headers.paths || '[]');
106
+ const path = req.headers.path;
107
+
108
+ // normal visit
109
+ const finalPaths = [].concat(global.QZ_CONFIG.paths, paths);
110
+ const normalVisit = finalPaths.includes(path);
111
+ req.logger.info(methodName, 'paths', paths);
112
+ req.logger.info(methodName, 'path', path);
113
+ req.logger.info(methodName, 'finalPaths', finalPaths);
114
+ req.logger.info(methodName, 'normalVisit', normalVisit);
115
+ if (normalVisit) {
116
+ res.jsonSuccess('normal path');
117
+ return;
118
+ }
119
+
120
+ // check userid and usertoken
121
+ const userid = req.headers.userid;
122
+ const usertoken = req.headers.usertoken;
123
+ if (!userid || !usertoken) {
124
+ res.jsonFail('缺少用户token!');
125
+ return;
126
+ }
127
+
128
+ // get user
129
+ const userItemRes = await getUserItemById(req, res, userid);
130
+ if (!userItemRes) {
131
+ res.jsonFail('非法token!');
132
+ return;
133
+ }
134
+
135
+ // check token
136
+ const username = userItemRes['user_item_name'];
137
+ const password = userItemRes['user_item_password'];
138
+ const rUsertoken = AESEncrypt(username + password, global.QZ_CONFIG.encryptKey);
139
+ if (rUsertoken !== decodeURIComponent(usertoken)) {
140
+ res.jsonFail('非法token!');
141
+ return;
142
+ }
143
+
144
+ // r
145
+ res.jsonSuccess('合法token!');
146
+ };
@@ -0,0 +1,28 @@
1
+ exports.getUserItemByName = `
2
+ select
3
+ *
4
+ from
5
+ t_user_item
6
+ where
7
+ user_item_name=?
8
+ and
9
+ del_tag='0'
10
+ `;
11
+
12
+ exports.addUserItem = `
13
+ insert into
14
+ t_user_item
15
+ values
16
+ (null, ?, ?, now(), '0')
17
+ `;
18
+
19
+ exports.getUserItemById = `
20
+ select
21
+ *
22
+ from
23
+ t_user_item
24
+ where
25
+ id=?
26
+ and
27
+ del_tag='0'
28
+ `;