Skip to content

Commit 682d38d

Browse files
author
chenchangqin
committed
develop#项目文件
1 parent b5a4079 commit 682d38d

File tree

11 files changed

+317
-10
lines changed

11 files changed

+317
-10
lines changed

.idea/php-docker-settings.xml

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/php.xml

Lines changed: 71 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,5 @@ services:
5656
- "9200:9200"
5757
networks:
5858
- hyperf
59-
command: /bin/bash -c "rm -rf /opt/www/hyperf/runtime/container && php /opt/www/hyperf/bin/hyperf.php start"
59+
# command: /bin/bash -c "rm -rf /opt/www/hyperf/runtime/container && php /opt/www/hyperf/bin/hyperf.php start"
6060
tty: true

hyperf/.env.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ APP_NAME=skeleton
22
APP_ENV=dev
33

44
DB_DRIVER=mysql
5-
DB_HOST=localhost
5+
DB_HOST=mysql
66
DB_PORT=3306
77
DB_DATABASE=hyperf
88
DB_USERNAME=root
9-
DB_PASSWORD=
9+
DB_PASSWORD=hSMZYpAGbNwVmERI
1010
DB_CHARSET=utf8mb4
1111
DB_COLLATION=utf8mb4_unicode_ci
1212
DB_PREFIX=
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Controller;
6+
7+
use Hyperf\HttpServer\Contract\RequestInterface;
8+
use Hyperf\HttpServer\Contract\ResponseInterface;
9+
10+
class HouseController extends AbstractController
11+
{
12+
public function index(RequestInterface $request, ResponseInterface $response)
13+
{
14+
return $response->raw('Hello Hyperf!');
15+
}
16+
}
Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* Created by PhpStorm.
45
* User: chenchangqin
@@ -8,23 +9,68 @@
89

910
namespace App\Controller;
1011

11-
use Hyperf\HttpServer\Annotation\AutoController;
12+
use App\Service\UserService;
13+
use Hyperf\Di\Annotation\Inject;
14+
use Hyperf\HttpServer\Annotation\Controller;
15+
use Hyperf\HttpServer\Annotation\RequestMapping;
1216
use Hyperf\HttpServer\Contract\RequestInterface;
1317

1418
/**
15-
* @AutoController()
19+
* @Controller()
1620
*/
1721
class UserController extends AbstractController
1822
{
23+
24+
/**
25+
* @Inject
26+
* @var UserService
27+
*/
28+
private $userService;
29+
30+
/**
31+
* @param RequestInterface $request
32+
* @return string
33+
* @RequestMapping(path="index",methods={"get"})
34+
*/
1935
public function index(RequestInterface $request)
2036
{
2137
$id = $request->getMethod();
2238
return $id;
2339
}
2440

25-
public function info(RequestInterface $request)
41+
/**
42+
* @param RequestInterface $request
43+
* @param int $id
44+
* @return array
45+
* @RequestMapping(path="/user/{id:\d+}",methods={"get"})
46+
*/
47+
public function info(RequestInterface $request,int $id)
2648
{
27-
$id = $request->getMethod();
28-
return $id;
49+
$result = $this->userService->user($id);
50+
51+
$method = $request->getMethod();
52+
return [
53+
'method' => $method,
54+
'result' => $result
55+
];
2956
}
57+
58+
/**
59+
* @param RequestInterface $request
60+
* @param int $id
61+
* @return array
62+
* @RequestMapping(path="update/[{id:\d+}]",methods={"put"})
63+
*/
64+
public function update(RequestInterface $request,int $id)
65+
{
66+
$data = $request->post();
67+
$result = $this->userService->updateUser($id,$data);
68+
69+
$method = $request->getMethod();
70+
return [
71+
'method' => $method,
72+
'result' => $result
73+
];
74+
}
75+
3076
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Middleware;
6+
7+
use Hyperf\Utils\Context;
8+
use Psr\Container\ContainerInterface;
9+
use Psr\Http\Message\ResponseInterface;
10+
use Psr\Http\Server\MiddlewareInterface;
11+
use Psr\Http\Message\ServerRequestInterface;
12+
use Psr\Http\Server\RequestHandlerInterface;
13+
14+
class CorsMiddleware implements MiddlewareInterface
15+
{
16+
/**
17+
* @var ContainerInterface
18+
*/
19+
protected $container;
20+
21+
public function __construct(ContainerInterface $container)
22+
{
23+
$this->container = $container;
24+
}
25+
26+
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
27+
{
28+
$response = Context::get(ResponseInterface::class);
29+
$response = $response->withHeader('Access-Control-Allow-Origin', '*')
30+
->withHeader('Access-Control-Allow-Credentials', 'true')
31+
// Headers 可以根据实际情况进行改写。
32+
->withHeader('Access-Control-Allow-Headers', 'DNT,Keep-Alive,User-Agent,Cache-Control,Content-Type,Authorization');
33+
34+
Context::set(ResponseInterface::class, $response);
35+
36+
if ($request->getMethod() == 'OPTIONS') {
37+
return $response;
38+
}
39+
40+
return $handler->handle($request);
41+
}
42+
}

hyperf/app/Model/User.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare (strict_types=1);
4+
namespace App\Model;
5+
6+
use Hyperf\DbConnection\Model\Model;
7+
/**
8+
* @property int $id
9+
* @property string $username
10+
* @property int $status
11+
* @property int $admin
12+
* @property \Carbon\Carbon $created_at
13+
* @property \Carbon\Carbon $updated_at
14+
*/
15+
class User extends Model
16+
{
17+
/**
18+
* The table associated with the model.
19+
*
20+
* @var string
21+
*/
22+
protected $table = 'user';
23+
/**
24+
* The attributes that are mass assignable.
25+
*
26+
* @var array
27+
*/
28+
protected $fillable = [];
29+
/**
30+
* The attributes that should be cast to native types.
31+
*
32+
* @var array
33+
*/
34+
protected $casts = ['id' => 'integer', 'status' => 'integer', 'admin' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];
35+
}

hyperf/app/Service/SystemService.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Service;
6+
7+
use Hyperf\Di\Annotation\Inject;
8+
use Hyperf\Cache\Listener\DeleteListenerEvent;
9+
use Psr\EventDispatcher\EventDispatcherInterface;
10+
11+
class SystemService
12+
{
13+
/**
14+
* @Inject
15+
* @var EventDispatcherInterface
16+
*/
17+
protected $dispatcher;
18+
19+
public function flushCache($userId)
20+
{
21+
$this->dispatcher->dispatch(new DeleteListenerEvent('user-update', [$userId]));
22+
23+
return true;
24+
}
25+
}

hyperf/app/Service/UserService.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* Created by PhpStorm.
5+
* User: chenchangqin
6+
* Date: 2020-07-16
7+
* Time: 14:51
8+
*/
9+
10+
namespace App\Service;
11+
12+
use App\Model\User;
13+
use Hyperf\Cache\Annotation\Cacheable;
14+
use Hyperf\Cache\Annotation\CachePut;
15+
16+
class UserService
17+
{
18+
/**
19+
* @Cacheable(prefix="user", ttl=9000, listener="user-update")
20+
*/
21+
public function user(int $id)
22+
{
23+
$user = User::query()->where('id',$id)->first();
24+
25+
if($user){
26+
return $user->toArray();
27+
}
28+
29+
return null;
30+
}
31+
32+
/**
33+
* @CachePut(prefix="user", ttl=3601)
34+
*/
35+
public function updateUser(int $id,array $data)
36+
{
37+
38+
$user = User::query()->find($id);
39+
foreach ($data as $key => $value) {
40+
$user->{$key} = $value;
41+
}
42+
$user->save();
43+
44+
return [
45+
'user' => $user->toArray(),
46+
];
47+
}
48+
}

hyperf/config/autoload/databases.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
return [
1313
'default' => [
1414
'driver' => env('DB_DRIVER', 'mysql'),
15-
'host' => env('DB_HOST', 'localhost'),
15+
'host' => env('DB_HOST', 'mysql'),
1616
'port' => env('DB_PORT', 3306),
1717
'database' => env('DB_DATABASE', 'hyperf'),
1818
'username' => env('DB_USERNAME', 'root'),
19-
'password' => env('DB_PASSWORD', ''),
19+
'password' => env('DB_PASSWORD', 'hSMZYpAGbNwVmERI'),
2020
'charset' => env('DB_CHARSET', 'utf8'),
2121
'collation' => env('DB_COLLATION', 'utf8_unicode_ci'),
2222
'prefix' => env('DB_PREFIX', ''),

0 commit comments

Comments
 (0)