Skip to content

Commit

Permalink
Merge pull request #133 from Qsnh/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Qsnh authored Sep 7, 2020
2 parents 19897e5 + b82f198 commit dc84bb4
Show file tree
Hide file tree
Showing 53 changed files with 2,177 additions and 1,139 deletions.
60 changes: 60 additions & 0 deletions app/Hooks/MpWechatMessageReplyHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of the Qsnh/meedu.
*
* (c) XiaoTeng <616896861@qq.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace App\Hooks;

use App\Meedu\Hooks\HookParams;
use App\Meedu\Hooks\HookRuntimeInterface;
use App\Services\Other\Services\MpWechatService;
use App\Services\Other\Interfaces\MpWechatServiceInterface;

class MpWechatMessageReplyHook implements HookRuntimeInterface
{
public const MSG_TYPE_TEXT = 'text';
public const MSG_TYPE_EVENT = 'event';

public function handle(HookParams $params, \Closure $next)
{
$types = [self::MSG_TYPE_TEXT => 1, self::MSG_TYPE_EVENT => 1];
$msgType = $params->getValue('MsgType', '');
if (!isset($types[$msgType])) {
return $next($params);
}

/**
* @var HookParams $response
*/
$response = $next($params);
if ($response && $response->getResponse()) {
// 如果已经有其它的中间件处理了该条消息并返回了值
// 那么这里就不需要继续处理了
// 当前中间件的权重是最低的
return $response;
}

/**
* @var MpWechatService $mpWechatService
*/
$mpWechatService = app()->make(MpWechatServiceInterface::class);

try {
if ($msgType === self::MSG_TYPE_TEXT) {
// 文本消息
return $mpWechatService->textMessageReplyFind($params->getValue('raw.Content', ''));
} elseif ($msgType === self::MSG_TYPE_EVENT) {
// 事件消息
return $mpWechatService->eventMessageReplyFind($params->getValue('raw.Event'), $params->getValue('raw.EventKey'));
}
} catch (\Exception $e) {
exception_record($e);
}
}
}
38 changes: 38 additions & 0 deletions app/Http/Controllers/Api/Wechat/MpWechatController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the Qsnh/meedu.
*
* (c) XiaoTeng <616896861@qq.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace App\Http\Controllers\Api\Wechat;

use App\Meedu\Wechat;
use App\Meedu\Hooks\HookRun;
use App\Meedu\Hooks\HookParams;
use App\Meedu\Hooks\Constant\PositionConstant;
use App\Http\Controllers\Api\V2\BaseController;

