|
1 | | -import { Injectable } from '@angular/core'; |
2 | | -import { Http, RequestOptions, URLSearchParams } from '@angular/http'; |
| 1 | +import {Injectable} from '@angular/core'; |
| 2 | +import {Http, Headers, RequestOptions, URLSearchParams, RequestOptionsArgs} from '@angular/http'; |
3 | 3 | import 'rxjs/add/operator/map'; |
| 4 | +import { BASE_URL } from './constant'; |
| 5 | +import { Authentication } from './authentication'; |
4 | 6 |
|
5 | 7 | /** |
6 | 8 | * Api is a generic REST Api handler. Set your API url first. |
7 | 9 | */ |
8 | 10 | @Injectable() |
9 | 11 | export class Api { |
10 | | - url: string = 'https://example.com/api/v1'; |
| 12 | + baseUrl: string = BASE_URL; |
| 13 | + language:any; |
| 14 | + headers:Headers; |
11 | 15 |
|
12 | | - constructor(public http: Http) { |
| 16 | + constructor(public http: Http, public authentication: Authentication) { |
13 | 17 | } |
14 | 18 |
|
15 | | - get(endpoint: string, params?: any, options?: RequestOptions) { |
16 | | - if (!options) { |
17 | | - options = new RequestOptions(); |
18 | | - } |
19 | | - |
20 | | - // Support easy query params for GET requests |
21 | | - if (params) { |
22 | | - let p = new URLSearchParams(); |
23 | | - for (let k in params) { |
24 | | - p.set(k, params[k]); |
| 19 | + init() { |
| 20 | + this.language='en-US,en;q=0.5'; // q是权重系数,范围 0 =< q <= 1,q 值越大,请求越倾向于获得其“;”之前的类型表示的内容,若没有指定 q 值,则默认为1,若被赋值为0,则用于提醒服务器哪些是浏览器不接受的内容类型。 |
| 21 | + if (localStorage.langKey) { |
| 22 | + const lang = localStorage.langKey; |
| 23 | + if (lang === 'zh') { |
| 24 | + this.language='zh-cn,zh;q=0.5'; |
25 | 25 | } |
26 | | - // Set the search field if we have params and don't already have |
27 | | - // a search field set in options. |
28 | | - options.search = !options.search && p || options.search; |
29 | 26 | } |
| 27 | + this.headers = new Headers({ 'Content-Type': 'application/json', 'charset': 'UTF-8','Accept-Language': this.language, 'Authorization': 'Bearer ' + this.authentication.getToken() }); |
| 28 | + } |
30 | 29 |
|
31 | | - return this.http.get(this.url + '/' + endpoint, options); |
| 30 | + get(endpoint: string, paramMap?: any) { |
| 31 | + return this.http.get(this.baseUrl + '/' + endpoint, new RequestOptions({ |
| 32 | + search: this.buildURLSearchParams(paramMap), |
| 33 | + headers: this.headers |
| 34 | + })); |
32 | 35 | } |
33 | 36 |
|
34 | 37 | post(endpoint: string, body: any, options?: RequestOptions) { |
35 | | - return this.http.post(this.url + '/' + endpoint, body, options); |
| 38 | + return this.http.post(this.baseUrl + '/' + endpoint, body, this.getOptions(options)); |
36 | 39 | } |
37 | 40 |
|
38 | 41 | put(endpoint: string, body: any, options?: RequestOptions) { |
39 | | - return this.http.put(this.url + '/' + endpoint, body, options); |
| 42 | + return this.http.put(this.baseUrl + '/' + endpoint, body, this.getOptions(options)); |
40 | 43 | } |
41 | 44 |
|
42 | 45 | delete(endpoint: string, options?: RequestOptions) { |
43 | | - return this.http.delete(this.url + '/' + endpoint, options); |
| 46 | + return this.http.delete(this.baseUrl + '/' + endpoint, this.getOptions(options)); |
44 | 47 | } |
45 | 48 |
|
46 | 49 | patch(endpoint: string, body: any, options?: RequestOptions) { |
47 | | - return this.http.put(this.url + '/' + endpoint, body, options); |
| 50 | + return this.http.put(this.baseUrl + '/' + endpoint, body, this.getOptions(options)); |
| 51 | + } |
| 52 | + |
| 53 | + private getOptions(options): RequestOptionsArgs { |
| 54 | + if (!options) { |
| 55 | + options = new RequestOptions({ |
| 56 | + headers: this.headers |
| 57 | + }); |
| 58 | + return options; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private buildURLSearchParams(paramMap): URLSearchParams { |
| 63 | + let params = new URLSearchParams(); |
| 64 | + if (!paramMap) { |
| 65 | + return params; |
| 66 | + } |
| 67 | + for (let key in paramMap) { |
| 68 | + let val = paramMap[key]; |
| 69 | + params.set(key, val); |
| 70 | + } |
| 71 | + return params; |
48 | 72 | } |
49 | 73 | } |
0 commit comments