TaoLer/app/index/controller/Collection.php

64 lines
1.7 KiB
PHP
Raw Normal View History

2020-01-01 13:17:19 +08:00
<?php
namespace app\index\controller;
use app\common\controller\BaseController;
use think\facade\Session;
use app\common\model\Collection as CollectionModel;
use app\common\model\Article;
use think\facade\Request;
use think\facade\Db;
class Collection extends BaseController
{
// protected $type = [
// 'cid' => 'integer',
// ];
//文章收藏
public function add(){
//$data = Request::param();
$data['article_id'] = intval(input('cid'));
2021-05-25 18:17:45 +08:00
$data['user_id'] = $this->uid;
$arts = Article::with(['user'])->field('id,title,user_id')->find($data['article_id']);
$data['collect_title'] = $arts['title'];
$data['auther'] = $arts->user->name;
2020-01-01 13:17:19 +08:00
$result = CollectionModel::create($data);
if($result){
$res['status'] = 0;
//$res=['type' => 'add','type' => 'remove', 'msg' => '收藏成功'];
}
return $res;
}
//取消收藏
public function remove(){
$cid = input('cid');
$aid = intval($cid);
//$result = CollectionModel::where('cid',$arid)->select();
2021-05-25 18:17:45 +08:00
$result = Db::name('collection')->where(['article_id' => $aid,'user_id' => $this->uid])->delete();
2020-01-01 13:17:19 +08:00
if($result){
$res['status'] = 0;
//$res=['type' => 'add','type' => 'remove', 'msg' => '收藏成功'];
}
return $res;
}
//收藏查询
public function find(){
//$cid = Request::param();
$cid = input('cid');
$aid = intval($cid);
2021-05-25 18:17:45 +08:00
$collectData = Db::name('collection')->where(['article_id' => $aid,'user_id' => $this->uid])->find();
2020-01-01 13:17:19 +08:00
if($collectData){
$res['status'] = 0;
$res['data']['collection'] = $collectData['article_id'];
} else {
$res['status'] = 0;
$res['data'] = '';
}
return json($res);
}
}