邀请码支持手机号
parent
f04c8080aa
commit
4c22e744fd
|
|
@ -6,10 +6,11 @@ use App\Constants\Device;
|
|||
use App\Endpoint\Api\Http\Controllers\Controller;
|
||||
use App\Endpoint\Api\Http\Requests\RegisterRequest;
|
||||
use App\Exceptions\BizException;
|
||||
use App\Helpers\PhoneNumber;
|
||||
use App\Models\SmsCode;
|
||||
use App\Models\User;
|
||||
use App\Models\UserInfo;
|
||||
use App\Services\SmsCodeService;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Throwable;
|
||||
|
||||
|
|
@ -32,17 +33,9 @@ class RegisterController extends Controller
|
|||
$input['verify_code']
|
||||
);
|
||||
|
||||
$inviter = null;
|
||||
|
||||
if (
|
||||
$request->filled('code') &&
|
||||
is_null($inviter = UserInfo::where('code', $input['code'])->first())
|
||||
) {
|
||||
throw new BizException(__('Invalid invitation code'));
|
||||
}
|
||||
|
||||
$time = now();
|
||||
$ip = $request->realIp();
|
||||
$inviter = $this->findUserByCode((string) Arr::get($input, 'code'));
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
|
@ -70,4 +63,31 @@ class RegisterController extends Controller
|
|||
$user->createDeviceToken(Device::UNIAPP)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过邀请码搜索用户
|
||||
*
|
||||
* @param string $code
|
||||
* @return \App\Models\User|null
|
||||
*
|
||||
* @throws \App\Exceptions\BizException
|
||||
*/
|
||||
protected function findUserByCode(string $code): ?User
|
||||
{
|
||||
if ($code === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$user = User::when(PhoneNumber::validate($code), function ($query) use ($code) {
|
||||
$query->where('phone', $code);
|
||||
}, function ($query) use ($code) {
|
||||
$query->whereRelation('userInfo', 'code', $code);
|
||||
})->first();
|
||||
|
||||
if ($user === null) {
|
||||
throw new BizException(__('Inviter does not exist'));
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
namespace App\Helpers;
|
||||
|
||||
class PhoneNumber
|
||||
{
|
||||
/**
|
||||
* 确认参数是否是一个有效的手机号
|
||||
*
|
||||
* @param string $var
|
||||
* @return bool
|
||||
*/
|
||||
public static function validate(string $var): bool
|
||||
{
|
||||
return (bool) preg_match('/^1[3-9]\d{9}$/', $var);
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Rules;
|
||||
|
||||
use App\Helpers\PhoneNumber as PhoneNumberHelper;
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
|
||||
class PhoneNumber implements Rule
|
||||
|
|
@ -15,7 +16,7 @@ class PhoneNumber implements Rule
|
|||
*/
|
||||
public function passes($attribute, $value): bool
|
||||
{
|
||||
return (bool) preg_match('/^1[3-9]\d{9}$/', $value);
|
||||
return PhoneNumberHelper::validate($value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -2,9 +2,9 @@
|
|||
":resource not found": ":resource 未找到",
|
||||
"Incorrect account or password": "账户或密码错误",
|
||||
"Invalid captcha": "无效的验证码",
|
||||
"Invalid invitation code": "无效的邀请码",
|
||||
"Invalid verification code": "无效的验证码",
|
||||
"Invalid verification code type": "无效的验证码类型",
|
||||
"Inviter does not exist": "邀请人不存在",
|
||||
"Registration failed, please try again": "注册失败,请重试",
|
||||
"Sending too frequently, please try again later": "发送过于频繁,请稍后再试",
|
||||
"The phone number is already registered": "手机号码已被注册",
|
||||
|
|
|
|||
Loading…
Reference in New Issue