diff --git a/app/Console/Tasks/CleanLogTask.php b/app/Console/Tasks/CleanLogTask.php index 4e171edb..dc499431 100644 --- a/app/Console/Tasks/CleanLogTask.php +++ b/app/Console/Tasks/CleanLogTask.php @@ -2,8 +2,6 @@ namespace App\Console\Tasks; -use Phalcon\Cli\Task; - class CleanLogTask extends Task { @@ -165,9 +163,9 @@ class CleanLogTask extends Task if (strtotime($today) - strtotime($date) >= $keepDays * 86400) { $deleted = unlink($file); if ($deleted) { - echo "Delete {$file} success" . PHP_EOL; + echo "delete {$file} success" . PHP_EOL; } else { - echo "Delete {$file} failed" . PHP_EOL; + echo "delete {$file} failed" . PHP_EOL; } } } diff --git a/app/Console/Tasks/CleanSessionTask.php b/app/Console/Tasks/CleanSessionTask.php index 2be57e51..53b6f1f8 100644 --- a/app/Console/Tasks/CleanSessionTask.php +++ b/app/Console/Tasks/CleanSessionTask.php @@ -2,19 +2,13 @@ namespace App\Console\Tasks; -use App\Library\Cache\Backend\Redis as RedisCache; -use Phalcon\Cli\Task; -use Phalcon\Config; - class CleanSessionTask extends Task { public function mainAction() { $config = $this->getConfig(); - $cache = $this->getCache(); - $redis = $cache->getRedis(); $redis->select($config->path('session.db')); @@ -47,24 +41,4 @@ class CleanSessionTask extends Task return $cache->queryKeys('_PHCR', $limit); } - protected function getConfig() - { - /** - * @var Config $config - */ - $config = $this->getDI()->get('config'); - - return $config; - } - - protected function getCache() - { - /** - * @var RedisCache $cache - */ - $cache = $this->getDI()->get('cache'); - - return $cache; - } - } diff --git a/app/Console/Tasks/CloseOrderTask.php b/app/Console/Tasks/CloseOrderTask.php index b6818ba7..713d225c 100644 --- a/app/Console/Tasks/CloseOrderTask.php +++ b/app/Console/Tasks/CloseOrderTask.php @@ -3,7 +3,6 @@ namespace App\Console\Tasks; use App\Models\Order as OrderModel; -use Phalcon\Cli\Task; use Phalcon\Mvc\Model\Resultset; use Phalcon\Mvc\Model\ResultsetInterface; diff --git a/app/Console/Tasks/CloseTradeTask.php b/app/Console/Tasks/CloseTradeTask.php index 28665b6e..3918d40c 100644 --- a/app/Console/Tasks/CloseTradeTask.php +++ b/app/Console/Tasks/CloseTradeTask.php @@ -5,7 +5,6 @@ namespace App\Console\Tasks; use App\Models\Trade as TradeModel; use App\Services\Pay\Alipay as AlipayService; use App\Services\Pay\Wxpay as WxpayService; -use Phalcon\Cli\Task; use Phalcon\Mvc\Model\Resultset; use Phalcon\Mvc\Model\ResultsetInterface; diff --git a/app/Console/Tasks/CourseIndexTask.php b/app/Console/Tasks/CourseIndexTask.php index 050e50b5..23a148cc 100644 --- a/app/Console/Tasks/CourseIndexTask.php +++ b/app/Console/Tasks/CourseIndexTask.php @@ -5,7 +5,6 @@ namespace App\Console\Tasks; use App\Models\Course as CourseModel; use App\Services\Search\CourseDocument; use App\Services\Search\CourseSearcher; -use Phalcon\Cli\Task; use Phalcon\Mvc\Model\Resultset; use Phalcon\Mvc\Model\ResultsetInterface; @@ -24,7 +23,7 @@ class CourseIndexTask extends Task $query = $params[0] ?? null; if (!$query) { - exit("please special a query word" . PHP_EOL); + exit('please special a query word' . PHP_EOL); } $result = $this->searchCourses($query); @@ -61,11 +60,11 @@ class CourseIndexTask extends Task $index = $handler->getXS()->getIndex(); - echo "start clean index" . PHP_EOL; + echo 'start clean index' . PHP_EOL; $index->clean(); - echo "end clean index" . PHP_EOL; + echo 'end clean index' . PHP_EOL; } /** @@ -75,9 +74,7 @@ class CourseIndexTask extends Task { $courses = $this->findCourses(); - if ($courses->count() == 0) { - return; - } + if ($courses->count() == 0) return; $handler = new CourseSearcher(); @@ -85,7 +82,7 @@ class CourseIndexTask extends Task $index = $handler->getXS()->getIndex(); - echo "start rebuild index" . PHP_EOL; + echo 'start rebuild index' . PHP_EOL; $index->beginRebuild(); @@ -96,7 +93,7 @@ class CourseIndexTask extends Task $index->endRebuild(); - echo "end rebuild index" . PHP_EOL; + echo 'end rebuild index' . PHP_EOL; } /** diff --git a/app/Console/Tasks/DeliverTask.php b/app/Console/Tasks/DeliverTask.php index 5c39371d..a7ff11be 100644 --- a/app/Console/Tasks/DeliverTask.php +++ b/app/Console/Tasks/DeliverTask.php @@ -74,7 +74,6 @@ class DeliverTask extends Task $task->update(); $logger->info('Order Process Exception ' . kg_json_encode([ - 'line' => $e->getLine(), 'code' => $e->getCode(), 'message' => $e->getMessage(), 'task' => $task->toArray(), diff --git a/app/Console/Tasks/GroupIndexTask.php b/app/Console/Tasks/GroupIndexTask.php index 2cd79419..4e9c2918 100644 --- a/app/Console/Tasks/GroupIndexTask.php +++ b/app/Console/Tasks/GroupIndexTask.php @@ -5,7 +5,6 @@ namespace App\Console\Tasks; use App\Models\ImGroup as GroupModel; use App\Services\Search\GroupDocument; use App\Services\Search\GroupSearcher; -use Phalcon\Cli\Task; use Phalcon\Mvc\Model\Resultset; use Phalcon\Mvc\Model\ResultsetInterface; @@ -24,7 +23,7 @@ class GroupIndexTask extends Task $query = $params[0] ?? null; if (!$query) { - exit("please special a query word" . PHP_EOL); + exit('please special a query word' . PHP_EOL); } $result = $this->searchGroups($query); @@ -61,11 +60,11 @@ class GroupIndexTask extends Task $index = $handler->getXS()->getIndex(); - echo "start clean index" . PHP_EOL; + echo 'start clean index' . PHP_EOL; $index->clean(); - echo "end clean index" . PHP_EOL; + echo 'end clean index' . PHP_EOL; } /** @@ -75,9 +74,7 @@ class GroupIndexTask extends Task { $groups = $this->findGroups(); - if ($groups->count() == 0) { - return; - } + if ($groups->count() == 0) return; $handler = new GroupSearcher(); @@ -85,7 +82,7 @@ class GroupIndexTask extends Task $index = $handler->getXS()->getIndex(); - echo "start rebuild index" . PHP_EOL; + echo 'start rebuild index' . PHP_EOL; $index->beginRebuild(); @@ -96,7 +93,7 @@ class GroupIndexTask extends Task $index->endRebuild(); - echo "end rebuild index" . PHP_EOL; + echo 'end rebuild index' . PHP_EOL; } /** diff --git a/app/Console/Tasks/LiveNotifyTask.php b/app/Console/Tasks/LiveNotifyTask.php index b2f792c4..abd33a2a 100644 --- a/app/Console/Tasks/LiveNotifyTask.php +++ b/app/Console/Tasks/LiveNotifyTask.php @@ -2,54 +2,42 @@ namespace App\Console\Tasks; -use App\Library\Cache\Backend\Redis as RedisCache; use App\Models\CourseUser as CourseUserModel; use App\Repos\Chapter as ChapterRepo; use App\Services\LiveNotify as LiveNotifyService; use App\Services\Smser\Live as LiveSmser; -use Phalcon\Cli\Task; class LiveNotifyTask extends Task { - /** - * @var RedisCache - */ - protected $cache; - - /** - * @var \Redis - */ - protected $redis; - public function mainAction() { - $this->cache = $this->getDI()->get('cache'); + $cache = $this->getCache(); - $this->redis = $this->cache->getRedis(); + $redis = $cache->getRedis(); $service = new LiveNotifyService(); $key = $service->getNotifyKey(); - $chapterIds = $this->redis->sMembers($key); + $chapterIds = $redis->sMembers($key); if (!$chapterIds) return; $sentKey = $service->getSentNotifyKey(); - $sentChapterIds = $this->redis->sMembers($sentKey); + $sentChapterIds = $redis->sMembers($sentKey); foreach ($chapterIds as $chapterId) { if (!in_array($chapterId, $sentChapterIds)) { $this->sendNotification($chapterId); } else { - $this->redis->sAdd($sentKey, $chapterId); + $redis->sAdd($sentKey, $chapterId); } } - if ($this->redis->sCard($sentKey) == 1) { - $this->redis->expire($sentKey, 86400); + if ($redis->sCard($sentKey) == 1) { + $redis->expire($sentKey, 86400); } } @@ -74,9 +62,6 @@ class LiveNotifyTask extends Task protected function findTargetUserIds($courseId) { - /** - * 只给付费和vip用户发通知 - */ $sourceTypes = [ CourseUserModel::SOURCE_CHARGE, CourseUserModel::SOURCE_VIP, @@ -88,11 +73,11 @@ class LiveNotifyTask extends Task ->inWhere('source_type', $sourceTypes) ->execute(); - if ($rows->count() > 0) { - return kg_array_column($rows->toArray(), 'user_id'); + if ($rows->count() == 0) { + return []; } - return []; + return kg_array_column($rows->toArray(), 'user_id'); } } diff --git a/app/Console/Tasks/MaintainTask.php b/app/Console/Tasks/MaintainTask.php index f8b1d1e6..4b526448 100644 --- a/app/Console/Tasks/MaintainTask.php +++ b/app/Console/Tasks/MaintainTask.php @@ -2,149 +2,31 @@ namespace App\Console\Tasks; -use App\Caches\Setting as SettingCache; -use App\Library\Cache\Backend\Redis as RedisCache; -use App\Models\Setting as SettingModel; -use Phalcon\Cli\Task; -use Phalcon\Config; +use App\Caches\IndexFreeCourseList as IndexFreeCourseListCache; +use App\Caches\IndexNewCourseList as IndexNewCourseListCache; +use App\Caches\IndexVipCourseList as IndexVipCourseListCache; class MaintainTask extends Task { - public function mainAction() + public function rebuildIndexCourseCacheAction($params) { - $this->resetSettingAction(); - $this->resetAnnotationAction(); - $this->resetMetadataAction(); - $this->resetVoltAction(); - } + $section = $params[0] ?? null; - /** - * 重置设置 - * - * @command: php console.php maintain reset_setting - */ - public function resetSettingAction() - { - echo "start reset setting..." . PHP_EOL; - - $rows = SettingModel::query()->columns('section')->distinct(true)->execute(); - - foreach ($rows as $row) { - $cache = new SettingCache(); - $cache->rebuild($row->section); + if (!$section || $section == 'new_course') { + $cache = new IndexNewCourseListCache(); + $cache->rebuild(); } - echo "end reset setting..." . PHP_EOL; - } - - /** - * 重置注解 - * - * @command: php console.php maintain reset_annotation - */ - public function resetAnnotationAction() - { - $config = $this->getConfig(); - $cache = $this->getCache(); - $redis = $cache->getRedis(); - - $dbIndex = $config->path('annotation.db'); - $statsKey = $config->path('annotation.statsKey'); - - $redis->select($dbIndex); - - $keys = $redis->sMembers($statsKey); - - echo "start reset annotation..." . PHP_EOL; - - if (count($keys) > 0) { - - $keys = $this->handlePhKeys($keys); - - $redis->del(...$keys); - $redis->del($statsKey); + if (!$section || $section == 'free_course') { + $cache = new IndexFreeCourseListCache(); + $cache->rebuild(); } - echo "end reset annotation..." . PHP_EOL; - } - - /** - * 重置元数据 - * - * @command: php console.php maintain reset_metadata - */ - public function resetMetadataAction() - { - $config = $this->getConfig(); - $cache = $this->getCache(); - $redis = $cache->getRedis(); - - $dbIndex = $config->path('metadata.db'); - $statsKey = $config->path('metadata.statsKey'); - - $redis->select($dbIndex); - - $keys = $redis->sMembers($statsKey); - - echo "start reset metadata..." . PHP_EOL; - - if (count($keys) > 0) { - - $keys = $this->handlePhKeys($keys); - - $redis->del(...$keys); - $redis->del($statsKey); + if (!$section || $section == 'vip_course') { + $cache = new IndexVipCourseListCache(); + $cache->rebuild(); } - - echo "start reset metadata..." . PHP_EOL; - } - - /** - * 重置模板 - * - * @command: php console.php maintain reset_volt - */ - public function resetVoltAction() - { - echo "start reset volt..." . PHP_EOL; - - $dir = cache_path('volt'); - - foreach (scandir($dir) as $file) { - if (strpos($file, '.php')) { - unlink($dir . '/' . $file); - } - } - - echo "end reset volt..." . PHP_EOL; - } - - protected function getConfig() - { - /** - * @var Config $config - */ - $config = $this->getDI()->get('config'); - - return $config; - } - - protected function getCache() - { - /** - * @var RedisCache $cache - */ - $cache = $this->getDI()->get('cache'); - - return $cache; - } - - protected function handlePhKeys($keys) - { - return array_map(function ($key) { - return "_PHCR{$key}"; - }, $keys); } } diff --git a/app/Console/Tasks/RefundTask.php b/app/Console/Tasks/RefundTask.php index 0af0a715..434cfdfb 100644 --- a/app/Console/Tasks/RefundTask.php +++ b/app/Console/Tasks/RefundTask.php @@ -111,7 +111,6 @@ class RefundTask extends Task $task->update(); $logger->info('Refund Task Exception ' . kg_json_encode([ - 'line' => $e->getLine(), 'code' => $e->getCode(), 'message' => $e->getMessage(), 'task' => $task->toArray(), diff --git a/app/Console/Tasks/RevokeVipTask.php b/app/Console/Tasks/RevokeVipTask.php index 56a11e1d..58380c4b 100644 --- a/app/Console/Tasks/RevokeVipTask.php +++ b/app/Console/Tasks/RevokeVipTask.php @@ -3,7 +3,6 @@ namespace App\Console\Tasks; use App\Models\User as UserModel; -use Phalcon\Cli\Task; use Phalcon\Mvc\Model\Resultset; use Phalcon\Mvc\Model\ResultsetInterface; diff --git a/app/Console/Tasks/SiteMapTask.php b/app/Console/Tasks/SiteMapTask.php index b698b533..0d389e26 100644 --- a/app/Console/Tasks/SiteMapTask.php +++ b/app/Console/Tasks/SiteMapTask.php @@ -51,7 +51,7 @@ class SiteMapTask extends Task $settings = $service->getSectionSettings('site'); - return $settings['base_url'] ?? ''; + return $settings['url'] ?? ''; } protected function addIndex() @@ -62,7 +62,7 @@ class SiteMapTask extends Task protected function addCourses() { /** - * @var Resultset $courses + * @var Resultset|CourseModel[] $courses */ $courses = CourseModel::query()->where('published = 1')->execute(); diff --git a/app/Console/Tasks/SyncCourseIndexTask.php b/app/Console/Tasks/SyncCourseIndexTask.php index 8911734d..a3b9f8cc 100644 --- a/app/Console/Tasks/SyncCourseIndexTask.php +++ b/app/Console/Tasks/SyncCourseIndexTask.php @@ -2,7 +2,6 @@ namespace App\Console\Tasks; -use App\Library\Cache\Backend\Redis as RedisCache; use App\Repos\Course as CourseRepo; use App\Services\Search\CourseDocument; use App\Services\Search\CourseSearcher; @@ -11,30 +10,15 @@ use App\Services\Syncer\CourseIndex as CourseIndexSyncer; class SyncCourseIndexTask extends Task { - /** - * @var RedisCache - */ - protected $cache; - - /** - * @var \Redis - */ - protected $redis; - public function mainAction() { - $this->cache = $this->getDI()->get('cache'); + $cache = $this->getCache(); - $this->redis = $this->cache->getRedis(); + $redis = $cache->getRedis(); - $this->rebuild(); - } - - protected function rebuild() - { $key = $this->getSyncKey(); - $courseIds = $this->redis->sRandMember($key, 1000); + $courseIds = $redis->sRandMember($key, 1000); if (!$courseIds) return; @@ -42,9 +26,7 @@ class SyncCourseIndexTask extends Task $courses = $courseRepo->findByIds($courseIds); - if ($courses->count() == 0) { - return; - } + if ($courses->count() == 0) return; $document = new CourseDocument(); @@ -67,7 +49,7 @@ class SyncCourseIndexTask extends Task $index->closeBuffer(); - $this->redis->sRem($key, ...$courseIds); + $redis->sRem($key, ...$courseIds); } protected function getSyncKey() diff --git a/app/Console/Tasks/SyncGroupIndexTask.php b/app/Console/Tasks/SyncGroupIndexTask.php index ebb81e2f..2f33e9b6 100644 --- a/app/Console/Tasks/SyncGroupIndexTask.php +++ b/app/Console/Tasks/SyncGroupIndexTask.php @@ -2,7 +2,6 @@ namespace App\Console\Tasks; -use App\Library\Cache\Backend\Redis as RedisCache; use App\Repos\ImGroup as GroupRepo; use App\Services\Search\GroupDocument; use App\Services\Search\GroupSearcher; @@ -11,30 +10,15 @@ use App\Services\Syncer\GroupIndex as GroupIndexSyncer; class SyncGroupIndexTask extends Task { - /** - * @var RedisCache - */ - protected $cache; - - /** - * @var \Redis - */ - protected $redis; - public function mainAction() { - $this->cache = $this->getDI()->get('cache'); + $cache = $this->getCache(); - $this->redis = $this->cache->getRedis(); + $redis = $cache->getRedis(); - $this->rebuild(); - } - - protected function rebuild() - { $key = $this->getSyncKey(); - $groupIds = $this->redis->sRandMember($key, 1000); + $groupIds = $redis->sRandMember($key, 1000); if (!$groupIds) return; @@ -42,9 +26,7 @@ class SyncGroupIndexTask extends Task $groups = $groupRepo->findByIds($groupIds); - if ($groups->count() == 0) { - return; - } + if ($groups->count() == 0) return; $document = new GroupDocument(); @@ -67,7 +49,7 @@ class SyncGroupIndexTask extends Task $index->closeBuffer(); - $this->redis->sRem($key, ...$groupIds); + $redis->sRem($key, ...$groupIds); } protected function getSyncKey() diff --git a/app/Console/Tasks/SyncLearningTask.php b/app/Console/Tasks/SyncLearningTask.php index d44006c5..5c82b879 100644 --- a/app/Console/Tasks/SyncLearningTask.php +++ b/app/Console/Tasks/SyncLearningTask.php @@ -2,7 +2,6 @@ namespace App\Console\Tasks; -use App\Library\Cache\Backend\Redis as RedisCache; use App\Models\Course as CourseModel; use App\Models\Learning as LearningModel; use App\Repos\Chapter as ChapterRepo; @@ -11,32 +10,21 @@ use App\Repos\Course as CourseRepo; use App\Repos\CourseUser as CourseUserRepo; use App\Repos\Learning as LearningRepo; use App\Services\Syncer\Learning as LearningSyncer; -use Phalcon\Cli\Task; class SyncLearningTask extends Task { - /** - * @var RedisCache - */ - protected $cache; - - /** - * @var \Redis - */ - protected $redis; - public function mainAction() { - $this->cache = $this->getDI()->get('cache'); + $cache = $this->getCache(); - $this->redis = $this->cache->getRedis(); + $redis = $cache->getRedis(); $syncer = new LearningSyncer(); $syncKey = $syncer->getSyncKey(); - $requestIds = $this->redis->sMembers($syncKey); + $requestIds = $redis->sMembers($syncKey); if (!$requestIds) return; @@ -46,7 +34,7 @@ class SyncLearningTask extends Task $this->handleLearning($itemKey); - $this->redis->sRem($syncKey, $requestId); + $redis->sRem($syncKey, $requestId); } } @@ -153,15 +141,11 @@ class SyncLearningTask extends Task $courseLessons = $courseRepo->findLessons($learning->course_id); - if ($courseLessons->count() == 0) { - return; - } + if ($courseLessons->count() == 0) return; $userLearnings = $courseRepo->findUserLearnings($learning->course_id, $learning->user_id, $learning->plan_id); - if ($userLearnings->count() == 0) { - return; - } + if ($userLearnings->count() == 0) return; $consumedUserLearnings = []; @@ -171,9 +155,7 @@ class SyncLearningTask extends Task } } - if (count($consumedUserLearnings) == 0) { - return; - } + if (count($consumedUserLearnings) == 0) return; $duration = 0; diff --git a/app/Console/Tasks/SyncUserIndexTask.php b/app/Console/Tasks/SyncUserIndexTask.php index bd7c8aea..9ddb2fb4 100644 --- a/app/Console/Tasks/SyncUserIndexTask.php +++ b/app/Console/Tasks/SyncUserIndexTask.php @@ -2,7 +2,6 @@ namespace App\Console\Tasks; -use App\Library\Cache\Backend\Redis as RedisCache; use App\Repos\User as UserRepo; use App\Services\Search\UserDocument; use App\Services\Search\UserSearcher; @@ -11,30 +10,15 @@ use App\Services\Syncer\UserIndex as UserIndexSyncer; class SyncUserIndexTask extends Task { - /** - * @var RedisCache - */ - protected $cache; - - /** - * @var \Redis - */ - protected $redis; - public function mainAction() { - $this->cache = $this->getDI()->get('cache'); + $cache = $this->getCache(); - $this->redis = $this->cache->getRedis(); + $redis = $cache->getRedis(); - $this->rebuild(); - } - - protected function rebuild() - { $key = $this->getSyncKey(); - $userIds = $this->redis->sRandMember($key, 1000); + $userIds = $redis->sRandMember($key, 1000); if (!$userIds) return; @@ -42,9 +26,7 @@ class SyncUserIndexTask extends Task $users = $userRepo->findByIds($userIds); - if ($users->count() == 0) { - return; - } + if ($users->count() == 0) return; $document = new UserDocument(); @@ -67,7 +49,7 @@ class SyncUserIndexTask extends Task $index->closeBuffer(); - $this->redis->sRem($key, ...$userIds); + $redis->sRem($key, ...$userIds); } protected function getSyncKey() diff --git a/app/Console/Tasks/Task.php b/app/Console/Tasks/Task.php index dc875cd1..b2b8cc79 100644 --- a/app/Console/Tasks/Task.php +++ b/app/Console/Tasks/Task.php @@ -2,14 +2,38 @@ namespace App\Console\Tasks; -use App\Library\Logger; +use App\Library\Cache\Backend\Redis as RedisCache; +use App\Library\Logger as AppLogger; +use Phalcon\Config as PhConfig; +use Phalcon\Logger\Adapter\File as PhLogger; + class Task extends \Phalcon\Cli\Task { + /** + * @return PhConfig + */ + public function getConfig() + { + return $this->getDI()->get('config'); + } + + /** + * @return RedisCache + */ + public function getCache() + { + return $this->getDI()->get('cache'); + } + + /** + * @param null $channel + * @return PhLogger + */ public function getLogger($channel = null) { - $logger = new Logger(); + $logger = new AppLogger(); return $logger->getInstance($channel); } diff --git a/app/Console/Tasks/UnlockUserTask.php b/app/Console/Tasks/UnlockUserTask.php index 0f21f6a0..d499c8f8 100644 --- a/app/Console/Tasks/UnlockUserTask.php +++ b/app/Console/Tasks/UnlockUserTask.php @@ -3,7 +3,6 @@ namespace App\Console\Tasks; use App\Models\User as UserModel; -use Phalcon\Cli\Task; use Phalcon\Mvc\Model\Resultset; use Phalcon\Mvc\Model\ResultsetInterface; @@ -14,13 +13,10 @@ class UnlockUserTask extends Task { $users = $this->findUsers(); - if ($users->count() == 0) { - return; - } + if ($users->count() == 0) return; foreach ($users as $user) { - $user->locked = 0; - $user->update(); + $user->update(['locked' => 0]); } } diff --git a/app/Console/Tasks/UpgradeTask.php b/app/Console/Tasks/UpgradeTask.php new file mode 100644 index 00000000..913696b1 --- /dev/null +++ b/app/Console/Tasks/UpgradeTask.php @@ -0,0 +1,127 @@ +resetSettingAction(); + $this->resetAnnotationAction(); + $this->resetMetadataAction(); + $this->resetVoltAction(); + } + + /** + * 重置系统设置 + * + * @command: php console.php upgrade reset_setting + */ + public function resetSettingAction() + { + echo "start reset setting..." . PHP_EOL; + + $rows = SettingModel::query()->columns('section')->distinct(true)->execute(); + + foreach ($rows as $row) { + $cache = new SettingCache(); + $cache->rebuild($row->section); + } + + echo "end reset setting..." . PHP_EOL; + } + + /** + * 重置注解 + * + * @command: php console.php upgrade reset_annotation + */ + public function resetAnnotationAction() + { + $config = $this->getConfig(); + $cache = $this->getCache(); + $redis = $cache->getRedis(); + + $dbIndex = $config->path('annotation.db'); + $statsKey = $config->path('annotation.statsKey'); + + $redis->select($dbIndex); + + $keys = $redis->sMembers($statsKey); + + echo "start reset annotation..." . PHP_EOL; + + if (count($keys) > 0) { + + $keys = $this->handlePhKeys($keys); + + $redis->del(...$keys); + $redis->del($statsKey); + } + + echo "end reset annotation..." . PHP_EOL; + } + + /** + * 重置元数据 + * + * @command: php console.php upgrade reset_metadata + */ + public function resetMetadataAction() + { + $config = $this->getConfig(); + $cache = $this->getCache(); + $redis = $cache->getRedis(); + + $dbIndex = $config->path('metadata.db'); + $statsKey = $config->path('metadata.statsKey'); + + $redis->select($dbIndex); + + $keys = $redis->sMembers($statsKey); + + echo "start reset metadata..." . PHP_EOL; + + if (count($keys) > 0) { + + $keys = $this->handlePhKeys($keys); + + $redis->del(...$keys); + $redis->del($statsKey); + } + + echo "start reset metadata..." . PHP_EOL; + } + + /** + * 重置模板 + * + * @command: php console.php upgrade reset_volt + */ + public function resetVoltAction() + { + echo "start reset volt..." . PHP_EOL; + + $dir = cache_path('volt'); + + foreach (scandir($dir) as $file) { + if (strpos($file, '.php')) { + unlink($dir . '/' . $file); + } + } + + echo "end reset volt..." . PHP_EOL; + } + + protected function handlePhKeys($keys) + { + return array_map(function ($key) { + return "_PHCR{$key}"; + }, $keys); + } + +} diff --git a/app/Console/Tasks/UserIndexTask.php b/app/Console/Tasks/UserIndexTask.php index 3ec4ce51..6ce9c7d6 100644 --- a/app/Console/Tasks/UserIndexTask.php +++ b/app/Console/Tasks/UserIndexTask.php @@ -5,7 +5,6 @@ namespace App\Console\Tasks; use App\Models\User as UserModel; use App\Services\Search\UserDocument; use App\Services\Search\UserSearcher; -use Phalcon\Cli\Task; use Phalcon\Mvc\Model\Resultset; use Phalcon\Mvc\Model\ResultsetInterface; @@ -24,7 +23,7 @@ class UserIndexTask extends Task $query = $params[0] ?? null; if (!$query) { - exit("please special a query word" . PHP_EOL); + exit('please special a query word' . PHP_EOL); } $result = $this->searchUsers($query); @@ -61,11 +60,11 @@ class UserIndexTask extends Task $index = $handler->getXS()->getIndex(); - echo "start clean index" . PHP_EOL; + echo 'start clean index' . PHP_EOL; $index->clean(); - echo "end clean index" . PHP_EOL; + echo 'end clean index' . PHP_EOL; } /** @@ -75,9 +74,7 @@ class UserIndexTask extends Task { $users = $this->findUsers(); - if ($users->count() == 0) { - return; - } + if ($users->count() == 0) return; $handler = new UserSearcher(); @@ -85,7 +82,7 @@ class UserIndexTask extends Task $index = $handler->getXS()->getIndex(); - echo "start rebuild index" . PHP_EOL; + echo 'start rebuild index' . PHP_EOL; $index->beginRebuild(); @@ -96,7 +93,7 @@ class UserIndexTask extends Task $index->endRebuild(); - echo "end rebuild index" . PHP_EOL; + echo 'end rebuild index' . PHP_EOL; } /** diff --git a/app/Console/Tasks/VodEventTask.php b/app/Console/Tasks/VodEventTask.php index 07795c5b..c51e0c1d 100644 --- a/app/Console/Tasks/VodEventTask.php +++ b/app/Console/Tasks/VodEventTask.php @@ -6,7 +6,6 @@ use App\Models\Chapter as ChapterModel; use App\Repos\Chapter as ChapterRepo; use App\Services\CourseStat as CourseStatService; use App\Services\Vod as VodService; -use Phalcon\Cli\Task; class VodEventTask extends Task { diff --git a/app/Http/Desktop/Controllers/Controller.php b/app/Http/Desktop/Controllers/Controller.php index 287c3cb1..add4d228 100644 --- a/app/Http/Desktop/Controllers/Controller.php +++ b/app/Http/Desktop/Controllers/Controller.php @@ -10,6 +10,7 @@ use App\Models\User as UserModel; use App\Services\Auth\Desktop as DesktopAuth; use App\Traits\Response as ResponseTrait; use App\Traits\Security as SecurityTrait; +use Phalcon\Config; use Phalcon\Mvc\Dispatcher; class Controller extends \Phalcon\Mvc\Controller @@ -120,13 +121,32 @@ class Controller extends \Phalcon\Mvc\Controller { $cache = new SettingCache(); + $websocket = $this->getConfig()->get('websocket'); + + /** + * ssl通过nginx转发实现 + */ + if ($this->request->isSecure()) { + $websocket->connect_url = sprintf('wss://%s/wss', $this->request->getHttpHost()); + } else { + $websocket->connect_url = sprintf('ws://%s', $websocket->connect_address); + } + return [ 'main' => $cache->get('im.main'), 'cs' => $cache->get('im.cs'), - 'websocket' => $this->config->websocket, + 'websocket' => $websocket, ]; } + /** + * @return Config + */ + protected function getConfig() + { + return $this->getDI()->get('config'); + } + protected function checkSiteStatus() { if ($this->siteInfo['enabled'] == 0) { diff --git a/app/Http/Desktop/Controllers/SearchController.php b/app/Http/Desktop/Controllers/SearchController.php index de036304..c7a8b7b1 100644 --- a/app/Http/Desktop/Controllers/SearchController.php +++ b/app/Http/Desktop/Controllers/SearchController.php @@ -21,7 +21,7 @@ class SearchController extends Controller public function indexAction() { $query = $this->request->get('query', ['trim', 'string']); - $type = $this->request->get('type', ['trim'], 'course'); + $type = $this->request->get('type', ['trim', 'string'], 'course'); if (empty($query)) { return $this->response->redirect(['for' => 'desktop.course.list']); @@ -31,9 +31,9 @@ class SearchController extends Controller $service = $this->getSearchService($type); - $hotQueries = $service->hotQuery(); + $hotQueries = $service->getHotQuery(); - $relatedQueries = $service->relatedQuery($query); + $relatedQueries = $service->getRelatedQuery($query); $pager = $service->search(); diff --git a/app/Http/Desktop/Services/Im.php b/app/Http/Desktop/Services/Im.php index c5ebbe32..04a1caf7 100644 --- a/app/Http/Desktop/Services/Im.php +++ b/app/Http/Desktop/Services/Im.php @@ -341,9 +341,9 @@ class Im extends Service protected function getRegisterAddress() { - $config = $this->getDI()->get('config'); + $config = $this->getConfig(); - return $config->websocket->register_address; + return $config->path('websocket.register_address'); } } diff --git a/app/Http/Desktop/Services/Live.php b/app/Http/Desktop/Services/Live.php index c5ba0c10..0b43410f 100644 --- a/app/Http/Desktop/Services/Live.php +++ b/app/Http/Desktop/Services/Live.php @@ -15,7 +15,7 @@ class Live extends Service { $redis = $this->getRedis(); - $key = $this->getRedisListKey($id); + $key = $this->getRecentChatKey($id); $redis->expire($key, 3 * 3600); @@ -132,12 +132,12 @@ class Live extends Service protected function getRegisterAddress() { - $config = $this->getDI()->get('config'); + $config = $this->getConfig(); - return $config->websocket->register_address; + return $config->path('websocket.register_address'); } - protected function getRedisListKey($id) + protected function getRecentChatKey($id) { return "live_recent_chat:{$id}"; } diff --git a/app/Http/Desktop/Views/partials/js_vars.volt b/app/Http/Desktop/Views/partials/js_vars.volt index 0bf820b7..dcdd59e7 100644 --- a/app/Http/Desktop/Views/partials/js_vars.volt +++ b/app/Http/Desktop/Views/partials/js_vars.volt @@ -21,7 +21,8 @@ enabled: '{{ im_info.cs.enabled }}' }, websocket: { - url: '{{ im_info.websocket.url }}' + connect_url: '{{ im_info.websocket.connect_url }}', + ping_interval: '{{ im_info.websocket.ping_interval }}' } }; diff --git a/app/Library/Logger.php b/app/Library/Logger.php index 38824ecf..24b0202f 100644 --- a/app/Library/Logger.php +++ b/app/Library/Logger.php @@ -3,7 +3,7 @@ namespace App\Library; use Phalcon\Di; -use Phalcon\Logger as PhalconLogger; +use Phalcon\Logger as PhLogger; use Phalcon\Logger\Adapter\File as FileLogger; class Logger @@ -23,7 +23,7 @@ class Logger $path = log_path($filename); - $level = $config->env != ENV_DEV ? $config->log->level : PhalconLogger::DEBUG; + $level = $config->env != ENV_DEV ? $config->log->level : PhLogger::DEBUG; $logger = new FileLogger($path); diff --git a/app/Services/Frontend/Search/Course.php b/app/Services/Frontend/Search/Course.php index 3bba34c1..12907727 100644 --- a/app/Services/Frontend/Search/Course.php +++ b/app/Services/Frontend/Search/Course.php @@ -4,10 +4,9 @@ namespace App\Services\Frontend\Search; use App\Library\Paginator\Adapter\XunSearch as XunSearchPaginator; use App\Library\Paginator\Query as PagerQuery; -use App\Services\Frontend\Service as FrontendService; use App\Services\Search\CourseSearcher as CourseSearcherService; -class Course extends FrontendService +class Course extends Handler { public function search() @@ -33,14 +32,14 @@ class Course extends FrontendService return $this->handleCourses($pager); } - public function hotQuery($limit = 10, $type = 'total') + public function getHotQuery($limit = 10, $type = 'total') { $searcher = new CourseSearcherService(); return $searcher->getHotQuery($limit, $type); } - public function relatedQuery($query, $limit = 10) + public function getRelatedQuery($query, $limit = 10) { $searcher = new CourseSearcherService(); diff --git a/app/Services/Frontend/Search/Group.php b/app/Services/Frontend/Search/Group.php index 85ca0d7f..527cf362 100644 --- a/app/Services/Frontend/Search/Group.php +++ b/app/Services/Frontend/Search/Group.php @@ -4,10 +4,9 @@ namespace App\Services\Frontend\Search; use App\Library\Paginator\Adapter\XunSearch as XunSearchPaginator; use App\Library\Paginator\Query as PagerQuery; -use App\Services\Frontend\Service as FrontendService; use App\Services\Search\GroupSearcher as GroupSearcherService; -class Group extends FrontendService +class Group extends Handler { public function search() @@ -33,14 +32,14 @@ class Group extends FrontendService return $this->handleGroups($pager); } - public function hotQuery($limit = 10, $type = 'total') + public function getHotQuery($limit = 10, $type = 'total') { $searcher = new GroupSearcherService(); return $searcher->getHotQuery($limit, $type); } - public function relatedQuery($query, $limit = 10) + public function getRelatedQuery($query, $limit = 10) { $searcher = new GroupSearcherService(); diff --git a/app/Services/Frontend/Search/Handler.php b/app/Services/Frontend/Search/Handler.php new file mode 100644 index 00000000..1ea5e77f --- /dev/null +++ b/app/Services/Frontend/Search/Handler.php @@ -0,0 +1,16 @@ +handleUsers($pager); } - public function hotQuery($limit = 10, $type = 'total') + public function getHotQuery($limit = 10, $type = 'total') { $searcher = new UserSearcherService(); return $searcher->getHotQuery($limit, $type); } - public function relatedQuery($query, $limit = 10) + public function getRelatedQuery($query, $limit = 10) { $searcher = new UserSearcherService(); diff --git a/app/Services/Service.php b/app/Services/Service.php index 8604bbe5..0f1c3964 100644 --- a/app/Services/Service.php +++ b/app/Services/Service.php @@ -3,9 +3,11 @@ namespace App\Services; use App\Caches\Setting as SettingCache; +use App\Library\Cache\Backend\Redis as RedisCache; use App\Library\Logger as AppLogger; use App\Traits\Auth as AuthTrait; -use Phalcon\Logger\Adapter\File as FileLogger; +use Phalcon\Config as PhConfig; +use Phalcon\Logger\Adapter\File as PhLogger; use Phalcon\Mvc\User\Component; class Service extends Component @@ -13,11 +15,27 @@ class Service extends Component use AuthTrait; + /** + * @return PhConfig + */ + public function getConfig() + { + return $this->getDI()->get('config'); + } + + /** + * @return RedisCache + */ + public function getCache() + { + return $this->getDI()->get('cache'); + } + /** * 获取Logger * * @param string $channel - * @return FileLogger + * @return PhLogger */ public function getLogger($channel = null) { diff --git a/config/config.default.php b/config/config.default.php index a108f80a..8fd72d5e 100644 --- a/config/config.default.php +++ b/config/config.default.php @@ -163,12 +163,17 @@ $config['throttle']['lifetime'] = 60; $config['throttle']['rate_limit'] = 60; /** - * 客户端连接地址(外部可访问的ip或域名) + * 客户端ping服务端间隔(秒) */ -$config['websocket']['url'] = 'ws://127.0.0.1:8282'; +$config['websocket']['ping_interval'] = 50; /** - * gateway和worker注册地址(内部访问) + * 客户端连接地址(外部可访问的域名或ip),带端口号 + */ +$config['websocket']['connect_address'] = 'your_domain.com:8282'; + +/** + * gateway和worker注册地址,带端口号 */ $config['websocket']['register_address'] = '127.0.0.1:1238'; diff --git a/public/static/desktop/js/chapter.live.chat.js b/public/static/desktop/js/chapter.live.chat.js index 625dfc52..92376a41 100644 --- a/public/static/desktop/js/chapter.live.chat.js +++ b/public/static/desktop/js/chapter.live.chat.js @@ -3,7 +3,7 @@ layui.use(['jquery', 'form', 'helper'], function () { var $ = layui.jquery; var form = layui.form; var helper = layui.helper; - var socket = new WebSocket(window.im.websocket.url); + var socket = new WebSocket(window.im.websocket.connect_url); var bindUserUrl = $('input[name="bind_user_url"]').val(); var liveStatsUrl = $('input[name="live_stats_url"]').val(); var $chatContent = $('input[name=content]'); @@ -13,7 +13,7 @@ layui.use(['jquery', 'form', 'helper'], function () { console.log('socket connect success'); setInterval(function () { socket.send('ping'); - }, 30000); + }, 1000 * parseInt(window.im.websocket.ping_interval)); }; socket.onclose = function () { diff --git a/public/static/desktop/js/im.cs.js b/public/static/desktop/js/im.cs.js index e2c821ea..353490c5 100644 --- a/public/static/desktop/js/im.cs.js +++ b/public/static/desktop/js/im.cs.js @@ -16,10 +16,13 @@ layui.use(['jquery', 'layim'], function () { welcome: $('input[name="cs_user.welcome"]').val() }; - var socket = new WebSocket(window.im.websocket.url); + var socket = new WebSocket(window.im.websocket.connect_url); socket.onopen = function () { console.log('socket connect success'); + setInterval(function () { + socket.send('ping'); + }, 1000 * parseInt(window.im.websocket.ping_interval)); }; socket.onclose = function () { @@ -33,9 +36,7 @@ layui.use(['jquery', 'layim'], function () { socket.onmessage = function (e) { var data = JSON.parse(e.data); console.log(data); - if (data.type === 'ping') { - socket.send('pong...'); - } else if (data.type === 'bind_user') { + if (data.type === 'bind_user') { bindUser(data); } else if (data.type === 'show_chat_msg') { showChatMessage(data); @@ -48,18 +49,18 @@ layui.use(['jquery', 'layim'], function () { id: me.id, username: me.name, avatar: me.avatar, - status: 'online', + status: 'online' } }, brief: true, - maxLength: 1000, + maxLength: window.im.main.msg_max_length }); layim.chat({ id: csUser.id, name: csUser.name, avatar: csUser.avatar, - type: 'friend', + type: 'friend' }); layim.on('sendMessage', function (res) { @@ -95,7 +96,7 @@ layui.use(['jquery', 'layim'], function () { avatar: csUser.avatar, content: csUser.welcome, timestamp: new Date().getTime(), - type: 'friend', + type: 'friend' }); } diff --git a/public/static/desktop/js/im.js b/public/static/desktop/js/im.js index dfafbd71..e03d6c2f 100644 --- a/public/static/desktop/js/im.js +++ b/public/static/desktop/js/im.js @@ -2,13 +2,14 @@ layui.use(['jquery', 'layim'], function () { var $ = layui.jquery; var layim = layui.layim; - var socket = new WebSocket(window.im.websocket.url); + var socket = new WebSocket(window.im.websocket.connect_url); socket.onopen = function () { console.log('socket connect success'); setInterval(function () { socket.send('ping'); - }, 30000); + console.log('ping...'); + }, 1000 * parseInt(window.im.websocket.ping_interval)); }; socket.onclose = function () { diff --git a/websocket/start_business_worker.php b/websocket/start_business_worker.php index e0132056..0ae0c96d 100644 --- a/websocket/start_business_worker.php +++ b/websocket/start_business_worker.php @@ -23,7 +23,7 @@ require_once dirname(__DIR__) . '/vendor/autoload.php'; $worker = new BusinessWorker(); // worker名称 -$worker->name = 'ChatBusinessWorker'; +$worker->name = 'ImBusinessWorker'; // businessWorker进程数量 $worker->count = 4; diff --git a/websocket/start_gateway.php b/websocket/start_gateway.php index 313903b4..b2e2145f 100644 --- a/websocket/start_gateway.php +++ b/websocket/start_gateway.php @@ -23,7 +23,7 @@ require_once dirname(__DIR__) . '/vendor/autoload.php'; $gateway = new Gateway("websocket://0.0.0.0:8282"); // gateway名称,status方便查看 -$gateway->name = 'ChatGateway'; +$gateway->name = 'ImGateway'; // gateway进程数 $gateway->count = 4;