feat(media): 添加获取WordPress媒体库列表功能

新增MediaController和WPService.getMedia方法,用于通过API获取WordPress站点的媒体文件列表
支持分页参数和站点ID验证

refactor(template): 更新模板种子数据逻辑
修改模板种子数据逻辑,支持更新已存在的模板
新增product.title模板配置
This commit is contained in:
tikkhun 2025-12-03 16:29:04 +08:00
parent d91ec7bc60
commit 62f9ca947a
3 changed files with 91 additions and 11 deletions

View File

@ -0,0 +1,26 @@
import { Controller, Get, Inject, Query } from '@midwayjs/core';
import { WPService } from '../service/wp.service';
import { successResponse, errorResponse } from '../utils/response.util';
@Controller('/media')
export class MediaController {
@Inject()
wpService: WPService;
@Get('/list')
async list(
@Query('siteId') siteId: number,
@Query('page') page: number = 1,
@Query('pageSize') pageSize: number = 20
) {
try {
if (!siteId) {
return errorResponse('siteId is required');
}
const result = await this.wpService.getMedia(siteId, page, pageSize);
return successResponse(result);
} catch (error) {
return errorResponse(error.message);
}
}
}

View File

@ -9,7 +9,7 @@ import { Template } from '../../entity/template.entity';
export default class TemplateSeeder implements Seeder { export default class TemplateSeeder implements Seeder {
/** /**
* @method run * @method run
* @description . product_sku ,. * @description .,;,.
* @param {DataSource} dataSource - , repository. * @param {DataSource} dataSource - , repository.
* @param {SeederFactoryManager} factoryManager - Seeder . * @param {SeederFactoryManager} factoryManager - Seeder .
*/ */
@ -20,17 +20,38 @@ export default class TemplateSeeder implements Seeder {
// 获取 Template 实体的 repository // 获取 Template 实体的 repository
const templateRepository = dataSource.getRepository(Template); const templateRepository = dataSource.getRepository(Template);
// 检查名为 'product_sku' 的模板是否已存在 const templates = [
const existingTemplate = await templateRepository.findOne({ {
where: { name: 'product_sku' }, name: 'product.sku',
}); value: '<%= it.brand %>-<%=it.category%>-<%= it.flavor %>-<%= it.strength %>-<%= it.humidity %>',
description: '产品SKU模板',
},
{
name: 'product.title',
value: '<%= it.brand %> <%= it.flavor %> <%= it.strength %> <%= it.humidity %>',
description: '产品标题模板',
},
];
// 如果模板不存在,则创建并保存 for (const t of templates) {
if (!existingTemplate) { // 检查模板是否已存在
const template = new Template(); const existingTemplate = await templateRepository.findOne({
template.name = 'product_sku'; where: { name: t.name },
template.value = '{{brand}}-{{flavor}}-{{strength}}-{{humidity}}'; });
await templateRepository.save(template);
if (existingTemplate) {
// 如果存在,则更新
existingTemplate.value = t.value;
existingTemplate.description = t.description;
await templateRepository.save(existingTemplate);
} else {
// 如果不存在,则创建并保存
const template = new Template();
template.name = t.name;
template.value = t.value;
template.description = t.description;
await templateRepository.save(template);
}
} }
} }
} }

View File

@ -403,4 +403,37 @@ export class WPService {
}; };
return await axios.request(config); return await axios.request(config);
} }
/**
* WordPress
* @param siteId ID
* @param page
* @param perPage
*/
async getMedia(siteId: number, page: number = 1, perPage: number = 20): Promise<{ items: any[], total: number, totalPages: number }> {
const site = await this.siteService.get(siteId, true);
if (!site) {
throw new Error('站点不存在');
}
const endpoint = 'wp/v2/media';
const apiUrl = site.apiUrl;
const { consumerKey, consumerSecret } = site as any;
// 构建 URL,规避多/或少/问题
const url = this.buildURL(apiUrl, '/wp-json', endpoint);
const auth = Buffer.from(`${consumerKey}:${consumerSecret}`).toString('base64');
const response = await axios.get(url, {
headers: { Authorization: `Basic ${auth}` },
params: { page, per_page: perPage }
});
const total = Number(response.headers['x-wp-total'] || 0);
const totalPages = Number(response.headers['x-wp-totalpages'] || 0);
return {
items: response.data,
total,
totalPages
};
}
} }