Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
ydLife
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Sweet Zhang
ydLife
Commits
fe401d3b
Commit
fe401d3b
authored
Dec 05, 2019
by
Sweet Zhang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加拦截器
parent
2813f6a9
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
61 additions
and
58 deletions
+61
-58
src/app/app.module.ts
+2
-10
src/app/auth/auth.service.ts
+5
-48
src/app/http-interceptors/auth-interceptor.ts
+16
-0
src/app/http-interceptors/index.ts
+13
-0
src/app/http-interceptors/trim-name-interceptor.ts
+25
-0
No files found.
src/app/app.module.ts
View file @
fe401d3b
...
...
@@ -8,7 +8,7 @@ import {Router} from '@angular/router';
import
{
AuthModule
}
from
'./auth/auth.module'
;
import
{
HttpClientModule
}
from
'@angular/common/http'
;
import
{
LifeCommonModule
}
from
'./common/life-common.module'
;
import
{
HashLocationStrategy
,
LocationStrategy
}
from
"@angular/common"
;
import
{
httpInterceptorProviders
}
from
'./http-interceptors/index'
;
@
NgModule
({
declarations
:
[
...
...
@@ -23,16 +23,8 @@ import {HashLocationStrategy, LocationStrategy} from "@angular/common";
HttpClientModule
,
AppRoutingModule
],
providers
:
[],
providers
:
[
httpInterceptorProviders
],
bootstrap
:
[
AppComponent
]
})
export
class
AppModule
{
// // Diagnostic only: inspect router configuration
// constructor(router: Router) {
// // Use a custom replacer to display function names in the route configs
// const replacer = (key, value) => (typeof value === 'function') ? value.name : value;
//
// console.log('Routes: ', JSON.stringify(router.config, replacer, 2));
// }
// { provide: LocationStrategy, useClass: HashLocationStrategy }
}
src/app/auth/auth.service.ts
View file @
fe401d3b
import
{
Injectable
}
from
'@angular/core'
;
import
{
Observable
,
of
}
from
'rxjs'
;
import
{
tap
,
delay
}
from
'rxjs/operators'
;
import
{
environment
}
from
'../../environments/environment'
;
import
{
HttpClient
,
HttpHeaders
}
from
'@angular/common/http'
;
import
{
retry
}
from
'rxjs/internal/operators'
;
import
{
HttpClient
}
from
'@angular/common/http'
;
@
Injectable
({
providedIn
:
'root'
,
...
...
@@ -20,51 +18,21 @@ export class AuthService {
// 经纪人登陆
login
(
loginInfo
):
Observable
<
any
>
{
const
api
=
this
.
API
+
'/practitionerLogin'
;
const
httpOptions
=
{
headers
:
new
HttpHeaders
({
'Content-Type'
:
'application/json'
,
'X-Authorization'
:
localStorage
.
getItem
(
'lifeToken'
)
?
localStorage
.
getItem
(
'lifeToken'
)
:
''
}
)
};
return
this
.
http
.
post
(
api
,
JSON
.
stringify
(
loginInfo
),
httpOptions
)
.
pipe
(
retry
(
3
),
);
return
this
.
http
.
post
(
api
,
JSON
.
stringify
(
loginInfo
));
}
// 发送验证码
verificationCode
(
verticalCode
)
{
const
url
=
this
.
API
+
'/verificationCode'
;
const
httpOptions
=
{
headers
:
new
HttpHeaders
({
'Content-Type'
:
'application/json'
,
'X-Authorization'
:
localStorage
.
getItem
(
'lifeToken'
)
?
localStorage
.
getItem
(
'lifeToken'
)
:
''
}
)
};
return
this
.
http
.
post
(
url
,
JSON
.
stringify
(
verticalCode
),
httpOptions
)
.
pipe
(
tap
(
response
=>
response
)
);
.
post
(
url
,
JSON
.
stringify
(
verticalCode
));
}
// 校验验证码
compare
(
compareCode
)
{
const
url
=
this
.
API
+
'/checkVerificationCode'
;
const
httpOptions
=
{
headers
:
new
HttpHeaders
({
'Content-Type'
:
'application/json'
,
'X-Authorization'
:
localStorage
.
getItem
(
'lifeToken'
)
?
localStorage
.
getItem
(
'lifeToken'
)
:
''
}
)
};
return
this
.
http
.
post
(
url
,
JSON
.
stringify
(
compareCode
),
httpOptions
)
.
pipe
(
tap
(
response
=>
response
)
);
.
post
(
url
,
JSON
.
stringify
(
compareCode
));
}
/**
...
...
@@ -75,17 +43,6 @@ export class AuthService {
const
ticketObj
=
{
ticket
:
'life'
};
const
httpOptions
=
{
headers
:
new
HttpHeaders
({
'Content-Type'
:
'application/json'
}
)
};
return
this
.
http
.
post
(
url
,
JSON
.
stringify
(
ticketObj
),
httpOptions
)
.
pipe
(
tap
(
response
=>
{
return
response
;
})
);
return
this
.
http
.
post
(
url
,
JSON
.
stringify
(
ticketObj
));
}
}
src/app/http-interceptors/auth-interceptor.ts
0 → 100644
View file @
fe401d3b
import
{
Injectable
}
from
'@angular/core'
;
import
{
HttpEvent
,
HttpInterceptor
,
HttpHandler
,
HttpRequest
}
from
'@angular/common/http'
;
import
{
Observable
}
from
"rxjs/index"
;
@
Injectable
()
export
class
AuthInterceptor
implements
HttpInterceptor
{
intercept
(
req
:
HttpRequest
<
any
>
,
next
:
HttpHandler
):
Observable
<
HttpEvent
<
any
>>
{
const
authReq
=
req
.
clone
({
headers
:
req
.
headers
.
set
(
'X-Authorization'
,
localStorage
.
getItem
(
'lifeToken'
)
?
localStorage
.
getItem
(
'lifeToken'
)
:
''
),
setHeaders
:
{
'Content-Type'
:
'application/json'
}
});
return
next
.
handle
(
authReq
);
}
}
src/app/http-interceptors/index.ts
0 → 100644
View file @
fe401d3b
/* "Barrel" of Http Interceptors */
import
{
HTTP_INTERCEPTORS
}
from
'@angular/common/http'
;
import
{
AuthInterceptor
}
from
'./auth-interceptor'
;
import
{
TrimNameInterceptor
}
from
'./trim-name-interceptor'
;
/** Http interceptor providers in outside-in order */
export
const
httpInterceptorProviders
=
[
{
provide
:
HTTP_INTERCEPTORS
,
useClass
:
TrimNameInterceptor
,
multi
:
true
},
{
provide
:
HTTP_INTERCEPTORS
,
useClass
:
AuthInterceptor
,
multi
:
true
}
];
src/app/http-interceptors/trim-name-interceptor.ts
0 → 100644
View file @
fe401d3b
/**
* Created by Sweet on 2019/12/5.
*/
import
{
Injectable
}
from
'@angular/core'
;
import
{
HttpEvent
,
HttpInterceptor
,
HttpHandler
,
HttpRequest
}
from
'@angular/common/http'
;
import
{
Observable
}
from
'rxjs'
;
@
Injectable
()
export
class
TrimNameInterceptor
implements
HttpInterceptor
{
intercept
(
req
:
HttpRequest
<
any
>
,
next
:
HttpHandler
):
Observable
<
HttpEvent
<
any
>>
{
const
body
=
req
.
body
;
if
(
!
body
||
!
body
.
name
)
{
return
next
.
handle
(
req
);
}
// copy the body and trim whitespace from the name property
const
newBody
=
{
...
body
,
name
:
body
.
name
.
trim
()
};
// clone request and set its body
const
newReq
=
req
.
clone
({
body
:
newBody
});
// send the cloned request to the next handler.
return
next
.
handle
(
newReq
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment