Skip to content

Commit 0bc642c

Browse files
committed
完善登录逻辑
1 parent d9ee930 commit 0bc642c

File tree

9 files changed

+181
-8
lines changed

9 files changed

+181
-8
lines changed

README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,75 @@ composer require kydev/work-wx-user
1212

1313
## 使用
1414

15+
### 发布配置
16+
17+
```shell
18+
php bin/hyperf.php vendor:publish kydev/work-wx-user
19+
```
20+
1521
### 登录
1622

23+
创建控制器
24+
25+
```php
26+
<?php
27+
28+
declare(strict_types=1);
29+
30+
namespace App\Controller;
31+
32+
use Hyperf\Di\Annotation\Inject;
33+
use KY\WorkWxUser\Request\AuthorizeRequest;
34+
use KY\WorkWxUser\UserService;
35+
use KY\WorkWxUser\WeChat\WeChat;
36+
37+
class OAuthController extends Controller
38+
{
39+
#[Inject]
40+
protected WeChat $wx;
41+
42+
#[Inject]
43+
protected UserService $service;
44+
45+
public function authorize(AuthorizeRequest $request)
46+
{
47+
$url = (string) $request->input('redirect_uri');
48+
$state = (string) $request->input('state');
49+
50+
$redirectUrl = $this->wx->authorize($url, $state);
51+
52+
return $this->response->redirect($redirectUrl);
53+
}
54+
55+
public function login()
56+
{
57+
$code = $this->request->input('code');
58+
59+
$result = $this->service->login($code);
60+
61+
return $this->response->success([
62+
'token' => $result->getToken(),
63+
'user' => $result->getUser()->toArray(),
64+
]);
65+
}
66+
}
67+
68+
```
69+
70+
增加路由
71+
72+
```php
73+
<?php
74+
75+
Router::get('/oauth/authorize', [App\Controller\OAuthController::class, 'authorize']);
76+
Router::get('/oauth/login', [App\Controller\OAuthController::class, 'login']);
77+
Router::post('/oauth/login', [App\Controller\OAuthController::class, 'login']);
78+
```
79+
80+
浏览器访问以下地址,并扫码
81+
82+
http://127.0.0.1:9501/oauth/authorize?state=STATE&redirect_uri=http://127.0.0.1:9501/oauth/login
83+
1784
### 验证登录态
1885

1986
`middlewares.php` 配置中,增加对应的中间件。

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"hyperf/http-server": "3.0.*",
3131
"hyperf/model-cache": "3.0.*",
3232
"hyperf/redis": "3.0.*",
33+
"hyperf/validation": "3.0.*",
3334
"limingxinleo/easywechat-classmap": "^1.0",
3435
"limingxinleo/hyperf-utils": "^3.3",
3536
"w7corp/easywechat": "^6.7"

src/Exception/ApiException.php

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+
* This file is part of KnowYourself.
6+
*
7+
* @link https://www.zhiwotansuo.cn
8+
* @document https://github.com/kydever/work-wx-user/blob/main/README.md
9+
* @contact l@hyperf.io
10+
* @license https://github.com/kydever/work-wx-user/blob/main/LICENSE
11+
*/
12+
namespace KY\WorkWxUser\Exception;
13+
14+
class ApiException extends \RuntimeException
15+
{
16+
}

src/Request/AuthorizeRequest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of KnowYourself.
6+
*
7+
* @link https://www.zhiwotansuo.cn
8+
* @document https://github.com/kydever/work-wx-user/blob/main/README.md
9+
* @contact l@hyperf.io
10+
* @license https://github.com/kydever/work-wx-user/blob/main/LICENSE
11+
*/
12+
namespace KY\WorkWxUser\Request;
13+
14+
use Hyperf\Validation\Request\FormRequest;
15+
16+
class AuthorizeRequest extends FormRequest
17+
{
18+
/**
19+
* Determine if the user is authorized to make this request.
20+
*/
21+
public function authorize(): bool
22+
{
23+
return true;
24+
}
25+
26+
/**
27+
* Get the validation rules that apply to the request.
28+
*/
29+
public function rules(): array
30+
{
31+
return [
32+
'redirect_uri' => 'required|string',
33+
'state' => 'string',
34+
];
35+
}
36+
}

src/UserAuth.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515
use Hyperf\Redis\Redis;
1616
use Hyperf\Utils\Codec\Json;
1717
use KY\WorkWxUser\Exception\TokenInvalidException;
18+
use KY\WorkWxUser\Model\User;
1819

