```
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\BaseController;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use App\Models\User;
use Validator;
class UserController extends BaseController
{
/**
* 注册
* @author Neo
* @param Request $request 请求
* @return mixed
*/
public function register(Request $request)
{
// 验证参数
$validator = Validator::make($request->all(), [
'name' => 'required|string',
'email' => 'required|string|email',
'password' => 'required|string|confirmed',
]);
if ($validator->fails())
{
return response()->json(['failed' => [],'status' => 'error', 'message' => '格式错误']);
}
// 字符@让name和email获得唯一性,可用name或者email做登录,
// 日后加入电话号码,再分1.电话和2.用户名/邮箱的输入框
$nameStr = strstr(request('name'), '@');
if($nameStr)
{
return response()->json(['status' => 'success', 'code'=> 201, 'message' => '用户名不可有@字符']);
}
$name = User::where('name', request('name'))->first();
if(!empty($name))
{
return response()->json(['status' => 'success', 'code'=> 201, 'message' => '用户名已经存在']);
}
$email = User::where('email', request('email'))->first();
if(!empty($email))
{
return response()->json(['status' => 'success', 'code'=> 202, 'message' => '邮箱已经存在']);
}
// 创建新用户
try {
$user = new User();
$user->name = request('name');
$user->email = request('email');
$user->password = app('hash')->make(request('password'));
if ($user->save())
{
return $this->login($request);
}
} catch (\Exception $e) {
return response()->json(['status' => 'error', 'message' => $e->getMessage()]);
}
}
/**
* 登录
* @author Neo
* @param Request $request 请求
* @return mixed
*/
public function login(Request $request)
{
$validator = Validator::make($request->all(), [
'username' => 'required|string',
'password' => 'required|string',
'remember_me' => 'boolean',
]);
if ($validator->fails())
{
return response()->json(['failed' => [],'status' => 'error', 'message' => '格式错误']);
}
$user = User::where('name', request('username'))->first();
if(empty($user))
{
return response()->json(['status' => 'success', 'code'=> 202, 'message' => '用户名和邮箱还没有注册']);
} else
{
$credentials['email'] = $user->email;
$credentials['password'] = request('password');
}
if(!Auth::attempt($credentials))
return response()->json([
'message' => 'Unauthorized'
], 401);
$user = $request->user();
$tokenResult = $user->createToken('Personal Access Token');
$token = $tokenResult->token;
if ($request->remember_me)
{
$token->expires_at = Carbon::now()->addWeeks(1);
}
$token->save();
$data = [
'status' => true,
'userId' => $user['id'],
'username' => $user['name'],
// 'accessToken' => $tokenResult->accessToken,
'token' => $tokenResult->accessToken,
'tokenType' => 'Bearer',
'expiresAt' => Carbon::parse(
$tokenResult->token->expires_at
)->toDateTimeString(),
];
return response()->json([ 'code' => 200, 'message' => '登录成功', 'data' => $data ]);
}
/**
* 登出
* @author Neo
* @param Request $request 请求
* @return mixed
*/
public function logout(Request $request)
{
try {
auth()->user()->tokens()->each(function ($token) {
$token->delete();
});
return response()->json(['status' => 'success', 'message' => '登出成功']);
} catch (\Exception $e) {
return response()->json(['status' => 'error', 'message' => $e->getMessage()]);
}
}
/**
* 用户数据
* @author Neo
* @param Request $request 请求
* @return mixed
*/
public function info(Request $request)
{
return response()->json($request->user());
}
}
```
- 分布式架构设计概要
- 技术管理
- 1.流程规范
- 2.精准效率
- 3.同步工具
- 代码审核
- 编程思想
- 控制反转和依赖注入
- public、private、protected
- MySQL
- MySQL安装
- MySQL常用
- MySQL主从配置
- 读写分离
- 分库分表
- 高可用
- MySQL集群
- MySQL优化
- MySQL慢查询优化
- 数据结构
- MySQL备份
- MySQL導出導入
- MSSQL
- 導出導入
- Redis
- Redis日常操作
- Redis安装
- Redis主从配置
- Redis哨兵
- 队列
- 分布式式锁
- Redis集群
- redis的缓存雪崩与缓存穿透
- Linux
- Linux基本操作
- Linux基础操作1
- Linux安装
- Linux安装Nginx
- Linux安装MySQL
- Linux安装PHP
- Linux安装Redis
- Linux安装Swoole
- Linux安装Elasticsearch
- Linux安装Mycat
- Linux优化
- 集群
- 服务器管理
- Nginx
- Nginx 首页外链接出现404问题
- Nginx常用命令行
- LNMP一键安装的404错误
- http和https站点设置
- Linux shell expect spawn
- Ubuntu环境
- 宝塔
- 定时任务
- Windows
- 生成OpenSSL证书
- Git
- Git基本操作
- Git练习
- Git上传到远程仓库,远程仓库更新到指定文件夹
- Git案例1
- Git案例2
- Git部署
- 远程连接
- 服务器原理
- 进程和线程
- 腾讯云
- 宝塔命令行
- 消息队列系统
- RabbitMQ
- RabbitMQ+Laravel
- Kafka
- Elasticsearch
- Laravel+Elasticsearch
- Elasticsearch Demo:ES搜索题目和内容
- 分布式
- PHP
- Laravel高级集成开发
- PHP技术栈
- LNMP
- LNMP+Laravel出现问题
- PHP+MySQL
- PHP连接MySQL
- PHP+Redis
- PHP连接Redis
- PHP+Redis简单操作
- Laravel+Redis
- Laravel+Swoole
- Swoole
- Swoole TCP案例
- Swoole HTTP案例
- Sswoole WebSocket案例
- Swoole发短信案例
- ThinkPHP
- Java技术栈
- 文档参考
- docker
- docker一般使用
- docker项目1
- docker+lnmp
- docker搭建wordpress
- Docker部署单体到微服务架构(Laravel)
- Docker部署单体到微服务架构(Spring Boot)
- docker部署vue项目
- Dockerfile
- 其他
- 笔记1
- 笔记2
- 开发速度
- 代码质量
- 踩过的坑
- 编辑器
- VSCode