@shun-js/user 0.4.2 → 0.4.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.4.2",
3
+ "version": "0.4.4",
4
4
  "description": "user",
5
5
  "keywords": [
6
6
  "shun.js",
@@ -34,5 +34,5 @@
34
34
  "access": "public",
35
35
  "registry": "https://registry.npmjs.org/"
36
36
  },
37
- "gitHead": "e1d550be00818ca0da0e1f89cfe81a6df70123ed"
37
+ "gitHead": "2defaa9089506eb53bcae4d248d281b98465e6ad"
38
38
  }
@@ -2,18 +2,18 @@
2
2
  const sql = require('../sql/UserInfoSQL.js');
3
3
 
4
4
  /**
5
- * getUserInfoById
5
+ * isUserInfoExists
6
6
  * @param {*} req
7
7
  * @param {*} res
8
8
  * @param {*} id
9
9
  * @returns
10
10
  */
11
- exports.getUserInfoById = async (req, res, id) => {
12
- const methodName = 'getUserInfoById';
11
+ exports.isUserInfoExists = async (req, res, id) => {
12
+ const methodName = 'isUserInfoExists';
13
13
 
14
14
  // get user item
15
15
  try {
16
- return await req.db.query(sql.getUserInfoById, [id]);
16
+ return await req.db.query(sql.isUserInfoExists, [id]);
17
17
  } catch (error) {
18
18
  const msg = '获取用户信息失败!';
19
19
  req.logger.error(methodName, msg, error.name, error.message);
@@ -46,3 +46,31 @@ exports.addUserInfo = async (req, res, userItemId, githubUserinfo) => {
46
46
  res.jsonFail(msg);
47
47
  }
48
48
  };
49
+
50
+ /**
51
+ * getUserInfoById
52
+ * @param {*} req
53
+ * @param {*} res
54
+ * @param {*} id
55
+ * @returns
56
+ */
57
+ exports.getUserInfoById = async (req, res, id) => {
58
+ const methodName = 'getUserInfoById';
59
+
60
+ // get user item
61
+ try {
62
+ const getUserInfoByIdRes = await req.db.query(sql.getUserInfoById, [id]);
63
+ if (getUserInfoByIdRes.length !== 1) {
64
+ const msg = '用户信息长度非1!';
65
+ req.logger.error(methodName, msg);
66
+ res.jsonFail(msg);
67
+ return;
68
+ }
69
+
70
+ return getUserInfoByIdRes[0];
71
+ } catch (error) {
72
+ const msg = '获取用户信息失败!';
73
+ req.logger.error(methodName, msg, error.name, error.message);
74
+ res.jsonFail(msg);
75
+ }
76
+ };
@@ -1,5 +1,5 @@
1
1
  // model
2
- const { getUserInfoById, addUserInfo } = require('../model/UserInfoModel.js');
2
+ const { addUserInfo, isUserInfoExists } = require('../model/UserInfoModel.js');
3
3
 
4
4
  // github
5
5
  const { getGithubUserinfo } = require('../util/github.js');
@@ -42,9 +42,9 @@ exports.userGithub = async (req, res) => {
42
42
  req.logger.info(methodName, 'github login or reg ok');
43
43
 
44
44
  // user info
45
- const getUserInfoByIdRes = await getUserInfoById(req, res, userItem.id);
46
- if (!getUserInfoByIdRes) return;
47
- if (getUserInfoByIdRes.length === 0) {
45
+ const isUserInfoExistsRes = await isUserInfoExists(req, res, userItem.id);
46
+ if (!isUserInfoExistsRes) return;
47
+ if (isUserInfoExistsRes.length === 0) {
48
48
  const addUserInfoRes = await addUserInfo(req, res, userItem.id, githubUserinfo);
49
49
  if (!addUserInfoRes) return;
50
50
  }
@@ -1,6 +1,9 @@
1
1
  // model
2
2
  const { getUserInfoById } = require('../model/UserInfoModel.js');
3
3
 
4
+ // util
5
+ const { getUserAccessToken } = require('../util/user.js');
6
+
4
7
  /**
5
8
  * userInfo
6
9
  * @param {*} req
@@ -23,6 +26,10 @@ exports.userInfo = async (req, res) => {
23
26
  const getUserInfoByIdRes = await getUserInfoById(req, res, userid);
24
27
  if (!getUserInfoByIdRes) return;
25
28
 
29
+ // access token
30
+ const accessToken = await getUserAccessToken(req, userid);
31
+ getUserInfoByIdRes.accessToken = accessToken;
32
+
26
33
  // r
27
34
  res.jsonSuccess('登录成功!', getUserInfoByIdRes);
28
35
  };
@@ -1,12 +1,13 @@
1
- exports.getUserInfoById = `
1
+ exports.isUserInfoExists = `
2
2
  select
3
- *
3
+ 1
4
4
  from
5
5
  t_user_info
6
6
  where
7
7
  user_item_id=?
8
8
  and
9
9
  del_tag='0'
10
+ LIMIT 1
10
11
  `;
11
12
 
12
13
  exports.addUserInfo = `
@@ -15,3 +16,15 @@ insert into
15
16
  values
16
17
  (null, ?, ?, ?, ?, now(), '0')
17
18
  `;
19
+
20
+ exports.getUserInfoById = `
21
+ select
22
+ *
23
+ from
24
+ t_user_info
25
+ where
26
+ user_item_id=?
27
+ and
28
+ del_tag='0'
29
+ LIMIT 1
30
+ `;
@@ -1,5 +1,5 @@
1
1
  // qiao
2
- const { AESEncrypt } = require('qiao-encode');
2
+ const { AESEncrypt, uuid } = require('qiao-encode');
3
3
 
4
4
  // model
5
5
  const { getUserItemByName, addUserItem } = require('../model/UserItemModel.js');
@@ -60,3 +60,21 @@ exports.loginORRegUser = async (req, res, mobileOREmail) => {
60
60
  // r
61
61
  return userItem;
62
62
  };
63
+
64
+ /**
65
+ * getUserAccessToken
66
+ * @param {*} req
67
+ * @param {*} userid
68
+ * @returns
69
+ */
70
+ exports.getUserAccessToken = async (req, userid) => {
71
+ // get
72
+ const acKey = `ac-${userid}`;
73
+ const accessToken = await req.redis.get(acKey);
74
+ if (accessToken) return accessToken;
75
+
76
+ // set
77
+ const newAccessToken = uuid();
78
+ await req.redis.set(acKey, newAccessToken);
79
+ return newAccessToken;
80
+ };