Skip to content

Commit 8056a56

Browse files
committed
fix demo api request test
1 parent fa4a5d1 commit 8056a56

File tree

4 files changed

+60
-16
lines changed

4 files changed

+60
-16
lines changed

src/app/demos/demos.component.ts

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { HttpClient } from '@angular/common/http';
1+
import { HttpClient, HttpParams } from '@angular/common/http';
22
import { Component, ElementRef, ViewChild } from '@angular/core';
33
import { NgForm, NonNullableFormBuilder, Validators } from '@angular/forms';
4-
import { Observable, catchError, map, } from 'rxjs';
4+
import { Observable, catchError, map, of, filter } from 'rxjs';
55
import { NzNotificationService } from 'ng-zorro-antd/notification';
66
import { UrlModel } from './models/demo.model';
77
import { Params } from '../../utils/params.service';
88
import { APIForm } from '@/types/demo';
9+
import { DemosService } from './demos.service'
910

1011
@Component({
1112
selector: 'app-demos',
@@ -41,9 +42,10 @@ export class DemosComponent {
4142

4243
constructor (
4344
private http: HttpClient,
45+
private service: DemosService,
4446
private params: Params,
4547
private notification: NzNotificationService,
46-
private fb: NonNullableFormBuilder
48+
private fb: NonNullableFormBuilder,
4749
) {
4850
this.apiForm = this.fb.group<APIForm>({
4951
email: '',
@@ -63,12 +65,18 @@ export class DemosComponent {
6365
this.notification.warning('警告', '请输入正确格式的状态码');
6466
return;
6567
}
66-
console.log("this.status:", this.status)
67-
// this.http.post('/test/status/' + this.status, {}).subscribe((res) => {
68-
// console.log(res);
69-
// }, (err) => {
70-
// console.log(err);
71-
// });
68+
this.service.testStatus(new HttpParams().set('status', this.status)).subscribe({
69+
next(res){
70+
console.log(res);
71+
},
72+
// error: console.error,
73+
error(err){
74+
console.log(err);
75+
},
76+
complete(){
77+
console.log('compelete...')
78+
}
79+
});
7280
}
7381

7482
public queryIPGeo () {
@@ -134,14 +142,25 @@ export class DemosComponent {
134142
}
135143

136144
apiSubmit() {
145+
// const observable$ = of(1, 2, 3);
146+
// const modifiedObservable$ = observable$.pipe(
147+
// map(value => value * 2),
148+
// filter(value => value > 1)
149+
// );
150+
// modifiedObservable$.subscribe(console.log);
137151
if (this.apiForm.valid) {
138152
console.log('submit', this.apiForm.value);
139153
const { url, method, params } = this.apiForm.value
140-
const data = this.http.get(url).pipe(map(res => {
141-
console.log(res, '...')
142-
return res
143-
}))
144-
console.log("data:", data)
154+
this.service.testApi(url, params).subscribe({
155+
next: (res) => {
156+
this.apiForm.controls.res.setValue(JSON.stringify(res, null, 4))
157+
},
158+
error: (res) => {
159+
this.notification.error('Error', res.toString())
160+
},
161+
complete: () => {
162+
}
163+
})
145164
} else {
146165
Object.values(this.apiForm.controls).forEach(control => {
147166
if (control.invalid) {

src/app/demos/demos.module.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { DemosRouting } from './demos.routing';
88
import { OutletComponent } from './out-let.component';
99
import { UploadFileModule } from '@components/upload-file/upload-file.module';
1010
import { ZorroAntdModule } from '@/antd/ngModule'
11+
import { DemosService } from './demos.service';
1112

1213
@NgModule({
1314
imports: [
@@ -22,6 +23,8 @@ import { ZorroAntdModule } from '@/antd/ngModule'
2223
OutletComponent,
2324
DemosComponent,
2425
],
25-
providers: [ ],
26+
providers: [
27+
DemosService
28+
],
2629
})
2730
export default class DemosModule {}

src/app/demos/demos.service.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { HttpClient, HttpParams } from '@angular/common/http';
2+
import { Injectable } from '@angular/core';
3+
import { Observable } from 'rxjs';
4+
// import { Params } from '../../../utils/params.service';
5+
6+
@Injectable()
7+
export class DemosService {
8+
constructor (
9+
private http: HttpClient,
10+
) {}
11+
12+
testStatus (params: HttpParams) {
13+
return this.http.get('/article', {params});
14+
}
15+
16+
testApi (url: string, data: any) {
17+
return this.http.get(url, {params: data});
18+
}
19+
20+
}

src/utils/httpInterceptor/httpInterceptor.service.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ export class HttpClientInterceptor implements HttpInterceptor {
2020
}
2121

2222
public intercept (req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
23+
const isHttpEntry = /^http/.test(req.url)
24+
const url = `${isHttpEntry ? '' : this.baseUrl}${req.url}`
2325
const request = req.clone({
24-
url: this.baseUrl + req.url,
26+
url,
2527
setHeaders: {
2628
'X-Requested-With': 'XMLHttpRequest',
2729
'Authorization-User': this.store.getItem('ACCESS_TOKEN') || 'no_token',

0 commit comments

Comments
 (0)