Commit 65a67c6f by 杨树贤

完善登录注册

parent cfc11a12
......@@ -20,20 +20,6 @@ class InvalidRequestException extends \Exception
public function render(Request $request)
{
$request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
$path_info = parse_url($request_uri);
$err_info = [
'domain' => isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '',
'interface' => isset($path_info) ? $path_info['path'] : '',
'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '',
'ip' => request()->getClientIp(),
'time' => time(),
'other' => '',
'request_params' => $_REQUEST,
'msg' => $this->getMessage(),
"code" => $this->getCode(),
];
Log::error(json_encode($err_info, JSON_UNESCAPED_UNICODE));
return response()->json(json_decode(Response::setError($this->message), true));
}
}
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Requests\UserLogin;
use App\Http\Requests\UserRegister;
use App\Http\Services\UserService;
use Illuminate\Http\Request;
class AuthController extends Controller
{
public function register(UserRegister $request)
{
$data = $request->only([
'mobile',
'email',
'password',
'salt',
]);
$ucId = UserService::register($data);
return $this->setSuccessData(['uc_id' => $ucId]);
}
public function login(UserLogin $request)
{
$data = $request->only([
'mobile',
'email',
'password',
]);
$ucId = UserService::login($data);
if (empty($ucId)) {
$this->setError('账号和密码校验失败');
}
return $this->setSuccessData(['uc_id' => $ucId]);
}
}
......@@ -3,44 +3,8 @@
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Requests\UserLogin;
use App\Http\Requests\UserRegister;
use App\Http\Services\UserService;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function register(UserRegister $request)
{
$data = $request->only([
'mobile',
'email',
'password',
'salt',
]);
$ucId = UserService::register($data);
$this->setSuccessData(['uc_id' => $ucId]);
}
public function login(UserLogin $request)
{
$data = $request->only([
'mobile',
'email',
'password',
]);
$ucId = UserService::login($data);
if (empty($ucId)) {
$this->setError('账号和密码校验失败');
}
$this->setSuccessData(['uc_id' => $ucId]);
}
}
......@@ -22,8 +22,7 @@ class Kernel extends HttpKernel
\App\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
// \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\Monitor::class
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];
/**
......@@ -49,7 +48,6 @@ class Kernel extends HttpKernel
\App\Http\Middleware\ApiAuthenticate::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\TrimStrings::class,
\App\Http\Middleware\Permission::class,
],
'sync' => [
......@@ -74,6 +72,5 @@ class Kernel extends HttpKernel
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
// 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'monitor' => \App\Http\Middleware\Monitor::class,
];
}
......@@ -2,7 +2,10 @@
namespace App\Http\Requests;
use App\Exceptions\InvalidRequestException;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
class UserLogin extends FormRequest
{
......@@ -13,7 +16,7 @@ class UserLogin extends FormRequest
*/
public function authorize()
{
return false;
return true;
}
/**
......@@ -24,7 +27,14 @@ class UserLogin extends FormRequest
public function rules()
{
return [
//
'password' => 'required',
'mobile' => 'required_without:email',
'email' => 'required_without:mobile'
];
}
protected function failedValidation(Validator $validator)
{
throw new InvalidRequestException($validator->errors()->first());
}
}
......@@ -2,7 +2,10 @@
namespace App\Http\Requests;
use App\Exceptions\InvalidRequestException;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
class UserRegister extends FormRequest
{
......@@ -30,4 +33,9 @@ class UserRegister extends FormRequest
'email' => 'required_without:mobile'
];
}
protected function failedValidation(Validator $validator)
{
throw new InvalidRequestException($validator->errors()->first());
}
}
......@@ -2,6 +2,7 @@
namespace App\Http\Services;
use App\Exceptions\InvalidRequestException;
use App\Http\Models\UserModel;
class UserService
......@@ -14,16 +15,21 @@ class UserService
if (!empty($data['mobile'])) {
$ucId = UserModel::where('mobile', $data['mobile'])->value('uc_id');
} else {
$ucId = UserModel::where('email', $data['email'])->value('uc_id');
}
if ($ucId) {
return $ucId;
} else {
//去新增注册
$data['create_time'] = time();
$ucId = UserModel::insertGetId($data);
}
return $ucId;
}
//登录接口
public function login($data)
public static function login($data)
{
$loginQuery = UserModel::where('status', UserModel::STATUS_ENABLE);
if (!empty($data['mobile'])) {
......@@ -34,6 +40,10 @@ class UserService
$loginQuery->where('email', $data['email']);
}
if (empty($salt)) {
throw new InvalidRequestException('该账号不存在');
}
$passwordHash = PasswordService::passwordHash($data['password'], $salt);
$user = $loginQuery->where('password', $passwordHash)->first();
......
......@@ -37,10 +37,10 @@ return [
// 新魔方
'mysql' => [
'driver' => 'mysql',
'host' => get_resource_config_section('db', 'db_topic')['host'],
'database' => get_resource_config_section('db', 'db_topic')['db'], // liexin_crm
'username' => get_resource_config_section('db', 'db_topic')['user'],
'password' => get_resource_config_section('db', 'db_topic')['passwd'],
'host' => get_resource_config_section('db', 'db_ucenter')['host'],
'database' => get_resource_config_section('db', 'db_ucenter')['db'], // liexin_crm
'username' => get_resource_config_section('db', 'db_ucenter')['user'],
'password' => get_resource_config_section('db', 'db_ucenter')['passwd'],
'port' => 3306,
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
......
......@@ -4,5 +4,6 @@ use Illuminate\Support\Facades\Route;
//接口相关路由
Route::namespace('Api')->group(function () {
Route::match(["get", "post"], '/customFormData/exportCustomFormData', 'CustomFormDataController@exportCustomFormData');
Route::match(["GET", "POST"], '/auth/register', 'AuthController@register');
Route::match(["GET", "POST"], '/auth/login', 'AuthController@login');
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment