quanwei
2025-12-04 12913c1069347ea4b1f6ab87f480da0f8d8c646a
admin/app/api/controller/supplier/Product.php
@@ -4,6 +4,7 @@
use app\api\controller\Controller;
use app\api\model\product\Product as ProductModel;
use app\supplier\service\ProductService;
/**
 * 供应商产品
@@ -49,5 +50,69 @@
        }
        return $this->renderSuccess('操作成功');
    }
    /**
     * 获取添加商品所需的基础数据
     */
    public function getBaseData()
    {
        $data = ProductService::getEditData(null, 'add', $this->supplierUser['shop_supplier_id']);
        return $this->renderSuccess('', $data);
    }
    /**
     * 添加商品
     */
    public function add()
    {
        $data = $this->postData();
        $params = json_decode($data['params'], true);
        $params['shop_supplier_id'] = $this->supplierUser['shop_supplier_id'];
        $model = new ProductModel;
        if ($model->add($params)) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }
    /**
     * 获取编辑商品所需的数据
     */
    public function getEditData()
    {
        $data = $this->postData();
        $product_id = $data['product_id'];
        // 获取商品数据
        $model = ProductModel::detail($product_id);
        if($this->supplierUser['shop_supplier_id'] != $model['shop_supplier_id']){
            return $this->renderError('非法请求');
        }
        $result = ProductService::getEditData($model, 'edit', $this->supplierUser['shop_supplier_id']);
        $result['model'] = $model;
        return $this->renderSuccess('', $result);
    }
    /**
     * 编辑商品
     */
    public function edit()
    {
        $data = $this->postData();
        $product_id = $data['product_id'];
        $params = json_decode($data['params'], true);
        // 获取商品数据
        $model = ProductModel::detail($product_id);
        if($this->supplierUser['shop_supplier_id'] != $model['shop_supplier_id']){
            return $this->renderError('非法请求');
        }
        if ($model->edit($params)) {
            return $this->renderSuccess('编辑成功');
        }
        return $this->renderError($model->getError() ?: '编辑失败');
    }
   
}