324 lines
9.8 KiB
PHP
324 lines
9.8 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers;
|
||
|
||
use App\Models\Platform;
|
||
use App\Models\ShopAuth;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Facades\Validator;
|
||
use Illuminate\Support\Facades\Http;
|
||
|
||
class PlatformController extends Controller
|
||
{
|
||
/**
|
||
* 平台商品列表
|
||
*/
|
||
public function index(Request $request)
|
||
{
|
||
$query = Platform::with('shopAuth');
|
||
|
||
// 搜索条件
|
||
if ($request->filled('shop_auth_id')) {
|
||
$query->where('shop_auth_id', $request->shop_auth_id);
|
||
}
|
||
|
||
if ($request->filled('platform')) {
|
||
$query->whereHas('shopAuth', function ($q) use ($request) {
|
||
$q->where('platform', $request->platform);
|
||
});
|
||
}
|
||
|
||
if ($request->filled('title')) {
|
||
$query->where('title', 'like', '%' . $request->title . '%');
|
||
}
|
||
|
||
if ($request->filled('status')) {
|
||
$query->where('status', $request->status);
|
||
}
|
||
|
||
if ($request->filled('sync_status')) {
|
||
$query->where('sync_status', $request->sync_status);
|
||
}
|
||
|
||
// 排序
|
||
$sortField = $request->input('sort_field', 'created_at');
|
||
$sortOrder = $request->input('sort_order', 'desc');
|
||
$query->orderBy($sortField, $sortOrder);
|
||
|
||
$list = $query->paginate($request->input('limit', 10));
|
||
|
||
return $this->success([
|
||
'list' => $list->items(),
|
||
'total' => $list->total(),
|
||
'current_page' => $list->currentPage(),
|
||
'last_page' => $list->lastPage(),
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* 创建平台商品(手动添加)
|
||
*/
|
||
public function store(Request $request)
|
||
{
|
||
$validator = Validator::make($request->all(), [
|
||
'shop_auth_id' => 'required|exists:shop_auths,id',
|
||
'platform_product_id' => 'required|string|max:100',
|
||
'platform_sku_id' => 'nullable|string|max:100',
|
||
'title' => 'required|string|max:200',
|
||
'description' => 'nullable|string',
|
||
'price' => 'required|numeric|min:0',
|
||
'original_price' => 'nullable|numeric|min:0',
|
||
'stock' => 'required|integer|min:0',
|
||
'sold' => 'nullable|integer|min:0',
|
||
'images' => 'nullable|array',
|
||
'images.*' => 'url',
|
||
'specs' => 'nullable|array',
|
||
'status' => 'required|in:on_sale,off_sale,deleted',
|
||
]);
|
||
|
||
if ($validator->fails()) {
|
||
return $this->error(422, '参数错误', $validator->errors());
|
||
}
|
||
|
||
// 检查是否已存在
|
||
$exists = Platform::where('shop_auth_id', $request->shop_auth_id)
|
||
->where('platform_product_id', $request->platform_product_id)
|
||
->when($request->platform_sku_id, function ($q) use ($request) {
|
||
$q->where('platform_sku_id', $request->platform_sku_id);
|
||
})
|
||
->exists();
|
||
|
||
if ($exists) {
|
||
return $this->error(409, '该平台商品已存在');
|
||
}
|
||
|
||
$platform = Platform::create($request->only([
|
||
'shop_auth_id',
|
||
'platform_product_id',
|
||
'platform_sku_id',
|
||
'title',
|
||
'description',
|
||
'price',
|
||
'original_price',
|
||
'stock',
|
||
'sold',
|
||
'images',
|
||
'specs',
|
||
'status',
|
||
]));
|
||
|
||
return $this->success($platform, '创建成功');
|
||
}
|
||
|
||
/**
|
||
* 平台商品详情
|
||
*/
|
||
public function show($id)
|
||
{
|
||
$platform = Platform::with('shopAuth')->find($id);
|
||
if (!$platform) {
|
||
return $this->error(404, '平台商品不存在');
|
||
}
|
||
return $this->success($platform);
|
||
}
|
||
|
||
/**
|
||
* 更新平台商品
|
||
*/
|
||
public function update(Request $request, $id)
|
||
{
|
||
$platform = Platform::find($id);
|
||
if (!$platform) {
|
||
return $this->error(404, '平台商品不存在');
|
||
}
|
||
|
||
$validator = Validator::make($request->all(), [
|
||
'title' => 'sometimes|string|max:200',
|
||
'description' => 'nullable|string',
|
||
'price' => 'sometimes|numeric|min:0',
|
||
'original_price' => 'nullable|numeric|min:0',
|
||
'stock' => 'sometimes|integer|min:0',
|
||
'sold' => 'nullable|integer|min:0',
|
||
'images' => 'nullable|array',
|
||
'images.*' => 'url',
|
||
'specs' => 'nullable|array',
|
||
'status' => 'sometimes|in:on_sale,off_sale,deleted',
|
||
]);
|
||
|
||
if ($validator->fails()) {
|
||
return $this->error(422, '参数错误', $validator->errors());
|
||
}
|
||
|
||
$platform->update($request->only([
|
||
'title',
|
||
'description',
|
||
'price',
|
||
'original_price',
|
||
'stock',
|
||
'sold',
|
||
'images',
|
||
'specs',
|
||
'status',
|
||
]));
|
||
|
||
return $this->success($platform, '更新成功');
|
||
}
|
||
|
||
/**
|
||
* 删除平台商品
|
||
*/
|
||
public function destroy($id)
|
||
{
|
||
$platform = Platform::find($id);
|
||
if (!$platform) {
|
||
return $this->error(404, '平台商品不存在');
|
||
}
|
||
$platform->delete();
|
||
return $this->success(null, '删除成功');
|
||
}
|
||
|
||
/**
|
||
* 同步平台商品(从平台API拉取)
|
||
*/
|
||
public function sync(Request $request)
|
||
{
|
||
$validator = Validator::make($request->all(), [
|
||
'shop_auth_id' => 'required|exists:shop_auths,id',
|
||
'platform_product_ids' => 'nullable|array',
|
||
'platform_product_ids.*' => 'string',
|
||
'sync_all' => 'nullable|boolean',
|
||
]);
|
||
|
||
if ($validator->fails()) {
|
||
return $this->error(422, '参数错误', $validator->errors());
|
||
}
|
||
|
||
$shopAuth = ShopAuth::find($request->shop_auth_id);
|
||
if (!$shopAuth) {
|
||
return $this->error(404, '店铺授权不存在');
|
||
}
|
||
|
||
// 更新同步状态
|
||
Platform::where('shop_auth_id', $shopAuth->id)
|
||
->where('sync_status', '!=', 'syncing')
|
||
->update(['sync_status' => 'pending']);
|
||
|
||
// 这里应该调用平台API获取商品数据
|
||
// 由于各平台API不同,这里只做示例
|
||
$this->syncFromPlatform($shopAuth, $request->platform_product_ids ?? [], $request->sync_all ?? false);
|
||
|
||
return $this->success(null, '同步任务已开始');
|
||
}
|
||
|
||
/**
|
||
* 批量更新平台商品状态
|
||
*/
|
||
public function batchUpdate(Request $request)
|
||
{
|
||
$validator = Validator::make($request->all(), [
|
||
'ids' => 'required|array',
|
||
'ids.*' => 'exists:platforms,id',
|
||
'status' => 'required|in:on_sale,off_sale,deleted',
|
||
]);
|
||
|
||
if ($validator->fails()) {
|
||
return $this->error(422, '参数错误', $validator->errors());
|
||
}
|
||
|
||
Platform::whereIn('id', $request->ids)->update([
|
||
'status' => $request->status,
|
||
'sync_status' => 'pending', // 状态变更后需要重新同步
|
||
]);
|
||
|
||
return $this->success(null, '批量更新成功');
|
||
}
|
||
|
||
/**
|
||
* 获取平台商品统计
|
||
*/
|
||
public function stats(Request $request)
|
||
{
|
||
$shopAuthId = $request->input('shop_auth_id');
|
||
|
||
$query = Platform::query();
|
||
if ($shopAuthId) {
|
||
$query->where('shop_auth_id', $shopAuthId);
|
||
}
|
||
|
||
$stats = [
|
||
'total' => $query->count(),
|
||
'on_sale' => $query->where('status', 'on_sale')->count(),
|
||
'off_sale' => $query->where('status', 'off_sale')->count(),
|
||
'deleted' => $query->where('status', 'deleted')->count(),
|
||
'pending_sync' => $query->where('sync_status', 'pending')->count(),
|
||
'syncing' => $query->where('sync_status', 'syncing')->count(),
|
||
'synced' => $query->where('sync_status', 'synced')->count(),
|
||
'failed' => $query->where('sync_status', 'failed')->count(),
|
||
];
|
||
|
||
return $this->success($stats);
|
||
}
|
||
|
||
/**
|
||
* 从平台API同步商品数据(示例方法)
|
||
*/
|
||
private function syncFromPlatform(ShopAuth $shopAuth, array $productIds = [], bool $syncAll = false)
|
||
{
|
||
// 这里应该根据平台类型调用不同的API
|
||
// 示例:淘宝商品API
|
||
if ($shopAuth->platform === 'taobao') {
|
||
$this->syncFromTaobao($shopAuth, $productIds, $syncAll);
|
||
}
|
||
// 其他平台...
|
||
}
|
||
|
||
/**
|
||
* 从淘宝API同步商品(示例)
|
||
*/
|
||
private function syncFromTaobao(ShopAuth $shopAuth, array $productIds = [], bool $syncAll = false)
|
||
{
|
||
try {
|
||
// 调用淘宝API获取商品数据
|
||
// 这里只是示例,实际需要根据淘宝开放平台API文档实现
|
||
$response = Http::withHeaders([
|
||
'Authorization' => 'Bearer ' . $shopAuth->access_token,
|
||
])->get('https://api.taobao.com/router/rest', [
|
||
'method' => 'taobao.items.onsale.get',
|
||
'fields' => 'num_iid,title,price,pic_url,sku,etc',
|
||
'page_no' => 1,
|
||
'page_size' => 100,
|
||
]);
|
||
|
||
if ($response->successful()) {
|
||
$data = $response->json();
|
||
// 处理并保存商品数据
|
||
// ...
|
||
}
|
||
} catch (\Exception $e) {
|
||
\Log::error('同步淘宝商品失败: ' . $e->getMessage());
|
||
}
|
||
}
|
||
|
||
// 统一响应方法
|
||
private function success($data = null, $message = 'success', $code = 200)
|
||
{
|
||
return response()->json([
|
||
'code' => $code,
|
||
'data' => $data,
|
||
'message' => $message
|
||
], $code);
|
||
}
|
||
|
||
private function error($code, $message, $errors = null)
|
||
{
|
||
$response = [
|
||
'code' => $code,
|
||
'message' => $message,
|
||
];
|
||
if ($errors) {
|
||
$response['errors'] = $errors;
|
||
}
|
||
return response()->json($response, $code);
|
||
}
|
||
}
|