1920
class UserAuth implements \JsonSerializable
2021
{
2122
public const AUTH_TOKEN = 'work-wx-token';
2223

24+
protected ?User $user = null;
25+
2326
public function __construct(protected int $id, protected string $token)
2427
{
2528
}
@@ -31,6 +34,17 @@ public function jsonSerialize()
3134
];
3235
}
3336

37+
public function getUser(): ?User
38+
{
39+
return $this->user;
40+
}
41+
42+
public function setUser(?User $user): static
43+
{
44+
$this->user = $user;
45+
return $this;
46+
}
47+
3448
public function getId(): int
3549
{
3650
return $this->id;

src/UserService.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use KY\WorkWxUser\Dao\DepartmentDao;
1616
use KY\WorkWxUser\Dao\UserDao;
1717
use KY\WorkWxUser\Model\Department;
18-
use KY\WorkWxUser\Model\User;
1918
use KY\WorkWxUser\Translator\UserTranslator;
2019
use KY\WorkWxUser\WeChat\DepartmentWeChat;
2120
use KY\WorkWxUser\WeChat\UserWeChat;
@@ -55,14 +54,22 @@ public function syncToDatabase(): void
5554
}
5655
}
5756

58-
public function login(User $user): UserAuth
57+
public function login(string $code): UserAuth
5958
{
59+
$info = di()->get(UserWeChat::class)->getUserInfo($code);
60+
61+
$user = di()->get(UserDao::class)->firstByUserid($info['userid']);
62+
63+
$user = di()->get(UserTranslator::class)->translate($info, $user);
64+
65+
$user->save();
66+
6067
$token = sprintf('%d_%s', $user->id, md5(uniqid()));
6168

6269
$userAuth = new UserAuth($user->id, $token);
6370

6471
$userAuth->save();
6572

66-
return $userAuth;
73+
return $userAuth->setUser($user);
6774
}
6875
}

src/WeChat/Api.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of KnowYourself.
6+
*
7+
* @link https://www.zhiwotansuo.cn
8+
* @document https://github.com/kydever/work-wx-user/blob/main/README.md
9+
* @contact l@hyperf.io
10+
* @license https://github.com/kydever/work-wx-user/blob/main/LICENSE
11+
*/
12+
namespace KY\WorkWxUser\WeChat;
13+
14+
use KY\WorkWxUser\Exception\ApiException;
15+
16+
abstract class Api
17+
{
18+
public function format(array $result): array
19+
{
20+
if ($result['errcode'] > 0) {
21+
throw new ApiException($result['errmsg'], $result['errcode']);
22+
}
23+
24+
return $result;
25+
}
26+
}

src/WeChat/DepartmentWeChat.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@
1414
use EasyWeChat\Work\Application;
1515
use GuzzleHttp\RequestOptions;
1616

17-
class DepartmentWeChat
17+
class DepartmentWeChat extends Api
1818
{
1919
public function __construct(protected Application $wx)
2020
{
2121
}
2222

2323
public function departments(int $id): array
2424
{
25-
return $this->wx->getClient()->get('/cgi-bin/department/list', [
25+
$result = $this->wx->getClient()->get('/cgi-bin/department/list', [
2626
RequestOptions::QUERY => [
2727
'id' => $id,
2828
],
2929
])->toArray();
30+
31+
return $this->format($result);
3032
}
3133
}

src/WeChat/UserWeChat.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,33 @@
1414
use EasyWeChat\Work\Application;
1515
use GuzzleHttp\RequestOptions;
1616

17-
class UserWeChat
17+
class UserWeChat extends Api
1818
{
1919
public function __construct(protected Application $wx)
2020
{
2121
}
2222

2323
public function infoByUserid(string $userid): array
2424
{
25-
return $this->wx->getClient()->get('/cgi-bin/user/get', [
25+
$result = $this->wx->getClient()->get('/cgi-bin/user/get', [
2626
RequestOptions::QUERY => [
2727
'userid' => $userid,
2828
],
2929
])->toArray();
30+
31+
return $this->format($result);
3032
}
3133

3234
public function listByDepartmentId(int $departmentId, bool $fetchChild = false): array
3335
{
34-
return $this->wx->getClient()->get('/cgi-bin/user/list', [
36+
$result = $this->wx->getClient()->get('/cgi-bin/user/list', [
3537
RequestOptions::QUERY => [
3638
'department_id' => $departmentId,
3739
'fetch_child' => (int) $fetchChild,
3840
],
3941
])->toArray();
42+
43+
return $this->format($result);
4044
}
4145

4246
public function getUserInfo(string $code): array

0 commit comments

Comments
 (0)