完成大概

This commit is contained in:
giaogiao 2024-03-08 17:50:07 +08:00
parent 3e28197644
commit 46caff6949
6 changed files with 97 additions and 6 deletions

View File

@ -1,2 +1,18 @@
<?php
// 应用公共文件
/**
* 通用化API数据格式输出
* @param $status
* @param string $message
* @param array $data
* @param int $httpStatus
* @return \think\response\Json
*/
function show($status, $message = 'error', $data = [], $httpStatus = 200){
$result = [
"status" => $status,
"message" => $message,
"result" => $data
];
return json($result, $httpStatus);
}

View File

@ -1,7 +1,10 @@
<?php
namespace app\controller;
use app\BaseController;
use think\Facade\Db;
use Carbon\Carbon;
class Index extends BaseController
{
@ -14,4 +17,68 @@ class Index extends BaseController
{
return 'hello,' . $name;
}
/**
* 更新时间
*/
public function updated($name = 'ThinkPHP6')
{
// 获取当前时间
$current = Carbon::now();
// 添加 21 天到当前时间
$trialExpires = $current->addDays(21);
$data = ['date' => $trialExpires->toDateString(), 'tip' => ' '];
// $result = Db::table("demo")->where("id",1)->find();
// dump($result);
Db::table('time_log')->insert($data);
return show(200, 'ok', ['time' => $trialExpires]);
}
/**
* 每天访问一次的任务
*/
public function scheduled_tasks($name = 'ThinkPHP6')
{
$db_time=Db::table('time_log')->where('id', Db::table('time_log')->max('id'))->find();;
//dump($db_time);
$time =$db_time['date'];
// 获取当前时间
$current = Carbon::now();
for ($i=0; $i < 8; $i++) {
$current->subDay($i);
if(strcmp($current->toDateString(),$time)==0){
echo "to do ";
//$this->updated("");
$i==7?$this->updated(""):'';
return show(200, 'ok', ['time' => $current->toDateString(),'time2'=>$time]);
}
}
}
/**
* 更新时间
*/
public function send_msg($title,$body)
{
// 添加 30 天到当前时间
// 1.当前任务将由哪个类来负责处理。
// 当轮到该任务时,系统将生成一个该类的实例,并调用其 fire 方法
$jobHandlerClassName = 'app\job\Hello';
// 2.当前任务归属的队列名称,如果为新队列,会自动创建
$jobQueueName = "helloJobQueue";
// 3.当前任务所需的业务数据 . 不能为 resource 类型其他类型最终将转化为json形式的字符串
// ( jobData 为对象时存储其public属性的键值对 )
$jobData = ['ts' => time(), 'bizId' => uniqid(), 'a' => 1,'title'=>$title,'body'=>$body];
// 4.将该任务推送到消息队列,等待对应的消费者去执行
$isPushed = Queue::push($jobHandlerClassName, $jobData, $jobQueueName);
// database 驱动时,返回值为 1|false ; redis 驱动时,返回值为 随机字符串|false
if ($isPushed !== false) {
Log::info(date('Y-m-d H:i:s')."消息已经推送");
} else {
Log::error(date('Y-m-d H:i:s')."消息推送失败请检查队列驱动");
}
}
}

View File

@ -23,7 +23,7 @@ namespace app\controller;
// 3.当前任务所需的业务数据 . 不能为 resource 类型其他类型最终将转化为json形式的字符串
// ( jobData 为对象时存储其public属性的键值对 )
$jobData = [ 'ts' => time(), 'bizId' => uniqid() , 'a' => 1 ] ;
$jobData = ['ts' => time(), 'bizId' => uniqid(), 'a' => 1,'title'=>"周雪莲子",'body'=>'女神节快乐'];
// 4.将该任务推送到消息队列,等待对应的消费者去执行
$isPushed = Queue::push( $jobHandlerClassName , $jobData , $jobQueueName );

View File

@ -64,9 +64,16 @@ class Hello
private function doHelloJob($data)
{
$curl = new Curl();
$title="";
$mssg="";
$curl->get('https://api.day.app/3vrp4DbTGmSWxVbzHnUnPB/'.$title.$mssg);
$title=$data['title'];
$mssg=$data['body'];
$curl->get('https://api.day.app/3vrp4DbTGmSWxVbzHnUnPB/'.$title.'/'.$mssg);
if ($curl->error) {
echo 'Error: ' . $curl->errorMessage . "\n";
$curl->diagnose();
} else {
echo 'Response:' . "\n";
var_dump($curl->response);
}
print("<info>Hello Job Started. job Data is: " . var_export($data, true) . "</info> \n");
print("<info>Hello Job is Fired at " . date('Y-m-d H:i:s') . "</info> \n");
print("<info>Hello Job is Done!" . "</info> \n");

View File

@ -25,7 +25,8 @@
"topthink/think-orm": "^2.0",
"topthink/think-filesystem": "^1.0",
"topthink/think-queue": "^3.0",
"php-curl-class/php-curl-class": "^9.19"
"php-curl-class/php-curl-class": "^9.19",
"nesbot/carbon": "^2.72"
},
"require-dev": {
"symfony/var-dumper": "^4.2",

2
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "6f1dedd32ad3424b3151c92ff5c86d89",
"content-hash": "dea40365caf14bbc8f865784ad92e670",
"packages": [
{
"name": "carbonphp/carbon-doctrine-types",