引入Carbon 完成大致内容

This commit is contained in:
lingling 2024-04-20 14:59:53 +08:00
parent a44ee531c7
commit d59d078fa0
4 changed files with 96 additions and 13 deletions

View File

@ -4,6 +4,8 @@ namespace app\controller;
use support\Request;
use support\Db;
use app\result\Result;
use Carbon\Carbon;
class IndexController
{
@ -17,22 +19,45 @@ class IndexController
return $readme;
}
public function view(Request $request)
/**
* 更新时间
*/
public function updated($name = 'ThinkPHP6')
{
return view('index/view', ['name' => 'webman']);
}
public function json(Request $request)
{
$assets = Db::table('asset')->where('scan_time', null)->count();
// 获取当前时间
$current = Carbon::now();
return json(['code' => 0, 'msg' => 'ok','data'=>$assets]);
// 添加 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 Result::show(200, 'ok', ['time' => $trialExpires]);
}
public function text(Request $request)
/**
* 每天访问一次的任务
*/
public function scheduled_tasks($name = 'ThinkPHP6')
{
$db_time = Db::table('time_log')->where('id', Db::table('time_log')->max('id'))->first();
//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) {
return json(['code' => 0, 'msg' => 'ok','data'=>'text']);
echo "to do ";
//$this->updated("");
$i == 7 ? $this->updated("") : '';
return Result::show(200, 'ok', ['time' => $current->toDateString(), 'time2' => $time]);
}
}
}
public function send_msg($title, $body)
{
}
}

57
app/result/Result.php Normal file
View File

@ -0,0 +1,57 @@
<?php
/*
统一格式的返回json数据
*/
namespace app\result;
class Result
{
//success:code值为0,data数据
static public function Success($data)
{
$rs = [
'code' => 0,
'msg' => "",
'data' => $data,
];
return json($rs);
}
//ErrorCode需要code/msg参数
static public function ErrorCode($code, $msg)
{
$rs = [
'code' => $code,
'msg' => $msg,
'data' => "",
];
return json($rs);
}
//error,传入定义的数组常量
static public function Error($arr)
{
$rs = [
'code' => $arr['code'],
'msg' => $arr['msg'],
'data' => "",
];
return json($rs);
}
/**
* 通用化API数据格式输出
* @param $status
* @param string $message
* @param array $data
* @param int $httpStatus
* @return \think\response\Json
*/
static public function show($status, $message = 'error', $data = [], $httpStatus = 200)
{
$result = [
"status" => $status,
"message" => $message,
"result" => $data
];
return json($result, $httpStatus);
}
}

View File

@ -30,7 +30,8 @@
"illuminate/database": "^8.83",
"illuminate/pagination": "^8.83",
"illuminate/events": "^8.83",
"symfony/var-dumper": "^5.4"
"symfony/var-dumper": "^5.4",
"nesbot/carbon": "^2.72"
},
"suggest": {
"ext-event": "For better performance. "

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": "d19de0b71c97f1ea3e3daa830cd58c6a",
"content-hash": "ccc5378ed1f0e86d78b011265d36f7cf",
"packages": [
{
"name": "carbonphp/carbon-doctrine-types",