diff --git a/app/controller/IndexController.php b/app/controller/IndexController.php index 131ae12..7944461 100644 --- a/app/controller/IndexController.php +++ b/app/controller/IndexController.php @@ -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) + { } - } diff --git a/app/result/Result.php b/app/result/Result.php new file mode 100644 index 0000000..fd61d7e --- /dev/null +++ b/app/result/Result.php @@ -0,0 +1,57 @@ + 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); + } +} diff --git a/composer.json b/composer.json index 5ab5763..8cfdfe5 100644 --- a/composer.json +++ b/composer.json @@ -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. " diff --git a/composer.lock b/composer.lock index 8074e69..e377960 100644 --- a/composer.lock +++ b/composer.lock @@ -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",