FavoriteController.php
2.9 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
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------
namespace app\user\controller;
use cmf\controller\UserBaseController;
use app\user\model\UserFavoriteModel;
use think\Db;
class FavoriteController extends UserBaseController
{
/**
* 个人中心我的收藏列表
*/
public function index()
{
$userFavoriteModel = new UserFavoriteModel();
$data = $userFavoriteModel->favorites();
$user = cmf_get_current_user();
$this->assign($user);
$this->assign("page", $data['page']);
$this->assign("lists", $data['lists']);
return $this->fetch();
}
/**
* 用户取消收藏
*/
public function delete()
{
$id = $this->request->param("id", 0, "intval");
$userFavoriteModel = new UserFavoriteModel();
$data = $userFavoriteModel->deleteFavorite($id);
if ($data) {
$this->success("取消收藏成功!");
} else {
$this->error("取消收藏失败!");
}
}
/**
* 用户收藏
*/
public function add()
{
$data = $this->request->param();
$result = $this->validate($data, 'Favorite');
if ($result !== true) {
$this->error($result);
}
$id = $this->request->param('id', 0, 'intval');
$table = $this->request->param('table');
$findFavoriteCount = Db::name("user_favorite")->where([
'object_id' => $id,
'table_name' => $table,
'user_id' => cmf_get_current_user_id()
])->count();
if ($findFavoriteCount > 0) {
$this->error("您已收藏过啦");
}
$title = base64_decode($this->request->param('title'));
$url = $this->request->param('url');
$url = base64_decode($url);
$description = $this->request->param('description', '', 'base64_decode');
$description = empty($description) ? $title : $description;
Db::name("user_favorite")->insert([
'user_id' => cmf_get_current_user_id(),
'title' => $title,
'description' => $description,
'url' => $url,
'object_id' => $id,
'table_name' => $table,
'create_time' => time()
]);
$this->success('收藏成功');
}
}