Collect.php 1.6 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/24
 * Time: 8:44
 */

namespace app\home\controller;


use app\common\controller\WechatBase;
use think\Db;

class Collect extends WechatBase
{
    protected $user_id;
    function _initialize()
    {
        parent::_initialize();
        //判断是否授权
        $user_id = get_current_user_id();
        if(empty($user_id)){
            $this->redirect('user/authorization_view');
        }
        $this->user_id = $user_id;
    }
    public function index(){
        $type = $this->request->param('type',0,'intval');
        $goods_id = $this->request->param('goods_id',0,'intval');
        if(empty($type) || empty($goods_id)){
            $this->error('404');
        }
        if($type == 1){
            //收藏
            if(empty($collect)){
                $arr['user_id'] = $this->user_id;
                $arr['goods_id'] = $goods_id;
                $arr['createtime'] = time();
                Db::name('collect')->insert($arr);
            }
        }else if($type == 2){
            //取消收藏
            Db::name('collect')->where(['user_id'=>$this->user_id,'goods_id'=>$goods_id])->delete();
        }
        $this->success('SUCCESS');
    }
    public function get_all(){
        $data = Db::name('collect')
            ->alias('c')
            ->field('g.*,c.id as c_id')
            ->join('fa_goods g','g.id = c.goods_id')
            ->where(['c.user_id'=>$this->user_id])->order('c.id desc')->page(1,5)->select();
        $this->assign('data',$data);
        $this->assign('title','我的收藏');
        return $this->fetch();
    }
}