Commit cfc11a12 by 杨树贤

简单的注册登录接口

parent 6cc59598
......@@ -3,13 +3,15 @@
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(Request $request)
public function register(UserRegister $request)
{
$data = $request->only([
'mobile',
......@@ -18,7 +20,26 @@ class UserController extends Controller
'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;
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 @@
namespace App\Http\Services;
use App\Http\Models\UserModel;
class UserService
{
//注册接口
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'])) {
$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 ?: [];
}
}
......@@ -2,34 +2,34 @@
return [
'attributes' => [
'address' => '地址',
'age' => '年龄',
'available' => '可用的',
'city' => '城市',
'content' => '内容',
'country' => '国家',
'date' => '日期',
'day' => '天',
'description' => '描述',
'email' => '邮箱',
'excerpt' => '摘要',
'first_name' => '名',
'gender' => '性别',
'hour' => '时',
'last_name' => '姓',
'minute' => '分',
'mobile' => '手机',
'month' => '月',
'name' => '名称',
'password' => '密码',
'address' => '地址',
'age' => '年龄',
'available' => '可用的',
'city' => '城市',
'content' => '内容',
'country' => '国家',
'date' => '日期',
'day' => '天',
'description' => '描述',
'email' => '邮箱',
'excerpt' => '摘要',
'first_name' => '名',
'gender' => '性别',
'hour' => '时',
'last_name' => '姓',
'minute' => '分',
'mobile' => '手机',
'month' => '月',
'name' => '名称',
'password' => '密码',
'password_confirmation' => '确认密码',
'phone' => '电话',
'second' => '秒',
'sex' => '性别',
'size' => '大小',
'time' => '时间',
'title' => '标题',
'username' => '用户名',
'year' => '年',
'phone' => '电话',
'second' => '秒',
'sex' => '性别',
'size' => '大小',
'time' => '时间',
'title' => '标题',
'username' => '用户名',
'year' => '年',
],
];
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