項(xiàng)目GItHub https分支
1.這里提供統(tǒng)一攔截后端接口數(shù)據(jù)處理,api統(tǒng)一文件管理方式:
統(tǒng)一攔截如下
// 目錄src/service/request.service.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpResponse, HttpErrorResponse } from '@angular/common/http';
import { LoadingController, AlertController } from '@ionic/angular';
import { Observable } from 'rxjs';
import { finalize, tap } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class RequestService {
constructor(private httpClient: HttpClient, private loading: LoadingController,public alertController: AlertController) { }
headerOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'my-auth-token'
}),
}
public request (method:string,url:string,data: Object):Observable<HttpResponse<any>> {
this.loadShow()
return this.httpClient.request(method,url,{ ...data, ...this.headerOptions, ...{observe: 'response'}}).pipe(
tap((response) => {
this.handleSuccess(response)
},
(error) => {
this.handleError(error)
}),
finalize(() => {
this.loadHide()
})
)
}
private handleSuccess(response) {
}
private handleError(error: HttpErrorResponse) {
if (error.error instanceof ErrorEvent) {
} else {
if (error.status === 400) {
this.alert('請求失敗')
} else if (error.status === 404) {
this.alert('參數(shù)有誤,請檢查')
} else if (error.status === 500) {
this.alert('接口有錯,聯(lián)系后臺管理員')
} else {
this.alert(error.message)
}
}
}
private async alert(message) {
const alert = await this.alertController.create({
header: '提示',
message,
buttons: ['確認(rèn)']
});
await alert.present();
}
private async loadShow() {
const loading = await this.loading.create({
message: '加載中...',
duration: 2000,
translucent: true,
cssClass: 'custom-class custom-loading'
});
await loading.present();
}
private async loadHide() {
await this.loading.dismiss()
}
}
2. 統(tǒng)一管理api
// 目錄src/api/api.ts
import { Injectable } from '@angular/core';
import { RequestService } from 'src/service/request.service'
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class test {
constructor(public requestService: RequestService){}
public login (data: any) {
return this.requestService.request(
'get',
'https://api.apiopen.top/getJoke',
{params: data}
)
}
}
3.調(diào)用
目錄/src/pages/tab1/tab1-page.ts
import { Component } from '@angular/core';
import { InAppBrowser } from '@ionic-native/in-app-browser/ngx';
import { HttpClient, HttpHeaders, HttpResponse } from '@angular/common/http';
import { Router } from '@angular/router';
import {HttpServiceService} from 'src/service/http-service.service'
import { test } from 'src/api/api'
import { Observable, from } from 'rxjs';
@Component({
selector: 'app-tab1',
templateUrl: 'tab1.page.html',
styleUrls: ['tab1.page.scss']
})
export class Tab1Page {
httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json;application/x-www-form-urlencodeed; charset=utf-8', "Referer": "https://www.aganchina.cn" })
};
constructor(private test: test, private httpClient: HttpClient, private router: Router,private httpService: HttpServiceService) { }
goDetails() {
this.test.login({page:1,count:2,type: 'video'}).subscribe(res => {
console.log(res)
})
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。