import {Injectable} from '@angular/core' import {Router} from '@angular/router' import {HttpClient, HttpHeaders, HttpHeaderResponse, HttpResponse} from '@angular/common/http'; import 'rxjs/add/operator/map'; import {Observable} from 'rxjs/Observable' import Config, {default as config} from '../../config' import DimmerService from "../dimmer/index"; import {ToastrService} from 'ngx-toastr'; import StorageService from "../storage/index"; declare const $: any; @Injectable() export default class BasicService { constructor(private http: HttpClient, private storageService: StorageService, private router: Router, private toastr: ToastrService, private dimmerService: DimmerService) { } private configHeader(noParam = false) { let headers = {}; /*if (noParam) { headers['Content-Type'] = 'application/json'; } else { headers['Content-Type'] = 'application/x-www-form-urlencoded;'; } headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';*/ headers['Content-Type'] = 'application/json'; headers['Accept'] = 'application/json'; return headers; } send(api, data = {}, method: string = 'POST', noParam = true) { let url = Config.apiHost + api; let observable: Observable; if (method.toUpperCase() === 'GET') { if (data && $.param(data)) { url += '?' + $.param(data) } observable = this.http.get(url, { headers: this.configHeader(noParam), //withCredentials: true, // observe: 'response' }); } else { observable = this.http.post(url, noParam ? data : $.param(data), { headers: this.configHeader(noParam), //withCredentials: true }) } return new Promise((resolve, reject) => { observable.subscribe(data => { if (data.error == '没有权限') { window.location.href = config.loginUrl; this.storageService.clear() } resolve(data); }, err => { if (err.status == 404) { window.location.href = config.loginUrl; this.storageService.clear() } reject(err); }) }); } get pageSize() { return this.storageService.get('pageSize') || config.limit; } }