swoole-framwork/app/controller/UserController.php
2024-05-31 14:57:01 +08:00

92 lines
2.2 KiB
PHP

<?php
namespace App\controller;
use App\model\User;
use Core\annotations\Bean;
use Core\annotations\Value;
use Core\annotations\Redis;
use Core\annotations\Lock;
use Core\annotations\DB;
use Core\annotations\RequestMapping;
use Core\http\Request;
use Core\http\Response;
use Core\init\MyDB;
use Core\init\RedisHelper;
use DI\Attribute\Inject;
use Hyperf\Database\Connection;
use Mockery;
use \PDO;
use Hyperf\Database\Query\Grammars\MySqlGrammar;
use Core\BeanFactory;
/**
* @Bean()
*/
class UserController
{
/**
* @DB()
* @var MyDB
*/
private $db;
/**
* @Value ()
*/
public $version = '1.0';
#[Inject]
private User $user;
/**
* @RequestMapping(value="/user/test",method={"GET"})
*/
public function test()
{
///$connection = new Connection(PDO::class);
///$connection = $connection->setQueryGrammar(new MySqlGrammar());
///$sql = $connection->table('eb_user')->toSql();
// var_dump($connection->query($sql));
//var_dump($user);
//$this->db->select('select sleep(1)');
//User::query('select sleep(3)');
$user = User::first();
return ['uid' => 33, 'username' => $user];
}
/**
* @RequestMapping(value="/user/{uid:\d+}")
*/
public function user(int $uid, Request $request, Response $response)
{
//BeanFactory::getBean('');
//$user = $this->db->select('select * from eb_user where id = 1');
return ['code' => 1, 'uid' => $uid];
}
/**
* @RequestMapping(value="/redirect")
*/
public function redirect(Request $request, Response $response)
{
$response->writeRedirect('http://www.baihand.com');
}
/**
* @RequestMapping(value="/user/json")
* @Redis(key="name")
*/
public function json()
{
$user = $this->db->table('user')->get();
return ['code' => 1, 'msg' => 'success', 'data' => $user];
}
/**
* @Lock(prefix="lock",key="#0")
* @RequestMapping(value="/user/lock/{uid:\d+}")
*/
public function lock(Request $request, $uid, Response $response)
{
return $this->db->table('user')->get();
}
}