244 lines
7.8 KiB
PHP
244 lines
7.8 KiB
PHP
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||
// +----------------------------------------------------------------------
|
||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||
// | 开源版本可自由商用,可去除界面版权logo
|
||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||
// | 访问官网:https://www.likeadmin.cn
|
||
// | likeadmin团队 版权所有 拥有最终解释权
|
||
// +----------------------------------------------------------------------
|
||
// | author: likeadminTeam
|
||
// +----------------------------------------------------------------------
|
||
|
||
|
||
namespace app\adminapi\controller;
|
||
|
||
|
||
use app\adminapi\controller\BaseAdminController;
|
||
use app\adminapi\lists\GoodsLists;
|
||
use app\adminapi\logic\GoodsLogic;
|
||
use app\adminapi\validate\GoodsValidate;
|
||
use think\facade\Db;
|
||
|
||
|
||
/**
|
||
* Goods控制器
|
||
* Class GoodsController
|
||
* @package app\adminapi\controller
|
||
*/
|
||
class GoodsController extends BaseAdminController
|
||
{
|
||
|
||
|
||
/**
|
||
* @notes 获取列表
|
||
* @return \think\response\Json
|
||
* @author likeadmin
|
||
* @date 2025/05/08 15:34
|
||
*/
|
||
public function lists()
|
||
{
|
||
return $this->dataLists(new GoodsLists());
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 添加
|
||
* @return \think\response\Json
|
||
* @author likeadmin
|
||
* @date 2025/05/08 15:34
|
||
*/
|
||
public function add()
|
||
{
|
||
$post = $this->request->post();
|
||
$post['del'] = 0;
|
||
|
||
//主表验证
|
||
$result = $this->validate($post, 'app\adminapi\validate\GoodsValidate.add');
|
||
if ($result !== true) {
|
||
return $this->fail($result);
|
||
}
|
||
|
||
//单规格验证
|
||
if ($post['spec_type'] == 1) {
|
||
$result = $this->validate($post, 'app\adminapi\validate\GoodsOneSpecValidate');
|
||
if ($result !== true) {
|
||
return $this->fail($result);
|
||
}
|
||
}
|
||
|
||
//多规格验证
|
||
$spec_lists = [];
|
||
if ($post['spec_type'] == 2) {
|
||
$spec_lists = $post;
|
||
|
||
// 规格值验证长度验证
|
||
foreach($spec_lists['spec_value_str'] as $key => $item) {
|
||
$itemArr = explode(',', $item);
|
||
foreach($itemArr as $subItem) {
|
||
if(mb_strlen($subItem) > 64) {
|
||
return $this->fail('第'. ($key+1) .'个SKU规格值超过了64个字符');
|
||
}
|
||
}
|
||
}
|
||
|
||
unset($spec_lists['goods_image']);
|
||
unset($spec_lists['spec_name']);
|
||
unset($spec_lists['spec_values']);
|
||
unset($spec_lists['spec_id']);
|
||
unset($spec_lists['spec_value_ids']);
|
||
unset($spec_lists['spec_image']);
|
||
$spec_lists = form_to_linear($spec_lists);
|
||
|
||
//规格验证
|
||
if (empty($spec_lists)) {
|
||
return $this->fail('至少添加一个规格');
|
||
}
|
||
$result = $this->validate($post, 'app\adminapi\validate\GoodsMoreSpecValidate');
|
||
if ($result !== true) {
|
||
return $this->fail($result);
|
||
}
|
||
|
||
//规格商品列表验证
|
||
foreach ($spec_lists as $v) {
|
||
$result = $this->validate($v, 'app\adminapi\validate\GoodsMoreSpecListsValidate');
|
||
if ($result !== true) {
|
||
return $this->fail($result);
|
||
}
|
||
}
|
||
// 校验规格
|
||
$total_stock = array_sum(array_column($spec_lists, 'stock'));
|
||
if ($total_stock <= 0) {
|
||
return $this->fail('至少有一个规格的库存大于0');
|
||
}
|
||
}
|
||
|
||
//添加商品
|
||
$result = GoodsLogic::add($post, $spec_lists);
|
||
|
||
if ($result !== true) {
|
||
return $this->fail('添加失败:' . $result);
|
||
}
|
||
return $this->success('添加成功');
|
||
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 编辑
|
||
* @return \think\response\Json
|
||
* @author likeadmin
|
||
* @date 2025/05/08 15:34
|
||
*/
|
||
public function edit()
|
||
{
|
||
|
||
$post = $this->request->post();
|
||
|
||
$post['del'] = 0;
|
||
|
||
|
||
//主表验证
|
||
$result = $this->validate($post, 'app\adminapi\validate\GoodsValidate');
|
||
|
||
if ($result !== true) {
|
||
return $this->fail($result);
|
||
}
|
||
|
||
//单规格验证
|
||
if ($post['spec_type'] == 1) {
|
||
$result = $this->validate($post, 'app\adminapi\validate\GoodsOneSpecValidate');
|
||
if ($result !== true) {
|
||
return $this->fail($result);
|
||
}
|
||
}
|
||
|
||
//多规格验证
|
||
$spec_lists = [];
|
||
if ($post['spec_type'] == 2) {
|
||
$spec_lists = $post;
|
||
unset($spec_lists['goods_image']);
|
||
unset($spec_lists['spec_name']);
|
||
unset($spec_lists['spec_values']);
|
||
unset($spec_lists['spec_id']);
|
||
unset($spec_lists['spec_value_ids']);
|
||
$spec_lists = form_to_linear($spec_lists);
|
||
|
||
//规格验证
|
||
if (empty($spec_lists)) {
|
||
return $this->fail('至少添加一个规格');
|
||
}
|
||
$result = $this->validate($post, 'app\adminapi\validate\GoodsMoreSpecValidate');
|
||
if ($result !== true) {
|
||
return $this->fail($result);
|
||
}
|
||
|
||
//规格商品列表验证
|
||
foreach ($spec_lists as $v) {
|
||
$result = $this->validate($v, 'app\adminapi\validate\GoodsMoreSpecListsValidate');
|
||
if ($result !== true) {
|
||
return $this->fail($result);
|
||
}
|
||
}
|
||
// 校验规格
|
||
$total_stock = array_sum(array_column($spec_lists, 'stock'));
|
||
if ($total_stock <= 0) {
|
||
$this->fail('至少有一个规格的库存大于0');
|
||
}
|
||
}
|
||
|
||
if ($post['status'] == 0) {
|
||
$status = Db::name('goods')
|
||
->where(['id' => $post['id']])
|
||
->value('status');
|
||
if ($status == 1) {
|
||
$res = Db::name('team_activity')
|
||
->where(['status' => 1, 'goods_id'=> $post['id']])
|
||
->find();
|
||
if ($res) {
|
||
return $this->fail('该商品正在参与拼团,请先关闭后才允许下架');
|
||
}
|
||
}
|
||
}
|
||
|
||
//添加商品
|
||
$result = GoodsLogic::edit($post, $spec_lists);
|
||
if ($result !== true) {
|
||
return $this->fail('添加失败:' . $result);
|
||
}
|
||
return $this->success('修改成功');
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 删除
|
||
* @return \think\response\Json
|
||
* @author likeadmin
|
||
* @date 2025/05/08 15:34
|
||
*/
|
||
public function delete()
|
||
{
|
||
$params = (new GoodsValidate())->post()->goCheck('delete');
|
||
GoodsLogic::delete($params);
|
||
return $this->success('删除成功', [], 1, 1);
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 获取详情
|
||
* @return \think\response\Json
|
||
* @author likeadmin
|
||
* @date 2025/05/08 15:34
|
||
*/
|
||
public function detail()
|
||
{
|
||
$params = (new GoodsValidate())->goCheck('detail');
|
||
// $result = GoodsLogic::detail($params);
|
||
$result = GoodsLogic::info($params['id']);
|
||
return $this->data($result);
|
||
}
|
||
|
||
|
||
} |