From 7109eb02385547e178e8ec4ff446e551ec994c6a Mon Sep 17 00:00:00 2001 From: kuaifan Date: Wed, 1 Feb 2023 12:13:25 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=8A=E7=8F=AD=E6=89=93=E5=BC=80?= =?UTF-8?q?=E6=AF=8F=E6=97=A5=E5=BC=80=E5=BF=83/=E4=B8=8B=E7=8F=AD?= =?UTF-8?q?=E6=89=93=E5=8D=A1=E5=BF=83=E7=81=B5=E9=B8=A1=E6=B1=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.docker | 3 + app/Http/Controllers/Api/PublicController.php | 24 ++++--- app/Http/Controllers/IndexController.php | 6 +- app/Tasks/JokeSoupTask.php | 71 +++++++++++++++++++ app/Tasks/JokeTask.php | 51 ------------- 5 files changed, 90 insertions(+), 65 deletions(-) create mode 100644 app/Tasks/JokeSoupTask.php delete mode 100644 app/Tasks/JokeTask.php diff --git a/.env.docker b/.env.docker index ef9a63b14..50370288d 100644 --- a/.env.docker +++ b/.env.docker @@ -53,6 +53,9 @@ PUSHER_APP_KEY= PUSHER_APP_SECRET= PUSHER_APP_CLUSTER=mt1 +JUKE_KEY_JOKE= +JUKE_KEY_SOUP= + MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}" MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}" diff --git a/app/Http/Controllers/Api/PublicController.php b/app/Http/Controllers/Api/PublicController.php index 69bbd8d8e..ab6355ce1 100755 --- a/app/Http/Controllers/Api/PublicController.php +++ b/app/Http/Controllers/Api/PublicController.php @@ -129,17 +129,19 @@ class PublicController extends AbstractController } // if ($checkins && $botUser = User::botGetOrCreate('check-in')) { - $getJoke = function() { - $jokes = Base::json2array(Cache::get("JokeTask:rands")); - if ($jokes) { - $jokev = $jokes[array_rand($jokes)]; - if ($jokev) { - return $jokev; + $getJokeSoup = function($type) { + $pre = $type == "up" ? "每日开心:" : "心灵鸡汤:"; + $key = $type == "up" ? "JokeSoupTask:jokes" : "JokeSoupTask:soups"; + $array = Base::json2array(Cache::get($key)); + if ($array) { + $item = $array[array_rand($array)]; + if ($item) { + return $pre . $item; } } return null; }; - $sendMsg = function($type, UserCheckinMac $checkin) use ($getJoke, $botUser, $nowDate) { + $sendMsg = function($type, UserCheckinMac $checkin) use ($getJokeSoup, $botUser, $nowDate) { $cacheKey = "Checkin::sendMsg-{$nowDate}-{$type}:" . $checkin->userid; if (Cache::get($cacheKey) === "yes") { return; @@ -151,10 +153,10 @@ class PublicController extends AbstractController $hi = date("H:i"); $pre = $type == "up" ? "上班" : "下班"; $remark = $checkin->remark ? " ({$checkin->remark})": ""; - $text = "

{$pre}打卡成功,打卡时间: {$hi} {$remark}

"; - $joke = $getJoke(); - if ($joke) { - $text = "
{$text}

----------

每日开心:{$joke}。

"; + $text = "

{$pre}打卡成功,打卡时间: {$hi}{$remark}

"; + $suff = $getJokeSoup($type); + if ($suff) { + $text = "{$text}

----------

{$suff}

"; } WebSocketDialogMsg::sendMsg(null, $dialog->id, 'text', ['text' => $text], $botUser->userid); } diff --git a/app/Http/Controllers/IndexController.php b/app/Http/Controllers/IndexController.php index f4a3832c6..472171561 100755 --- a/app/Http/Controllers/IndexController.php +++ b/app/Http/Controllers/IndexController.php @@ -10,7 +10,7 @@ use App\Tasks\AppPushTask; use App\Tasks\AutoArchivedTask; use App\Tasks\DeleteTmpTask; use App\Tasks\EmailNoticeTask; -use App\Tasks\JokeTask; +use App\Tasks\JokeSoupTask; use App\Tasks\LoopTask; use Arr; use Cache; @@ -191,8 +191,8 @@ class IndexController extends InvokeController Task::deliver(new DeleteTmpTask('tmp', 24)); // 周期任务 Task::deliver(new LoopTask()); - // 获取笑话 - Task::deliver(new JokeTask()); + // 获取笑话/心灵鸡汤 + Task::deliver(new JokeSoupTask()); return "success"; } diff --git a/app/Tasks/JokeSoupTask.php b/app/Tasks/JokeSoupTask.php new file mode 100644 index 000000000..e2e45a773 --- /dev/null +++ b/app/Tasks/JokeSoupTask.php @@ -0,0 +1,71 @@ +addDay()); + // + $jokeKey = env("JUKE_KEY_JOKE"); + if ($jokeKey) { + $array = Base::json2array(Cache::get("JokeSoupTask:jokes")); + $res = Ihttp::ihttp_get("http://v.juhe.cn/joke/randJoke.php?key=" . $jokeKey); + if (Base::isSuccess($res)) { + $data = Base::json2array($res['data']); + if ($data['reason'] === 'success') { + foreach ($data['result'] as $item) { + if ($text = trim($item['content'])) { + $array[] = $text; + } + } + } + } + Cache::forever("JokeSoupTask:jokes", Base::array2json(array_slice($array, -100))); + } + // + $soupKey = env("JUKE_KEY_SOUP"); + if ($soupKey) { + $array = Base::json2array(Cache::get("JokeSoupTask:soups")); + $res = Ihttp::ihttp_get("https://apis.juhe.cn/fapig/soup/query?key=" . $soupKey); + if (Base::isSuccess($res)) { + $data = Base::json2array($res['data']); + if ($data['reason'] === 'success' && $text = trim($data['result']['text'])) { + $array[] = $text; + } + } + Cache::forever("JokeSoupTask:soups", Base::array2json(array_slice($array, -100))); + } + } + + public function end() + { + + } +} diff --git a/app/Tasks/JokeTask.php b/app/Tasks/JokeTask.php deleted file mode 100644 index 04847c46e..000000000 --- a/app/Tasks/JokeTask.php +++ /dev/null @@ -1,51 +0,0 @@ -addDay()); - // - $array = []; - for ($i = 0; $i < 10; $i++) { - $res = Ihttp::ihttp_get("https://api.vvhan.com/api/joke?type=json"); // 备用 https://api.ghser.com/xiaohua?type=json - if (Base::isError($res)) { - return; - } - $data = Base::json2array($res['data']); - if ($data['success'] !== true) { - return; - } - $array[] = $data['joke']; - } - Cache::forever("JokeTask:rands", Base::array2json($array)); - } - - public function end() - { - - } -}