Bläddra i källkod

增加对接 sso接口 获取token

wanghao 1 månad sedan
förälder
incheckning
9e0d25152b
3 ändrade filer med 49 tillägg och 0 borttagningar
  1. 5 0
      app.js
  2. 36 0
      router/user.router.js
  3. 8 0
      service/user.service.js

+ 5 - 0
app.js

@@ -22,6 +22,9 @@ app.use(
  */
 app.use(async (ctx, next) => {
   try {
+    console.log("aap.js 中间件处理")
+    console.log(ctx)
+
     await next();
   } catch (err) {
     if (err.name === 'UnauthorizedError') {
@@ -64,10 +67,12 @@ app.use(
   }),
 );
 
+//addbywanghao  sso   20250512   /^\/api\/user\/ssoLogin/,
 // token 鉴权
 app.use(
   koajwt({ secret: config.JWT_SECRET }).unless({
     path: [
+      /^\/api\/user\/ssoLogin/,
       /^\/api\/user\/login/,
       /^\/api\/user\/wechat/,
       /^\/api\/user\/sendEmail/,

+ 36 - 0
router/user.router.js

@@ -39,6 +39,9 @@ router.post('/login', async (ctx) => {
     }
   }
 
+  console.log("user res信息")
+  console.log(res)
+
   const token = util.createToken({ userName, userId: res.id, nickName: res.nickName });
   userService.updateUserInfo(res.id);
   util.success(ctx, {
@@ -46,6 +49,9 @@ router.post('/login', async (ctx) => {
     userName,
     token,
   });
+
+  console.log("新的token:"+token)
+
 });
 
 /**
@@ -439,4 +445,34 @@ router.post('/subUser/delete', async (ctx) => {
     util.fail(ctx, '删除失败,请重试');
   }
 });
+
+//addbywanghao  sso   20250512
+router.post('/ssoLogin', async (ctx) => {
+  const { userIdno } = ctx.request.body;
+  if (!userIdno ) {
+    util.fail(ctx, 'userIdno不能为空');
+    return;
+  }
+  const res = await userService.findUserSso(userIdno);
+
+  if (!res) {
+    util.fail(ctx, 'userIdno 不存在,暂无权限');
+    return;
+  }
+
+  console.log("user res信息")
+  console.log(res)
+
+  const token = util.createToken({ userName: res.userName, userId: res.id, nickName: res.nickName });
+  userService.updateUserInfo(res.id);
+  util.success(ctx, {
+    userId: res.id,
+    userName: res.userName,
+    token,
+  });
+
+  console.log("sso新的token:"+token)
+
+});
+
 module.exports = router;

+ 8 - 0
service/user.service.js

@@ -96,5 +96,13 @@ class UserService {
     const [result] = await connection.execute(statement, [id, parentId]);
     return result;
   }
+
+  //addbywanghao  sso   20250512
+  async findUserSso(userName) {
+    const statement =
+      'SELECT id, open_id as openId, user_name as userName, nick_name as nickName, avatar FROM users WHERE  user_name = ? and parent_id is null ;';
+    const [result] = await connection.execute(statement, [userName]);
+    return result[0];
+  }
 }
 module.exports = new UserService();