@shun-js/user 0.3.2 → 0.3.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shun-js/user",
3
- "version": "0.3.2",
3
+ "version": "0.3.4",
4
4
  "description": "user",
5
5
  "keywords": [
6
6
  "shun.js",
@@ -23,6 +23,7 @@
23
23
  "dependencies": {
24
24
  "@shun-js/shun-config": "^0.3.1",
25
25
  "@shun-js/shun-service": "^0.3.1",
26
+ "qiao-ajax": "^5.2.4",
26
27
  "qiao-encode": "^5.0.6",
27
28
  "qiao-log": "^5.1.9",
28
29
  "qiao-mysql": "^5.8.6",
@@ -33,5 +34,5 @@
33
34
  "access": "public",
34
35
  "registry": "https://registry.npmjs.org/"
35
36
  },
36
- "gitHead": "4fa47fe5cf1b00acfe54f47af836101291093f06"
37
+ "gitHead": "ff2465fb870eea80a1447911e3bfbe8121b5ed28"
37
38
  }
@@ -0,0 +1,12 @@
1
+ // service
2
+ const service = require('../service/UserGithubService.js');
3
+
4
+ /**
5
+ * controller
6
+ */
7
+ module.exports = (app) => {
8
+ // user github
9
+ app.post('/user/github', (req, res) => {
10
+ service.userGithub(req, res);
11
+ });
12
+ };
@@ -0,0 +1,36 @@
1
+ // github
2
+ const { getGithubUserinfo } = require('../util/github.js');
3
+
4
+ /**
5
+ * userGithub
6
+ * @param {*} req
7
+ * @param {*} res
8
+ * @returns
9
+ */
10
+ exports.userGithub = async (req, res) => {
11
+ const methodName = 'userGithub';
12
+
13
+ // fallback url
14
+ const fallbackUrl = global.QZ_CONFIG.github.fallbackUrl;
15
+
16
+ // const
17
+ const code = req.body.code;
18
+ if (!code) {
19
+ const msg = 'need code';
20
+ req.logger.warn(methodName, msg, req.body);
21
+ res.jsonFail(msg);
22
+ return;
23
+ }
24
+
25
+ // userinfo
26
+ const githubUserinfo = await getGithubUserinfo(req, code);
27
+ if (!githubUserinfo) {
28
+ req.logger.error(methodName, 'githubUserinfo is null');
29
+ res.redirect(fallbackUrl);
30
+ return;
31
+ }
32
+ req.logger.info(methodName, 'githubUserinfo', githubUserinfo);
33
+
34
+ // r
35
+ res.jsonSuccess('登录成功!');
36
+ };
@@ -2,10 +2,10 @@
2
2
  const { AESEncrypt } = require('qiao-encode');
3
3
 
4
4
  // model
5
- const { getUserItemByName, addUserItem, getUserItemById } = require('../model/UserItemModel.js');
5
+ const { getUserItemById } = require('../model/UserItemModel.js');
6
6
 
7
- // feishu
8
- const { feishuBot } = require('@shun-js/shun-service');
7
+ // util
8
+ const { loginORRegUser } = require('../util/user.js');
9
9
 
