其实商品收藏夹也就是对商品收藏夹表的增删改查,用户一进来商品收藏夹页面,先把收藏夹列表查询出来,点击收藏夹里面的商品,讲商品ID传进来,把收藏夹商品的详情查询出来。用户点击一下星号,将用户ID以及商品ID传进来,处理数据,将数据写入数据库里面。用户再点击一下星号,即取消商品收藏,将用户ID以及商品ID和收藏状态传进来,将收藏夹商品进行软删除。
此处为控制器
<?php
namespace app\mobile\controller;
use app\common\service\GoodsFavoriteService;
use think\Request;
class GoodsFavorite extends Base
{
protected $GoodsFavoriteService;
public function __construct(Request $request = null){
parent::__construct($request);
$this->GoodsFavoriteService = new GoodsFavoriteService();
}
/**
* 添加数据
* @throws \app\common\exception\CommonException
* @throws \think\Exception
* @throws \think\exception\DbException
*/
public function create(){
$data =input();
$data['customer_id'] = $this->loginSuccessInfo['customer_id'];
$res = $this->GoodsFavoriteService->create($data);
$this->returnJson($res);
}
/**
* 删除数据
* @throws \app\common\exception\CommonException
* @throws \think\exception\DbException
*/
public function delete(){
$data =input();
$data['customer_id'] = $this->loginSuccessInfo['customer_id'];
$res = $this->GoodsFavoriteService->delete($data);
$this->returnJson($res);
}
/**
* 编辑数据
* @throws \app\common\exception\CommonException
* @throws \think\exception\DbException
*/
public function edit(){
$res = $this->GoodsFavoriteService->edit();
$this->returnJson($res);
}
/**
* 获取列表
* @throws \app\common\exception\CommonException
*/
public function finds(){
$data =input();
$data['customer_id'] = $this->loginSuccessInfo['customer_id'];
$res = $this->GoodsFavoriteService->finds($data);
$this->returnJson($res);
}
/**
* 获取详情
* @throws \app\common\exception\CommonException
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function detail(){
$res = $this->GoodsFavoriteService->detail();
$this->returnJson($res);
}
/**
* 修改状态
* @throws \app\common\exception\CommonException
* @throws \think\exception\DbException
*/
public function status(){
$res = $this->GoodsFavoriteService->status();
$this->returnJson($res);
}
/**
* 取消收藏
* @throws \app\common\exception\CommonException
* @throws \think\Exception
* @throws \think\exception\DbException
*/
public function favorite(){
$data =input();
$data['customer_id'] = $this->loginSuccessInfo['customer_id'];
$res = $this->GoodsFavoriteService->favorite($data);
$this->returnJson($res);
}
/**
* 收藏检测
* @throws \app\common\exception\CommonException
* @throws \think\Exception
* @throws \think\exception\DbException
*/
public function checkFavorite(){
$data =input();
$data['customer_id'] = $this->loginSuccessInfo['customer_id'];
$res = $this->GoodsFavoriteService->checkFavorite($data);
$this->returnJson($res);
}
}
此处为服务层 方便控制器调用
<?php
namespace app\common\service;
use app\common\model\BaseModel;
use app\common\model\GoodsFavoriteModel;
use app\common\validate\GoodsFavoriteValidate;
use app\common\exception\CommonException;
use think\Db;
use think\Log;
class GoodsFavoriteService extends BaseService
{
private $GoodsFavoriteModel;
private $GoodsFavoriteValidate;
public function __construct(){
parent::__construct();
$this->GoodsFavoriteModel = new GoodsFavoriteModel();
$this->GoodsFavoriteValidate = new GoodsFavoriteValidate();
}
/**
* 添加数据
* @param array $data
* @return array
* @throws CommonException
* @throws \think\Exception
* @throws \think\exception\DbException
*/
public function create($data = []){
$data = self::validate($data,$this->GoodsFavoriteValidate,'create');
$data['create_time'] = date("Y-m-d H:i:s");
$data['is_favorite'] = 2;//已收藏
$res = $this->GoodsFavoriteModel->allowField(true)->save($data);
if($res === false) throw new CommonException('数据创建失败');
return ['goods_favorite_id'=>$this->GoodsFavoriteModel->getLastInsID()];
}
/**
* 获取列表
* @param array $data
* @param bool $is_all
* @return mixed
* @throws CommonException
*/
public function finds($data = [],$is_all = false){
$data = self::validate($data,$this->GoodsFavoriteValidate,'finds');
$this->GoodsFavoriteModel->alias('f');
/*根据库存*/
if (isset($data['goods_num'])){
$this->GoodsFavoriteModel->join('goods g','g.goods_id=f.goods_id','LEFT');
$this->GoodsFavoriteModel->where(['g.goods_num'=>['>',0]]);
unset($data['goods_num']);
}
BaseModel::addPrefix($data,'f');
$this->GoodsFavoriteModel->where($data);
$this->GoodsFavoriteModel->where(['f.is_delete'=>1]);
$this->GoodsFavoriteModel->with('goods,goodsImages');
$this->GoodsFavoriteModel->order(['f.goods_favorite_id'=>'desc']);
$list = BaseModel::pagination($this->GoodsFavoriteModel,$is_all)->toArray();
$list['favorite_num'] = count($list['data']);
if($is_all){
$_list['total'] = $list['total'];
$_list['data'] = $list['data'];
$list = $_list;
}
return $list;
}
/**
* 获取详情
* @param array $data
* @return array
* @throws CommonException
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function detail($data = []){
$data = self::validate($data,$this->GoodsFavoriteValidate,'detail');
$this->GoodsFavoriteModel->where($data);
$this->GoodsFavoriteModel->where(['is_delete'=>1]);
$res = $this->GoodsFavoriteModel->find();
if(!$res) return [];
return $res->toArray();
}
/**
* 删除数据
* @param array $data
* @param bool $is_fake
* @return array
* @throws CommonException
* @throws \think\exception\DbException
*/
public function delete($data=[],$is_fake = false){
$data = self::validate($data,$this->GoodsFavoriteValidate,'delete');
$GoodsFavoriteData = GoodsFavoriteModel::get(['goods_id'=>$data['goods_id'],'is_delete'=>1,'customer_id'=>$data['customer_id']]);
/*查询当前数据*/
$result = GoodsFavoriteModel::get(['goods_favorite_id'=>$GoodsFavoriteData['goods_favorite_id'],'is_delete'=>1]);
if(!$result) throw new CommonException('数据不存在');
if($is_fake){
$res = GoodsFavoriteModel::update(['goods_favorite_id'=>$GoodsFavoriteData['goods_favorite_id'],'is_delete'=>2]);
}else{
$res = GoodsFavoriteModel::destroy(['goods_favorite_id'=>$GoodsFavoriteData['goods_favorite_id']]);
}
if($res === false) throw new CommonException('删除失败');
return ['goods_favorite_id'=>$GoodsFavoriteData['goods_favorite_id']];
}
/**
* 编辑数据
* @param array $data
* @return array
* @throws CommonException
* @throws \think\exception\DbException
*/
public function edit($data =[]){
$data = self::validate($data,$this->GoodsFavoriteValidate,'edit');
/*查询当前数据*/
$result = GoodsFavoriteModel::get(['goods_favorite_id'=>$data['goods_favorite_id'],'is_delete'=>1]);
if(!$result) throw new CommonException('数据不存在');
$res = GoodsFavoriteModel::update($data);
if($res === false) throw new CommonException('修改失败');
return ['goods_favorite_id'=>$data['goods_favorite_id']];
}
/**
* 修改状态
* @param array $data
* @return array
* @throws CommonException
* @throws \think\exception\DbException
*/
public function status($data = []){
$data = self::validate($data,$this->GoodsFavoriteValidate,'status');
/*查询当前数据*/
$result = GoodsFavoriteModel::get(['goods_favorite_id'=>$data['goods_favorite_id'],'is_delete'=>1]);
if(!$result) throw new CommonException('数据不存在');
/*更新数据*/
$result->status = $data['status'];
$res = $result->save();
if($res === false) throw new CommonException('状态修改失败');
return ['status'=>$data['status']];
}
/**
* 修改收藏状态
* @param array $data
* @return array
* @throws CommonException
* @throws \think\exception\DbException
*/
public function favorite($data = []){
$data = self::validate($data,$this->GoodsFavoriteValidate,'favorite');
/*创建*/
if ($data['is_favorite']==1){
$data['create_time'] = date("Y-m-d H:i:s");
$data['is_favorite'] = 1;//已收藏
$res = $this->GoodsFavoriteModel->allowField(true)->save($data);
return ['is_favorite'=>$data['is_favorite']];
}else{
/*删除*/
$GoodsFavoriteData = GoodsFavoriteModel::get(['goods_id'=>$data['goods_id'],'is_delete'=>1,'customer_id'=>$data['customer_id']]);
/*查询当前数据*/
$result = GoodsFavoriteModel::get(['goods_favorite_id'=>$GoodsFavoriteData['goods_favorite_id'],'is_delete'=>1]);
if(!$result) throw new CommonException('数据不存在');
GoodsFavoriteModel::destroy(['goods_favorite_id'=>$GoodsFavoriteData['goods_favorite_id']]);
return ['is_favorite'=>$data['is_favorite']];
}
}
/**
* 检查收藏商品是否收藏
* @param array $data
* @return array
* @throws CommonException
* @throws \think\exception\DbException
*/
public function checkFavorite($data = []){
$data = self::validate($data,$this->GoodsFavoriteValidate,'checkFavorite');
/*查询当前数据*/
$result = GoodsFavoriteModel::get(['goods_id'=>$data['goods_id'],'is_delete'=>1,'customer_id'=>$data['customer_id']]);
if($result){
return ['is_favorite'=>1];
}else{
return ['is_favorite'=>2];
}
}
}