swoole-framwork/app/controller/UserController.php

82 lines
1.7 KiB
PHP
Raw Normal View History

2024-05-20 11:51:07 +08:00
<?php
2024-05-22 17:48:54 +08:00
2024-05-20 11:51:07 +08:00
namespace App\controller;
2024-05-22 17:48:54 +08:00
2024-05-21 14:33:56 +08:00
use App\model\User;
2024-05-20 11:51:07 +08:00
use Core\annotations\Bean;
use Core\annotations\Value;
2024-05-22 15:50:08 +08:00
use Core\annotations\Redis;
use Core\annotations\Lock;
2024-05-20 11:51:07 +08:00
use Core\annotations\DB;
use Core\annotations\RequestMapping;
use Core\http\Request;
use Core\http\Response;
use Core\init\MyDB;
2024-05-22 15:50:08 +08:00
use Core\init\RedisHelper;
2024-05-22 14:49:06 +08:00
use DI\Attribute\Inject;
2024-05-20 11:51:07 +08:00
/**
* @Bean()
*/
class UserController
{
/**
* @DB()
* @var MyDB
*/
private $db;
2024-05-22 15:50:08 +08:00
2024-05-20 11:51:07 +08:00
/**
* @Value ()
*/
public $version = '1.0';
2024-05-22 14:49:06 +08:00
#[Inject]
private User $user;
2024-05-20 11:51:07 +08:00
/**
2024-05-23 10:31:16 +08:00
* @RequestMapping(value="/user/test",method={"GET"})
2024-05-20 11:51:07 +08:00
*/
public function test()
{
2024-05-21 14:33:56 +08:00
//$this->db->select('select sleep(1)');
//User::query('select sleep(3)');
$user = User::first();
2024-05-22 17:48:54 +08:00
return ['uid' => 33, 'username' => $user];
2024-05-20 11:51:07 +08:00
}
2024-05-22 17:48:54 +08:00
2024-05-20 11:51:07 +08:00
/**
* @RequestMapping(value="/user/{uid:\d+}")
*/
2024-05-22 17:48:54 +08:00
public function user(int $uid, Request $request, Response $response)
2024-05-20 11:51:07 +08:00
{
2024-05-22 17:48:54 +08:00
//$user = $this->db->select('select * from eb_user where id = 1');
return ['code' => 1, 'uid' => $uid];
2024-05-20 11:51:07 +08:00
}
2024-05-22 17:48:54 +08:00
2024-05-20 11:51:07 +08:00
/**
* @RequestMapping(value="/redirect")
*/
2024-05-22 17:48:54 +08:00
public function redirect(Request $request, Response $response)
2024-05-20 11:51:07 +08:00
{
$response->writeRedirect('http://www.baihand.com');
}
2024-05-22 14:49:06 +08:00
/**
* @RequestMapping(value="/user/json")
2024-05-22 15:50:08 +08:00
* @Redis(key="name")
2024-05-22 14:49:06 +08:00
*/
public function json()
{
2024-05-22 15:50:08 +08:00
$user = $this->db->table('user')->get();
2024-05-22 14:49:06 +08:00
return ['code' => 1, 'msg' => 'success', 'data' => $user];
}
2024-05-22 15:50:08 +08:00
/**
* @Lock(prefix="lock",key="#0")
* @RequestMapping(value="/user/lock/{uid:\d+}")
*/
2024-05-22 17:48:54 +08:00
public function lock(Request $request, $uid, Response $response)
2024-05-22 15:50:08 +08:00
{
return $this->db->table('user')->get();
}
2024-05-20 11:51:07 +08:00
}