mirror of
https://github.com/kuaifan/dootask.git
synced 2026-02-21 07:50:30 +00:00
feat: 上班打开每日开心/下班打卡心灵鸡汤
This commit is contained in:
parent
fff929d2b8
commit
7109eb0238
@ -53,6 +53,9 @@ PUSHER_APP_KEY=
|
|||||||
PUSHER_APP_SECRET=
|
PUSHER_APP_SECRET=
|
||||||
PUSHER_APP_CLUSTER=mt1
|
PUSHER_APP_CLUSTER=mt1
|
||||||
|
|
||||||
|
JUKE_KEY_JOKE=
|
||||||
|
JUKE_KEY_SOUP=
|
||||||
|
|
||||||
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
|
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
|
||||||
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
|
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
|
||||||
|
|
||||||
|
|||||||
@ -129,17 +129,19 @@ class PublicController extends AbstractController
|
|||||||
}
|
}
|
||||||
//
|
//
|
||||||
if ($checkins && $botUser = User::botGetOrCreate('check-in')) {
|
if ($checkins && $botUser = User::botGetOrCreate('check-in')) {
|
||||||
$getJoke = function() {
|
$getJokeSoup = function($type) {
|
||||||
$jokes = Base::json2array(Cache::get("JokeTask:rands"));
|
$pre = $type == "up" ? "每日开心:" : "心灵鸡汤:";
|
||||||
if ($jokes) {
|
$key = $type == "up" ? "JokeSoupTask:jokes" : "JokeSoupTask:soups";
|
||||||
$jokev = $jokes[array_rand($jokes)];
|
$array = Base::json2array(Cache::get($key));
|
||||||
if ($jokev) {
|
if ($array) {
|
||||||
return $jokev;
|
$item = $array[array_rand($array)];
|
||||||
|
if ($item) {
|
||||||
|
return $pre . $item;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
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;
|
$cacheKey = "Checkin::sendMsg-{$nowDate}-{$type}:" . $checkin->userid;
|
||||||
if (Cache::get($cacheKey) === "yes") {
|
if (Cache::get($cacheKey) === "yes") {
|
||||||
return;
|
return;
|
||||||
@ -152,9 +154,9 @@ class PublicController extends AbstractController
|
|||||||
$pre = $type == "up" ? "上班" : "下班";
|
$pre = $type == "up" ? "上班" : "下班";
|
||||||
$remark = $checkin->remark ? " ({$checkin->remark})": "";
|
$remark = $checkin->remark ? " ({$checkin->remark})": "";
|
||||||
$text = "<p>{$pre}打卡成功,打卡时间: {$hi}{$remark}</p>";
|
$text = "<p>{$pre}打卡成功,打卡时间: {$hi}{$remark}</p>";
|
||||||
$joke = $getJoke();
|
$suff = $getJokeSoup($type);
|
||||||
if ($joke) {
|
if ($suff) {
|
||||||
$text = "<pre>{$text}<p>----------</p><p>每日开心:{$joke}。</p></pre>";
|
$text = "{$text}<p>----------</p><p>{$suff}</p>";
|
||||||
}
|
}
|
||||||
WebSocketDialogMsg::sendMsg(null, $dialog->id, 'text', ['text' => $text], $botUser->userid);
|
WebSocketDialogMsg::sendMsg(null, $dialog->id, 'text', ['text' => $text], $botUser->userid);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,7 +10,7 @@ use App\Tasks\AppPushTask;
|
|||||||
use App\Tasks\AutoArchivedTask;
|
use App\Tasks\AutoArchivedTask;
|
||||||
use App\Tasks\DeleteTmpTask;
|
use App\Tasks\DeleteTmpTask;
|
||||||
use App\Tasks\EmailNoticeTask;
|
use App\Tasks\EmailNoticeTask;
|
||||||
use App\Tasks\JokeTask;
|
use App\Tasks\JokeSoupTask;
|
||||||
use App\Tasks\LoopTask;
|
use App\Tasks\LoopTask;
|
||||||
use Arr;
|
use Arr;
|
||||||
use Cache;
|
use Cache;
|
||||||
@ -191,8 +191,8 @@ class IndexController extends InvokeController
|
|||||||
Task::deliver(new DeleteTmpTask('tmp', 24));
|
Task::deliver(new DeleteTmpTask('tmp', 24));
|
||||||
// 周期任务
|
// 周期任务
|
||||||
Task::deliver(new LoopTask());
|
Task::deliver(new LoopTask());
|
||||||
// 获取笑话
|
// 获取笑话/心灵鸡汤
|
||||||
Task::deliver(new JokeTask());
|
Task::deliver(new JokeSoupTask());
|
||||||
|
|
||||||
return "success";
|
return "success";
|
||||||
}
|
}
|
||||||
|
|||||||
71
app/Tasks/JokeSoupTask.php
Normal file
71
app/Tasks/JokeSoupTask.php
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Tasks;
|
||||||
|
|
||||||
|
use App\Module\Base;
|
||||||
|
use App\Module\Ihttp;
|
||||||
|
use Cache;
|
||||||
|
use Carbon\Carbon;
|
||||||
|
|
||||||
|
@error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取笑话、心灵鸡汤
|
||||||
|
*
|
||||||
|
* 在.env添加笑话 JUKE_KEY_JOKE
|
||||||
|
* 在.env添加鸡汤 JUKE_KEY_SOUP
|
||||||
|
*
|
||||||
|
* 每日小时采集1次
|
||||||
|
*/
|
||||||
|
class JokeSoupTask extends AbstractTask
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function start()
|
||||||
|
{
|
||||||
|
// 判断每小时执行一次
|
||||||
|
if (Cache::get("JokeSoupTask:YmdH") == date("YmdH")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Cache::put("JokeSoupTask:YmdH", date("YmdH"), Carbon::now()->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()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,51 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Tasks;
|
|
||||||
|
|
||||||
use App\Module\Base;
|
|
||||||
use App\Module\Ihttp;
|
|
||||||
use Cache;
|
|
||||||
use Carbon\Carbon;
|
|
||||||
|
|
||||||
@error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取笑话大全
|
|
||||||
*
|
|
||||||
* 每日小时采集1次
|
|
||||||
*/
|
|
||||||
class JokeTask extends AbstractTask
|
|
||||||
{
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
parent::__construct();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function start()
|
|
||||||
{
|
|
||||||
if (Cache::get("JokeTask:lastYmdH") == date("YmdH")) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Cache::put("JokeTask:lastYmdH", date("YmdH"), Carbon::now()->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()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user