From fff929d2b8a468f76ada8c1b55bbd37e71e7f65b Mon Sep 17 00:00:00 2001 From: kuaifan Date: Wed, 1 Feb 2023 08:43:28 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=8A=E7=8F=AD=E6=89=93=E5=8D=A1?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=AF=8F=E6=97=A5=E5=BC=80=E5=BF=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Api/PublicController.php | 18 ++++++- app/Http/Controllers/IndexController.php | 3 ++ app/Tasks/JokeTask.php | 51 +++++++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 app/Tasks/JokeTask.php diff --git a/app/Http/Controllers/Api/PublicController.php b/app/Http/Controllers/Api/PublicController.php index 828f54a0b..69bbd8d8e 100755 --- a/app/Http/Controllers/Api/PublicController.php +++ b/app/Http/Controllers/Api/PublicController.php @@ -129,7 +129,17 @@ class PublicController extends AbstractController } // if ($checkins && $botUser = User::botGetOrCreate('check-in')) { - $sendMsg = function($type, UserCheckinMac $checkin) use ($botUser, $nowDate) { + $getJoke = function() { + $jokes = Base::json2array(Cache::get("JokeTask:rands")); + if ($jokes) { + $jokev = $jokes[array_rand($jokes)]; + if ($jokev) { + return $jokev; + } + } + return null; + }; + $sendMsg = function($type, UserCheckinMac $checkin) use ($getJoke, $botUser, $nowDate) { $cacheKey = "Checkin::sendMsg-{$nowDate}-{$type}:" . $checkin->userid; if (Cache::get($cacheKey) === "yes") { return; @@ -141,7 +151,11 @@ class PublicController extends AbstractController $hi = date("H:i"); $pre = $type == "up" ? "上班" : "下班"; $remark = $checkin->remark ? " ({$checkin->remark})": ""; - $text = "{$pre}打卡成功,打卡时间: {$hi} {$remark}"; + $text = "

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

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

----------

每日开心:{$joke}。

"; + } 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 cd0933354..f4a3832c6 100755 --- a/app/Http/Controllers/IndexController.php +++ b/app/Http/Controllers/IndexController.php @@ -10,6 +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\LoopTask; use Arr; use Cache; @@ -190,6 +191,8 @@ class IndexController extends InvokeController Task::deliver(new DeleteTmpTask('tmp', 24)); // 周期任务 Task::deliver(new LoopTask()); + // 获取笑话 + Task::deliver(new JokeTask()); return "success"; } diff --git a/app/Tasks/JokeTask.php b/app/Tasks/JokeTask.php new file mode 100644 index 000000000..04847c46e --- /dev/null +++ b/app/Tasks/JokeTask.php @@ -0,0 +1,51 @@ +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() + { + + } +}