feat(media): 添加获取WordPress媒体库列表功能
新增MediaController和WPService.getMedia方法,用于通过API获取WordPress站点的媒体文件列表 支持分页参数和站点ID验证 refactor(template): 更新模板种子数据逻辑 修改模板种子数据逻辑,支持更新已存在的模板 新增product.title模板配置
This commit is contained in:
parent
d91ec7bc60
commit
62f9ca947a
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -9,7 +9,7 @@ import { Template } from '../../entity/template.entity';
|
|||
export default class TemplateSeeder implements Seeder {
|
||||
/**
|
||||
* @method run
|
||||
* @description 执行数据填充操作.如果 product_sku 模板不存在,则创建它.
|
||||
* @description 执行数据填充操作.如果模板不存在,则创建它;如果存在,则更新它.
|
||||
* @param {DataSource} dataSource - 数据源实例,用于获取 repository.
|
||||
* @param {SeederFactoryManager} factoryManager - Seeder 工厂管理器.
|
||||
*/
|
||||
|
|
@ -20,17 +20,38 @@ export default class TemplateSeeder implements Seeder {
|
|||
// 获取 Template 实体的 repository
|
||||
const templateRepository = dataSource.getRepository(Template);
|
||||
|
||||
// 检查名为 'product_sku' 的模板是否已存在
|
||||
const existingTemplate = await templateRepository.findOne({
|
||||
where: { name: 'product_sku' },
|
||||
});
|
||||
const templates = [
|
||||
{
|
||||
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: '产品标题模板',
|
||||
},
|
||||
];
|
||||
|
||||
// 如果模板不存在,则创建并保存
|
||||
if (!existingTemplate) {
|
||||
const template = new Template();
|
||||
template.name = 'product_sku';
|
||||
template.value = '{{brand}}-{{flavor}}-{{strength}}-{{humidity}}';
|
||||
await templateRepository.save(template);
|
||||
for (const t of templates) {
|
||||
// 检查模板是否已存在
|
||||
const existingTemplate = await templateRepository.findOne({
|
||||
where: { name: t.name },
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -403,4 +403,37 @@ export class WPService {
|
|||
};
|
||||
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
|
||||
};
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue