swoole-framwork/app/controller/UserController.php

92 lines
2.2 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-31 14:57:01 +08:00
use Hyperf\Database\Connection;
use Mockery;
use \PDO;
use Hyperf\Database\Query\Grammars\MySqlGrammar;
use Core\BeanFactory;
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-31 14:57:01 +08:00
///$connection = new Connection(PDO::class);
///$connection = $connection->setQueryGrammar(new MySqlGrammar());
///$sql = $connection->table('eb_user')->toSql();
// var_dump($connection->query($sql));
//var_dump($user);
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-31 14:57:01 +08:00
//BeanFactory::getBean('');
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
}