diff --git a/CHANGELOG.md b/CHANGELOG.md index 95530986..af6041a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### [v1.2.0](https://gitee.com/koogua/course-tencent-cloud/releases/v1.2.0)(2020-11-25) +- 增加客户端api +- 代码优化以及问题修复 + ### [v1.1.0](https://gitee.com/koogua/course-tencent-cloud/releases/v1.1.0)(2020-10-08) - 增加运营统计功能 diff --git a/app/Builders/ImGroupUserList.php b/app/Builders/ImGroupUserList.php index 917eecd6..467db5a6 100644 --- a/app/Builders/ImGroupUserList.php +++ b/app/Builders/ImGroupUserList.php @@ -36,7 +36,7 @@ class ImGroupUserList extends Builder $userRepo = new UserRepo(); - $columns = ['id', 'name', 'avatar', 'title', 'about', 'vip']; + $columns = ['id', 'name', 'avatar', 'title', 'about', 'vip', 'gender', 'area']; $users = $userRepo->findByIds($ids, $columns); diff --git a/app/Builders/LiveList.php b/app/Builders/LiveList.php index e010b6eb..a331653e 100644 --- a/app/Builders/LiveList.php +++ b/app/Builders/LiveList.php @@ -4,6 +4,7 @@ namespace App\Builders; use App\Repos\Chapter as ChapterRepo; use App\Repos\Course as CourseRepo; +use App\Repos\User as UserRepo; class LiveList extends Builder { @@ -32,19 +33,38 @@ class LiveList extends Builder public function getCourses(array $lives) { - $ids = kg_array_column($lives, 'course_id'); + $courseIds = kg_array_column($lives, 'course_id'); $courseRepo = new CourseRepo(); - $courses = $courseRepo->findByIds($ids, ['id', 'title', 'cover']); + $courses = $courseRepo->findByIds($courseIds, ['id', 'title', 'cover', 'teacher_id']); + + $teacherIds = kg_array_column($courses->toArray(), 'teacher_id'); + + $userRepo = new UserRepo(); + + $users = $userRepo->findByIds($teacherIds, ['id', 'name', 'title', 'avatar', 'about']); $baseUrl = kg_cos_url(); + $teachers = []; + + foreach ($users->toArray() as $user) { + $user['avatar'] = $baseUrl . $user['avatar']; + $teachers[$user['id']] = $user; + } + $result = []; foreach ($courses->toArray() as $course) { $course['cover'] = $baseUrl . $course['cover']; - $result[$course['id']] = $course; + $course['teacher'] = $teachers[$course['teacher_id']] ?? new \stdClass(); + $result[$course['id']] = [ + 'id' => $course['id'], + 'title' => $course['title'], + 'cover' => $course['cover'], + 'teacher' => $course['teacher'], + ]; } return $result; diff --git a/app/Http/Admin/Controllers/AppController.php b/app/Http/Admin/Controllers/AppController.php deleted file mode 100644 index 5871cb68..00000000 --- a/app/Http/Admin/Controllers/AppController.php +++ /dev/null @@ -1,127 +0,0 @@ -getApps(); - - $this->view->setVar('pager', $pager); - } - - /** - * @Get("/add", name="admin.app.add") - */ - public function addAction() - { - $appService = new AppService(); - - $types = $appService->getAppTypes(); - - $this->view->setVar('types', $types); - } - - /** - * @Post("/create", name="admin.app.create") - */ - public function createAction() - { - $appService = new AppService(); - - $appService->createApp(); - - $location = $this->url->get(['for' => 'admin.app.list']); - - $content = [ - 'location' => $location, - 'msg' => '创建应用成功', - ]; - - return $this->jsonSuccess($content); - } - - /** - * @Get("/{id:[0-9]+}/edit", name="admin.app.edit") - */ - public function editAction($id) - { - $appService = new AppService; - - $app = $appService->getApp($id); - $types = $appService->getAppTypes(); - - $this->view->setVar('app', $app); - $this->view->setVar('types', $types); - } - - /** - * @Post("/{id:[0-9]+}/update", name="admin.app.update") - */ - public function updateAction($id) - { - $appService = new AppService(); - - $appService->updateApp($id); - - $location = $this->url->get(['for' => 'admin.app.list']); - - $content = [ - 'location' => $location, - 'msg' => '更新应用成功', - ]; - - return $this->jsonSuccess($content); - } - - /** - * @Post("/{id:[0-9]+}/delete", name="admin.app.delete") - */ - public function deleteAction($id) - { - $appService = new AppService(); - - $appService->deleteApp($id); - - $location = $this->request->getHTTPReferer(); - - $content = [ - 'location' => $location, - 'msg' => '删除应用成功', - ]; - - return $this->jsonSuccess($content); - } - - /** - * @Post("/{id:[0-9]+}/restore", name="admin.app.restore") - */ - public function restoreAction($id) - { - $appService = new AppService(); - - $appService->restoreApp($id); - - $location = $this->request->getHTTPReferer(); - - $content = [ - 'location' => $location, - 'msg' => '还原应用成功', - ]; - - return $this->jsonSuccess($content); - } - -} diff --git a/app/Http/Admin/Services/App.php b/app/Http/Admin/Services/App.php deleted file mode 100644 index 100daf8a..00000000 --- a/app/Http/Admin/Services/App.php +++ /dev/null @@ -1,135 +0,0 @@ -getParams(); - - $params['deleted'] = $params['deleted'] ?? 0; - - $sort = $pagerQuery->getSort(); - $page = $pagerQuery->getPage(); - $limit = $pagerQuery->getLimit(); - - $appRepo = new AppRepo(); - - return $appRepo->paginate($params, $sort, $page, $limit); - } - - public function getApp($id) - { - return $this->findOrFail($id); - } - - public function createApp() - { - $post = $this->request->getPost(); - - $validator = new AppValidator(); - - $data = []; - - $data['type'] = $validator->checkType($post['type']); - $data['name'] = $validator->checkName($post['name']); - $data['remark'] = $validator->checkRemark($post['remark']); - - $page = new AppModel(); - - $page->create($data); - - $this->rebuildAppCache($page); - - return $page; - } - - public function updateApp($id) - { - $app = $this->findOrFail($id); - - $post = $this->request->getPost(); - - $validator = new AppValidator(); - - $data = []; - - if (isset($post['type'])) { - $data['type'] = $validator->checkType($post['type']); - } - - if (isset($post['name'])) { - $data['name'] = $validator->checkName($post['name']); - } - - if (isset($post['remark'])) { - $data['remark'] = $validator->checkRemark($post['remark']); - } - - if (isset($post['published'])) { - $data['published'] = $validator->checkPublishStatus($post['published']); - } - - $app->update($data); - - $this->rebuildAppCache($app); - - return $app; - } - - public function deleteApp($id) - { - $app = $this->findOrFail($id); - - $app->deleted = 1; - - $app->update(); - - $this->rebuildAppCache($app); - - return $app; - } - - public function restoreApp($id) - { - $app = $this->findOrFail($id); - - $app->deleted = 0; - - $app->update(); - - $this->rebuildAppCache($app); - - return $app; - } - - public function getAppTypes() - { - return AppModel::types(); - } - - protected function rebuildAppCache(AppModel $app) - { - $cache = new AppCache(); - - $cache->rebuild($app->key); - } - - protected function findOrFail($id) - { - $validator = new AppValidator(); - - return $validator->checkApp($id); - } - -} diff --git a/app/Http/Admin/Services/AuthNode.php b/app/Http/Admin/Services/AuthNode.php index 9f321357..43fb64fd 100644 --- a/app/Http/Admin/Services/AuthNode.php +++ b/app/Http/Admin/Services/AuthNode.php @@ -746,37 +746,6 @@ class AuthNode extends Service ], ], ], - [ - 'id' => '5-2', - 'title' => '应用管理', - 'type' => 'menu', - 'children' => [ - [ - 'id' => '5-2-1', - 'title' => '应用列表', - 'type' => 'menu', - 'route' => 'admin.app.list', - ], - [ - 'id' => '5-2-2', - 'title' => '添加应用', - 'type' => 'menu', - 'route' => 'admin.app.add', - ], - [ - 'id' => '5-2-3', - 'title' => '编辑应用', - 'type' => 'button', - 'route' => 'admin.app.edit', - ], - [ - 'id' => '5-2-4', - 'title' => '删除应用', - 'type' => 'button', - 'route' => 'admin.app.delete', - ], - ], - ], ], ]; } diff --git a/app/Http/Admin/Views/app/add.volt b/app/Http/Admin/Views/app/add.volt deleted file mode 100644 index 7ff4f88e..00000000 --- a/app/Http/Admin/Views/app/add.volt +++ /dev/null @@ -1,41 +0,0 @@ -{% extends 'templates/main.volt' %} - -{% block content %} - -
-
- 添加应用 -
-
- -
- -
-
-
- -
- -
-
-
- -
- -
-
-
- -
- - -
-
-
- -{% endblock %} \ No newline at end of file diff --git a/app/Http/Admin/Views/app/edit.volt b/app/Http/Admin/Views/app/edit.volt deleted file mode 100644 index 921acd3e..00000000 --- a/app/Http/Admin/Views/app/edit.volt +++ /dev/null @@ -1,49 +0,0 @@ -{% extends 'templates/main.volt' %} - -{% block content %} - -
-
- 编辑应用 -
-
- -
- -
-
-
- -
- -
-
-
- -
- -
-
-
- -
- - -
-
-
- -
- - -
-
-
- -{% endblock %} \ No newline at end of file diff --git a/app/Http/Admin/Views/app/list.volt b/app/Http/Admin/Views/app/list.volt deleted file mode 100644 index 3c3a5394..00000000 --- a/app/Http/Admin/Views/app/list.volt +++ /dev/null @@ -1,83 +0,0 @@ -{% extends 'templates/main.volt' %} - -{% block content %} - - {%- macro type_info(value) %} - {% if value == 'pc' %} - PC客户端 - {% elseif value == 'h5' %} - H5客户端 - {% elseif value == 'ios' %} - IOS客户端 - {% elseif value == 'android' %} - Android客户端 - {% elseif value == 'mp_weixin' %} - 微信小程序 - {% elseif value == 'mp_alipay' %} - 支付宝小程序 - {% else %} - 未知 - {% endif %} - {%- endmacro %} - -
-
- - 应用管理 - -
-
- - - - - - - - - - - - - - - - - - - - - - - - {% for item in pager.items %} - {% set edit_url = url({'for':'admin.app.edit','id':item.id}) %} - {% set update_url = url({'for':'admin.app.update','id':item.id}) %} - {% set delete_url = url({'for':'admin.app.delete','id':item.id}) %} - {% set restore_url = url({'for':'admin.app.restore','id':item.id}) %} - - - - - - - - - - {% endfor %} - -
编号名称类型Key / Secret创建时间发布操作
{{ item.id }}{{ item.name }}{{ type_info(item.type) }}{{ item.key }} / {{ item.secret }}{{ date('Y-m-d H:i:s',item.create_time) }} -
- - -
-
- -{% endblock %} \ No newline at end of file diff --git a/app/Http/Api/Controllers/Controller.php b/app/Http/Api/Controllers/Controller.php index 50a6611d..d33b4eeb 100644 --- a/app/Http/Api/Controllers/Controller.php +++ b/app/Http/Api/Controllers/Controller.php @@ -15,19 +15,12 @@ class Controller extends \Phalcon\Mvc\Controller public function beforeExecuteRoute(Dispatcher $dispatcher) { - /** - * 存在Origin头信息才设置跨域 - */ if ($this->request->getHeader('Origin')) { $this->setCors(); } - /** - * Options请求不验证签名和限流 - */ if (!$this->request->isOptions()) { - //$this->checkApiSignature(); - //$this->checkRateLimit(); + $this->checkRateLimit(); } return true; diff --git a/app/Http/Api/Controllers/HelpController.php b/app/Http/Api/Controllers/HelpController.php index 60879eb3..c64e78da 100644 --- a/app/Http/Api/Controllers/HelpController.php +++ b/app/Http/Api/Controllers/HelpController.php @@ -24,7 +24,7 @@ class HelpController extends Controller } /** - * @Get("/{id:[0-9]+}", name="api.help.info") + * @Get("/{id:[0-9]+}/info", name="api.help.info") */ public function infoAction($id) { diff --git a/app/Http/Api/Controllers/PageController.php b/app/Http/Api/Controllers/PageController.php index b18e31f6..255e82f4 100644 --- a/app/Http/Api/Controllers/PageController.php +++ b/app/Http/Api/Controllers/PageController.php @@ -11,7 +11,7 @@ class PageController extends Controller { /** - * @Get("/{id:[0-9]+}", name="api.page.info") + * @Get("/{id:[0-9]+}/info", name="api.page.info") */ public function infoAction($id) { diff --git a/app/Http/Home/Views/macros/order.volt b/app/Http/Home/Views/macros/order.volt index 57dd015f..1f963258 100644 --- a/app/Http/Home/Views/macros/order.volt +++ b/app/Http/Home/Views/macros/order.volt @@ -1,12 +1,12 @@ {%- macro item_info(order) %} - {% if order.item_type == '1' %} + {% if order.item_type == 1 %} {% set course = order.item_info.course %}

课程名称:{{ course.title }}

市场价格:{{ '¥%0.2f'|format(course.market_price) }}会员价格:{{ '¥%0.2f'|format(course.vip_price) }}

学习期限:{{ date('Y-m-d',course.study_expiry_time) }}退款期限:{{ date('Y-m-d',course.refund_expiry_time) }}

- {% elseif order.item_type == '2' %} + {% elseif order.item_type == 2 %} {% set courses = order.item_info.courses %} {% for course in courses %}
@@ -15,20 +15,20 @@

学习期限:{{ date('Y-m-d',course.study_expiry_time) }}退款期限:{{ date('Y-m-d',course.refund_expiry_time) }}

{% endfor %} - {% elseif order.item_type == '3' %} + {% elseif order.item_type == 3 %} {% set course = order.item_info.course %} {% set reward = order.item_info.reward %}

课程名称:{{ course.title }}

赞赏金额:{{ '¥%0.2f'|format(reward.price) }}

- {% elseif order.item_type == '4' %} + {% elseif order.item_type == 4 %} {% set vip = order.item_info.vip %}

商品名称:{{ order.subject }}

商品价格:{{ '¥%0.2f'|format(order.amount) }}

- {% elseif order.item_type == '99' %} + {% elseif order.item_type == 99 %}

商品名称:{{ order.subject }}

商品价格:{{ '¥%0.2f'|format(order.amount) }}

@@ -38,30 +38,30 @@ {%- macro status_history(items) %} {% for item in items %} - {% if item.status == '1' %} + {% if item.status == 1 %}

创建时间:{{ date('Y-m-d H:i:s',item.create_time) }}

- {% elseif item.status == '2' %} + {% elseif item.status == 2 %}

支付时间:{{ date('Y-m-d H:i:s',item.create_time) }}

- {% elseif item.status == '3' %} + {% elseif item.status == 3 %}

完成时间:{{ date('Y-m-d H:i:s',item.create_time) }}

- {% elseif item.status == '4' %} + {% elseif item.status == 4 %}

关闭时间:{{ date('Y-m-d H:i:s',item.create_time) }}

- {% elseif item.status == '5' %} + {% elseif item.status == 5 %}

退款时间:{{ date('Y-m-d H:i:s',item.create_time) }}

{% endif %} {% endfor %} {%- endmacro %} {%- macro order_status(value) %} - {% if value == '1' %} + {% if value == 1 %} 待支付 - {% elseif value == '2' %} + {% elseif value == 2 %} 发货中 - {% elseif value == '3' %} + {% elseif value == 3 %} 已完成 - {% elseif value == '4' %} + {% elseif value == 4 %} 已关闭 - {% elseif value == '5' %} + {% elseif value == 5 %} 已退款 {% endif %} {%- endmacro %} diff --git a/app/Library/AppInfo.php b/app/Library/AppInfo.php index d2198fa6..569370b3 100644 --- a/app/Library/AppInfo.php +++ b/app/Library/AppInfo.php @@ -11,7 +11,7 @@ class AppInfo protected $link = 'https://gitee.com/koogua'; - protected $version = '1.1.0'; + protected $version = '1.2.0'; public function __get($name) { diff --git a/app/Models/App.php b/app/Models/App.php deleted file mode 100644 index 3dd844c1..00000000 --- a/app/Models/App.php +++ /dev/null @@ -1,136 +0,0 @@ -addBehavior( - new SoftDelete([ - 'field' => 'deleted', - 'value' => 1, - ]) - ); - } - - public function beforeCreate() - { - $this->key = Text::random(Text::RANDOM_ALNUM, 16); - $this->secret = Text::random(Text::RANDOM_ALNUM, 16); - $this->create_time = time(); - } - - public function beforeUpdate() - { - if ($this->deleted == 1) { - $this->published = 0; - } - - $this->update_time = time(); - } - - public static function types() - { - return [ - self::TYPE_PC => 'PC客户端', - self::TYPE_H5 => 'H5客户端', - self::TYPE_IOS => 'IOS客户端', - self::TYPE_ANDROID => 'Android客户端', - self::TYPE_MP_WEIXIN => '微信小程序', - self::TYPE_MP_ALIPAY => '支付宝小程序', - ]; - } - -} diff --git a/app/Models/AppVersion.php b/app/Models/AppVersion.php deleted file mode 100644 index 5dc49c6a..00000000 --- a/app/Models/AppVersion.php +++ /dev/null @@ -1,126 +0,0 @@ -addBehavior( - new SoftDelete([ - 'field' => 'deleted', - 'value' => 1, - ]) - ); - } - - public function beforeCreate() - { - $this->create_time = time(); - } - - public function beforeUpdate() - { - if ($this->deleted == 1) { - $this->published = 0; - } - - $this->update_time = time(); - } - - public function afterCreate() - { - $cache = new MaxCategoryIdCache(); - - $cache->rebuild(); - } - - public static function types() - { - return [ - self::TYPE_COURSE => '课程', - self::TYPE_HELP => '帮助', - ]; - } - -} diff --git a/app/Repos/App.php b/app/Repos/App.php deleted file mode 100644 index 028c280a..00000000 --- a/app/Repos/App.php +++ /dev/null @@ -1,78 +0,0 @@ -modelsManager->createBuilder(); - - $builder->from(AppModel::class); - - $builder->where('1 = 1'); - - if (!empty($where['id'])) { - $builder->andWhere('id = :id:', ['id' => $where['id']]); - } - - if (!empty($where['key'])) { - $builder->andWhere('key = :key:', ['key' => $where['key']]); - } - - if (!empty($where['type'])) { - $builder->andWhere('type = :type:', ['type' => $where['type']]); - } - - if (!empty($where['published'])) { - $builder->andWhere('published = :published:', ['published' => $where['published']]); - } - - if (isset($where['deleted'])) { - $builder->andWhere('deleted = :deleted:', ['deleted' => $where['deleted']]); - } - - switch ($sort) { - default: - $orderBy = 'id DESC'; - break; - } - - $builder->orderBy($orderBy); - - $pager = new PagerQueryBuilder([ - 'builder' => $builder, - 'page' => $page, - 'limit' => $limit, - ]); - - return $pager->paginate(); - } - - /** - * @param int $id - * @return AppModel|Model|bool - */ - public function findById($id) - { - return AppModel::findFirst($id); - } - - /** - * @param string $appKey - * @return AppModel|Model|bool - */ - public function findByAppKey($appKey) - { - return AppModel::findFirst([ - 'conditions' => 'key = :key:', - 'bind' => ['key' => $appKey], - ]); - } - -} diff --git a/app/Repos/Consult.php b/app/Repos/Consult.php index 78f97eec..548a6569 100644 --- a/app/Repos/Consult.php +++ b/app/Repos/Consult.php @@ -95,15 +95,15 @@ class Consult extends Repository } /** - * @param int $chapterId + * @param int $courseId * @param int $userId * @return ConsultModel|Model|bool */ - public function findUserLastChapterConsult($chapterId, $userId) + public function findUserLastCourseConsult($courseId, $userId) { return ConsultModel::findFirst([ - 'conditions' => 'chapter_id = ?1 AND owner_id = ?2 AND deleted = 0', - 'bind' => [1 => $chapterId, 2 => $userId], + 'conditions' => 'course_id = ?1 AND owner_id = ?2 AND deleted = 0', + 'bind' => [1 => $courseId, 2 => $userId], 'order' => 'id DESC', ]); } diff --git a/app/Services/Logic/Consult/ConsultCreate.php b/app/Services/Logic/Consult/ConsultCreate.php index 7d9a4002..fe29e49a 100644 --- a/app/Services/Logic/Consult/ConsultCreate.php +++ b/app/Services/Logic/Consult/ConsultCreate.php @@ -20,29 +20,83 @@ class ConsultCreate extends Service public function handle() { - $post = $this->request->getPost(); + $chapterId = $this->request->getPost('chapter_id', 'int', 0); + $courseId = $this->request->getPost('course_id', 'int', 0); $user = $this->getLoginUser(); - $chapter = $this->checkChapter($post['chapter_id']); - - $course = $this->checkCourse($chapter->course_id); - $validator = new UserLimitValidator(); $validator->checkDailyConsultLimit($user); + $validator = new UserLimitValidator(); + + $validator->checkDailyConsultLimit($user); + + if ($chapterId > 0) { + + $chapter = $this->checkChapter($chapterId); + + return $this->handleChapterConsult($chapter, $user); + + } else { + + $course = $this->checkCourse($courseId); + + return $this->handleCourseConsult($course, $user); + } + } + + protected function handleCourseConsult(CourseModel $course, UserModel $user) + { + $post = $this->request->getPost(); + $validator = new ConsultValidator(); $question = $validator->checkQuestion($post['question']); + $private = $validator->checkPrivateStatus($post['private']); - $validator->checkIfDuplicated($question, $chapter->id, $user->id); + $validator->checkIfDuplicated($course->id, $user->id, $question); $priority = $this->getPriority($course, $user); $consult = new ConsultModel(); $consult->question = $question; + $consult->private = $private; + $consult->priority = $priority; + $consult->course_id = $course->id; + $consult->owner_id = $user->id; + $consult->published = 1; + + $consult->create(); + + $this->incrCourseConsultCount($course); + + $this->incrUserDailyConsultCount($user); + + return $consult; + } + + protected function handleChapterConsult(ChapterModel $chapter, UserModel $user) + { + $course = $this->checkCourse($chapter->course_id); + + $post = $this->request->getPost(); + + $validator = new ConsultValidator(); + + $question = $validator->checkQuestion($post['question']); + $private = $validator->checkPrivateStatus($post['private']); + + $validator->checkIfDuplicated($course->id, $user->id, $question); + + $priority = $this->getPriority($course, $user); + + $consult = new ConsultModel(); + + $consult->question = $question; + $consult->private = $private; $consult->priority = $priority; $consult->course_id = $course->id; $consult->chapter_id = $chapter->id; diff --git a/app/Services/Logic/Live/LiveChapter.php b/app/Services/Logic/Live/LiveChapter.php index be734728..49a0df55 100644 --- a/app/Services/Logic/Live/LiveChapter.php +++ b/app/Services/Logic/Live/LiveChapter.php @@ -4,6 +4,7 @@ namespace App\Services\Logic\Live; use App\Services\Logic\ChapterTrait; use App\Services\Logic\Service; +use App\Validators\Live as LiveValidator; use GatewayClient\Gateway; class LiveChapter extends Service @@ -97,7 +98,9 @@ class LiveChapter extends Service $content = $this->request->getPost('content', ['trim', 'striptags']); - $content = kg_substr($content, 0, 80); + $validator = new LiveValidator(); + + $validator->checkMessage($content); Gateway::$registerAddress = $this->getRegisterAddress(); diff --git a/app/Services/Logic/Search/Course.php b/app/Services/Logic/Search/Course.php index 1b37f80c..0749bf1f 100644 --- a/app/Services/Logic/Search/Course.php +++ b/app/Services/Logic/Search/Course.php @@ -62,11 +62,11 @@ class Course extends Handler $items[] = [ 'id' => (int)$item['id'], - 'title' => $item['title'], - 'cover' => $item['cover'], - 'summary' => $item['summary'], - 'model' => $item['model'], - 'level' => $item['level'], + 'title' => (string)$item['title'], + 'cover' => (string)$item['cover'], + 'summary' => (string)$item['summary'], + 'model' => (int)$item['model'], + 'level' => (int)$item['level'], 'market_price' => (float)$item['market_price'], 'vip_price' => (float)$item['vip_price'], 'user_count' => (int)$item['user_count'], diff --git a/app/Services/Logic/Search/Group.php b/app/Services/Logic/Search/Group.php index bd956015..003520c3 100644 --- a/app/Services/Logic/Search/Group.php +++ b/app/Services/Logic/Search/Group.php @@ -62,10 +62,10 @@ class Group extends Handler $items[] = [ 'id' => (int)$item['id'], - 'type' => $item['type'], - 'name' => $item['name'], - 'avatar' => $item['avatar'], - 'about' => $item['about'], + 'type' => (int)$item['type'], + 'name' => (string)$item['name'], + 'avatar' => (string)$item['avatar'], + 'about' => (string)$item['about'], 'user_count' => (int)$item['user_count'], 'msg_count' => (int)$item['msg_count'], 'owner' => json_decode($item['owner'], true), diff --git a/app/Services/Logic/Search/User.php b/app/Services/Logic/Search/User.php index d08ce6a3..05e7be13 100644 --- a/app/Services/Logic/Search/User.php +++ b/app/Services/Logic/Search/User.php @@ -62,13 +62,13 @@ class User extends Handler $items[] = [ 'id' => (int)$item['id'], - 'name' => $item['name'], - 'avatar' => $item['avatar'], - 'title' => $item['title'], - 'about' => $item['about'], + 'name' => (string)$item['name'], + 'avatar' => (string)$item['avatar'], + 'title' => (string)$item['title'], + 'about' => (string)$item['about'], 'vip' => (int)$item['vip'], 'gender' => (int)$item['gender'], - 'area' => $item['area'], + 'area' => (string)$item['area'], ]; } diff --git a/app/Validators/ApiSecurity.php b/app/Validators/ApiSecurity.php deleted file mode 100644 index 855354bc..00000000 --- a/app/Validators/ApiSecurity.php +++ /dev/null @@ -1,133 +0,0 @@ -request->getQuery(); - - if (isset($query['_url'])) { - unset($query['_url']); - } - - $extra = [ - '_timestamp' => $this->checkTimestamp(), - '_nonce' => $this->checkNonce(), - ]; - - $appKey = $this->checkAppKey(); - - $app = $this->getApp($appKey); - - if (!$app || $app->published == 0) { - throw new BadRequestException('api.invalid_app_key'); - } - - $url = $this->getRequestUrl(); - - if ($this->request->getMethod() == 'POST') { - $mySignature = $this->httpPostSignature($url, $extra, $app->secret); - } else { - $params = array_merge($query, $extra); - $mySignature = $this->httpGetSignature($url, $params, $app->secret); - } - - $signature = $this->request->getHeader('X-Signature'); - - if ($signature != $mySignature) { - throw new BadRequestException('api.invalid_signature'); - } - - return $signature; - } - - protected function checkTimestamp() - { - $timestamp = $this->request->getHeader('X-Timestamp'); - - $timestamp = $timestamp > 0 ? $timestamp : 0; - - if (abs(time() - $timestamp) > 300) { - throw new BadRequestException('api.invalid_timestamp'); - } - - return $timestamp; - } - - protected function checkNonce() - { - $nonce = $this->request->getHeader('X-Nonce'); - - if (!$nonce) { - throw new BadRequestException('api.invalid_nonce'); - } - - return $nonce; - } - - protected function checkAppKey() - { - $appKey = $this->request->getHeader('X-App-Key'); - - if (!$appKey) { - throw new BadRequestException('api.invalid_app_key'); - } - - return $appKey; - } - - protected function checkPlatform() - { - $platform = $this->request->getHeader('X-Platform'); - - if (!array_key_exists($platform, AppModel::types())) { - throw new BadRequestException('api.invalid_platform'); - } - - return $platform; - } - - protected function getRequestUrl() - { - return sprintf('%s://%s%s', - $this->request->getScheme(), - $this->request->getHttpHost(), - $this->request->getURI() - ); - } - - protected function getApp($appKey) - { - $cache = new AppCache(); - - return $cache->get($appKey); - } - - protected function httpGetSignature($url, $params, $appSecret) - { - ksort($params); - - $query = http_build_query($params); - - return md5($url . $query . $appSecret); - } - - protected function httpPostSignature($url, $params, $appSecret) - { - ksort($params); - - $query = http_build_query($params); - - $body = $this->request->getRawBody(); - - return md5($url . $query . $body . $appSecret); - } - -} diff --git a/app/Validators/App.php b/app/Validators/App.php deleted file mode 100644 index 105fe914..00000000 --- a/app/Validators/App.php +++ /dev/null @@ -1,73 +0,0 @@ -findById($id); - - if (!$app) { - throw new BadRequestException('app.not_found'); - } - - return $app; - } - - public function checkName($name) - { - $value = $this->filter->sanitize($name, ['trim', 'string']); - - $length = kg_strlen($value); - - if ($length < 2) { - throw new BadRequestException('app.name_too_short'); - } - - if ($length > 50) { - throw new BadRequestException('app.name_too_long'); - } - - return $value; - } - - public function checkType($type) - { - if (!array_key_exists($type, AppModel::types())) { - throw new BadRequestException('app.invalid_type'); - } - - return $type; - } - - public function checkRemark($remark) - { - $value = $this->filter->sanitize($remark, ['trim', 'striptags']); - - $length = kg_strlen($value); - - if ($length > 255) { - throw new BadRequestException('app.remark_too_long'); - } - - return $value; - } - - public function checkPublishStatus($status) - { - if (!in_array($status, [0, 1])) { - throw new BadRequestException('app.invalid_publish_status'); - } - - return $status; - } - -} diff --git a/app/Validators/Consult.php b/app/Validators/Consult.php index 2bdb67a6..e100cc87 100644 --- a/app/Validators/Consult.php +++ b/app/Validators/Consult.php @@ -126,11 +126,11 @@ class Consult extends Validator } } - public function checkIfDuplicated($question, $chapterId, $userId) + public function checkIfDuplicated($chapterId, $userId, $question) { $repo = new ConsultRepo(); - $consult = $repo->findUserLastChapterConsult($chapterId, $userId); + $consult = $repo->findUserLastCourseConsult($chapterId, $userId); if (!$consult) return; diff --git a/app/Validators/Live.php b/app/Validators/Live.php new file mode 100644 index 00000000..d6fbe57d --- /dev/null +++ b/app/Validators/Live.php @@ -0,0 +1,27 @@ +filter->sanitize($content, ['trim', 'striptags']); + + $length = kg_strlen($value); + + if ($length < 1) { + throw new BadRequestException('live.msg_too_short'); + } + + if ($length > 255) { + throw new BadRequestException('live.msg_too_long'); + } + + return $value; + } + +} diff --git a/app/Validators/Security.php b/app/Validators/Security.php index 534dff3d..24d293fb 100644 --- a/app/Validators/Security.php +++ b/app/Validators/Security.php @@ -45,15 +45,4 @@ class Security extends Validator } } - public function checkApiSignature() - { - $validator = new ApiSecurity(); - - $result = $validator->check(); - - if (!$result) { - throw new BadRequestException('security.invalid_api_signature'); - } - } - } diff --git a/config/config.default.php b/config/config.default.php index 1b8d0d39..dcdf4097 100644 --- a/config/config.default.php +++ b/config/config.default.php @@ -157,6 +157,16 @@ $config['cors']['enabled'] = true; */ $config['cors']['allow_origin'] = '*'; +/** + * 允许跨域字段(string|array) + */ +$config['cors']['allow_headers'] = '*'; + +/** + * 允许跨域方法 + */ +$config['cors']['allow_methods'] = ['GET', 'POST', 'OPTIONS']; + /** * 限流开启 */