/
ActionController.php
142 lines (127 loc) · 3.8 KB
/
ActionController.php
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
* author : forecho <caizhenghai@gmail.com>
* createTime : 15/4/19 下午3:11
* description:
*/
namespace frontend\modules\user\controllers;
use common\services\CommentService;
use common\services\TopicService;
use common\services\TweetService;
use Yii;
use common\components\Controller;
use yii\web\NotFoundHttpException;
class ActionController extends Controller
{
public function beforeAction($action)
{
if (Yii::$app->user->isGuest) {
Yii::$app->getResponse()->redirect(\Yii::$app->getUser()->loginUrl)->send();
}
return parent::beforeAction($action);
}
/**
* 赞话题和回复
* @param $type
* @param $id
* @return array|string
* @throws NotFoundHttpException
*/
public function actionLike($type, $id)
{
switch ($type) {
case 'topic':
$topicService = new TopicService();
list($result, $data) = $topicService->userDoAction($id, 'like');
break;
case 'tweet':
$tweetService = new TweetService();
list($result, $data) = $tweetService->userDoAction($id, 'like');
break;
case 'comment':
$commentService = new CommentService();
list($result, $data) = $commentService->userDoAction($id, 'like');
break;
default:
throw new NotFoundHttpException();
break;
}
if ($result) {
return $this->message('提交成功!', 'success');
} else {
return $this->message($data ? $data->getErrors() : '提交失败!');
}
}
/**
* 喝倒彩话题
* @param $type
* @param $id
* @return array|string
*/
public function actionHate($type, $id)
{
if ($type == 'topic') {
$topicService = new TopicService();
list($result, $data) = $topicService->userDoAction($id, 'hate');
if ($result) {
return $this->message('提交成功!', 'success');
} else {
return $this->message($data ? $data->getErrors() : '提交失败!');
}
}
}
/**
* 关注话题
* @param $type
* @param $id
* @return array|string
*/
public function actionFollow($type, $id)
{
if ($type == 'topic') {
$topicService = new TopicService();
list($result, $data) = $topicService->userDoAction($id, 'follow');
if ($result) {
return $this->message('提交成功!', 'success');
} else {
return $this->message($data ? $data->getErrors() : '提交失败!');
}
}
}
/**
* 感谢话题
* @param $type
* @param $id
* @return array|string
*/
public function actionThanks($type, $id)
{
if ($type == 'topic') {
$topicService = new TopicService();
list($result, $data) = $topicService->userDoAction($id, 'thanks');
if ($result) {
return $this->message('提交成功!', 'success');
} else {
return $this->message($data ? $data->getErrors() : '提交失败!');
}
}
}
/**
* 收藏话题
* @param $type
* @param $id
* @return array|string
*/
public function actionFavorite($type, $id)
{
if ($type == 'topic') {
$topicService = new TopicService();
list($result, $data) = $topicService->userDoAction($id, 'favorite');
if ($result) {
return $this->message('提交成功!', 'success');
} else {
return $this->message($data ? $data->getErrors() : '提交失败!');
}
}
}
}