Feature: 增加上下架功能 #15
|
|
@ -39,7 +39,7 @@ export default {
|
|||
},
|
||||
{
|
||||
id: '2',
|
||||
wpApiUrl: 'http://t1-shop.local/',
|
||||
wpApiUrl: 'http://t2-shop.local/',
|
||||
consumerKey: 'ck_a369473a6451dbaec63d19cbfd74a074b2c5f742',
|
||||
consumerSecret: 'cs_0946bbbeea1bfefff08a69e817ac62a48412df8c',
|
||||
siteName: 'Local',
|
||||
|
|
|
|||
|
|
@ -75,7 +75,7 @@ export class WebhookController {
|
|||
switch (topic) {
|
||||
case 'product.created':
|
||||
case 'product.updated':
|
||||
const site = await this.wpProductService.geSite(siteId);
|
||||
const site = await this.wpProductService.getSite(siteId);
|
||||
// 变体更新
|
||||
if (body.type === 'variation') {
|
||||
const variation = await this.wpApiService.getVariation(
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import {
|
|||
Query,
|
||||
Put,
|
||||
Body,
|
||||
Config,
|
||||
} from '@midwayjs/core';
|
||||
import { WpProductService } from '../service/wp_product.service';
|
||||
import { errorResponse, successResponse } from '../utils/response.util';
|
||||
|
|
@ -19,9 +20,21 @@ import {
|
|||
UpdateWpProductDTO,
|
||||
} from '../dto/wp_product.dto';
|
||||
import { WPService } from '../service/wp.service';
|
||||
import { WpSite } from '../interface';
|
||||
|
||||
@Controller('/wp_product')
|
||||
export class WpProductController {
|
||||
@Inject()
|
||||
wpService: WPService;
|
||||
|
||||
@Config('wpSite')
|
||||
sites: WpSite[];
|
||||
|
||||
getSite(id: string): WpSite {
|
||||
let idx = this.sites.findIndex(item => item.id === id);
|
||||
return this.sites[idx];
|
||||
}
|
||||
|
||||
@Inject()
|
||||
private readonly wpProductService: WpProductService;
|
||||
|
||||
|
|
@ -73,6 +86,22 @@ export class WpProductController {
|
|||
}
|
||||
}
|
||||
|
||||
@ApiOkResponse({
|
||||
type: BooleanRes
|
||||
})
|
||||
@Post('/updateState/:id')
|
||||
async updateWPProductState(
|
||||
@Param('id') id: number,
|
||||
@Body() body: any, // todo
|
||||
) {
|
||||
try {
|
||||
await this.wpProductService.updateProductStatus(id, body?.status);
|
||||
return successResponse(true);
|
||||
} catch (error) {
|
||||
return errorResponse(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新产品接口
|
||||
* @param productId 产品 ID
|
||||
|
|
@ -96,7 +125,7 @@ export class WpProductController {
|
|||
if (isDuplicate) {
|
||||
return errorResponse('SKU已存在');
|
||||
}
|
||||
const site = await this.wpProductService.geSite(siteId);
|
||||
const site = await this.wpProductService.getSite(siteId);
|
||||
const result = await this.wpApiService.updateProduct(
|
||||
site,
|
||||
productId,
|
||||
|
|
@ -136,7 +165,7 @@ export class WpProductController {
|
|||
if (isDuplicate) {
|
||||
return errorResponse('SKU已存在');
|
||||
}
|
||||
const site = await this.wpProductService.geSite(siteId);
|
||||
const site = await this.wpProductService.getSite(siteId);
|
||||
const result = await this.wpApiService.updateVariation(
|
||||
site,
|
||||
productId,
|
||||
|
|
|
|||
|
|
@ -85,7 +85,7 @@ export class LogisticsService {
|
|||
@Config('wpSite')
|
||||
sites: WpSite[];
|
||||
|
||||
geSite(id: string): WpSite {
|
||||
getSite(id: string): WpSite {
|
||||
let idx = this.sites.findIndex(item => item.id === id);
|
||||
return this.sites[idx];
|
||||
}
|
||||
|
|
@ -263,7 +263,7 @@ export class LogisticsService {
|
|||
|
||||
try {
|
||||
// 同步订单状态到woocommerce
|
||||
const site = await this.geSite(order.siteId);
|
||||
const site = await this.getSite(order.siteId);
|
||||
if (order.status === OrderStatus.COMPLETED) {
|
||||
await this.wpService.updateOrder(site, order.externalOrderId, {
|
||||
status: OrderStatus.PROCESSING,
|
||||
|
|
@ -367,7 +367,7 @@ export class LogisticsService {
|
|||
const tracking_provider = 'UniUni'; // todo: id未确定,后写进常数
|
||||
|
||||
// 同步物流信息到woocommerce
|
||||
const site = await this.geSite(order.siteId);
|
||||
const site = await this.getSite(order.siteId);
|
||||
const res = await this.wpService.createShipment(site, order.externalOrderId, {
|
||||
tracking_number: resShipmentOrder.data.tno,
|
||||
tracking_provider: tracking_provider,
|
||||
|
|
@ -493,7 +493,7 @@ export class LogisticsService {
|
|||
const order = await this.orderModel.findOneBy({
|
||||
id: orderShipment.order_id,
|
||||
});
|
||||
const site = this.geSite(order.siteId);
|
||||
const site = this.getSite(order.siteId);
|
||||
await this.wpService.updateOrder(site, order.externalOrderId, {
|
||||
status: OrderStatus.COMPLETED,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import { WpSite } from '../interface';
|
|||
import { WpProduct } from '../entity/wp_product.entity';
|
||||
import { Variation } from '../entity/variation.entity';
|
||||
import { UpdateVariationDTO, UpdateWpProductDTO } from '../dto/wp_product.dto';
|
||||
import { ProductStatus } from '../enums/base.enum';
|
||||
|
||||
@Provide()
|
||||
export class WPService {
|
||||
|
|
@ -222,6 +223,23 @@ export class WPService {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新 WooCommerce 产品 上下架状态
|
||||
* @param productId 产品 ID
|
||||
* @param status 状态
|
||||
*/
|
||||
async updateProductStatus(
|
||||
site: WpSite,
|
||||
productId: string,
|
||||
status: ProductStatus,
|
||||
): Promise<Boolean> {
|
||||
const res = await this.updateData(`/wc/v3/products/${productId}`, site, {
|
||||
status
|
||||
});
|
||||
console.log('res', res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新 WooCommerce 产品变体
|
||||
* @param productId 产品 ID
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import {
|
|||
UpdateWpProductDTO,
|
||||
} from '../dto/wp_product.dto';
|
||||
import { Product } from '../entity/product.entty';
|
||||
import { ProductStatus } from '../enums/base.enum';
|
||||
|
||||
@Provide()
|
||||
export class WpProductService {
|
||||
|
|
@ -26,7 +27,7 @@ export class WpProductService {
|
|||
@InjectEntityModel(Variation)
|
||||
variationModel: Repository<Variation>;
|
||||
|
||||
geSite(id: string): WpSite {
|
||||
getSite(id: string): WpSite {
|
||||
let idx = this.sites.findIndex(item => item.id === id);
|
||||
return this.sites[idx];
|
||||
}
|
||||
|
|
@ -45,7 +46,7 @@ export class WpProductService {
|
|||
}
|
||||
|
||||
async syncSite(siteId: string) {
|
||||
const site = this.geSite(siteId);
|
||||
const site = this.getSite(siteId);
|
||||
const products = await this.wpApiService.getProducts(site);
|
||||
for (const product of products) {
|
||||
const variations =
|
||||
|
|
@ -56,6 +57,20 @@ export class WpProductService {
|
|||
}
|
||||
}
|
||||
|
||||
// 控制产品上下架
|
||||
async updateProductStatus(id: number, status: ProductStatus) {
|
||||
const wpProduct = await this.wpProductModel.findOneBy({ id });
|
||||
const site = await this.getSite(wpProduct.siteId);
|
||||
wpProduct.status = status;
|
||||
const res = await this.wpApiService.updateProductStatus(site, wpProduct.externalProductId, status);
|
||||
if (res === true) {
|
||||
this.wpProductModel.save(wpProduct);
|
||||
return true;
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
async findProduct(
|
||||
siteId: string,
|
||||
externalProductId: string
|
||||
|
|
|
|||
Loading…
Reference in New Issue