import { Injectable } from '@angular/core' ; import { Http , Headers , Response } from '@angular/http' ; import { Observable } from 'rxjs/Rx' ; import { Image } from '../domain/entities' ; @ Injectable ( ) export class BingImageService { imageUrl : string ; headers = new Headers ( { 'Content-Type' : 'application/json' , 'Ocp-Apim-Subscription-Key' : '103ee07bf85a413e8212d556b011bf13' } ) ; constructor ( private http ) { const q = '北极+墙纸' ; const baseUrl = `https://api.cognitive.microsoft.com/bing/v5.0/images/search` ; this . imageUrl = baseUrl + `?q=${ q }&count=5&mkt=zh-CN&imageType=Photo&size=Large` ; } getImageUrl ( ) { return this . http . get ( this . imageUrl , { headers : this . headers } ) . map ( res => res . json ( ) . value as Image [ ] ) . catch ( this . handleError ) ; } private handleError ( error ) { console . error ( error ) ; return Observable . throw ( error . json ( ) . error || 'Server error' ) ; } }