10
10
  /**
11
11
  * userLogin
@@ -48,45 +48,8 @@ exports.userLogin = async (req, res) => {
48
48
  }
49
49
 
50
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);
51
+ const userItem = await loginORRegUser(mobile);
52
+ if (!userItem) return;
90
53
 
91
54
  // r
92
55
  res.jsonSuccess('登录成功!', userItem);
@@ -0,0 +1,126 @@
1
+ // ajax
2
+ const { get } = require('qiao-ajax');
3
+
4
+ /**
5
+ * getGithubUserinfo
6
+ * @param {*} req
7
+ * @param {*} code
8
+ */
9
+ exports.getGithubUserinfo = async (req, code) => {
10
+ const methodName = 'getGithubUserinfo';
11
+
12
+ try {
13
+ // get token
14
+ const tokenUrl = global.QZ_CONFIG.github.tokenUrl;
15
+ const tokenConfig = {
16
+ params: {
17
+ client_id: global.QZ_CONFIG.github.clientID,
18
+ client_secret: global.QZ_CONFIG.github.clientSecret,
19
+ redirect_uri: global.QZ_CONFIG.github.callbackUrl,
20
+ code: code,
21
+ },
22
+ headers: {
23
+ Accept: 'application/json',
24
+ },
25
+ };
26
+ const tokenResponse = await get(tokenUrl, tokenConfig);
27
+
28
+ // check
29
+ if (!tokenResponse) {
30
+ req.logger.error(methodName, 'tokenResponse is null');
31
+ return;
32
+ }
33
+ if (tokenResponse.status !== 200) {
34
+ req.logger.error(methodName, 'tokenResponse.status !== 200', tokenResponse.status);
35
+ return;
36
+ }
37
+ if (!tokenResponse.data) {
38
+ req.logger.error(methodName, 'tokenResponse.data is null');
39
+ return;
40
+ }
41
+ if (!tokenResponse.data.access_token || !tokenResponse.data.token_type) {
42
+ req.logger.error(methodName, 'tokenResponse.data.access_token is null or tokenResponse.data.token_type is null');
43
+ return;
44
+ }
45
+
46
+ // get user
47
+ req.logger.info(methodName, 'get token success');
48
+ const userUrl = global.QZ_CONFIG.github.userUrl;
49
+ const userConfig = {
50
+ headers: {
51
+ Authorization: `${tokenResponse.data.token_type} ${tokenResponse.data.access_token}`,
52
+ Accept: 'application/json',
53
+ },
54
+ };
55
+ const userResponse = await get(userUrl, userConfig);
56
+
57
+ // check
58
+ if (!userResponse) {
59
+ req.logger.error(methodName, 'userResponse is null');
60
+ return;
61
+ }
62
+ if (userResponse.status !== 200) {
63
+ req.logger.error(methodName, 'userResponse.status !== 200', userResponse.status);
64
+ return;
65
+ }
66
+ if (!userResponse.data) {
67
+ req.logger.error(methodName, 'userResponse.data is null');
68
+ return;
69
+ }
70
+ req.logger.info(methodName, 'get user success');
71
+
72
+ // r
73
+ return userResponse.data;
74
+ } catch (error) {
75
+ req.logger.error(methodName, 'error', error);
76
+ }
77
+ };
78
+
79
+ // {
80
+ // login: 'uikoo9',
81
+ // id: 10345351,
82
+ // node_id: 'MDQ6VXNlcjEwMzQ1MzUx',
83
+ // avatar_url: 'https://avatars.githubusercontent.com/u/10345351?v=4',
84
+ // gravatar_id: '',
85
+ // url: 'https://api.github.com/users/uikoo9',
86
+ // html_url: 'https://github.com/uikoo9',
87
+ // followers_url: 'https://api.github.com/users/uikoo9/followers',
88
+ // following_url: 'https://api.github.com/users/uikoo9/following{/other_user}',
89
+ // gists_url: 'https://api.github.com/users/uikoo9/gists{/gist_id}',
90
+ // starred_url: 'https://api.github.com/users/uikoo9/starred{/owner}{/repo}',
91
+ // subscriptions_url: 'https://api.github.com/users/uikoo9/subscriptions',
92
+ // organizations_url: 'https://api.github.com/users/uikoo9/orgs',
93
+ // repos_url: 'https://api.github.com/users/uikoo9/repos',
94
+ // events_url: 'https://api.github.com/users/uikoo9/events{/privacy}',
95
+ // received_events_url: 'https://api.github.com/users/uikoo9/received_events',
96
+ // type: 'User',
97
+ // user_view_type: 'private',
98
+ // site_admin: false,
99
+ // name: null,
100
+ // company: null,
101
+ // blog: '',
102
+ // location: null,
103
+ // email: 'uikoo9@qq.com',
104
+ // hireable: null,
105
+ // bio: null,
106
+ // twitter_username: null,
107
+ // notification_email: 'uikoo9@qq.com',
108
+ // public_repos: 27,
109
+ // public_gists: 0,
110
+ // followers: 91,
111
+ // following: 4,
112
+ // created_at: '2014-12-30T03:11:05Z',
113
+ // updated_at: '2026-01-29T09:08:41Z',
114
+ // private_gists: 0,
115
+ // total_private_repos: 33,
116
+ // owned_private_repos: 33,
117
+ // disk_usage: 1026748,
118
+ // collaborators: 4,
119
+ // two_factor_authentication: true,
120
+ // plan: {
121
+ // name: 'free',
122
+ // space: 976562499,
123
+ // collaborators: 0,
124
+ // private_repos: 10000
125
+ // }
126
+ // }
@@ -0,0 +1,62 @@
1
+ // qiao
2
+ const { AESEncrypt } = require('qiao-encode');
3
+
4
+ // model
5
+ const { getUserItemByName, addUserItem } = require('../model/UserItemModel.js');
6
+
7
+ // feishu
8
+ const { feishuBot } = require('@shun-js/shun-service');
9
+
10
+ /**
11
+ * loginORRegUser
12
+ * @param {*} req
13
+ * @param {*} res
14
+ * @param {*} mobileOREmail
15
+ * @returns
16
+ */
17
+ exports.loginORRegUser = async (req, res, mobileOREmail) => {
18
+ const methodName = 'loginORRegUser';
19
+
20
+ // user item
21
+ const getUserItemRes = await getUserItemByName(req, res, mobileOREmail);
22
+ if (!getUserItemRes) return;
23
+
24
+ // reg
25
+ if (getUserItemRes.length !== 1) {
26
+ // add user
27
+ const addUserItemRes = await addUserItem(req, res, mobileOREmail);
28
+ if (!addUserItemRes) return;
29
+
30
+ // feishu
31
+ const finalMsg = `【提醒】success - ${methodName} - new user reg - ${addUserItemRes.id}`;
32
+ const feishuBotRes = await feishuBot({
33
+ url: global.QZ_CONFIG.feishu.url,
34
+ feishuUrl: global.QZ_CONFIG.feishu.feishuUrl,
35
+ feishuMsg: finalMsg,
36
+ });
37
+ req.logger.warn(methodName, 'feishuBotRes', feishuBotRes);
38
+
39
+ // r
40
+ return addUserItemRes;
41
+ }
42
+
43
+ // login
44
+ const user = getUserItemRes[0];
45
+ const userItem = {
46
+ id: user.id,
47
+ usertoken: AESEncrypt(mobileOREmail + user.user_item_password, global.QZ_CONFIG.encryptKey),
48
+ usermobile: mobileOREmail,
49
+ };
50
+
51
+ // feishu
52
+ const finalMsg = `【提醒】success - ${methodName} - user login - ${userItem.id}`;
53
+ const feishuBotRes = await feishuBot({
54
+ url: global.QZ_CONFIG.feishu.url,
55
+ feishuUrl: global.QZ_CONFIG.feishu.feishuUrl,
56
+ feishuMsg: finalMsg,
57
+ });
58
+ req.logger.warn(methodName, 'feishuBotRes', feishuBotRes);
59
+
60
+ // r
61
+ return userItem;
62
+ };