<?php
|
|
namespace app\api\controller\supplier;
|
|
use app\api\controller\Controller;
|
use app\api\model\product\Product as ProductModel;
|
use app\supplier\service\ProductService;
|
use app\api\model\supplier\Supplier as SupplierModel;
|
|
/**
|
* 供应商产品
|
*/
|
class Product extends Controller
|
{
|
// user
|
private $user;
|
private $supplierUser;
|
|
/**
|
* 构造方法
|
*/
|
public function initialize()
|
{
|
parent::initialize();
|
$this->user = $this->getUser(); // 用户信息
|
$this->supplierUser = $this->getSupplierUser($this->user);
|
}
|
/**
|
* 供应商中心主页
|
*/
|
public function index()
|
{
|
$data = $this->postData();
|
// 获取商品列表数据
|
$model = new ProductModel;
|
$data['shop_supplier_id'] = $this->supplierUser['shop_supplier_id'];
|
$productList = $model->getList($data, $this->user);
|
return $this->renderSuccess('', compact('productList'));
|
}
|
|
//商品上下架
|
public function modify(){
|
$data = $this->postData();
|
// 获取商品数据
|
$model = ProductModel::detail($data['product_id']);
|
if($this->supplierUser['shop_supplier_id'] != $model['shop_supplier_id']){
|
return $this->renderError('非法请求');
|
}
|
if(!$model->editStatus($data)){
|
return $this->renderError('操作失败');
|
}
|
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'];
|
if ($this->supplierUser['status']==20){
|
return $this->renderError('未缴纳保证金,不能添加商品');
|
}
|
$model = new ProductModel;
|
$supplier = SupplierModel::detail($this->supplierUser['shop_supplier_id']);
|
$params['is_newcomer'] = $supplier['is_newcomer'];
|
$params['is_repurchase'] = $supplier['is_repurchase'];
|
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('非法请求');
|
}
|
$supplier = SupplierModel::detail($this->supplierUser['shop_supplier_id']);
|
$params['is_newcomer'] = $supplier['is_newcomer'];
|
$params['is_repurchase'] = $supplier['is_repurchase'];
|
if ($model->edit($params)) {
|
return $this->renderSuccess('编辑成功');
|
}
|
return $this->renderError($model->getError() ?: '编辑失败');
|
}
|
|
}
|