ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
``` <?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()); } } ```