1
+ /**
2
+ * @desc 访客信息处理
3
+ */
4
+
1
5
const express = require ( 'express' )
2
6
const { Octokit } = require ( '@octokit/core' )
3
7
const api = require ( '../http/server-api' )
@@ -6,77 +10,107 @@ const db = require('../db/')
6
10
const secret = require ( '../db/secret' )
7
11
8
12
// 存储访客信息
9
- router . post ( '/api/front/saveVisitor' , async ( req , res ) => {
10
- if ( req . body . type === '0' ) {
11
- const exist = await db . visitor . find ( { name : req . body . name } )
12
- if ( exist . length ) {
13
- res . json ( { status : 100 , info : '用户名已存在' } )
14
- return
13
+ router . post ( '/api/front/visitor/save' , async ( req , res ) => {
14
+ try {
15
+ // 自定义用户
16
+ if ( req . body . type === '0' ) {
17
+ const exist = await db . visitor . find ( { name : req . body . name } )
18
+ if ( exist . length ) {
19
+ res . json ( { status : 100 , info : '用户名已存在' } )
20
+ return
21
+ }
15
22
}
23
+
24
+ const doc = await new db . visitor ( req . body ) . save ( )
25
+
26
+ res . json ( {
27
+ status : 200 ,
28
+ data : doc ,
29
+ info : '访客信息存储成功'
30
+ } )
31
+ } catch ( e ) {
32
+ res . status ( 500 ) . end ( )
16
33
}
17
- new db . visitor ( req . body ) . save ( ( err , doc ) => {
18
- if ( err ) {
19
- res . status ( 500 ) . end ( )
20
- } else {
21
- res . json ( { status : 200 , data : doc } )
22
- }
23
- } )
24
34
} )
25
35
26
- //自定义用户名
27
- router . get ( '/api/searchVisitor' , ( req , res ) => {
28
- db . vistor . find ( { name : req . query . name } , ( err , doc ) => {
29
- if ( doc . length ) {
30
- res . json ( { status : 200 , exist : 1 } )
31
- } else {
32
- res . json ( { status : 200 , exist : 0 } )
36
+ // 查看访客是否已经被存储
37
+ router . post ( '/api/front/visitor/existed' , async ( req , res ) => {
38
+ try {
39
+ // QQ用户
40
+ if ( req . body . type === '1' ) {
41
+ const exist = await db . visitor . find ( { qqOpenId : req . body . qqOpenId } )
42
+ if ( exist . length ) {
43
+ // 仅更新昵称和头像
44
+ await db . visitor . update (
45
+ { qqOpenId : req . body . qqOpenId } ,
46
+ { $set : { name : req . body . name , imgUrl : req . body . imgUrl } }
47
+ )
48
+ const doc = await db . visitor . find ( { qqOpenId : req . body . qqOpenId } )
49
+ res . json ( { status : 200 , info : '访客信息已存在' , data : { info : doc [ 0 ] , _saved : 1 } } )
50
+ return
51
+ }
33
52
}
34
- } )
53
+ res . json ( {
54
+ status : 200 ,
55
+ data : {
56
+ _saved : 0
57
+ } ,
58
+ info : '访客信息不存在'
59
+ } )
60
+ } catch ( e ) {
61
+ res . status ( 500 ) . end ( )
62
+ }
35
63
} )
36
64
37
- //github登录
38
- router . get ( '/api/getGithub' , ( req , res ) => {
39
- db . vistor . find ( { githubID : req . query . id } , ( err , doc ) => {
40
- if ( err ) {
41
- res . status ( 500 ) . end ( )
42
- } else {
43
- res . json ( doc )
44
- }
45
- } )
46
- } )
65
+ // github登录
66
+
47
67
router . get ( '/login/git' , ( req , res ) => {
48
68
//请替换为自己的client_id
49
69
let path = `https://github.com/login/oauth/authorize?client_id=${ secret . github_client_id } &scope=['user']&redirect_uri=http://localhost:6180/login_github`
50
70
res . redirect ( path )
51
71
res . status ( 200 ) . end ( )
52
72
} )
53
73
router . get ( '/login_github' , ( req , res ) => {
54
- console . log ( '已经指向到login-github::' , req . query )
55
- const params = {
56
- client_id : secret . github_client_id ,
57
- client_secret : secret . github_client_secret ,
58
- code : req . query . code ,
59
- scope : [ 'user' ] ,
60
- redirect_uri : 'http://localhost:6180/login_github'
74
+ try {
75
+ const params = {
76
+ client_id : secret . github_client_id ,
77
+ client_secret : secret . github_client_secret ,
78
+ code : req . query . code ,
79
+ scope : [ 'user' ] ,
80
+ redirect_uri : 'http://localhost:6180/login_github'
81
+ }
82
+ api
83
+ . post ( 'https://github.com/login/oauth/access_token' , JSON . parse ( JSON . stringify ( params ) ) )
84
+ . then ( fullData => {
85
+ const arr1 = fullData . split ( '&' )
86
+ const arr2 = arr1 [ 0 ] . split ( '=' )
87
+ const token = arr2 [ 1 ]
88
+ return token
89
+ } )
90
+ . then ( async token => {
91
+ let userInfo = { }
92
+ const octokit = new Octokit ( { auth : `${ token } ` } )
93
+ const info = await octokit . request ( 'GET /user' )
94
+ // 查看访客表是否已经存在此用户
95
+ const visitor = await db . visitor . find ( { githubId : info . data . id } )
96
+ // 存在此用户
97
+ if ( visitor . length ) {
98
+ await db . visitor . update (
99
+ { githubId : info . data . id } ,
100
+ { $set : { name : info . data . login , imgUrl : info . data . avatar_url } }
101
+ )
102
+ const doc = await db . visitor . find ( { githubId : info . data . id } )
103
+ userInfo = {
104
+ ...doc [ 0 ] . toObject ( ) ,
105
+ // 前端判断存在的标识
106
+ _saved : 1
107
+ }
108
+ }
109
+ res . render ( 'gc_back.html' , { title : 'github登陆成功' , userInfo : JSON . stringify ( userInfo ) } )
110
+ } )
111
+ } catch ( e ) {
112
+ res . status ( 500 ) . end ( )
61
113
}
62
- api
63
- . post ( 'https://github.com/login/oauth/access_token' , params )
64
- . then ( fullData => {
65
- const arr1 = fullData . split ( '&' )
66
- const arr2 = arr1 [ 0 ] . split ( '=' )
67
- const token = arr2 [ 1 ]
68
- console . log ( '获取到token====>>>>>' , token )
69
- return token
70
- } )
71
- . then ( async token => {
72
- const octokit = new Octokit ( { auth : `${ token } ` } )
73
- const info = await octokit . request ( 'GET /user' )
74
- console . log ( '返回用户信息====>>>>' , info . data )
75
- res . render ( 'gc_back.html' , { title : 'github登陆成功' , userInfo : JSON . stringify ( info . data ) } )
76
- } )
77
- . catch ( err => {
78
- res . status ( 500 ) . end ( )
79
- } )
80
114
} )
81
115
82
116
module . exports = router
0 commit comments