|
@@ -0,0 +1,97 @@
|
|
|
+import { Interceptors } from './interceptors';
|
|
|
+export class HttpRequest {
|
|
|
+ public axios: any
|
|
|
+ constructor() {
|
|
|
+
|
|
|
+ this.axios = new Interceptors().getInterceptors();
|
|
|
+ }
|
|
|
+ public get(url: string, params: String, config: Object = {}) {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ let paramUrl = url
|
|
|
+ if (params) {
|
|
|
+ paramUrl += `?${params}`
|
|
|
+ }
|
|
|
+ this.axios.get(paramUrl, {
|
|
|
+ ...config
|
|
|
+ }).then((res: any) => {
|
|
|
+ this.resultHandle(res, resolve, reject, url);
|
|
|
+ }).catch((err: { message: any; }) => {
|
|
|
+ console.log(err)
|
|
|
+ reject(err.message);
|
|
|
+ })
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ public post(url: string, params: Object, config: Object = {}) {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ this.axios.post(url, params, {
|
|
|
+ ...config
|
|
|
+ }).then((res: any) => {
|
|
|
+ this.resultHandle(res, resolve, reject, url);
|
|
|
+ }).catch((err: { message: any; }) => {
|
|
|
+ reject(err.message);
|
|
|
+ })
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ public delete(url: string, params: String, config: Object = {}) {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ let paramUrl = url
|
|
|
+ if (params) {
|
|
|
+ paramUrl += `?${params}`
|
|
|
+ }
|
|
|
+ this.axios.delete(paramUrl, {
|
|
|
+ ...config
|
|
|
+ }).then((res: any) => {
|
|
|
+ this.resultHandle(res, resolve, reject, url);
|
|
|
+ }).catch((err: { message: any; }) => {
|
|
|
+ console.log(err)
|
|
|
+ reject(err.message);
|
|
|
+ })
|
|
|
+ })
|
|
|
+ }
|
|
|
+ public put(url: string, params: Object, config: Object = {}) {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ this.axios.put(url, params, {
|
|
|
+ ...config
|
|
|
+ }).then((res: any) => {
|
|
|
+ this.resultHandle(res, resolve, reject, url);
|
|
|
+ }).catch((err: { message: any; }) => {
|
|
|
+ reject(err.message);
|
|
|
+ })
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public resultHandle(res: any, resolve: { (value: unknown): void; (value: unknown): void; (arg0: any): void; },reject: { (value: unknown): void; (value: unknown): void; (arg0: any): void; }, url: string) {
|
|
|
+ if (res) {
|
|
|
+ if (res.code === 200 || (res.size > 0 && res.type)) {
|
|
|
+ resolve(res);
|
|
|
+ } else {
|
|
|
+ resolve(res);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public errorHandle(res: any, reject: any, url: string) {
|
|
|
+ console.error('错误接口:' + url)
|
|
|
+ if (res.hasOwnProperty('message')) {
|
|
|
+
|
|
|
+ } else if (res.hasOwnProperty('msg')) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if (res) {
|
|
|
+ switch (res.code) {
|
|
|
+ case -102:
|
|
|
+ break;
|
|
|
+ case -152:
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ reject(res);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export default HttpRequest
|