class MpWechatController extends BaseController
{
public function serve()
{
$mp = Wechat::getInstance();
$mp->server->push(function ($message) {
return HookRun::run(PositionConstant::MP_WECHAT_RECEIVER_MESSAGE, new HookParams([
'MsgType' => $message['MsgType'],
'ToUserName' => $message['ToUserName'],
'FromUserName' => $message['FromUserName'],
'CreateTime' => $message['CreateTime'],
'MsgId' => $message['MsgId'],
'raw' => $message,
]));
});

return $mp->server->serve();
}
}
13 changes: 7 additions & 6 deletions app/Http/Controllers/Backend/Api/V1/CourseCommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,20 @@ class CourseCommentController extends BaseController
public function index(Request $request)
{
$courseId = $request->input('course_id');
$userId = $request->input('user_id');

$comments = CourseComment::with(['course'])
->when($courseId, function ($query) use ($courseId) {
$query->where('course_id', $courseId);
})
->when($userId, function ($query) use ($userId) {
$query->where('user_id', $userId);
})
->orderByDesc('id')
->paginate($request->input('size', 12));
->paginate($request->input('size', 10));

$userIds = [];
foreach ($comments->items() as $item) {
$userIds[] = $item->user_id;
}
$users = User::query()
->whereIn('id', array_flip(array_flip($userIds)))
->whereIn('id', array_column($comments->items(), 'user_id'))
->select(['id', 'nick_name', 'mobile', 'avatar'])
->get()
->keyBy('id');
Expand Down
131 changes: 116 additions & 15 deletions app/Http/Controllers/Backend/Api/V1/CourseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@

namespace App\Http\Controllers\Backend\Api\V1;

use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Constant\BackendApiConstant;
use App\Services\Member\Models\User;
use App\Services\Course\Models\Video;
use App\Services\Course\Models\Course;
use App\Services\Member\Models\UserCourse;
use App\Http\Requests\Backend\CourseRequest;
use App\Services\Course\Models\CourseCategory;
use App\Services\Course\Models\CourseUserRecord;
Expand All @@ -26,26 +28,35 @@ public function index(Request $request)
{
$keywords = $request->input('keywords', '');
$cid = $request->input('cid');
$userId = $request->input('user_id');

// 排序
$sort = $request->input('sort', 'created_at');
$order = $request->input('order', 'desc');
$courses = Course::when($keywords, function ($query) use ($keywords) {
return $query->where('title', 'like', '%' . $keywords . '%');
})->when($cid, function ($query) use ($cid) {
return $query->whereCategoryId($cid);
})
->orderBy($sort, $order)
->paginate(12);

$courses->appends($request->input());
$courses = Course::query()
->when($keywords, function ($query) use ($keywords) {
return $query->where('title', 'like', '%' . $keywords . '%');
})
->when($cid, function ($query) use ($cid) {
return $query->whereCategoryId($cid);
})
->when($userId, function ($query) use ($userId) {
$courseIds = UserCourse::query()->where('user_id', $userId)->select(['course_id'])->get()->pluck('course_id')->toArray();
$courseIds || $courseIds = [0];
$query->whereIn('id', $courseIds);
})
->orderBy($sort, $order)
->paginate($request->input('size', 12));

$categories = CourseCategory::select(['id', 'name'])->orderBy('sort')->get();
$categories = CourseCategory::query()->select(['id', 'name'])->orderBy('sort')->get();

return $this->successData(compact('courses', 'categories'));
}

public function create()
{
$categories = CourseCategory::show()->get();
$categories = CourseCategory::query()->select(['id', 'name', 'sort'])->orderBy('sort')->get();
return $this->successData(compact('categories'));
}

Expand Down Expand Up @@ -73,7 +84,6 @@ public function update(CourseRequest $request, $id)
$originIsShow = $course->is_show;
$course->fill($data)->save();

// 判断是否修改了显示的状态
if ($originIsShow !== $data['is_show']) {
// 修改下面的视频显示状态
Video::where('course_id', $course->id)->update(['is_show' => $data['is_show']]);
Expand All @@ -88,16 +98,107 @@ public function destroy($id)
if ($course->videos()->exists()) {
return $this->error(BackendApiConstant::COURSE_BAN_DELETE_FOR_VIDEOS);
}

$course->delete();

return $this->success();
}

public function subscribeUsers(Request $request, $courseId)
// 课程观看记录
public function watchRecords(Request $request, $courseId)
{
$userId = (int)$request->input('user_id');
$watchStartAt = $request->input('watched_start_at');
$watchEndAt = $request->input('watched_end_at');

$data = CourseUserRecord::query()
->where('course_id', $courseId)
->when($userId, function ($query) use ($userId) {
$query->where('user_id', $userId);
})
->when($watchStartAt && $watchEndAt, function ($query) use ($watchStartAt, $watchEndAt) {
$query->whereBetween('watched_at', [$watchStartAt, $watchEndAt]);
})
->orderByDesc('id')
->paginate($request->input('size', 10));

// 用户
$users = User::query()
->select(['id', 'nick_name', 'avatar', 'mobile'])
->whereIn('id', array_column($data->items(), 'user_id'))
->get()
->keyBy('id');

// 订阅记录
$subscribeRecords = UserCourse::query()
->whereIn('user_id', array_column($data->items(), 'user_id'))
->where('course_id', $courseId)
->get()
->keyBy('user_id');

return $this->successData([
'data' => $data,
'users' => $users,
'subscribe_records' => $subscribeRecords,
]);
}

// 订阅记录
public function subscribes(Request $request, $courseId)
{
$userId = $request->input('user_id');
$subscribeStartAt = $request->input('subscribe_start_at');
$subscribeEndAt = $request->input('subscribe_end_at');

$data = UserCourse::query()
->when($userId, function ($query) use ($userId) {
$query->where('user_id', $userId);
})
->when($subscribeStartAt && $subscribeEndAt, function ($query) use ($subscribeStartAt, $subscribeEndAt) {
$query->whereBetween('created_at', [$subscribeStartAt, $subscribeEndAt]);
})
->where('course_id', $courseId)
->orderByDesc('created_at')
->paginate($request->input('size', 10));

$users = User::query()
->select(['id', 'nick_name', 'mobile', 'avatar'])
->whereIn('id', array_column($data->items(), 'user_id'))
->get()
->keyBy('id');

return $this->successData([
'data' => $data,
'users' => $users,
]);
}

public function createSubscribe(Request $request, $courseId)
{
$data = CourseUserRecord::whereCourseId($courseId)->paginate($request->input('size', 12));
$users = User::select(['id', 'nick_name', 'avatar', 'mobile'])->whereIn('id', array_column($data->all(), 'user_id'))->get()->keyBy('id');
return $this->successData(compact('data', 'users'));
$userId = $request->input('user_id');
$exists = UserCourse::query()->where('course_id', $courseId)->where('user_id', $userId)->exists();
if ($exists) {
return $this->error('订阅关系已存在');
}
if (!User::query()->where('id', $userId)->exists()) {
return $this->error('用户不存在');
}

UserCourse::create([
'course_id' => $courseId,
'user_id' => $userId,
'charge' => 0,
'created_at' => Carbon::now(),
]);

return $this->success();
}

public function deleteSubscribe(Request $request, $courseId)
{
$userId = $request->input('user_id');
UserCourse::query()->where('course_id', $courseId)->where('user_id', $userId)->delete();
return $this->success();
}

public function all()
Expand Down
Loading

0 comments on commit dc84bb4

Please sign in to comment.