You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
7.6 KiB

<?php
namespace app\fire\controller\api;
use app\chat\model\ChatFriendsRoomMessage;
use app\fire\model\FireProduct;
use app\fire\model\FireProductCategory;
use app\fire\model\FireSupply;
class Supply extends Base
{
/**
* 1-供应商中心
* @return \think\response\Json
*/
public function supplyCenter()
{
$supply_model = new FireSupply();
$message_model = new ChatFriendsRoomMessage();
$supply = $supply_model->with(['userHead'])->where(['user_id'=>USER_ID,'status'=>2])->field('id,user_id,company_name,is_publish')->find();
if(!$supply)
{
return sendErrorMessage(1,'您还不是供应商');
}
unset($supply['userHead']);
$no_read = $message_model->where(['to_user_id'=>USER_ID,'is_read'=>0])->count();
$is_red = $no_read?2:1;
return sendMessage(['supply'=>$supply,'is_red'=>$is_red]);
}
/**
* 2-供应商中心-商品列表
* @return \think\response\Json
*/
public function supplyProductList()
{
$param = input('post.');
$validate = [
'index' => 'require',
'page' => 'require',
];
$this->validate($param, $validate);
$supply_model = new FireSupply();
$product_model = new FireProduct();
$supply = $supply_model->getOneData(['user_id'=>USER_ID,'status'=>2]);
$where = [['uid','=',UID],['supply_id','=',$supply['id']]];
switch ($param['index'])
{
case "1":
$where[] = ['status','=',4];
break;
case "2":
$where[] = ['status','=',3];
break;
case "3":
$where[] = ['status','=',1];
break;
case "4":
$where[] = ['status','=',2];
break;
case "5":
$where[] = ['status','=',5];
break;
}
if($param['keyword'])
{
$where[] = ['name','like','%'.$param['keyword'].'%'];
}
$field = 'id,name,cover_img,status,create_time';
$list = $product_model
->field($field)
->order('id desc')
->where($where)
->paginate(['list_rows' => 10, 'page' => $param['page']], false)
->each(function ($item) {
$item['show_create_time'] = date('Y.m.d H:i',strtotime($item['create_time']));
$item['status_text'] = $item['status_text'];
return $item;
});
return sendMessage(['list' => $list]);
}
/**
* 2-供应商中心-删除商品
* @return \think\response\Json
*/
public function supplyDeleteProduct()
{
$param = input('post.');
$validate = [
'product_id' => 'require',
];
$this->validate($param, $validate);
$product_model = new FireProduct();
$res = $product_model->where('id', $param['product_id'])->useSoftDelete('delete_time', time())->delete();
if($res === false)
{
return sendErrorMessage(1,'删除失败');
}
return sendSuccessMessage([],'删除成功');
}
/**
* 2-供应商中心-上架商品
* @return \think\response\Json
*/
public function supplyPutProduct()
{
$param = input('post.');
$validate = [
'product_id' => 'require',
];
$this->validate($param, $validate);
$product_model = new FireProduct();
$product = $product_model->getOneData(['id'=>$param['product_id']]);
if($product['status'] != 3)
{
return sendErrorMessage(1,'商品状态错误');
}
$res = $product_model->where(['id'=> $param['product_id']])->update(['status'=>4]);
if($res === false)
{
return sendErrorMessage(1,'上架失败');
}
return sendSuccessMessage([],'上架成功');
}
/**
* 2-供应商中心-下架商品
* @return \think\response\Json
*/
public function supplyOutProduct()
{
$param = input('post.');
$validate = [
'product_id' => 'require',
];
$this->validate($param, $validate);
$product_model = new FireProduct();
$product = $product_model->getOneData(['id'=>$param['product_id']]);
if($product['status'] != 4)
{
return sendErrorMessage(1,'商品状态错误');
}
$res = $product_model->where(['id'=> $param['product_id']])->update(['status'=>3]);
if($res === false)
{
return sendErrorMessage(1,'下架失败');
}
return sendSuccessMessage([],'下架成功');
}
/**
* 3-供应商中心-添加/修改商品数据
* @return \think\response\Json
*/
public function getProductData()
{
$param = input('post.');
$validate = [
'product_id' => 'require',
];
$this->validate($param, $validate);
$product_model = new FireProduct();
$category_model = new FireProductCategory();
$category = $category_model->getAllData(['uid'=>UID],'id,name','sort desc');
$field = 'id,cover_img,img_path,video_url,video_cover_img,name,category_id,detail,detail_img,report,report_img,price,price_img,status,examine_idea';
$product_data = $product_model->getOneData(['id'=>$param['product_id']],$field);
return sendMessage(['category'=>$category,'product_data'=>$product_data]);
}
/**
* 3-供应商中心-添加/修改商品暂存/提交
* @return \think\response\Json
*/
public function supplyAddProduct()
{
$param = input('post.');
$validate = [
'type' => 'require',
'id' => 'require',
'name' => 'require',
];
$check_data = [
'name.require'=>'请输入商品名称',
];
if($param['type'] == 2)
{
$validate['cover_img'] = 'require';
$validate['category_id'] = 'require';
$check_data['cover_img.require'] = '请上传封面图';
$check_data['category_id.require'] = '请选择商品分类';
}
$this->validate($param, $validate,$check_data);
$supply_model = new FireSupply();
$product_model = new FireProduct();
$supply = $supply_model->getOneData(['user_id'=>USER_ID,'status'=>2]);
$product_data = [
'uid' => UID,
'supply_id' => $supply['id'],
'cover_img' => $param['cover_img'],
'img_path' => $param['img_path']?explode(',',$param['img_path']):'',
'video_url' => $param['video_url'],
'video_cover_img' => $param['video_url']?get_oss_video_screenshot($param['video_url'])['url']:'',
'name' => $param['name'],
'category_id' => $param['category_id'],
'detail' => $param['detail'],
'detail_img' => $param['detail_img']?explode(',',$param['detail_img']):'',
'report' => $param['report'],
'report_img' => $param['report_img']?explode(',',$param['report_img']):'',
'price' => $param['price'],
'price_img' => $param['price_img']?explode(',',$param['price_img']):'',
'status' => $param['type']
];
if($param['id'])
{
$product_data['id'] = $param['id'];
}
$res = $product_model->dataUpdate($product_data);
if(!$res)
{
return sendErrorMessage(1,'保存失败');
}
return sendSuccessMessage([],'保存成功');
}
}