FavoritesController.php
3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------
namespace api\user\controller;
use api\user\model\UserFavoriteModel;
use cmf\controller\RestUserBaseController;
class FavoritesController extends RestUserBaseController
{
protected $userFavoriteModel;
public function __construct(UserFavoriteModel $userFavoriteModel)
{
parent::__construct();
$this->userFavoriteModel = $userFavoriteModel;
}
/**
* 显示收藏列表
*/
public function getFavorites()
{
$userId = $this->getUserId();
$param = $this->request->param();
$param['where'] = [
'user_id' => $userId
];
$param['order'] = '-create_time';
$favoriteData = $this->userFavoriteModel->getDatas($param);
$this->success('请求成功', $favoriteData);
}
/**
* [setFavorites 添加收藏]
* @Author: wuwu<15093565100@163.com>
* @DateTime: 2017-08-03T09:03:40+0800
* @since: 1.0
*/
public function setFavorites()
{
$input = $this->request->param();
//组装数据
$data = $this->_FavoritesObject($input['title'], $input['url'], $input['description'], $input['table_name'], $input['object_id']);
if (!$data) {
$this->error('收藏失败');
}
if ($this->userFavoriteModel->where(['user_id' => $this->getUserId(), 'object_id' => $input['object_id']])->where('table_name', $input['table_name'])->count() > 0) {
$this->error('已收藏');
}
if ($this->userFavoriteModel->setFavorite($data)) {
$this->success('收藏成功');
} else {
$this->error('收藏失败');
}
}
/**
* [_FavoritesObject 收藏数据组装]
* @Author: wuwu<15093565100@163.com>
* @DateTime: 2017-08-03T09:39:06+0800
* @since: 1.0
* @return [type] [description]
*/
protected function _FavoritesObject($title, $url, $description, $table_name, $object_id)
{
$data['user_id'] = $this->getUserId();
$data['create_time'] = THINK_START_TIME;
if (empty($title)) {
return false;
} else if (empty($url)) {
return false;
} elseif (empty($table_name)) {
return false;
} elseif (empty($object_id)) {
return false;
}
$data['title'] = $title;
$data['url'] = htmlspecialchars_decode($url);
$data['description'] = $description;
$data['table_name'] = $table_name;
$data['object_id'] = $object_id;
return $data;
}
/**
* [unsetFavorites 取消收藏]
* @Author: wuwu<15093565100@163.com>
* @DateTime: 2017-08-03T09:04:31+0800
* @since: 1.0
* @return [type] [description]
*/
public function unsetFavorites()
{
$id = $this->request->param('id', 0, 'intval');
$userId = $this->getUserId();
$count = $this->userFavoriteModel->where(['id' => $id, 'user_id' => $userId])->count();
if ($count == 0) {
$this->error('收藏不存在,无法取消');
}
$this->userFavoriteModel->where(['id' => $id])->delete();
$this->success('取消成功');
}
}