Files
CKB-Interface/application/store/controller/VendorController.php
2026-03-24 10:39:16 +08:00

239 lines
8.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\store\controller;
use app\store\model\VendorPackageModel;
use app\store\model\VendorProjectModel;
use app\store\model\VendorOrderModel;
use think\facade\Log;
/**
* 供应商套餐控制器
*/
class VendorController extends BaseController
{
/**
* 获取供应商套餐列表
* GET /v2/store/vendor/list
*
* @return \think\response\Json
*/
public function getList()
{
try {
$page = intval($this->request->param('page', 1));
$limit = intval($this->request->param('limit', $this->request->param('pageSize', 10))); // 兼容 pageSize 参数
$keyword = $this->request->param('keyword', '');
$status = $this->request->param('status', '');
// 确保分页参数有效
if ($page <= 0) $page = 1;
if ($limit <= 0) $limit = 10;
$where = [
['isDel', '=', 0]
];
// 关键词搜索
if (!empty($keyword)) {
$where[] = ['name', 'like', "%{$keyword}%"];
}
// 状态筛选1=上架0=下架)
if ($status !== '') {
$where[] = ['status', '=', intval($status)];
} else {
// 默认只显示上架的套餐
$where[] = ['status', '=', 1];
}
$list = VendorPackageModel::where($where)
->field('id, userId, companyId, name, originalPrice, price, discount, advancePayment, tags, description, cover, status, createTime, updateTime')
->order('id', 'desc')
->page($page, $limit)
->select();
$total = VendorPackageModel::where($where)->count();
// 格式化返回数据
$result = [];
foreach ($list as $item) {
$result[] = [
'id' => intval($item['id']),
'userId' => intval($item['userId'] ?? 0),
'companyId' => intval($item['companyId'] ?? 0),
'name' => $item['name'],
'originalPrice' => floatval($item['originalPrice']),
'price' => floatval($item['price']),
'discount' => $item->discount,
'advancePayment' => floatval($item['advancePayment'] ?? 0),
'tags' => $item->tags,
'description' => $item['description'] ?? '',
'cover' => $item['cover'] ?? '',
'status' => intval($item['status']),
'createTime' => !empty($item['createTime']) && is_numeric($item['createTime']) ? date('Y-m-d H:i:s', intval($item['createTime'])) : '',
'updateTime' => !empty($item['updateTime']) && is_numeric($item['updateTime']) ? date('Y-m-d H:i:s', intval($item['updateTime'])) : '',
];
}
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'list' => $result,
'total' => $total,
'page' => $page,
'limit' => $limit
]
]);
} catch (\Exception $e) {
Log::error('获取供应商套餐列表失败:' . $e->getMessage());
return json(['code' => 500, 'msg' => '获取失败:' . $e->getMessage()]);
}
}
/**
* 获取供应商套餐详情
* GET /v2/store/vendor/detail
*
* @return \think\response\Json
*/
public function detail()
{
try {
$id = intval($this->request->param('id', 0));
if (empty($id)) {
return json(['code' => 400, 'msg' => '参数错误']);
}
// 查询套餐基本信息
$package = VendorPackageModel::where([
['id', '=', $id],
['isDel', '=', 0]
])->find();
if (empty($package)) {
return json(['code' => 404, 'msg' => '套餐不存在']);
}
// 查询项目列表
$projects = VendorProjectModel::where([
['packageId', '=', $id],
['isDel', '=', 0]
])->select();
// 格式化套餐信息
$packageData = [
'id' => intval($package['id']),
'userId' => intval($package['userId'] ?? 0),
'companyId' => intval($package['companyId'] ?? 0),
'name' => $package['name'],
'originalPrice' => floatval($package['originalPrice']),
'price' => floatval($package['price']),
'discount' => $package->discount,
'advancePayment' => floatval($package['advancePayment'] ?? 0),
'tags' => $package->tags,
'description' => $package['description'] ?? '',
'cover' => $package['cover'] ?? '',
'status' => intval($package['status']),
'createTime' => !empty($package['createTime']) && is_numeric($package['createTime']) ? date('Y-m-d H:i:s', intval($package['createTime'])) : '',
'updateTime' => !empty($package['updateTime']) && is_numeric($package['updateTime']) ? date('Y-m-d H:i:s', intval($package['updateTime'])) : '',
];
// 格式化项目信息
$projectList = [];
foreach ($projects as $project) {
$projectList[] = [
'id' => intval($project['id']),
'packageId' => intval($project['packageId']),
'name' => $project['name'],
'originalPrice' => floatval($project['originalPrice']),
'price' => floatval($project['price']),
'duration' => intval($project['duration'] ?? 0),
'image' => $project['image'] ?? '',
'detail' => $project['detail'] ?? '',
];
}
$packageData['projects'] = $projectList;
return json(['code' => 200, 'msg' => '获取成功', 'data' => $packageData]);
} catch (\Exception $e) {
Log::error('获取供应商套餐详情失败:' . $e->getMessage());
return json(['code' => 500, 'msg' => '获取失败:' . $e->getMessage()]);
}
}
/**
* 创建供应商订单
* POST /v2/store/vendor/order
*
* @return \think\response\Json
*/
public function createOrder()
{
try {
$packageId = intval($this->request->param('packageId', 0));
$remark = $this->request->param('remark', '');
if (empty($packageId)) {
return json(['code' => 400, 'msg' => '套餐ID不能为空']);
}
// 获取用户信息
$userId = $this->userInfo['id'] ?? 0;
$companyId = $this->userInfo['companyId'] ?? 0;
if (empty($userId)) {
return json(['code' => 401, 'msg' => '请先登录']);
}
if (empty($companyId)) {
return json(['code' => 400, 'msg' => '公司信息不存在']);
}
// 检查套餐是否存在且上架
$package = VendorPackageModel::where([
['id', '=', $packageId],
['isDel', '=', 0],
['status', '=', 1]
])->find();
if (empty($package)) {
return json(['code' => 404, 'msg' => '套餐不存在或已下架']);
}
// 创建订单
$order = VendorOrderModel::createOrder(
$userId,
$companyId,
$package->id,
$package->name,
floatval($package->price),
floatval($package->price),
floatval($package->advancePayment ?? 0),
$remark
);
if (!$order) {
return json(['code' => 500, 'msg' => '订单创建失败']);
}
return json([
'code' => 200,
'msg' => '订单创建成功',
'data' => [
'orderId' => intval($order['id']),
'orderNo' => $order['orderNo']
]
]);
} catch (\Exception $e) {
Log::error('创建供应商订单失败:' . $e->getMessage());
return json(['code' => 500, 'msg' => '创建订单失败:' . $e->getMessage()]);
}
}
}