Collect.php 1.1 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/11
 * Time: 15:20
 */

namespace app\index\controller;


use app\common\controller\Frontend;
use app\index\model\Goodscollect;

class Collect extends Frontend
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    public function index(){
        $user_id = $this->auth->id;
        $goods_id = $this->request->param('goods_id',0,'intval');
        if(empty($goods_id)){
            $this->error('缺少必要参数');
        }
        $where['user_id'] = ['eq',$user_id];
        $where['goods_id'] = ['eq',$goods_id];
        $collectModel = new Goodscollect();
        $data = $collectModel->findData($where);
        $arr['user_id'] = $user_id;
        $arr['goods_id'] = $goods_id;
        $arr['createtime'] = time();
        if(empty($data)){
            $result = $collectModel->insertData($arr);
        }else{
            $result = $collectModel->deleteData($where);
        }
        if(empty($result)){
            $this->error('sql执行失败');
        }
        $this->success('SUCCESS',$result);
    }
}