@shun-js/recommend 0.3.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 +21 -0
- package/README.md +46 -0
- package/app.js +31 -0
- package/package.json +35 -0
- package/server/controller/RecommendController.js +22 -0
- package/server/log-options.js +30 -0
- package/server/model/RecommendModel.js +101 -0
- package/server/service/RecommendService.js +82 -0
- package/server/sql/recommend-sql.json +6 -0
- package/server/util/check.js +29 -0
- package/server/util/feishu.js +16 -0
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/README.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# @shun-js/recommend
|
|
2
|
+
|
|
3
|
+
shun.js服务:推荐服务
|
|
4
|
+
|
|
5
|
+
## 配置文件
|
|
6
|
+
|
|
7
|
+
以`recommend.json`命名
|
|
8
|
+
|
|
9
|
+
```json
|
|
10
|
+
{
|
|
11
|
+
"port": 8004,
|
|
12
|
+
"db": {
|
|
13
|
+
"connectionLimit": 20,
|
|
14
|
+
"host": "127.0.0.1",
|
|
15
|
+
"port": 3306,
|
|
16
|
+
"database": "db_recommend",
|
|
17
|
+
"user": "root",
|
|
18
|
+
"password": "xx",
|
|
19
|
+
"timezone": "Asia/Shanghai",
|
|
20
|
+
"charset": "utf8mb4"
|
|
21
|
+
},
|
|
22
|
+
"feishu": {
|
|
23
|
+
"url": "http://127.0.0.1:8001/feishu/bot",
|
|
24
|
+
"feishuUrl": "https://open.feishu.cn/open-apis/bot/v2/hook/2cd0xx"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## 启动
|
|
30
|
+
|
|
31
|
+
```shell
|
|
32
|
+
# 全局安装该服务
|
|
33
|
+
npm i -g @shun-js/user
|
|
34
|
+
|
|
35
|
+
# 安装shun-cli
|
|
36
|
+
npm i -g @shun-js/shun-cli
|
|
37
|
+
|
|
38
|
+
# 启动,在recommend.json所在目录下
|
|
39
|
+
shunjs start @shun-js/recommend
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## 请求
|
|
43
|
+
|
|
44
|
+
```shell
|
|
45
|
+
|
|
46
|
+
```
|
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,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@shun-js/recommend",
|
|
3
|
+
"version": "0.3.2",
|
|
4
|
+
"description": "recommend",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"shun.js",
|
|
7
|
+
"recommend"
|
|
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.3.0",
|
|
25
|
+
"@shun-js/shun-service": "^0.3.1",
|
|
26
|
+
"qiao-log": "^5.1.9",
|
|
27
|
+
"qiao-mysql": "^5.1.9",
|
|
28
|
+
"qiao-z": "^5.7.7"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public",
|
|
32
|
+
"registry": "https://registry.npmjs.org/"
|
|
33
|
+
},
|
|
34
|
+
"gitHead": "a31fd2bf7127a36ce9d8759e28ba55fd750b26bd"
|
|
35
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// service
|
|
2
|
+
const service = require('../service/RecommendService.js');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* recommend controller
|
|
6
|
+
*/
|
|
7
|
+
module.exports = (app) => {
|
|
8
|
+
// recommend add
|
|
9
|
+
app.post('/recommend/add', (req, res) => {
|
|
10
|
+
service.recommendAdd(req, res);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
// recommend list
|
|
14
|
+
app.post('/recommend/list', (req, res) => {
|
|
15
|
+
service.recommendList(req, res);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
// recommend change
|
|
19
|
+
app.post('/recommend/change', (req, res) => {
|
|
20
|
+
service.recommendChange(req, res);
|
|
21
|
+
});
|
|
22
|
+
};
|
|
@@ -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,101 @@
|
|
|
1
|
+
// mysql
|
|
2
|
+
const sql = require('../sql/recommend-sql.json');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* addRecommend
|
|
6
|
+
* @param {*} req
|
|
7
|
+
* @param {*} res
|
|
8
|
+
*/
|
|
9
|
+
exports.addRecommend = async (req, res) => {
|
|
10
|
+
const methodName = 'addRecommend';
|
|
11
|
+
|
|
12
|
+
// const
|
|
13
|
+
const userId = req.body.userId;
|
|
14
|
+
const newUserId = req.body.newUserId;
|
|
15
|
+
|
|
16
|
+
// params
|
|
17
|
+
const params = [];
|
|
18
|
+
params.push(userId);
|
|
19
|
+
params.push(newUserId);
|
|
20
|
+
|
|
21
|
+
// add recommend
|
|
22
|
+
try {
|
|
23
|
+
await req.db.query(sql.addRecommend, params);
|
|
24
|
+
|
|
25
|
+
const msg = 'add success';
|
|
26
|
+
req.logger.info(methodName, msg);
|
|
27
|
+
res.jsonSuccess(msg);
|
|
28
|
+
} catch (error) {
|
|
29
|
+
const msg = 'add fail';
|
|
30
|
+
req.logger.error(methodName, msg, error.name, error.message);
|
|
31
|
+
res.jsonFail(msg);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* listRecommend
|
|
37
|
+
* @param {*} req
|
|
38
|
+
* @param {*} res
|
|
39
|
+
* @param {*} userId
|
|
40
|
+
*/
|
|
41
|
+
exports.listRecommend = async (req, res, userId) => {
|
|
42
|
+
const methodName = 'listRecommend';
|
|
43
|
+
|
|
44
|
+
// list recommend
|
|
45
|
+
try {
|
|
46
|
+
return await req.db.query(sql.listRecommend, [userId]);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
const msg = 'list fail';
|
|
49
|
+
req.logger.error(methodName, msg, error.name, error.message);
|
|
50
|
+
res.jsonFail(msg);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* getRecommend
|
|
56
|
+
* @param {*} req
|
|
57
|
+
* @param {*} res
|
|
58
|
+
*/
|
|
59
|
+
exports.getRecommend = async (req, res) => {
|
|
60
|
+
const methodName = 'getRecommend';
|
|
61
|
+
|
|
62
|
+
// const
|
|
63
|
+
const userId = req.body.userId;
|
|
64
|
+
const newUserId = req.body.newUserId;
|
|
65
|
+
|
|
66
|
+
// params
|
|
67
|
+
const params = [];
|
|
68
|
+
params.push(userId);
|
|
69
|
+
params.push(newUserId);
|
|
70
|
+
|
|
71
|
+
// get recommend
|
|
72
|
+
try {
|
|
73
|
+
return await req.db.query(sql.getRecommend, params);
|
|
74
|
+
} catch (error) {
|
|
75
|
+
const msg = 'get fail';
|
|
76
|
+
req.logger.error(methodName, msg, error.name, error.message);
|
|
77
|
+
res.jsonFail(msg);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* updateRecommend
|
|
83
|
+
* @param {*} req
|
|
84
|
+
* @param {*} res
|
|
85
|
+
*/
|
|
86
|
+
exports.updateRecommend = async (req, res, id) => {
|
|
87
|
+
const methodName = 'updateRecommend';
|
|
88
|
+
|
|
89
|
+
// get recommend
|
|
90
|
+
try {
|
|
91
|
+
await req.db.query(sql.updateRecommend, [id]);
|
|
92
|
+
|
|
93
|
+
const msg = 'update success';
|
|
94
|
+
req.logger.info(methodName, msg);
|
|
95
|
+
res.jsonSuccess(msg);
|
|
96
|
+
} catch (error) {
|
|
97
|
+
const msg = 'update fail';
|
|
98
|
+
req.logger.error(methodName, msg, error.name, error.message);
|
|
99
|
+
res.jsonFail(msg);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// util
|
|
2
|
+
const { checkParams } = require('../util/check.js');
|
|
3
|
+
const { feishuMsg } = require('../util/feishu.js');
|
|
4
|
+
|
|
5
|
+
// model
|
|
6
|
+
const { addRecommend, listRecommend, getRecommend, updateRecommend } = require('../model/RecommendModel.js');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* recommendAdd
|
|
10
|
+
* @param {*} req
|
|
11
|
+
* @param {*} res
|
|
12
|
+
*/
|
|
13
|
+
exports.recommendAdd = async (req, res) => {
|
|
14
|
+
// check params
|
|
15
|
+
const checkParamsRes = checkParams(req, res);
|
|
16
|
+
if (!checkParamsRes) return;
|
|
17
|
+
|
|
18
|
+
// add
|
|
19
|
+
await addRecommend(req, res);
|
|
20
|
+
|
|
21
|
+
// feishu
|
|
22
|
+
feishuMsg(`【提醒】用户-${req.body.userId},推荐新用户-${req.body.newUserId}注册成功!`);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* recommendList
|
|
27
|
+
* @param {*} req
|
|
28
|
+
* @param {*} res
|
|
29
|
+
* @returns
|
|
30
|
+
*/
|
|
31
|
+
exports.recommendList = async (req, res) => {
|
|
32
|
+
const methodName = 'recommendList';
|
|
33
|
+
|
|
34
|
+
// check
|
|
35
|
+
const userId = req.body.userId;
|
|
36
|
+
if (!userId) {
|
|
37
|
+
const msg = 'need userId';
|
|
38
|
+
req.logger.warn(methodName, msg, req.body);
|
|
39
|
+
res.jsonFail(msg);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// list
|
|
44
|
+
const list = await listRecommend(req, res, userId);
|
|
45
|
+
if (!list) return;
|
|
46
|
+
|
|
47
|
+
// r
|
|
48
|
+
res.jsonSuccess('list success', list);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* recommendChange
|
|
53
|
+
* @param {*} req
|
|
54
|
+
* @param {*} res
|
|
55
|
+
* @returns
|
|
56
|
+
*/
|
|
57
|
+
exports.recommendChange = async (req, res) => {
|
|
58
|
+
const methodName = 'recommendChange';
|
|
59
|
+
|
|
60
|
+
// check params
|
|
61
|
+
const checkParamsRes = checkParams(req, res);
|
|
62
|
+
if (!checkParamsRes) return;
|
|
63
|
+
|
|
64
|
+
// get
|
|
65
|
+
const recommends = await getRecommend(req, res);
|
|
66
|
+
if (!recommends) return;
|
|
67
|
+
|
|
68
|
+
// check recommends
|
|
69
|
+
if (recommends.length !== 1) {
|
|
70
|
+
const msg = '该记录已经兑换!';
|
|
71
|
+
req.logger.error(methodName, msg);
|
|
72
|
+
res.jsonFail(msg);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// update
|
|
77
|
+
const recommend = recommends[0];
|
|
78
|
+
await updateRecommend(req, res, recommend.id);
|
|
79
|
+
|
|
80
|
+
// feishu
|
|
81
|
+
feishuMsg(`【提醒】用户-${recommend.recommend_user_id}-${recommend.recommend_newuser_id}兑换VIP会员成功!`);
|
|
82
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"addRecommend": "insert into t_recommend values(null, ?,?,'0', now())",
|
|
3
|
+
"listRecommend": "select * from t_recommend where recommend_user_id=? order by id desc",
|
|
4
|
+
"getRecommend": "select * from t_recommend where recommend_user_id=? and recommend_newuser_id=? and recommend_exchange='0'",
|
|
5
|
+
"updateRecommend": "update t_recommend set recommend_exchange='1' where id=?"
|
|
6
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* checkParams
|
|
3
|
+
* @param {*} req
|
|
4
|
+
* @param {*} res
|
|
5
|
+
* @returns
|
|
6
|
+
*/
|
|
7
|
+
exports.checkParams = async (req, res) => {
|
|
8
|
+
const methodName = 'checkParams';
|
|
9
|
+
|
|
10
|
+
// const
|
|
11
|
+
const userId = req.body.userId;
|
|
12
|
+
const newUserId = req.body.newUserId;
|
|
13
|
+
|
|
14
|
+
// check
|
|
15
|
+
if (!userId) {
|
|
16
|
+
const msg = 'need userId';
|
|
17
|
+
req.logger.warn(methodName, msg, req.body);
|
|
18
|
+
res.jsonFail(msg);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
if (!newUserId) {
|
|
22
|
+
const msg = 'need newUserId';
|
|
23
|
+
req.logger.warn(methodName, msg, req.body);
|
|
24
|
+
res.jsonFail(msg);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return true;
|
|
29
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// services
|
|
2
|
+
const { feishuBot } = require('@shun-js/shun-service');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* feishuMsg
|
|
6
|
+
* @param {*} msg
|
|
7
|
+
*/
|
|
8
|
+
exports.feishuMsg = (msg) => {
|
|
9
|
+
if (global.QZ_CONFIG.env !== 'production') return;
|
|
10
|
+
|
|
11
|
+
feishuBot({
|
|
12
|
+
url: global.QZ_CONFIG.feishu.url,
|
|
13
|
+
feishuUrl: global.QZ_CONFIG.feishu.feishuUrl,
|
|
14
|
+
feishuMsg: msg,
|
|
15
|
+
});
|
|
16
|
+
};
|