96 lines
2.2 KiB
PHP
96 lines
2.2 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Controller;
|
||
|
||
use App\Exception\BusinessException;
|
||
use App\JsonRpc\UserExternalServiceInterface;
|
||
use App\Service\User\WechatAuthService;
|
||
use Hyperf\Di\Annotation\Inject;
|
||
use Hyperf\HttpServer\Annotation\AutoController;
|
||
use Hyperf\Utils\Collection;
|
||
use Hyperf\Paginator\Paginator;
|
||
|
||
/**
|
||
* 示例控制器
|
||
*/
|
||
#[AutoController]
|
||
class TestController extends BaseController
|
||
{
|
||
/**
|
||
* @var UserExternalServiceInterface
|
||
*/
|
||
#[Inject]
|
||
protected UserExternalServiceInterface $userService;
|
||
|
||
/**
|
||
* 返回数据示例
|
||
*
|
||
* @return array
|
||
*/
|
||
public function index() : array
|
||
{
|
||
$data['user_id'] = 'test';
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* 空返回示例
|
||
*
|
||
* @return void
|
||
*/
|
||
public function empty() : void
|
||
{}
|
||
|
||
/**
|
||
* 错误示例
|
||
*
|
||
* @return mixed
|
||
*/
|
||
public function error(): mixed
|
||
{
|
||
throw new BusinessException(500, 'error');
|
||
}
|
||
|
||
/**
|
||
* 分页示例
|
||
*
|
||
* @return Paginator
|
||
*/
|
||
public function page() : Paginator
|
||
{
|
||
$currentPage = (int) $this->request->input('page', 1);
|
||
$perPage = (int) $this->request->input('per_page', 2);
|
||
|
||
// 这里根据 $currentPage 和 $perPage 进行数据查询,以下使用 Collection 代替
|
||
$collection = new Collection([
|
||
['id' => 1, 'name' => 'Tom'],
|
||
['id' => 2, 'name' => 'Sam'],
|
||
['id' => 3, 'name' => 'Tim'],
|
||
['id' => 4, 'name' => 'Joe'],
|
||
]);
|
||
|
||
$users = array_values($collection->forPage($currentPage, $perPage)->toArray());
|
||
|
||
return new Paginator($users, $perPage, $currentPage);
|
||
}
|
||
|
||
/**
|
||
* 获取token(用于测试)
|
||
*
|
||
* @param WechatAuthService $wechatAuthService
|
||
* @return array
|
||
*/
|
||
public function token(WechatAuthService $wechatAuthService) : array
|
||
{
|
||
$openid = $this->request->input('openid', '1111111');
|
||
$user = $this->getServiceResult($this->userService->getUserInfo($openid, ['user_id', 'user_nickname']));
|
||
|
||
if (!empty($user)) {
|
||
return $wechatAuthService->getToken($user['user_id'], $user['user_nickname'], $openid);
|
||
}
|
||
|
||
return [];
|
||
}
|
||
} |