AuthorizationsController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Exceptions\SmsException;
  4. use App\Models\AdminRole;
  5. use App\Models\User;
  6. use App\Services\SmsService;
  7. use App\Services\TencentImAccountService;
  8. use App\Transformers\UserTransformer;
  9. use Carbon\Carbon;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Auth;
  12. use Illuminate\Support\Facades\Hash;
  13. use Illuminate\Support\Facades\Validator;
  14. class AuthorizationsController extends Controller
  15. {
  16. protected $tencentImAccountService;
  17. public function __construct(TencentImAccountService $tencentImAccountService)
  18. {
  19. $this->tencentImAccountService = $tencentImAccountService;
  20. }
  21. /**
  22. * 手机号登录
  23. * @param Request $request
  24. * @return \Illuminate\Http\JsonResponse
  25. */
  26. public function loginByMobile(Request $request)
  27. {
  28. $validator = Validator::make($request->all(), [
  29. 'mobile' => ['required', 'regex:/^1[3456789]\d{9}$/'],
  30. 'verifyKey' => 'bail|required|string',
  31. 'smsCode' => 'bail|required',
  32. ], [
  33. 'mobile.required'=>"手机号码必须",
  34. 'mobile.regex'=>"手机号码格式错误",
  35. 'verifyKey.required'=>"验证码必须",
  36. 'smsCode.required'=>"短信验证码必须",
  37. ]);
  38. if ($validator->fails()) {
  39. return $this->response()->errorForbidden($validator->messages()->first());
  40. }
  41. try {
  42. //验证短信验证码
  43. SmsService::checkSmsCodeByVerifyKey($request->verifyKey, $request->smsCode);
  44. } catch (SmsException $e) {
  45. abort(403, $e->getMessage());
  46. } catch (\Exception $e) {
  47. abort(403, '短信校验失败');
  48. }
  49. $user = User::firstOrCreate([
  50. 'mobile' => $request->input('mobile'),
  51. ]);
  52. if (!$user->ycode) {
  53. $user->ycode = $this->createCode();
  54. }
  55. // if (!$user->tencent_im_user_id) {
  56. // $user->tencent_im_user_id = $this->tencentImAccountService->accountImport($user);
  57. // }
  58. $user->save();
  59. $token = Auth::guard('api')->fromUser($user);
  60. self::updateLastLogin($user, $token);
  61. return $this->respondWithToken($token)->setStatusCode(201);
  62. }
  63. /**
  64. * 根据用户ID生成唯一邀请码
  65. * @param $user_id
  66. * @return string
  67. */
  68. public function createCode() {
  69. $code = create_invite_code();
  70. if(User::where(['ycode'=>$code])->first()){
  71. $code = create_invite_code();
  72. }
  73. return $code;
  74. }
  75. /**
  76. * 用户账号密码登录
  77. * @param Request $request
  78. * @return \Illuminate\Http\JsonResponse|void
  79. */
  80. public function loginByAccountPassword(Request $request)
  81. {
  82. $validator = Validator::make($request->all(), [
  83. 'mobile' => ['required', 'regex:/^1[3456789]\d{9}$/'],
  84. 'password' => 'required|string',
  85. ],[
  86. 'mobile.required'=>"手机号码必须",
  87. 'mobile.regex'=>"手机号码格式错误",
  88. 'password.required'=>"密码必须",
  89. ]);
  90. if ($validator->fails()) {
  91. return $this->response()->errorForbidden($validator->messages()->first());
  92. }
  93. if (!$user=User::where(['mobile' => $request->mobile])->first()) {
  94. return $this->response->errorNotFound('用户不存在!');
  95. }
  96. $credentials = $request->only('mobile', 'password');
  97. if (!$token = auth('api')->attempt($credentials)) {
  98. return $this->response->errorUnauthorized ('用户名或密码错误');
  99. }
  100. if (!$user->ycode) {
  101. $user->ycode = $this->createCode();
  102. }
  103. // if (!$user->tencent_im_user_id) {
  104. // $user->tencent_im_user_id = $this->tencentImAccountService->accountImport($user);
  105. // }
  106. // $user->save();
  107. self::updateLastLogin($user, $token);
  108. return $this->respondWithToken($token);
  109. }
  110. /**
  111. * 忘记密码
  112. */
  113. public function forgetPassword(Request $request){
  114. $validator = Validator::make($request->all(), [
  115. 'mobile' => ['required', 'regex:/^1[3456789]\d{9}$/'],
  116. 'verifyKey' => 'bail|required|string',
  117. 'smsCode' => 'bail|required',
  118. 'password' => 'bail|required',
  119. ],[
  120. 'mobile.required'=>"手机号码必须",
  121. 'mobile.regex'=>"手机号码格式错误",
  122. 'verifyKey.required'=>"验证码必须",
  123. 'smsCode.required'=>"短信验证码必须",
  124. 'password.required'=>"密码必须",
  125. ]);
  126. if ($validator->fails()) {
  127. return $this->response()->errorForbidden($validator->messages()->first());
  128. }
  129. // try {
  130. // //验证短信验证码
  131. // SmsService::checkSmsCodeByVerifyKey($request->verifyKey, $request->smsCode);
  132. // } catch (SmsException $e) {
  133. // abort(403, $e->getMessage());
  134. // } catch (\Exception $e) {
  135. // abort(403, '短信校验失败');
  136. // }
  137. $user = User::where(['mobile'=>$request->mobile])->first();
  138. $user->password =$request->password;// Hash::make($request->password);
  139. if($user->save()){
  140. return $this->response->noContent();
  141. }
  142. }
  143. /**
  144. * Get the authenticated User.
  145. *
  146. * @return \Illuminate\Http\JsonResponse
  147. */
  148. public function me()
  149. {
  150. $user = auth('api')->user();
  151. return $this->response->item($user, new UserTransformer());
  152. }
  153. /**
  154. * Log the user out (Invalidate the token).
  155. *
  156. * @return \Illuminate\Http\JsonResponse
  157. */
  158. public function logout()
  159. {
  160. auth('api')->logout();
  161. return response()->json(['message' => 'Successfully logged out']);
  162. }
  163. /**
  164. * Refresh a token.
  165. * 刷新token,如果开启黑名单,以前的token便会失效。
  166. * 值得注意的是用上面的getToken再获取一次Token并不算做刷新,两次获得的Token是并行的,即两个都可用。
  167. * @return \Illuminate\Http\JsonResponse
  168. */
  169. public function refresh()
  170. {
  171. return $this->respondWithToken(Auth::guard('api')->refresh());
  172. }
  173. static public function updateLastLogin(User $user, string $jwtToken)
  174. {
  175. $user->remember_token = $jwtToken;
  176. $user->last_login_time = Carbon::now();
  177. $user->last_login_ip = request()->ip();
  178. $user->save();
  179. }
  180. /**
  181. * Get the token array structure.
  182. *
  183. * @param string $token
  184. *
  185. * @return \Illuminate\Http\JsonResponse
  186. */
  187. protected function respondWithToken($token)
  188. {
  189. return response()->json([
  190. 'access_token' => $token,
  191. 'token_type' => 'Bearer',
  192. 'expires_in' => Auth::guard('api')->factory()->getTTL() * 60
  193. ]);
  194. }
  195. }