import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { createApi } from 'unsplash-js';
import { Picture, PictureSearchOption } from './picture-manager';
import { createClient, PhotosWithTotalResults } from 'pexels';
import { map } from 'rxjs/operators';
import { environment } from 'src/environments/environment';
@Injectable({
providedIn: 'root'
})
export class PictureManagerService {
// 图源:unsplash
unsplashApi = createApi({
// See https://unsplash.com/developers
accessKey: '-_sFxZBCYbuIdwxShP4nevLOHlC40a_KGaoCG9qnANM'
});
// 图源:
pexelsApi = createClient('563492ad6f9170000100000166ef6f190afa450fa4546c03ea9d2bce');
// picture cdn api
pictureCdnApi = `${environment.host}/picture-manager/cdn`;
pictureCdn: PictureCdn = null;
constructor(private http: HttpClient) {
this.getPictureCdn();
}
async getPictureCdn() {
if (this.pictureCdn === null) {
this.pictureCdn = await this.http.post<PictureCdn>(this.pictureCdnApi, null).toPromise();
}
return this.pictureCdn;
}
async search(option: PictureSearchOption) {
const { keyword, limit, page, user } = option;
if (option.origin === 'unsplash') {
if (keyword) {
return this.searchUnsplash(keyword, limit, page);
} else {
return [];
}
} else if (option.origin === 'pexels') {
if (keyword) {
return this.searchPexels(keyword, limit, page);
} else {
return [];
}
} else if (option.origin === 'pixabay') {
if (keyword) {
return this.searchPixabay(keyword, limit, page);
} else {
return [];
}
} else if (option.origin === 'shetu') {
return this.searchShetu(keyword, limit, page, user);
} else if (option.origin === 'haina-video') {
return this.searchHt(keyword, limit, page);
} else if (option.origin === 'haina-int') {
return this.searchHaina(keyword, limit, page, user);
}
return [];
}
async searchUnsplash(keyword: string, limit: number = 30, page: number = 1) {
const unsplashPictures = await this.unsplashApi.search.getPhotos({
page,
perPage: limit,
query: keyword,
// orientation: 'landscape'
});
let pictures: Picture[] = [];
if (unsplashPictures.status === 200 && unsplashPictures.response.results.length) {
pictures = unsplashPictures.response.results.map(p => ({
picture: {
id: new Date().getTime(),
name: `${p.id}`,
width: p.width,
height: p.height,
keywords: `${p.alt_description}#${keyword}`,
path: p.urls.full,
site: p.user.links.html,
size: p.likes || 0,
user: p.user.name,
date: new Date(p.created_at)
},
url: p.urls.small,
outside: 'unsplash'
}));
}
return pictures;
}
async searchPexels(keyword: string, limit: number = 30, page: number = 1) {
const pexelsPictures = await this.pexelsApi.photos.search({
query: keyword,
per_page: limit,
// orientation: 'landscape',
page
}) as PhotosWithTotalResults;
let pictures: Picture[] = [];
if (pexelsPictures && pexelsPictures.photos) {
pictures = pexelsPictures.photos.map(p => ({
picture: {
id: p.id,
name: `${p.id}`,
width: p.width,
height: p.height,
keywords: `${keyword}`,
path: p.src.original + '?auto=compress&w=1920',
site: p.url,
size: 1,
user: p.photographer + '#' + p.photographer_url,
date: new Date()
},
url: p.src.large,
outside: 'pexels'
}));
}
return pictures;
}
async searchPixabay(keyword: string, limit: number = 30, page: number = 1) {
const API_KEY = '21703909-be6d444048df58a348be34435';
const pixabayApi = 'https://pixabay.com/api/';
const params = new HttpParams()
.set('key', API_KEY)
.append('q', keyword)
.append('lang', 'zh')
.append('page', page.toString())
.append('per_page', limit.toString());
const pixabayPictures = await this.http.get<any>(pixabayApi, { params }).toPromise();
let pictures: Picture[] = [];
if (pixabayPictures.totalHits && pixabayPictures.hits) {
pictures = pixabayPictures.hits.map((p: any) => ({
picture: {
id: p.id,
name: `${p.id}`,
width: p.imageWidth,
height: p.imageHeight,
keywords: `${keyword}#${p.tags}`,
path: p.imageURL, // p.fullHDURL, 使用原图
site: p.pageURL,
size: p.imageSize,
user: p.user,
date: new Date()
},
url: p.webformatURL,
outside: 'pixabay'
}));
}
return pictures;
}
searchShetu(keyword: string, limit: number = 30, page: number = 1, user = '') {
// 摄图网支持拼音搜索。
return this.http.post<any>(`${environment.host}/picture-manager/shetu-search`, { limit, page, keyword, user }).pipe(
map(res => {
let pictures: Picture[] = [];
if (res && res.pics.length) {
pictures = res.pics.map((p: any) => ({
picture: {
id: p.id,
name: `${p.id}`,
width: p.width,
height: p.height,
keywords: p.alt,
path: p.src,
site: 'shetu',
size: 1,
user: 'shetu',
date: new Date()
},
url: p.src,
outside: 'shetu'
}));
}
return pictures;
})
).toPromise();
}
async downloadShetu(picture: Picture): Promise<string> {
// 摄图网支持拼音搜索。
const download = await this.http.post<any>(`${environment.host}/picture-manager/shetu-download`,
{ id: picture.picture.id }).toPromise();
// console.log(download);
// 是否获取授权证书:https://699pic.com/index.php?m=Home&c=MyDownload&a=enterpriseContractDownload&pid=501610937
return download.url;
}
searchHaina(keyword = '', limit = 30, page = 1, user = '') {
// 本地数据库图片
return this.http.post<Picture[]>(`${environment.host}/picture-manager/search`, { limit, page, keyword, user }).toPromise();
}
searchHt(keyword = '', limit = 30, page = 1) {
// 多媒体中心
return this.http.post<Picture[]>(`${environment.host}/picture-manager/search-ht`, { limit, page, keyword }).toPromise();
}
likes(id: number) {
return this.http.post<any>(`${environment.host}/picture-manager/likes`, { id });
}
}
interface PictureCdn {
'jp': string;
'vc': string;
'vac': string;
'ru': string;
'it': string;
'gm': string;
}