@@ -17,8 +17,20 @@ if (typeof window !== 'undefined') {
1717 resp = JSON . parse ( result )
1818 console . log ( "result" , resp ) ;
1919 if ( resp . access_token != null ) {
20- localStorage . setItem ( 'access_token' , resp . access_token ) ;
21- window . location . href = state + "?timestamp=" + Date . now ( ) ;
20+ //获取用户信息
21+ localStorage . removeItem ( 'gitee_user' ) ;
22+ const token = resp . access_token
23+ fetch ( "https://gitee.com/api/v5/user?access_token=" + token , {
24+ method : "GET"
25+ } ) . then ( response => response . text ( ) )
26+ . then ( result => {
27+ resp = JSON . parse ( result )
28+ console . log ( "result" , resp ) ;
29+ localStorage . setItem ( 'access_token' , token ) ;
30+ localStorage . setItem ( 'gitee_user' , result ) ;
31+ window . location . href = state + "?timestamp=" + Date . now ( ) ;
32+ } )
33+
2234 } else if ( "invalid_grant_accessibility" == resp . error ) {
2335 window . location . href = 'https://gitee.com/oauth/authorize?client_id=' + client_id + '&redirect_uri=' + gitee_redirect_uri + '&response_type=code' ;
2436 }
@@ -27,15 +39,35 @@ if (typeof window !== 'undefined') {
2739 }
2840 }
2941
42+ function getUserInfo ( ) {
43+ let access_token = checkAccessToken ( )
44+ if ( access_token == null ) {
45+ return
46+ }
47+ let user = localStorage . getItem ( 'gitee_user' )
48+ if ( user != null ) {
49+ return JSON . parse ( user )
50+ }
51+ }
52+
53+ function checkAccessToken ( ) {
54+ let access_token = localStorage . getItem ( 'access_token' )
55+ console . log ( "access_token" , access_token ) ;
56+ if ( access_token == null ) {
57+ window . location . href = 'https://gitee.com/oauth/authorize?client_id=' + client_id + '&redirect_uri=' + gitee_redirect_uri + '&response_type=code&state=' + location . href ;
58+ return null ;
59+ } else {
60+ return access_token ;
61+ }
62+ }
63+
3064 function checkStar ( owner , repo ) {
3165 //文档首页不校验
3266 if ( location . pathname === repo || location . pathname === repo + '/' ) {
3367 return ;
3468 }
35- access_token = localStorage . getItem ( 'access_token' )
36- console . log ( "access_token" , access_token ) ;
69+ let access_token = checkAccessToken ( )
3770 if ( access_token == null ) {
38- window . location . href = 'https://gitee.com/oauth/authorize?client_id=' + client_id + '&redirect_uri=' + gitee_redirect_uri + '&response_type=code&state=' + location . href ;
3971 return
4072 }
4173 fetch ( "https://gitee.com/api/v5/user/starred/" + owner + "/" + repo + "?access_token=" + access_token , {
@@ -49,24 +81,72 @@ if (typeof window !== 'undefined') {
4981 checkStar ( owner , repo )
5082 } else if ( response . status == 404 ) {
5183 //替换 html body中 class为 content-wrapper 中的内容,适配vuepress
52- d = document . querySelector ( ".content-wrapper" )
53- if ( d == null ) {
54- //适配 astro
55- d = document . querySelector ( ".main-pane" )
56- }
57- if ( d == null ) {
58- d = document . getElementsByTagName ( "body" ) [ 0 ]
59- }
60- d . innerHTML = `
84+ replaceHtml ( `
85+ <div>
6186 检测到您还未 Star 本项目,暂时无法访问本页面。<br/>
6287 请先前往:<a target="_blank" href="https://gitee.com/${ owner } /${ repo } ">https://gitee.com/${ owner } /${ repo } </a> 完成操作,再尝试刷新当前页面。
6388 </div>
64- `
89+ ` )
6590 }
6691 } )
6792 . catch ( error => console . error ( error ) ) ;
6893 }
6994
95+ function checkAuthorize ( owner , repo , file ) {
96+ let access_token = checkAccessToken ( )
97+ if ( access_token == null ) {
98+ return
99+ }
100+ let user = getUserInfo ( )
101+ fetch ( "https://gitee.com/api/v5/repos/" + owner + "/" + repo + "/contents/" + file + "?access_token=" + access_token , {
102+ method : "GET"
103+ } )
104+ . then ( response => {
105+ console . log ( "check result" , response ) ;
106+ if ( response . status == 200 ) {
107+ //解析body
108+ response . json ( ) . then ( function ( data ) {
109+ console . log ( "check result" , data ) ;
110+ //解析base64,
111+ let content = atob ( data . content )
112+ //转成utf8
113+ content = decodeURIComponent ( escape ( content ) )
114+
115+ console . log ( "check result" , content ) ;
116+ //解析json
117+ let json = JSON . parse ( content )
118+ if ( json [ user ?. login ] != null ) {
119+ console . log ( "check result" , "ok" ) ;
120+ } else {
121+ replaceHtml ( `<div>
122+ 未经授权暂时无法访问该页面。<br/>
123+ </div>
124+ ` )
125+ }
126+ } ) ;
127+ } else {
128+ console . log ( "check fail result" , response )
129+ localStorage . removeItem ( "access_token" )
130+ checkAuthorize ( owner , repo , file )
131+ }
132+ } )
133+ . catch ( error => console . error ( error ) ) ;
134+ }
135+
136+ function replaceHtml ( html ) {
137+ //替换 html body中 class为 content-wrapper 中的内容,适配vuepress
138+ d = document . querySelector ( ".content-wrapper" )
139+ if ( d == null ) {
140+ //适配 astro
141+ d = document . querySelector ( ".main-pane" )
142+ }
143+ if ( d == null ) {
144+ d = document . getElementsByTagName ( "body" ) [ 0 ]
145+ }
146+ d . innerHTML = html
147+ }
148+
70149 window . checkStar = checkStar ;
71150 window . redirect = redirect ;
151+ window . checkAuthorize = checkAuthorize ;
72152}
0 commit comments