Browse Source

feat: add findIdByPhone

Bay 5 years ago
parent
commit
a0989530eb
6 changed files with 34 additions and 34 deletions
  1. 2 1
      server/config/db.js
  2. 0 12
      server/controller/oidc.js
  3. 29 19
      server/controller/user.js
  4. 1 0
      server/index.js
  5. 1 1
      server/model/teacher.js
  6. 1 1
      server/routes/index.js

+ 2 - 1
server/config/db.js

@@ -1,6 +1,7 @@
 const Sequelize = require('sequelize')
 
-const sequelize = new Sequelize('mysql://root:123456@localhost:3306/seec_portal', {
+// const sequelize = new Sequelize('mysql://root:123456@localhost:3306/seec_portal', {
+const sequelize = new Sequelize('mysql://root:NJU67mysql@172.29.7.180:3306/seec_portal', {
   define: { timestamps: false },
   timezone: '+08:00' // 东八时区
 })

+ 0 - 12
server/controller/oidc.js

@@ -1,12 +0,0 @@
-
-const authenticate = async (ctx, provider) => {
-  console.log('ctx.session', ctx.session)
-  const session = await provider.setProviderSession(ctx.req, ctx.res, { account: ctx.req.headers.cookie })
-  console.log('ctx.', ctx.session)
-  const details = await provider.interactionDetails(ctx.req, ctx.res)
-  console.log('detail', JSON.stringify(details))
-}
-
-module.exports = {
-  authenticate
-}

+ 29 - 19
server/controller/user.js

@@ -24,20 +24,20 @@ class UserController {
       } else {
         // console.log('ctx',ctx);
         // console.log('ctx.cookies.redirect_uri',ctx.cookies.get('redirect_uri'));
-        let redirect_uri = ctx.cookies.get('redirect_uri')
-        let client_id = ctx.cookies.get('client_id')
+        let redirectUri = ctx.cookies.get('redirect_uri')
+        let clientId = ctx.cookies.get('client_id')
 
-        if (!redirect_uri) {
-          redirect_uri = ctx.origin + '/api/receiveIDToken'
+        if (!redirectUri) {
+          redirectUri = ctx.origin + '/api/receiveIDToken'
         }
-        if (!client_id) {
-          client_id = 'seec-portal'
+        if (!clientId) {
+          clientId = 'seec-portal'
         }
 
         const data = {
           iss: 'p.seecoder.cn',
           sub: user.phone,
-          aud: client_id,
+          aud: clientId,
           auth_time: Math.floor(new Date().getTime() / 1000),
           iat: Math.floor(new Date().getTime() / 1000),
           exp: Math.floor(new Date().getTime() / 1000 + 24 * 60 * 60),
@@ -53,7 +53,7 @@ class UserController {
           maxAge: 0
         })
 
-        ctx.body = Response.success({ token: token, name: user.name, path: redirect_uri })
+        ctx.body = Response.success({ token: token, name: user.name, path: redirectUri })
       }
     } else {
       ctx.body = Response.failed('此账号不存在')
@@ -106,13 +106,14 @@ class UserController {
       TemplateCode: 'SMS_181555598',
       TemplateParam: '{"code":"' + code + '"}'
     }
-    const options = {
-      timeout: 3000, // default 3000 ms
-      formatAction: true, // default true, format the action to Action
-      formatParams: true, // default true, format the parameter name to first letter upper case
-      method: 'GET', // set the http method, default is GET
-      headers: {} // set the http request headers
-    }
+    // TODO: ??? 这里写个 options 到底是要干啥
+    // const options = {
+    //   timeout: 3000, // default 3000 ms
+    //   formatAction: true, // default true, format the action to Action
+    //   formatParams: true, // default true, format the parameter name to first letter upper case
+    //   method: 'GET', // set the http method, default is GET
+    //   headers: {} // set the http request headers
+    // }
     try {
       const result = await aliClient.request('SendSms', params)
       // console.log("result:", result);
@@ -133,7 +134,7 @@ class UserController {
   }
 
   static async getInfo (ctx) {
-    const theUser = await getTokenUser(ctx)
+    const theUser = getTokenUser(ctx)
     if (!theUser) {
       ctx.status = 401
     } else {
@@ -148,7 +149,7 @@ class UserController {
   }
 
   static async saveName (ctx) {
-    const theUser = await getTokenUser(ctx)
+    const theUser = getTokenUser(ctx)
     if (!theUser) {
       ctx.status = 401
     } else {
@@ -231,8 +232,17 @@ class UserController {
     }
   }
 
-  static async findIdByName (ctx) {
-
+  /**
+   * 手机号搜id
+   */
+  static async findIdByPhone (ctx) {
+    const phone = ctx.params.phone
+    const user = await userModel.findByPhone(phone)
+    if (user) {
+      ctx.body = Response.success({ id: user.id })
+    } else {
+      ctx.body = Response.failed('此手机号未注册')
+    }
   }
 }
 

+ 1 - 0
server/index.js

@@ -109,6 +109,7 @@ async function start () {
       /\/api\/user\/register/,
       /\/api\/user\/sendMessage/,
       /\/api\/user\/resetPassword/,
+      /\/api\/user\/findIdByPhone/,
       /\/api\/interaction/,
       /\/api\/receiveIDToken/,
       /\/api\/authenticate/,

+ 1 - 1
server/model/teacher.js

@@ -13,7 +13,7 @@ class TeacherModel {
 
   static async create ({ realName, organization, jobNumber, certificate = '', id, phone, email }) {
     try {
-      const teacher = await Teacher.create({
+      await Teacher.create({
         realName: realName,
         organization: organization,
         jobNumber: jobNumber,

+ 1 - 1
server/routes/index.js

@@ -1,7 +1,6 @@
 const Router = require('koa-router')
 const UserController = require('../controller/user')
 const CourseController = require('../controller/course')
-const OIDCController = require('../controller/oidc')
 const AuthController = require('../controller/auth')
 const TeacherController = require('../controller/teacher')
 const AdminController = require('../controller/admin')
@@ -16,6 +15,7 @@ module.exports = () => {
   router.post('/user/saveEmail', UserController.saveEmail)
   router.post('/user/saveSignature', UserController.saveSignature)
   router.post('/user/resetPassword', UserController.resetPassword)
+  router.get('/user/findIdByPhone/:phone', UserController.findIdByPhone)
 
   router.get('/teacher/getInfo', TeacherController.getInfo)
   router.post('/teacher/apply', TeacherController.apply)