新版数智员工

This commit is contained in:
wong
2026-03-24 10:39:16 +08:00
parent 1853934d85
commit 7fb61b77a4
123 changed files with 38491 additions and 1018 deletions

View File

@@ -6,26 +6,30 @@ use app\store\model\VendorPackageModel;
use app\store\model\VendorProjectModel;
use app\store\model\VendorOrderModel;
use think\facade\Log;
use think\Db;
/**
* 套餐控制器
* 供应商套餐控制器
*/
class VendorController extends BaseController
{
/**
* 获取套餐列表
*
* 获取供应商套餐列表
* GET /v2/store/vendor/list
*
* @return \think\response\Json
*/
public function getList()
{
try {
$page = $this->request->param('page', 1);
$limit = $this->request->param('limit', 10);
$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]
];
@@ -35,41 +39,69 @@ class VendorController extends BaseController
$where[] = ['name', 'like', "%{$keyword}%"];
}
// 状态筛选
// 状态筛选1=上架0=下架)
if ($status !== '') {
$where[] = ['status', '=', $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' => $list,
'list' => $result,
'total' => $total,
'page' => $page,
'limit' => $limit
]
]);
} catch (\Exception $e) {
Log::error('获取套餐列表失败:' . $e->getMessage());
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 = $this->request->param('id', 0);
$id = intval($this->request->param('id', 0));
if (empty($id)) {
return json(['code' => 400, 'msg' => '参数错误']);
@@ -91,444 +123,116 @@ class VendorController extends BaseController
['isDel', '=', 0]
])->select();
$package['projects'] = $projects;
// 格式化套餐信息
$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'])) : '',
];
return json(['code' => 200, 'msg' => '获取成功', 'data' => $package]);
// 格式化项目信息
$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());
Log::error('获取供应商套餐详情失败:' . $e->getMessage());
return json(['code' => 500, 'msg' => '获取失败:' . $e->getMessage()]);
}
}
/**
* 添加套餐
*
* @return \think\response\Json
*/
public function add()
{
try {
if (!$this->request->isPost()) {
return json(['code' => 400, 'msg' => '请求方式错误']);
}
$param = $this->request->post();
// 参数验证
if (empty($param['name'])) {
return json(['code' => 400, 'msg' => '套餐名称不能为空']);
}
// 检查名称是否已存在
$exists = VendorPackageModel::where([
['name', '=', $param['name']],
['isDel', '=', 0]
])->find();
if ($exists) {
return json(['code' => 400, 'msg' => '该套餐名称已存在']);
}
Db::startTrans();
try {
// 创建套餐
$package = new VendorPackageModel;
$package->name = $param['name'];
$package->originalPrice = $param['originalPrice'] ?? 0;
$package->price = $param['price'] ?? 0;
$package->discount = $param['discount'] ?? 0;
$package->advancePayment = $param['advancePayment'] ?? 0;
$package->tags = $param['tags'] ?? '';
$package->description = $param['description'] ?? '';
$package->cover = $param['cover'] ?? '';
$package->status = $param['status'] ?? 1;
$package->createTime = time();
$package->updateTime = time();
$package->save();
// 处理项目信息
if (!empty($param['projects']) && is_array($param['projects'])) {
foreach ($param['projects'] as $projectData) {
if (empty($projectData['name'])) {
continue;
}
// 创建项目
$project = new VendorProjectModel;
$project->packageId = $package->id;
$project->name = $projectData['name'];
$project->originalPrice = $projectData['originalPrice'] ?? 0;
$project->price = $projectData['price'] ?? 0;
$project->duration = $projectData['duration'] ?? 0;
$project->image = $projectData['image'] ?? '';
$project->detail = $projectData['detail'] ?? '';
$project->createTime = time();
$project->updateTime = time();
$project->save();
}
}
Db::commit();
return json(['code' => 200, 'msg' => '添加成功', 'data' => ['id' => $package->id]]);
} catch (\Exception $e) {
Db::rollback();
Log::error('添加套餐失败:' . $e->getMessage());
return json(['code' => 500, 'msg' => '添加失败:' . $e->getMessage()]);
}
} catch (\Exception $e) {
Log::error('添加套餐异常:' . $e->getMessage());
return json(['code' => 500, 'msg' => '添加异常:' . $e->getMessage()]);
}
}
/**
* 编辑套餐
*
* @return \think\response\Json
*/
public function edit()
{
try {
if (!$this->request->isPost()) {
return json(['code' => 400, 'msg' => '请求方式错误']);
}
$param = $this->request->post();
// 参数验证
if (empty($param['id'])) {
return json(['code' => 400, 'msg' => '参数错误']);
}
if (empty($param['name'])) {
return json(['code' => 400, 'msg' => '套餐名称不能为空']);
}
// 检查套餐是否存在
$package = VendorPackageModel::where([
['id', '=', $param['id']],
['isDel', '=', 0]
])->find();
if (!$package) {
return json(['code' => 404, 'msg' => '套餐不存在']);
}
// 检查名称是否已存在
$exists = VendorPackageModel::where([
['name', '=', $param['name']],
['id', '<>', $param['id']],
['isDel', '=', 0]
])->find();
if ($exists) {
return json(['code' => 400, 'msg' => '该套餐名称已存在']);
}
Db::startTrans();
try {
// 更新套餐
$package->name = $param['name'];
$package->originalPrice = $param['originalPrice'] ?? $package->originalPrice;
$package->price = $param['price'] ?? $package->price;
$package->discount = $param['discount'] ?? $package->discount;
$package->advancePayment = $param['advancePayment'] ?? $package->advancePayment;
$package->tags = $param['tags'] ?? $package->tags;
$package->description = $param['description'] ?? $package->description;
$package->cover = $param['cover'] ?? $package->cover;
$package->status = $param['status'] ?? $package->status;
$package->updateTime = time();
$package->save();
Db::commit();
return json(['code' => 200, 'msg' => '更新成功']);
} catch (\Exception $e) {
Db::rollback();
Log::error('更新套餐失败:' . $e->getMessage());
return json(['code' => 500, 'msg' => '更新失败:' . $e->getMessage()]);
}
} catch (\Exception $e) {
Log::error('编辑套餐异常:' . $e->getMessage());
return json(['code' => 500, 'msg' => '编辑异常:' . $e->getMessage()]);
}
}
/**
* 删除套餐
*
* @return \think\response\Json
*/
public function delete()
{
try {
$id = $this->request->param('id', 0);
if (empty($id)) {
return json(['code' => 400, 'msg' => '参数错误']);
}
// 检查套餐是否存在
$package = VendorPackageModel::where([
['id', '=', $id],
['isDel', '=', 0]
])->find();
if (!$package) {
return json(['code' => 404, 'msg' => '套餐不存在']);
}
Db::startTrans();
try {
// 软删除套餐
$package->isDel = 1;
$package->updateTime = time();
$package->save();
// 软删除关联的项目
VendorProjectModel::where('packageId', $id)
->update([
'isDel' => 1,
'updateTime' => time()
]);
Db::commit();
return json(['code' => 200, 'msg' => '删除成功']);
} catch (\Exception $e) {
Db::rollback();
Log::error('删除套餐失败:' . $e->getMessage());
return json(['code' => 500, 'msg' => '删除失败:' . $e->getMessage()]);
}
} catch (\Exception $e) {
Log::error('删除套餐异常:' . $e->getMessage());
return json(['code' => 500, 'msg' => '删除异常:' . $e->getMessage()]);
}
}
/**
* 添加项目
*
* @return \think\response\Json
*/
public function addProject()
{
try {
if (!$this->request->isPost()) {
return json(['code' => 400, 'msg' => '请求方式错误']);
}
$param = $this->request->post();
// 参数验证
if (empty($param['packageId'])) {
return json(['code' => 400, 'msg' => '套餐ID不能为空']);
}
if (empty($param['name'])) {
return json(['code' => 400, 'msg' => '项目名称不能为空']);
}
// 检查套餐是否存在
$package = VendorPackageModel::where([
['id', '=', $param['packageId']],
['isDel', '=', 0]
])->find();
if (!$package) {
return json(['code' => 404, 'msg' => '套餐不存在']);
}
try {
// 创建项目
$project = new VendorProjectModel;
$project->packageId = $param['packageId'];
$project->name = $param['name'];
$project->originalPrice = $param['originalPrice'] ?? 0;
$project->price = $param['price'] ?? 0;
$project->duration = $param['duration'] ?? 0;
$project->image = $param['image'] ?? '';
$project->detail = $param['detail'] ?? '';
$project->createTime = time();
$project->updateTime = time();
$project->save();
return json(['code' => 200, 'msg' => '添加成功', 'data' => ['id' => $project->id]]);
} catch (\Exception $e) {
Log::error('添加项目失败:' . $e->getMessage());
return json(['code' => 500, 'msg' => '添加失败:' . $e->getMessage()]);
}
} catch (\Exception $e) {
Log::error('添加项目异常:' . $e->getMessage());
return json(['code' => 500, 'msg' => '添加异常:' . $e->getMessage()]);
}
}
/**
* 编辑项目
*
* @return \think\response\Json
*/
public function editProject()
{
try {
if (!$this->request->isPost()) {
return json(['code' => 400, 'msg' => '请求方式错误']);
}
$param = $this->request->post();
// 参数验证
if (empty($param['id'])) {
return json(['code' => 400, 'msg' => '项目ID不能为空']);
}
if (empty($param['name'])) {
return json(['code' => 400, 'msg' => '项目名称不能为空']);
}
// 检查项目是否存在
$project = VendorProjectModel::where([
['id', '=', $param['id']],
['isDel', '=', 0]
])->find();
if (!$project) {
return json(['code' => 404, 'msg' => '项目不存在']);
}
try {
// 更新项目
$project->name = $param['name'];
$project->originalPrice = $param['originalPrice'] ?? $project->originalPrice;
$project->price = $param['price'] ?? $project->price;
$project->duration = $param['duration'] ?? $project->duration;
$project->image = $param['image'] ?? $project->image;
$project->detail = $param['detail'] ?? $project->detail;
$project->updateTime = time();
$project->save();
return json(['code' => 200, 'msg' => '更新成功']);
} catch (\Exception $e) {
Log::error('更新项目失败:' . $e->getMessage());
return json(['code' => 500, 'msg' => '更新失败:' . $e->getMessage()]);
}
} catch (\Exception $e) {
Log::error('编辑项目异常:' . $e->getMessage());
return json(['code' => 500, 'msg' => '编辑异常:' . $e->getMessage()]);
}
}
/**
* 删除项目
*
* @return \think\response\Json
*/
public function deleteProject()
{
try {
$id = $this->request->param('id', 0);
if (empty($id)) {
return json(['code' => 400, 'msg' => '参数错误']);
}
// 检查项目是否存在
$project = VendorProjectModel::where([
['id', '=', $id],
['isDel', '=', 0]
])->find();
if (!$project) {
return json(['code' => 404, 'msg' => '项目不存在']);
}
try {
// 软删除项目
$project->isDel = 1;
$project->updateTime = time();
$project->save();
return json(['code' => 200, 'msg' => '删除成功']);
} catch (\Exception $e) {
Log::error('删除项目失败:' . $e->getMessage());
return json(['code' => 500, 'msg' => '删除失败:' . $e->getMessage()]);
}
} 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 {
if (!$this->request->isPost()) {
return json(['code' => 400, 'msg' => '请求方式错误']);
}
$packageId = intval($this->request->param('packageId', 0));
$remark = $this->request->param('remark', '');
$param = $this->request->post();
// 参数验证
if (empty($param['packageId'])) {
if (empty($packageId)) {
return json(['code' => 400, 'msg' => '套餐ID不能为空']);
}
// 检查套餐是否存在
$package = VendorPackageModel::where([
['id', '=', $param['packageId']],
['isDel', '=', 0],
['status', '=', 1]
])->find();
if (!$package) {
return json(['code' => 404, 'msg' => '套餐不存在或已下架']);
}
// 获取当前用户信息
$userId = $this->request->userInfo['id'];
// 获取用户信息
$userId = $this->userInfo['id'] ?? 0;
$companyId = $this->userInfo['companyId'] ?? 0;
if (empty($userId)) {
return json(['code' => 401, 'msg' => '请先登录']);
}
Db::startTrans();
try {
// 生成订单
$order = new VendorOrderModel;
$order->orderNo = VendorOrderModel::generateOrderNo();
$order->userId = $userId;
$order->packageId = $package->id;
$order->packageName = $package->name;
$order->totalAmount = $package->price;
$order->payAmount = $package->price;
$order->advancePayment = $package->advancePayment;
$order->status = VendorOrderModel::STATUS_UNPAID;
$order->remark = $param['remark'] ?? '';
$order->createTime = time();
$order->updateTime = time();
$order->save();
Db::commit();
return json([
'code' => 200,
'msg' => '订单创建成功',
'data' => [
'orderId' => $order->id,
'orderNo' => $order->orderNo
]
]);
} catch (\Exception $e) {
Db::rollback();
Log::error('创建订单失败:' . $e->getMessage());
return json(['code' => 500, 'msg' => '创建订单失败:' . $e->getMessage()]);
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()]);
Log::error('创建供应商订单失败' . $e->getMessage());
return json(['code' => 500, 'msg' => '创建订单失败' . $e->getMessage()]);
}
}
}
}