Files
cycle_api/app/Http/Controllers/Customer/CustomerUserController.php
2023-12-21 22:01:33 +08:00

252 lines
8.3 KiB
PHP

<?php
namespace App\Http\Controllers\Customer;
use App\Const\Im;
use App\Const\VrCode;
use App\Exceptions\AppException;
use App\Http\Controllers\Base\CustomerBaseController;
use App\Models\Customer\CustomerUser;
use App\Service\AuthService;
use App\Service\ImService;
use App\Service\VrCodeService;
use Illuminate\Support\Facades\Validator;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class CustomerUserController extends CustomerBaseController
{
public array $validateMethodParams = [
'signIn' => [
'phone_area' => 'required|alpha_dash:ascii|max:5',
'phone' => 'required|numeric|max:15',
'password' => 'required|alpha_dash:ascii|max:50',
'device' => 'required|numeric|max:10',
],
'register' => [
'phone_area' => 'required|alpha_dash:ascii|max:5',
'phone' => 'required|numeric|max:15',
'password' => 'required|alpha_dash:ascii|max:50',
'device' => 'required|numeric|max:2',
'vr_code' => 'required|numeric|max:10',
],
'checkAccount' => [
'username' => 'alpha_dash:ascii|max:50',
'phone_area' => 'alpha_dash:ascii|max:5',
'phone' => 'numeric|max:15',
'email' => 'email|max:30',
],
'setUserInfo' => [
'nickname' => 'max:20',
'email' => 'email|max:30',
'username' => 'alpha_dash:ascii|max:50',
'phone_area' => 'alpha_dash:ascii|max:5',
'phone' => 'numeric|max:15',
'is_google_auth' => 'numeric|max:2',
],
'sendVrcodeCode' => [
'topic' => 'required|numeric',
'phone_area' => 'alpha_dash:ascii|max:5',
'phone' => 'numeric|max:15',
],
];
function getCustomerUserInfo(): \Illuminate\Http\JsonResponse
{
$oAuthService = new AuthService();
$token = $oAuthService->getTokenFromReq();
$aUser = $oAuthService->getCurrentUser();
$data = [
'token' => $token,
'user' => [
'id' =>$aUser['id'],
'username' => $aUser['username'],
'nickname' => $aUser['nickname'],
'is_google_auth' => $aUser['is_google_auth'],
'created_at' => $aUser['created_at'],
'updated_at' => $aUser['updated_at'],
],
];
return $this->success($data);
}
function signIn(): \Illuminate\Http\JsonResponse
{
$request = request();
$username = $request->input('username');
$password = $request->input('password');
$device = $request->input('device');
if(!in_array($device,Im::PLATFORM)) return $this->error('invalid 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();
$oImService = new ImService();
$token = $oAuthService->createTokenToUser($oUser->id,$device);
$imToken = $oImService->authUserToken($oUser->id,$device);
$data = [
'token' => $token,
'im_token' => $imToken,
'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 setUserInfo(): \Illuminate\Http\JsonResponse
{
$request = request();
$aReqData = $request->only([
'nickname',
'email',
'phone_area',
'phone',
'is_google_auth',
]);
$oAuthService = new AuthService();
$aUser = $oAuthService->getCurrentUser();
$aReqData['id'] = $aUser['id'];
$aReqData = array_filter($aReqData);
$oCustomerUser = new CustomerUser();
if(!$oCustomerUser->updateItem($aReqData)) return $this->error();
return $this->success();
}
function signOut(): \Illuminate\Http\JsonResponse
{
$oAuthService = new AuthService();
$token = $oAuthService->getTokenFromReq();
$aUser = $oAuthService->getCurrentUser();
$oAuthService->delTokenToUser($aUser['id'],$token);
return $this->success();
}
function register(): \Illuminate\Http\JsonResponse
{
$request = request();
$username = $request->input('username');
$password = $request->input('password');
$device = $request->input('device');
$sVrCode = $request->input('vr_code');
$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('注册失败');
}
//向im注册
$oImService = new ImService();
if(!$oImService->userUserRegister($oUser->id)) throw new AppException('im register 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 checkAccount()
{
$request = request();
$aReqData = $request->only([
'nickname',
'email',
'phone_area',
'phone',
]);
$aReqData = array_filter($aReqData);
if (empty($aReqData)) return $this->error();
$oCustomerUser = new CustomerUser();
$oUser = $oCustomerUser->findItemByAccount($aReqData,['id']);
if ($oUser) {
return $this->error('用户已存在');
}
return $this->success();
}
/**
* @throws ContainerExceptionInterface
* @throws AppException
* @throws NotFoundExceptionInterface
*/
function sendVrcodeCode(): \Illuminate\Http\JsonResponse
{
//发送短信验证码
$request = request();
$aReqData = $request->only([
'topic',
'phone_area',
'phone',
]);
if(!in_array($aReqData['topic'],VrCode::TOPIC)) return $this->error('invalid vrcode type');
$oVrCodeService = new VrCodeService();
$oVrCodeService->setIType($aReqData['topic']);
if($aReqData['topic'] == VrCode::TOPIC_REGISTER){
$validator = Validator::make($aReqData, [
'phone_area' => 'required|alpha_dash:ascii|max:5',
'phone' => 'required|numeric|max:15',
]);
if ($validator->fails()) {
return $this->error($validator->errors()->first());
}
$oCustomerUser = new CustomerUser();
$oUser = $oCustomerUser->findItemByPhone($aReqData['phone_area'],$aReqData['phone'],['id']);
if ($oUser) {
return $this->error('vrcode error');
}
$oVrCodeService->setSPhoneArea($aReqData['phone_area']);
$oVrCodeService->setSPhone($aReqData['phone']);
$oVrCodeService->sendCode();
return $this->success();
}else{
$oAuthService = new AuthService();
$aUser = $oAuthService->getCurrentUser();
if($aUser == null) return $this->error('user not login');
$oVrCodeService->setIUid($aUser['id']);
$oVrCodeService->sendCode();
return $this->success();
}
}
}