Commit cfc11a12 by 杨树贤

简单的注册登录接口

parent 6cc59598
...@@ -3,13 +3,15 @@ ...@@ -3,13 +3,15 @@
namespace App\Http\Controllers\Api; namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Http\Requests\UserLogin;
use App\Http\Requests\UserRegister;
use App\Http\Services\UserService; use App\Http\Services\UserService;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class UserController extends Controller class UserController extends Controller
{ {
public function register(Request $request) public function register(UserRegister $request)
{ {
$data = $request->only([ $data = $request->only([
'mobile', 'mobile',
...@@ -18,7 +20,26 @@ class UserController extends Controller ...@@ -18,7 +20,26 @@ class UserController extends Controller
'salt', 'salt',
]); ]);
UserService::register($data); $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]);
} }
......
...@@ -7,4 +7,9 @@ use Illuminate\Database\Eloquent\Model; ...@@ -7,4 +7,9 @@ use Illuminate\Database\Eloquent\Model;
class UserModel extends Model class UserModel extends Model
{ {
// //
protected $table = 'user';
public $timestamps = false;
const STATUS_ENABLE = 1;
const STATUS_DISABLE = -1;
} }
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UserLogin extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
//
];
}
}
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UserRegister extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'password' => 'required',
'salt' => 'required',
'mobile' => 'required_without:email',
'email' => 'required_without:mobile'
];
}
}
<?php
namespace App\Http\Services;
use App\Http\Models\UserModel;
class PasswordService
{
public static function passwordHash($pwd, $salt)
{
return md5(md5($pwd) . $salt);
}
}
...@@ -2,14 +2,43 @@ ...@@ -2,14 +2,43 @@
namespace App\Http\Services; namespace App\Http\Services;
use App\Http\Models\UserModel;
class UserService class UserService
{ {
//注册接口
public static function register($data) public static function register($data)
{ {
//先去判断是否存在 //先去判断是否存在,存在的话就直接返回用户id即可
if (!empty($data['mobile'])) {
$ucId = UserModel::where('mobile', $data['mobile'])->value('uc_id');
} else {
//去新增注册
$data['create_time'] = time();
$ucId = UserModel::insertGetId($data);
}
return $ucId;
}
//登录接口
public function login($data)
{
$loginQuery = UserModel::where('status', UserModel::STATUS_ENABLE);
if (!empty($data['mobile'])) { if (!empty($data['mobile'])) {
$salt = UserModel::where('mobile', $data['mobile'])->value('salt');
$loginQuery->where('mobile', $data['mobile']);
} else {
$salt = UserModel::where('email', $data['email'])->value('salt');
$loginQuery->where('email', $data['email']);
} }
$passwordHash = PasswordService::passwordHash($data['password'], $salt);
$user = $loginQuery->where('password', $passwordHash)->first();
return $user ?: [];
} }
} }
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