Skip to content

Commit d3be6a6

Browse files
committed
😆 把state抽离出去 >_<#@!
1 parent 4106624 commit d3be6a6

File tree

6 files changed

+110
-136
lines changed

6 files changed

+110
-136
lines changed

src/git-comment.js

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import state from './state';
12
import { appendQuery, getQuery } from "./lib/utils";
2-
import http from "./lib/http";
3+
import * as github from './lib/github';
34

45
const STORAGE_TOKEN_KEY = 'STORAGE_TOKEN_KEY';
56

@@ -11,26 +12,15 @@ class GitComment {
1112
*/
1213
config(options) {
1314
const { client_id, client_secret } = options;
14-
this.client_id = client_id;
15-
this.client_secret = client_secret;
15+
state.client_id = client_id;
16+
state.client_secret = client_secret;
1617
this.init();
1718
}
1819

1920
//#region fields
2021

21-
client_id = ''
22-
23-
client_secret = ''
24-
25-
access_token = ''
26-
27-
/**
28-
* 是否登陆
29-
*
30-
* @memberof GitHub
31-
*/
32-
ifLogin = false
33-
22+
state = state
23+
3424
//#endregion
3525

3626
//#region private methods
@@ -67,16 +57,12 @@ class GitComment {
6757
* @memberof GitComment
6858
*/
6959
_getToken(code) {
70-
http.post('https://github.com/login/oauth/access_token', {
71-
client_id: this.client_id,
72-
client_secret: this.client_secret,
73-
code
74-
}, true)
75-
.then(body => {
76-
let token = getQuery(body, 'access_token');
60+
let replaceUrl = getQuery(window.location.search, 'state');
61+
replaceUrl = decodeURIComponent(replaceUrl);
62+
63+
github.getToken(state.client_id, state.client_secret, code)
64+
.then(token => {
7765
this._updateToken(token);
78-
let replaceUrl = getQuery(window.location.search, 'state');
79-
replaceUrl = decodeURIComponent(replaceUrl);
8066
window.history.replaceState(null, null, replaceUrl);
8167
})
8268
.catch(err => console.log(err));
@@ -90,14 +76,14 @@ class GitComment {
9076
*/
9177
_updateToken(token) {
9278
if (token && token.length) {
93-
this.ifLogin = true;
94-
this.access_token = token;
79+
state.ifLogin = true;
80+
state.access_token = token;
9581
}
9682
else {
97-
this.ifLogin = false;
98-
this.access_token = '';
83+
state.ifLogin = false;
84+
state.access_token = '';
9985
}
100-
window.localStorage.setItem(STORAGE_TOKEN_KEY, this.access_token);
86+
window.localStorage.setItem(STORAGE_TOKEN_KEY, state.access_token);
10187
}
10288

10389
//#endregion
@@ -110,14 +96,11 @@ class GitComment {
11096
* @memberof GitHub
11197
*/
11298
login() {
113-
let url = 'https://github.com/login/oauth/authorize';
114-
url = appendQuery(url, {
115-
client_id: this.client_id,
116-
redirect_uri: window.location.href,
117-
scope: 'public_repo',
118-
state: window.location.href
119-
});
120-
window.location.href = url;
99+
github.toAuthorize(state.client_id);
100+
}
101+
102+
getUserInfo() {
103+
github.getAuthUser().then(body => console.log(body));
121104
}
122105

123106
//#endregion

src/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ import gitComment from './git-comment';
2222

2323
gitComment.config(auth);
2424

25-
if (!gitComment.ifLogin) {
25+
if (!gitComment.state.ifLogin) {
2626
// gitComment.login();
2727
}
28+
else {
29+
gitComment.getUserInfo();
30+
}
2831

2932
window.gitComment = gitComment;
3033
console.log(gitComment);

src/lib/github.js

Lines changed: 67 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,73 @@
1-
import { appendQuery } from './utils';
2-
3-
class GitHub {
4-
5-
constructor(options) {
6-
const { client_id, client_secret } = options;
7-
this.client_id = client_id;
8-
this.client_secret = client_secret;
9-
}
10-
11-
client_id = ''
12-
13-
client_secret = ''
14-
15-
/**
16-
* 是否登陆
17-
*
18-
* @memberof GitHub
19-
*/
20-
ifLogin = false
21-
22-
/**
23-
* 去github获取权限
24-
*
25-
* @param {string} client_id
26-
* @memberof GitHub
27-
*/
28-
login(client_id) {
29-
let url = 'https://github.com/login/oauth/authorize';
30-
url = appendQuery(url, {
31-
client_id,
32-
redirect_uri: window.location.href,
33-
scope: 'public_repo'
34-
});
35-
window.location.href = url;
36-
}
1+
import http from './http';
2+
import { getQuery, appendQuery } from "./utils";
3+
import state from '../state';
374

5+
/**
6+
* 找到第一个符合的issue
7+
*
8+
* @export
9+
* @param {string} owner 仓库所有者
10+
* @param {string} repo 仓库名称
11+
* @param {string} labels labels 用逗号分隔
12+
* @returns {any>}
13+
*/
14+
export function getFirstIssue(owner, repo, labels) {
15+
return http.get(`/repos/${owner}/${repo}/issues`, {
16+
creator: owner,
17+
labels
18+
}).then(body => JSON.parse(body)[0]);
3819
}
3920

40-
const github = {
41-
/**
42-
* 是否登陆
43-
*/
44-
ifLogin: false,
21+
/**
22+
*
23+
*
24+
* @export
25+
* @param {string} owner
26+
* @param {string} repo
27+
* @param {number} number
28+
* @returns
29+
*/
30+
export function getIssue(owner, repo, number) {
31+
return http.get(`/repos/${owner}/${repo}/issues/${number}`)
32+
.then(body => JSON.parse(body));
33+
}
4534

46-
token: '',
35+
/**
36+
* 获取token
37+
*
38+
* @export
39+
* @param {string} client_id
40+
* @param {string} client_secret
41+
* @param {string} code
42+
* @returns {Promise<string>}
43+
*/
44+
export function getToken(client_id, client_secret, code) {
45+
return http
46+
.post('https://github.com/login/oauth/access_token', {
47+
client_id: state.client_id,
48+
client_secret: state.client_secret,
49+
code
50+
}, true)
51+
.then(body => getQuery(body, 'access_token'));
52+
}
4753

48-
/**
49-
* 去github获取权限
50-
*
51-
* @param {string} client_id
52-
*/
53-
login(client_id) {
54-
let url = 'https://github.com/login/oauth/authorize';
55-
url = appendQuery(url, {
56-
client_id,
57-
redirect_uri: window.location.href,
58-
scope: 'public_repo'
59-
});
60-
window.location.href = url;
61-
console.log(url);
62-
}
63-
};
54+
/**
55+
* 跳转去认证
56+
*
57+
* @export
58+
* @param {string} client_id
59+
*/
60+
export function toAuthorize(client_id) {
61+
let url = 'https://github.com/login/oauth/authorize';
62+
url = appendQuery(url, {
63+
client_id: client_id,
64+
redirect_uri: window.location.href,
65+
scope: 'public_repo',
66+
state: window.location.href
67+
});
68+
window.location.href = url;
69+
}
6470

65-
export default github;
71+
export function getAuthUser() {
72+
return http.get('/user');
73+
}

src/lib/http.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Deferred from './Deferred';
2-
import gitComment from '../git-comment';
2+
import state from '../state';
33
import { stringifyQuery, appendQuery } from "./utils";
44

55
const baseUrl = 'https://api.github.com';
@@ -41,8 +41,8 @@ function ajax(method, url, data = {}, proxy = false) {
4141

4242
xh.open(method, url, true);
4343
// xh.setRequestHeader('Accept', 'application/vnd.github.symmetra-preview+json');
44-
if (gitComment.access_token) {
45-
xh.setRequestHeader('Authorization', `token ${gitComment.access_token}`);
44+
if (state.access_token) {
45+
xh.setRequestHeader('Authorization', `token ${state.access_token}`);
4646
}
4747
if (method === 'POST') {
4848
xh.setRequestHeader('Content-Type', 'application/json');

src/lib/issue.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/state.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* 当前状态和全局数据
3+
*/
4+
export default {
5+
client_id: '',
6+
7+
client_secret: '',
8+
9+
access_token: '',
10+
11+
/**
12+
* 是否登录
13+
*/
14+
ifLogin: false
15+
};

0 commit comments

Comments
 (0)