商品收藏夹功能

其实商品收藏夹也就是对商品收藏夹表的增删改查,用户一进来商品收藏夹页面,先把收藏夹列表查询出来,点击收藏夹里面的商品,讲商品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];
        }
    }
}
  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
商品收藏功能测试用例可以包括以下几个方面: 1. 界面测试:检查商品收藏按钮是否显示在合适的位置,并且按钮的功能是否正常。 2. 功能测试:确保用户可以成功将商品添加到收藏夹中,并且可以从收藏夹中查看已收藏的商品。此外,还需要验证用户是否可以取消对商品的收藏。 3. 兼容性测试:针对不同浏览器和不同设备平台进行测试,包括PC端、iPad和手机端以及iOS和安卓手机的不同型号和品牌。验证在各个平台和设备上收藏功能是否正常工作。 4. 容错性测试:测试在商品详情页面中,如果商品已经下架或库存不足,是否能够正确提示用户不能收藏该商品。 5. 安全性测试:检查商品收藏功能是否存在安全漏洞,例如是否可以通过恶意操作或未授权访问来访问或修改他人的收藏夹。 综上所述,商品收藏功能测试用例应包括界面测试、功能测试、兼容性测试、容错性测试和安全性测试等方面的考虑。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [购物车测试用例](https://blog.csdn.net/qq_54850622/article/details/118553824)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值