发布推送

This commit is contained in:
ROmani
2024-02-26 00:41:25 +08:00
parent 70304f730b
commit 1a9008b318
33 changed files with 1515 additions and 216 deletions

View File

@ -1,17 +1,16 @@
<?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\Models\Customer\CustomerUserModel;
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;
use App\Tools\Tools;
class CustomerUserController extends CustomerBaseController
{
@ -21,6 +20,7 @@ class CustomerUserController extends CustomerBaseController
'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',
@ -35,7 +35,7 @@ class CustomerUserController extends CustomerBaseController
'phone' => 'numeric|max:15',
'email' => 'email|max:30',
],
'setUserInfo' => [
'updateUserInfo' => [
'nickname' => 'max:20',
'email' => 'email|max:30',
'username' => 'alpha_dash:ascii|max:50',
@ -43,11 +43,6 @@ class CustomerUserController extends CustomerBaseController
'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
@ -58,7 +53,7 @@ class CustomerUserController extends CustomerBaseController
$data = [
'token' => $token,
'user' => [
'id' =>$aUser['id'],
'id' => $aUser['id'],
'username' => $aUser['username'],
'nickname' => $aUser['nickname'],
'is_google_auth' => $aUser['is_google_auth'],
@ -69,29 +64,45 @@ class CustomerUserController extends CustomerBaseController
return $this->success($data);
}
/**
* @throws AppException
*/
function signIn(): \Illuminate\Http\JsonResponse
{
$request = request();
$username = $request->input('username');
$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');
if (!in_array($device, Im::PLATFORM)) return $this->error('invalid device');
$oCustomerUser = new CustomerUser();
$oUser = $oCustomerUser->findItemByUsername($username);
$oCustomerUser = new CustomerUserModel();
$oUser = $oCustomerUser->findItemByPhone($phone_area, $phone);
if (!$oUser) {
return $this->error('用户名不存在');
}
if (!$oCustomerUser->checkPasswd($oUser->id,$password)) {
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);
$token = $oAuthService->createTokenToUser($oUser->id, $device);
$imToken = $oImService->authUserToken($oUser->id, $device);
$data = [
'token' => $token,
@ -109,11 +120,12 @@ class CustomerUserController extends CustomerBaseController
}
function setUserInfo(): \Illuminate\Http\JsonResponse
function updateUserInfo(): \Illuminate\Http\JsonResponse
{
$request = request();
$aReqData = $request->only([
'nickname',
'username',
'email',
'phone_area',
'phone',
@ -123,8 +135,8 @@ class CustomerUserController extends CustomerBaseController
$aUser = $oAuthService->getCurrentUser();
$aReqData['id'] = $aUser['id'];
$aReqData = array_filter($aReqData);
$oCustomerUser = new CustomerUser();
if(!$oCustomerUser->updateItem($aReqData)) return $this->error();
$oCustomerUser = new CustomerUserModel();
if (!$oCustomerUser->updateItem($aReqData)) return $this->error();
return $this->success();
}
@ -133,29 +145,39 @@ class CustomerUserController extends CustomerBaseController
$oAuthService = new AuthService();
$token = $oAuthService->getTokenFromReq();
$aUser = $oAuthService->getCurrentUser();
$oAuthService->delTokenToUser($aUser['id'],$token);
$oAuthService->delTokenToUser($aUser['id'], $token);
return $this->success();
}
function register(): \Illuminate\Http\JsonResponse
{
$request = request();
$username = $request->input('username');
$phone_area = $request->input('phone_area');
$phone = $request->input('phone');
$password = $request->input('password');
$device = $request->input('device');
$sVrCode = $request->input('vr_code');
$oCustomerUser = new CustomerUser();
$oUser = $oCustomerUser->findItemByUsername($username,['id']);
//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([
'username' => $username,
'phone_area' => $phone_area,
'phone' => $phone,
'password' => $password,
'nickname' => $username,
'nickname' => 'user_' . Tools::generateRandStr(10),
]);
if (!$oUser) {
@ -164,10 +186,10 @@ class CustomerUserController extends CustomerBaseController
//向im注册
$oImService = new ImService();
if(!$oImService->userUserRegister($oUser->id)) throw new AppException('im register error');
if (!$oImService->userUserRegister($oUser->id)) throw new AppException('im register error');
$oAuthService = new AuthService();
$token = $oAuthService->createTokenToUser($oUser->id,$device);
$token = $oAuthService->createTokenToUser($oUser->id, $device);
$data = [
'token' => $token,
@ -188,64 +210,20 @@ class CustomerUserController extends CustomerBaseController
{
$request = request();
$aReqData = $request->only([
'nickname',
'username',
'email',
'phone_area',
'phone',
]);
$aReqData = array_filter($aReqData);
if (empty($aReqData)) return $this->error();
$oCustomerUser = new CustomerUser();
$oUser = $oCustomerUser->findItemByAccount($aReqData,['id']);
$oCustomerUser = new CustomerUserModel();
$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();
}
}
}