feat: 添加对订阅的显示与支持 #29

Merged
longbot merged 12 commits from dev_szk into main 2025-11-21 09:31:19 +00:00
1 changed files with 41 additions and 5 deletions
Showing only changes of commit dc070fadde - Show all commits

View File

@ -11,6 +11,24 @@ export class WPService {
@Config('wpSite') @Config('wpSite')
sites: WpSite[]; sites: WpSite[];
/**
* URL / /
* 使this.buildURL(wpApiUrl, '/wp-json', 'wc/v3/products', productId)
*/
private buildURL(base: string, ...parts: Array<string | number>): string {
// 去掉 base 末尾多余斜杠,但不影响协议中的 //
const baseSanitized = String(base).replace(/\/+$/g, '');
// 规范各段前后斜杠
const segments = parts
.filter((p) => p !== undefined && p !== null)
.map((p) => String(p))
.map((s) => s.replace(/^\/+|\/+$/g, ''))
.filter(Boolean);
const joined = [baseSanitized, ...segments].join('/');
// 折叠除协议外的多余斜杠,例如 https://example.com//a///b -> https://example.com/a/b
return joined.replace(/([^:])\/{2,}/g, '$1/');
}
/** /**
* WordPress * WordPress
* @param wpApiUrl WordPress REST API * @param wpApiUrl WordPress REST API
@ -31,7 +49,8 @@ export class WPService {
): Promise<T> { ): Promise<T> {
try { try {
const { wpApiUrl, consumerKey, consumerSecret } = site; const { wpApiUrl, consumerKey, consumerSecret } = site;
const url = `${wpApiUrl}/wp-json${endpoint}`; // 构建 URL规避多/或少/问题
const url = this.buildURL(wpApiUrl, '/wp-json', endpoint);
const auth = Buffer.from(`${consumerKey}:${consumerSecret}`).toString( const auth = Buffer.from(`${consumerKey}:${consumerSecret}`).toString(
'base64' 'base64'
); );
@ -64,7 +83,8 @@ export class WPService {
while (hasMore) { while (hasMore) {
const config: AxiosRequestConfig = { const config: AxiosRequestConfig = {
method: 'GET', method: 'GET',
url: `${wpApiUrl}/wp-json${endpoint}`, // 构建 URL规避多/或少/问题
url: this.buildURL(wpApiUrl, '/wp-json', endpoint),
headers: { headers: {
Authorization: `Basic ${auth}`, Authorization: `Basic ${auth}`,
}, },
@ -212,7 +232,8 @@ export class WPService {
); );
const config: AxiosRequestConfig = { const config: AxiosRequestConfig = {
method: 'PUT', method: 'PUT',
url: `${wpApiUrl}/wp-json${endpoint}`, // 构建 URL规避多/或少/问题
url: this.buildURL(wpApiUrl, '/wp-json', endpoint),
headers: { headers: {
Authorization: `Basic ${auth}`, Authorization: `Basic ${auth}`,
}, },
@ -310,7 +331,14 @@ export class WPService {
); );
const config: AxiosRequestConfig = { const config: AxiosRequestConfig = {
method: 'POST', method: 'POST',
url: `${wpApiUrl}/wp-json/wc-ast/v3/orders/${orderId}/shipment-trackings`, // 构建 URL规避多/或少/问题
url: this.buildURL(
wpApiUrl,
'/wp-json',
'wc-ast/v3/orders',
orderId,
'shipment-trackings'
),
headers: { headers: {
Authorization: `Basic ${auth}`, Authorization: `Basic ${auth}`,
}, },
@ -333,7 +361,15 @@ export class WPService {
// 删除接口: DELETE /wp-json/wc-shipment-tracking/v3/orders/<order_id>/shipment-trackings/<tracking_id> // 删除接口: DELETE /wp-json/wc-shipment-tracking/v3/orders/<order_id>/shipment-trackings/<tracking_id>
const config: AxiosRequestConfig = { const config: AxiosRequestConfig = {
method: 'DELETE', method: 'DELETE',
url: `${wpApiUrl}/wp-json/wc-ast/v3/orders/${orderId}/shipment-trackings/${trackingId}`, // 构建 URL规避多/或少/问题
url: this.buildURL(
wpApiUrl,
'/wp-json',
'wc-ast/v3/orders',
orderId,
'shipment-trackings',
trackingId
),
headers: { headers: {
Authorization: `Basic ${auth}`, Authorization: `Basic ${auth}`,
}, },