Files
cycle_api/app/Http/Controllers/Customer/CustomerUserController.php
2024-02-26 00:41:25 +08:00

230 lines
7.5 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\CustomerUserModel;
use App\Service\AuthService;
use App\Service\ImService;
use App\Service\VrCodeService;
use App\Tools\Tools;
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',
'vr_code' => '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',
],
'updateUserInfo' => [
'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',
],
];
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);
}
/**
* @throws AppException
*/
function signIn(): \Illuminate\Http\JsonResponse
{
$request = request();
$phone_area = $request->input('phone_area');
$phone = $request->input('phone');
$password = $request->input('password');
$vrcode = $request->input('vrcode');
$device = $request->input('device');
if (!in_array($device, Im::PLATFORM)) return $this->error('invalid device');
$oCustomerUser = new CustomerUserModel();
$oUser = $oCustomerUser->findItemByPhone($phone_area, $phone);
if (!$oUser) {
return $this->error('用户名不存在');
}
if (!empty($vrcode)) { //phone login
//check vrcode
$oVrCodeService = new VrCodeService();
$oVrCodeService->setIType(VrCode::TOPIC_LOGIN);
$oVrCodeService->setSPhoneArea($phone_area);
$oVrCodeService->setSPhone($phone);
if (!$oVrCodeService->checkCode($vrcode)) return $this->error('验证码错误');
} elseif (!empty($password)) { //password login
if (!$oCustomerUser->checkPasswd($oUser->id, $password)) {
return $this->error('密码错误');
}
} else {
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 updateUserInfo(): \Illuminate\Http\JsonResponse
{
$request = request();
$aReqData = $request->only([
'nickname',
'username',
'email',
'phone_area',
'phone',
'is_google_auth',
]);
$oAuthService = new AuthService();
$aUser = $oAuthService->getCurrentUser();
$aReqData['id'] = $aUser['id'];
$aReqData = array_filter($aReqData);
$oCustomerUser = new CustomerUserModel();
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();
$phone_area = $request->input('phone_area');
$phone = $request->input('phone');
$password = $request->input('password');
$device = $request->input('device');
$sVrCode = $request->input('vr_code');
//check vrcode
$oVrCodeService = new VrCodeService();
$oVrCodeService->setIType(VrCode::TOPIC_REGISTER);
$oVrCodeService->setSPhoneArea($phone_area);
$oVrCodeService->setSPhone($phone);
if (!$oVrCodeService->checkCode($sVrCode)) return $this->error('验证码错误');
$oCustomerUser = new CustomerUserModel();
$oUser = $oCustomerUser->findItemByPhone($phone_area, $phone);
if ($oUser) {
return $this->error('用户名已存在');
}
$oUser = $oCustomerUser->addUser([
'phone_area' => $phone_area,
'phone' => $phone,
'password' => $password,
'nickname' => 'user_' . Tools::generateRandStr(10),
]);
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([
'username',
'email',
'phone_area',
'phone',
]);
$aReqData = array_filter($aReqData);
if (empty($aReqData)) return $this->error();
$oCustomerUser = new CustomerUserModel();
$oUser = $oCustomerUser->findItemByAccount($aReqData, ['id']);
if ($oUser) {
return $this->error('用户已存在');
}
return $this->success();
}
}