ProjectController.php
18.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<?php
namespace App\Http\Container\AppSection\Controllers;
use App\Helpers\JsonResource;
use App\Http\Container\AppSection\Requests\ProjectEdtiorRequest;
use App\Http\Container\AppSection\Requests\ProjectHandleInviteRequest;
use App\Http\Container\AppSection\Requests\ProjectHandleOutRequest;
use App\Http\Container\AppSection\Requests\ProjectInviteRequest;
use App\Http\Container\AppSection\Requests\ProjectMemberOutRequest;
use App\Http\Service\ImSendService;
use App\Http\Service\ImSign;
use App\Http\Service\UserService;
use App\Models\Activity;
use App\Models\GroupHasMember;
use App\Models\Pivots\ProjectMemberPivot;
use App\Models\Pivots\UserProjectPivot;
use App\Models\Project;
use App\Models\ProjectVisitor;
use App\Models\SystemConfig;
use App\Models\User;
use App\Models\UserFollowRelation;
use Carbon\Carbon;
use Hikoon\LaravelApi\Support\ApiController;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
class ProjectController extends ApiController
{
/**
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\JsonResponse
*/
public function activityEditor(Request $request, int $id): JsonResponse
{
$request->validate(['p_is_top' => 'required|in:0,1'], ['p_is_top.required' => '请选择置顶状态', 'p_is_top.in' => '置顶状态不在置顶范围内']);
$isTop = $request->integer('p_is_top') === 1 ? now()->timestamp : 0;
$activity = Activity::query()->where('audit_status', 1)->findOrFail($id, ['id', 'project_id', 'song_type']);
$projectId = $activity->getAttribute('project_id');
if (!UserService::isProjectManager(Auth::user(), $projectId)) {
return JsonResource::fail('无权限操作');
}
if ($isTop && $activity::query()->where('project_id', $projectId)->where('song_type', $activity->getAttribute('song_type'))->where("audit_status", 1)->where('status', 1)->where('p_is_top', '!=', 0)->count() >= 3) {
return JsonResource::fail('最多允许置顶3条内容');
}
$activity->update(['p_is_top' => $isTop]);
return JsonResource::success(JsonResource::UPDATE_SUCCESS);
}
public function detail(Request $request)
{
$project_id = $request->get('id');
$data = Project::whereKey($project_id)
->select(['id', 'intro', 'name', 'cover', 'head_cover', 'master_id'])
->with(['master:id,nick_name,avatar', 'comfirmTime:project_id,average_day'])
->where('status', 1)
->first();
if (!$data) {
return JsonResource::fail(JsonResource::PROJECT_INVALIDED);
}
//记录访客
ProjectVisitor::create(['user_id' => Auth::id(), 'project_id' => $project_id]);
$data['visitor_count'] = ProjectVisitor::query()
->where('project_id', $data->getKey())
->join('users', 'users.id', '=', 'project_visitors.user_id')
->where('project_visitors.created_at', '>=', Carbon::now()->subDays(30))
->where('users.status', 1)
->select('project_visitors.user_id')
->distinct('project_visitors.user_id')
->count();
$data['is_master'] = UserProjectPivot::query()->where(['user_id' => Auth::id(), 'project_id' => $project_id])->exists();
$data['member_count'] = $data->members()->where('status', 1)->count();
return JsonResource::success(JsonResource::SUCCESS, $data);
}
public function visitors(Request $request)
{
$project_id = $request->get('id');
$page_size = $request->get('size');
$visitor_ids = ProjectVisitor::query()
->where('project_id', $project_id)
->where('created_at', '>=', Carbon::now()->subDays(30))
->orderByDesc('created_at')
->pluck('user_id')
->toArray();
$datas = User::query()
->with(['authTags:id,name', 'userTag:id,name,icon,frame'])
->whereIn('id', $visitor_ids)
->where('status', 1)
->select(['id', 'avatar', 'nick_name', 'identity', 'user_tag_id'])
->orderByRaw("FIELD(id, " . implode(',', $visitor_ids) . ")")
->paginate($page_size);
foreach ($datas as $data) {
$data['last_visitor_at'] = ProjectVisitor::query()
->where('user_id', $data['id'])
->where('project_id', $project_id)
->orderBy('created_at', 'desc')
->limit(1)
->value('created_at')
->format('Y-m-d H:i:s');
}
return JsonResource::success(JsonResource::SUCCESS, ['data' => $datas->items(), 'count' => $datas->total()]);
}
public function editor(ProjectEdtiorRequest $request)
{
$data = $request->validated();
$query = Project::query()->where('id', $data['id']);
unset($data['id']);
$query->update($data);
return JsonResource::success(JsonResource::UPDATE_SUCCESS);
}
public function masterList(Request $request)
{
$project_id = $request->get('id');
$page_size = $request->get('size');
$text = $request->get('text');
$ids = UserProjectPivot::query()->where('project_id', $project_id)->pluck('user_id')->toArray();
$query = User::query()
->with(['authTags:id,name', 'userTag:id,name,icon,frame'])
->whereIn('id', $ids)
->where('status', 1);
if ($text) {
$query->whereLike('nick_name', $text);
}
$datas = $query
->select(['id', 'avatar', 'nick_name', 'identity', 'user_tag_id'])
->orderByRaw("FIELD(id, " . implode(',', $ids) . ")")
->paginate($page_size);
foreach ($datas as $data) {
$data['is_master'] = Project::query()->where(['master_id' => $data['id'], 'id' => $project_id])->exists();
}
return JsonResource::success(JsonResource::SUCCESS, ['data' => $datas->items(), 'count' => $datas->total()]);
}
public function inviteList(Request $request)
{
$id = $request->get('id');
$type = $request->get('type', 'master');
$text = $request->get('text');
$size = $request->get('size');
if ($type === 'master') {
$ids = UserProjectPivot::query()->where('project_id', $id)->orderByDesc('created_at')->pluck('user_id')->toArray();
} else if ($type === 'team') {
$group_id = GroupHasMember::query()->where(['member_id' => Auth::id()])->value('group_id') ?? 0;
if (!$group_id) {
return JsonResource::fail(JsonResource::NO_PERMISSION);
}
$ids = GroupHasMember::query()->where(['group_id' => $group_id])->whereNot('member_id', Auth::id())->pluck('member_id')->toArray();
} //关注或粉丝
else {
if ($type === 'follow') {
$feild = 'follower_id';
$pluck_feild = 'following_id';
} else {
$feild = 'following_id';
$pluck_feild = 'follower_id';
}
$ids = UserFollowRelation::query()
->where($feild, Auth::id())
->orderByDesc('created_at')
->pluck($pluck_feild)
->toArray();
};
//去掉已经加入厂牌的用户
$member_ids = ProjectMemberPivot::query()->whereIn('status', [1, 3])->pluck('user_id')->toArray();
$difference = array_diff($ids, $member_ids);
$difference_str = implode(',', $difference);
$query = User::query()
->with(['authTags' => fn($q) => $q->orderByDesc('weight')->orderByDesc('updated_at'), 'userTag:id,name,icon,frame'])
->whereIn('id', $difference);
if ($text) {
$query->whereLike('nick_name', $text);
}
$data = $query
->orderByRaw(DB::raw("FIELD(id, {$difference_str})"))
->select(['id', 'nick_name', 'avatar', 'identity', 'user_tag_id'])
->paginate($size);
$defaultLimit = SystemConfig::query()->where('identifier', '7SrpXqHNr2mcnKGfZhfoK')->value('content') ?? 1;
foreach ($data as $item) {
$item->is_invite = ProjectMemberPivot::query()->where('created_at', '>=', now()->subDays($defaultLimit))->where(['invite_id' => Auth::id(), 'user_id' => $item['id'], 'project_id' => $id, 'status' => 0])->exists();
}
return JsonResource::success(JsonResource::SUCCESS, ['data' => $data->items(), 'count' => $data->total()]);
}
public function invite(ProjectInviteRequest $request)
{
$data = $request->validated();
$user_id = $data['user_id'];
$project_id = $data['project_id'];
//判断当前用户是否是厂牌管理员
$is_master = UserProjectPivot::query()->where(['project_id' => $project_id, 'user_id' => Auth::id()])->exists();
if (!$is_master) {
return JsonResource::fail(JsonResource::HANDEL_FAIL);
}
//判断被邀请人是否存在其他厂牌
$is_member = ProjectMemberPivot::query()->where(['user_id' => $user_id])->whereIn('status', [1, 3])->exists();
if ($is_member) {
return JsonResource::fail(JsonResource::HANDEL_FAIL);
}
//判断1天内是否重复邀请
$defaultLimit = SystemConfig::query()->where('identifier', '7SrpXqHNr2mcnKGfZhfoK')->value('content') ?? 1;
$is_limit = ProjectMemberPivot::query()->where('created_at', '>=', now()->subDays($defaultLimit))->where(['invite_id' => Auth::id(), 'user_id' => $user_id, 'project_id' => $project_id, 'status' => 0])->exists();
if ($is_limit) {
return JsonResource::fail(JsonResource::INVITE_LIMIT);
}
//判断自己邀请自己且是厂牌管理员 是直接进厂牌
$is_master = UserProjectPivot::query()->where(['project_id' => $project_id, 'user_id' => $user_id])->exists();
if ($is_master and $user_id == Auth::id()) {
$status = 1;
} else {
$status = 0;
}
//创建邀请数据
ProjectMemberPivot::updateOrCreate(['invite_id' => Auth::id(), 'user_id' => $user_id, 'project_id' => $project_id], [
'status' => $status,
'created_at' => date("Y-m-d H:i:s"),
'updated_at' => date("Y-m-d H:i:s")
]);
return JsonResource::success(JsonResource::CREATE_SUCCESS);
}
public function handleList(Request $request)
{
$id = $request->get('id');
$ids = ProjectMemberPivot::query()->where(['invite_id' => $id, 'user_id' => Auth::id(), 'status' => 0])->pluck('project_id')->toArray();
if (count($ids) == 0) {
return JsonResource::success(JsonResource::SUCCESS, ['data' => [], 'count' => 0]);
}
$data = Project::query()
->whereIn('id', $ids)
->where('status', 1)
->with('master:id,nick_name')
->select('id', 'name', 'cover', 'master_id')
->orderByRaw("FIELD(id, " . implode(',', $ids) . ")")
->get();
return JsonResource::success(JsonResource::SUCCESS, ['data' => $data, 'count' => count($data)]);
}
public function handle(ProjectHandleInviteRequest $request)
{
$data = $request->validated();
$user_id = $data['user_id'];
$project_id = $data['project_id'];
$status = $data['status'];
$is_handel = ProjectMemberPivot::query()->where(['invite_id' => $user_id, 'user_id' => Auth::id(), 'project_id' => $project_id, 'status' => 0])->exists();
if (!$is_handel) {
ProjectMemberPivot::query()->where(['invite_id' => $user_id, 'user_id' => Auth::id(), 'project_id' => $project_id, 'status' => 0])->update(['status' => 4]);
return JsonResource::fail(JsonResource::INVALIDED);
}
//判断超过7天限制
$defaultLimit = SystemConfig::query()->where('identifier', 'VnyE3PTXOUPDEb4WZLBjM')->value('content') ?? 7;
$is_invalid = ProjectMemberPivot::query()->where(['invite_id' => $user_id, 'user_id' => Auth::id(), 'project_id' => $project_id, 'status' => 0])->where('created_at', '<=', now()->subDays($defaultLimit))->exists();
if ($is_invalid) {
ProjectMemberPivot::query()->where(['invite_id' => $user_id, 'user_id' => Auth::id(), 'project_id' => $project_id, 'status' => 0])->update(['status' => 4]);
return JsonResource::fail(JsonResource::JOIN_TEAM_INVALID);
}
if ($status == 1) {
//判断自己有厂牌
$is_group = ProjectMemberPivot::query()->where(['user_id' => Auth::id(), 'status' => 1])->exists();
if ($is_group) {
ProjectMemberPivot::query()->where(['invite_id' => $user_id, 'user_id' => Auth::id(), 'project_id' => $project_id, 'status' => 0])->update(['status' => 4]);
return JsonResource::fail(JsonResource::IS_PROJECT_MEMBER);
}
//判断邀请人是否是厂牌管理员
$is_master = UserProjectPivot::query()->where(['user_id' => $user_id, 'project_id' => $project_id])->exists();
if (!$is_master) {
ProjectMemberPivot::query()->where(['invite_id' => $user_id, 'user_id' => Auth::id(), 'project_id' => $project_id, 'status' => 0])->update(['status' => 4]);
return JsonResource::fail(JsonResource::IS_NOT_MASTER);
}
//更改状态
ProjectMemberPivot::query()->where(['invite_id' => $user_id, 'user_id' => Auth::id(), 'project_id' => $project_id, 'status' => 0])->update(['status' => 1]);
//其他厂牌邀请状态改为失效
ProjectMemberPivot::query()->where(['user_id' => Auth::id(), 'status' => 0])->update(['status' => 4]);
} else {
ProjectMemberPivot::query()->where(['invite_id' => $user_id, 'user_id' => Auth::id(), 'project_id' => $project_id, 'status' => 0])->update(['status' => 2]);
}
return JsonResource::success(JsonResource::UPDATE_SUCCESS);
}
public function memberOut(ProjectMemberOutRequest $request)
{
$data = $request->validated();
$status = $data['status'];
$user_id = Auth::id();
//判断当前用户是否是厂牌管理员,是管理员直接退
$project_id = ProjectMemberPivot::query()->where(['user_id' => Auth::id()])->whereIn('status', [1, 3])->first()->project_id ?? 0;
if ($project_id == 0) {
return JsonResource::fail('当前用户还不是成员');
}
$project = Project::query()->find($project_id);
$master_ids = UserProjectPivot::query()->where('project_id', $project->id)->pluck('user_id')->toArray();
if (in_array(Auth::id(), $master_ids)) {
//更改成员状态
ProjectMemberPivot::query()->where(['user_id' => Auth::id(), 'status' => 1])->delete();
return JsonResource::success(JsonResource::UPDATE_SUCCESS);
}
// 申请退出
if ($status == 3) {
$MsgBody = [
[
"MsgType" => "TIMCustomElem",
"MsgContent" => [
"Data" => "{\"businessID\": \"exit_brand\",\"text\": \"申请退出厂牌\",\"project_id\": \"$project->id\",\"project_name\": \"$project->name\",\"project_cover\": \"$project->cover\",\"version\": 4}",
"Desc" => "notification",
"Ext" => "url",
"Sound" => "dingdong.aiff"
]
]
];
}
$OfflinePushInfo = [
"PushFlag" => 0,
"Title" => Auth::user()->nick_name,
"Desc" => "申请退出厂牌",
"Ext" => "申请退出厂牌",
"AndroidInfo" => [
"Sound" => "android.mp3"
],
"ApnsInfo" => [
"Sound" => "apns.mp3",
"BadgeMode" => 1,
"Image" => "www.image.com"
]
];
// 取消退出
if ($status == 1) {
$MsgBody = [
[
"MsgType" => "TIMCustomElem",
"MsgContent" => [
"Data" => "{\"businessID\": \"invite_tip\",\"text\": \"取消退出厂牌[$project->name]\",\"title\": \"取消退出厂牌\",\"content\": \"\",\"tipStatus\": 9,\"version\": 4}",
"Desc" => "notification",
"Ext" => "url",
"Sound" => "dingdong.aiff"
]
]
];
$OfflinePushInfo = [
"PushFlag" => 0,
"Title" => Auth::user()->nick_name,
"Desc" => "取消退出厂牌[$project->name]",
"Ext" => "取消退出厂牌[$project->name]",
"AndroidInfo" => [
"Sound" => "android.mp3"
],
"ApnsInfo" => [
"Sound" => "apns.mp3",
"BadgeMode" => 1,
"Image" => "www.image.com"
]
];
}
// IM消息推送
$getSign = new ImSign();
$sendService = new ImSendService();
$sign = $getSign->genUserSig('administrator');
$sendData = ['MsgBody' => $MsgBody, 'OfflinePushInfo' => $OfflinePushInfo];
$sendService->batchSend($sign, $master_ids, $user_id, $sendData, 2);
//更改成员状态
ProjectMemberPivot::query()->where(['user_id' => Auth::id()])->whereIn('status', [1, 3])->update(['status' => $status, "updated_at" => date("Y-m-d H:i:s")]);
return JsonResource::success(JsonResource::UPDATE_SUCCESS);
}
public function handleOut(ProjectHandleOutRequest $request)
{
$data = $request->validated();
$user_id = $data['user_id'];
$project_id = $data['project_id'];
//判断邀请人是否是厂牌管理员
$is_master = UserProjectPivot::query()->where(['user_id' => Auth::id(), 'project_id' => $project_id])->exists();
if (!$is_master) {
return JsonResource::fail(JsonResource::YOU_NOT_MASTER, code: 4003); //4003:操作失败,您已经不是该厂牌管理员
}
$query = ProjectMemberPivot::query()->where(['user_id' => $user_id, 'status' => 3, 'project_id' => $project_id])->first();
if (!$query) {
return JsonResource::fail(JsonResource::OUT_IS_HANDLE, code: 4005); //4005:操作失败,该申请已被其他管理员处理
}
$query->delete();
return JsonResource::success(JsonResource::UPDATE_SUCCESS);
}
}