117 lines
3.3 KiB
PHP
117 lines
3.3 KiB
PHP
<?php
|
|
namespace App\Http\Controllers\Customer;
|
|
|
|
use App\Http\Controllers\Base\CustomerBaseController;
|
|
use App\Models\Customer\CustomerUser;
|
|
use App\Service\AuthService;
|
|
use Illuminate\Support\Facades\Redis;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Support\ValidatedInput;
|
|
|
|
class CustomerUserController extends CustomerBaseController
|
|
{
|
|
public array $validateMethodParams = [
|
|
'signIn' => [
|
|
'username' => 'required|alpha_dash:ascii|max:50',
|
|
'password' => 'required|alpha_dash:ascii|max:50',
|
|
'device' => 'required|alpha_dash:ascii|max:10',
|
|
],
|
|
];
|
|
|
|
function test()
|
|
{
|
|
var_dump('asd');
|
|
$a = Redis::get('test');
|
|
var_dump($a);
|
|
}
|
|
|
|
function signIn(): \Illuminate\Http\JsonResponse
|
|
{
|
|
$request = request();
|
|
$username = $request->input('username');
|
|
$password = $request->input('password');
|
|
$device = $request->input('device');
|
|
|
|
$oCustomerUser = new CustomerUser();
|
|
$oUser = $oCustomerUser->findItemByUsername($username);
|
|
|
|
if (!$oUser) {
|
|
return $this->error('用户名不存在');
|
|
}
|
|
|
|
if (!$oCustomerUser->checkPasswd($oUser->id,$password)) {
|
|
return $this->error('密码错误');
|
|
}
|
|
|
|
$oAuthService = new AuthService();
|
|
$token = $oAuthService->createTokenToUser($oUser->id,$device);
|
|
|
|
$data = [
|
|
'token' => $token,
|
|
'user' => [
|
|
'id' => $oUser->id,
|
|
'username' => $oUser->username,
|
|
'nickname' => $oUser->nickname,
|
|
'is_google_auth' => $oUser->is_google_auth,
|
|
'created_at' => $oUser->created_at,
|
|
'updated_at' => $oUser->updated_at,
|
|
],
|
|
];
|
|
|
|
return $this->success($data);
|
|
|
|
}
|
|
|
|
function signOut(): \Illuminate\Http\JsonResponse
|
|
{
|
|
$oAuthService = new AuthService();
|
|
$token = $oAuthService->getTokenFromReq();
|
|
$aUser = $oAuthService->getCurrentUser();
|
|
$oAuthService->delTokenToUser($aUser['uid'],$token);
|
|
return $this->success();
|
|
}
|
|
|
|
function register()
|
|
{
|
|
$request = request();
|
|
$username = $request->input('username');
|
|
$password = $request->input('password');
|
|
$device = $request->input('device');
|
|
|
|
$oCustomerUser = new CustomerUser();
|
|
$oUser = $oCustomerUser->findItemByUsername($username,['id']);
|
|
|
|
if ($oUser) {
|
|
return $this->error('用户名已存在');
|
|
}
|
|
|
|
$oUser = $oCustomerUser->addUser([
|
|
'username' => $username,
|
|
'password' => $password,
|
|
'nickname' => $username,
|
|
]);
|
|
|
|
if (!$oUser) {
|
|
return $this->error('注册失败');
|
|
}
|
|
|
|
$oAuthService = new AuthService();
|
|
$token = $oAuthService->createTokenToUser($oUser->id,$device);
|
|
|
|
$data = [
|
|
'token' => $token,
|
|
'user' => [
|
|
'id' => $oUser->id,
|
|
'username' => $oUser->username,
|
|
'nickname' => $oUser->nickname,
|
|
'is_google_auth' => $oUser->is_google_auth,
|
|
'created_at' => $oUser->created_at,
|
|
'updated_at' => $oUser->updated_at,
|
|
],
|
|
];
|
|
|
|
return $this->success($data);
|
|
}
|
|
|
|
}
|