This commit is contained in:
c
2025-03-12 20:11:03 +08:00
commit e65b262527
1179 changed files with 148205 additions and 0 deletions
@@ -0,0 +1,97 @@
const API_URL = 'https://api.giphy.com/v1'
exports.search = async function (params = {}, config = {}){
const {keyword, page = 1, pageSize} = params
const {apiKey} = config
if (!apiKey) {
throw new Error('Giphy provider is not enabled')
}
if (pageSize > 50) {
throw new Error('pageSize should not be greater than 50')
}
const url = `${API_URL}/gifs/search?api_key=${apiKey}`
const res = await uniCloud.request({
url,
data: {
q: keyword,
offset: (page - 1) * pageSize,
limit: pageSize
}
})
const {statusCode, data} = res
if (statusCode !== 200) {
console.error(statusCode, res.data)
throw new Error(`statusCode: ${statusCode}`)
}
if (data.meta && data.meta.status !== 200) {
console.error(data.meta.status, data.meta.msg)
throw new Error(`status: ${data.meta.status}, errMsg: ${data.meta.msg}`)
}
if (!data.data) return []
return data.data.map(item => {
return {
id: item.id,
url: item.images.original.webp,
thumbUrl: item.images.preview_gif.url,
width: Number(item.images.original.width),
height: Number(item.images.original.height),
size: Number(item.images.original.webp_size),
description: item.title,
alt: item.alt_text,
originalName: item.slug || item.id || "'",
}
})
}
exports.detail = async function (params = {}, config = {}) {
const {id} = params
const {apiKey} = config
if (!apiKey) {
throw new Error('Giphy provider is not enabled')
}
if (!id) {
throw new Error('Giphy id is required')
}
const url = `${API_URL}/gifs/${id}?api_key=${apiKey}`
const res = await uniCloud.request({
url
})
const {statusCode, data} = res
if (statusCode !== 200) {
console.error(statusCode, data)
throw new Error(`statusCode: ${statusCode}`)
}
if (data.meta && data.meta.status !== 200) {
console.error(data.meta.status, data.meta.msg)
throw new Error(`status: ${data.meta.status}, errMsg: ${data.meta.msg}`)
}
const result = data.data
return {
id: result.id,
url: result.images.original.webp,
thumbUrl: result.images.preview_gif.url,
width: Number(result.images.original.width),
height: Number(result.images.original.height),
size: Number(result.images.original.webp_size),
description: result.title,
alt: result.alt_text,
originalName: result.slug || result.id || "",
fileType: 'webp'
}
}
@@ -0,0 +1,84 @@
const API_URL = 'https://api.pexels.com/v1'
exports.search = async function (params = {}, config = {}){
const {keyword, page, pageSize} = params
const {apiKey} = config
if (!apiKey) {
throw new Error('Pexels provider is not enabled')
}
const url = `${API_URL}/search`
const res = await uniCloud.request({
url,
data: {
query: keyword,
page,
per_page: pageSize
},
header: {
Authorization: `${apiKey}`
}
})
if (res.statusCode !== 200) {
console.error(res.statusCode, res.data)
throw new Error(`statusCode: ${res.statusCode}`)
}
return res.data.photos.map(item => {
return {
id: item.id,
url: item.src.original,
thumbUrl: item.src.tiny,
width: item.width,
height: item.height,
description: item.description,
alt: item.alt,
originalName: item.slug || item.id || "'",
}
})
}
exports.detail = async function (params = {}, config = {}) {
const {id} = params
const {apiKey} = config
if (!apiKey) {
throw new Error('Pexels provider is not enabled')
}
if (!id) {
throw new Error('Pexels id is required')
}
const url = `${API_URL}/photos/${id}`
const res = await uniCloud.request({
url,
header: {
Authorization: `${apiKey}`
}
})
if (res.statusCode !== 200) {
console.error(res.statusCode, res.data)
throw new Error(`statusCode: ${res.statusCode}`)
}
const {data} = res
const filename = data.src.original.split('/').pop()
const [name, ext = 'jpeg'] = filename.split('.')
return {
id: data.id,
url: data.src.original,
thumbUrl: data.src.tiny,
width: data.width,
height: data.height,
description: data.description,
alt: data.alt,
originalName: filename || "",
fileType: ext
}
}
@@ -0,0 +1,81 @@
const API_URL = 'https://api.unsplash.com'
exports.search = async function (params = {}, config = {}){
const {keyword, page, pageSize} = params
const {appId, accessKey, secretKey} = config
if (!appId || !accessKey || !secretKey) {
throw new Error('Unsplash provider is not enabled')
}
const url = `${API_URL}/search/photos`
const res = await uniCloud.request({
url,
data: {
query: keyword,
page,
per_page: pageSize
},
header: {
Authorization: `Client-ID ${accessKey}`
}
})
if (res.statusCode !== 200) {
console.error(res.statusCode, res.data)
throw new Error(`statusCode: ${res.statusCode}`)
}
return res.data.results.map(item => {
return {
id: item.id,
url: item.urls.raw,
thumbUrl: item.urls.thumb,
width: item.width,
height: item.height,
description: item.description,
alt: item.alt_description,
originalName: item.slug || item.id || "'",
}
})
}
exports.detail = async function (params = {}, config = {}) {
const {id} = params
const {appId, accessKey, secretKey} = config
if (!appId || !accessKey || !secretKey) {
throw new Error('Unsplash provider is not enabled')
}
if (!id) {
throw new Error('Unsplash id is required')
}
const url = `${API_URL}/photos/${id}`
const res = await uniCloud.request({
url,
header: {
Authorization: `Client-ID ${accessKey}`
}
})
if (res.statusCode !== 200) {
console.error(res.statusCode, res.data)
throw new Error(`statusCode: ${res.statusCode}`)
}
const {data} = res
return {
id: data.id,
url: data.urls.raw,
thumbUrl: data.urls.thumb,
width: data.width,
height: data.height,
description: data.description,
alt: data.alt_description,
originalName: data.slug || data.id || "",
}
}