AuthorizationsController.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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\DB;
  13. use Illuminate\Support\Facades\Hash;
  14. use Illuminate\Support\Facades\Validator;
  15. class AuthorizationsController extends Controller
  16. {
  17. protected $tencentImAccountService;
  18. public function __construct(TencentImAccountService $tencentImAccountService)
  19. {
  20. $this->tencentImAccountService = $tencentImAccountService;
  21. }
  22. /**
  23. * 手机号登录
  24. * @param Request $request
  25. * @return \Illuminate\Http\JsonResponse
  26. */
  27. public function login_by_mobile(Request $request)
  28. {
  29. $validator = Validator::make($request->all(), [
  30. 'mobile' => ['required', 'regex:/^1[3456789]\d{9}$/'],
  31. 'verifyKey' => 'bail|required|string',
  32. 'smsCode' => 'bail|required',
  33. ], [
  34. 'mobile.required'=>"手机号码必须",
  35. 'mobile.regex'=>"手机号码格式错误",
  36. 'verifyKey.required'=>"验证码必须",
  37. 'smsCode.required'=>"短信验证码必须",
  38. ]);
  39. if ($validator->fails()) {
  40. return $this->response()->errorForbidden($validator->messages()->first());
  41. }
  42. try {
  43. //验证短信验证码
  44. SmsService::checkSmsCodeByVerifyKey($request->verifyKey, $request->smsCode);
  45. } catch (SmsException $e) {
  46. abort(403, $e->getMessage());
  47. } catch (\Exception $e) {
  48. abort(403, '短信校验失败');
  49. }
  50. $user = User::firstOrCreate([
  51. 'mobile' => $request->input('mobile'),
  52. ]);
  53. if (!$user->ycode) {
  54. $user->ycode = $this->create_code();
  55. }
  56. if (!$user->tencent_im_user_id) {
  57. $user->tencent_im_user_id = $this->tencentImAccountService->accountImport($user);
  58. }
  59. $user->save();
  60. $token = Auth::guard('api')->fromUser($user);
  61. self::updateLastLogin($user, $token);
  62. $resdata['token'] = "Bearer ".$token;
  63. $resdata['sex'] = $user->sex;
  64. $resdata['password'] = $user->password?1:0;
  65. return response()->json($resdata);
  66. }
  67. /**
  68. * 根据用户ID生成唯一邀请码
  69. * @param $user_id
  70. * @return string
  71. */
  72. public function create_code() {
  73. $code = create_invite_code();
  74. if(User::where(['ycode'=>$code])->first()){
  75. $code = create_invite_code();
  76. }
  77. return $code;
  78. }
  79. /**
  80. * 用户账号密码登录
  81. * @param Request $request
  82. * @return \Illuminate\Http\JsonResponse|void
  83. */
  84. public function login_by_account_password(Request $request)
  85. {
  86. $validator = Validator::make($request->all(), [
  87. 'mobile' => ['required', 'regex:/^1[3456789]\d{9}$/'],
  88. 'password' => 'required|string',
  89. ],[
  90. 'mobile.required'=>"手机号码必须",
  91. 'mobile.regex'=>"手机号码格式错误",
  92. 'password.required'=>"密码必须",
  93. ]);
  94. if ($validator->fails()) {
  95. return $this->response()->errorForbidden($validator->messages()->first());
  96. }
  97. if (!$user=User::where(['mobile' => $request->mobile])->first()) {
  98. return $this->response->errorNotFound('用户不存在!');
  99. }
  100. $credentials = $request->only('mobile', 'password');
  101. if (!$token = auth('api')->attempt($credentials)) {
  102. return $this->response->errorUnauthorized ('用户名或密码错误');
  103. }
  104. if (!$user->ycode) {
  105. $user->ycode = $this->create_code();
  106. }
  107. if (!$user->tencent_im_user_id) {
  108. $user->tencent_im_user_id = $this->tencentImAccountService->accountImport($user);
  109. }
  110. $user->save();
  111. self::updateLastLogin($user, $token);
  112. $resdata['token'] ="Bearer ".$token;
  113. $resdata['sex'] = $user->sex;
  114. $resdata['password'] = $user->password?1:0;
  115. return response()->json($resdata);
  116. }
  117. /**
  118. * 注册账号
  119. */
  120. public function register(Request $request){
  121. $validator = Validator::make($request->all(), [
  122. 'mobile' => ['required', 'regex:/^1[3456789]\d{9}$/'],
  123. 'password' => 'bail|required',
  124. ],[
  125. 'mobile.required'=>"手机号码必须",
  126. 'mobile.regex'=>"手机号码格式错误",
  127. 'password.required'=>"密码必须",
  128. ]);
  129. if ($validator->fails()){
  130. return $this->response()->errorForbidden($validator->messages()->first());
  131. }
  132. if(User::where(['mobile'=>$request->mobile])->first()){
  133. return $this->response->errorForbidden("该手机号码已使用");
  134. }
  135. $ins = array();
  136. $ins['mobile'] = $request->mobile;
  137. $ins['password'] = $request->password;
  138. if(User::create($ins)){
  139. return response()->json(['message'=>"注册成功"]);
  140. }else{
  141. return $this->response->errorForbidden("注册失败");
  142. }
  143. }
  144. /**
  145. * 忘记密码
  146. */
  147. public function forget_password(Request $request){
  148. $validator = Validator::make($request->all(), [
  149. 'mobile' => ['required', 'regex:/^1[3456789]\d{9}$/'],
  150. 'verifyKey' => 'bail|required|string',
  151. 'smsCode' => 'bail|required',
  152. 'password' => 'bail|required',
  153. ],[
  154. 'mobile.required'=>"手机号码必须",
  155. 'mobile.regex'=>"手机号码格式错误",
  156. 'verifyKey.required'=>"验证码必须",
  157. 'smsCode.required'=>"短信验证码必须",
  158. 'password.required'=>"密码必须",
  159. ]);
  160. if ($validator->fails()) {
  161. return $this->response()->errorForbidden($validator->messages()->first());
  162. }
  163. try {
  164. //验证短信验证码
  165. SmsService::checkSmsCodeByVerifyKey($request->verifyKey, $request->smsCode);
  166. } catch (SmsException $e) {
  167. abort(403, $e->getMessage());
  168. } catch (\Exception $e) {
  169. abort(403, '短信校验失败');
  170. }
  171. $user = User::where(['mobile'=>$request->mobile])->first();
  172. $user->password =$request->password;// Hash::make($request->password);
  173. if($user->save()){
  174. return $this->response->noContent();
  175. }
  176. }
  177. /**
  178. * 用户协议
  179. */
  180. public function xieyi(Request $request){
  181. if(isset($request->cont) && $request->cont==1){
  182. $data = DB::table("document")->where(['id'=>$request->type])->first();
  183. return response()->json(['data'=>$data]);
  184. }else{
  185. if($request->type==1){
  186. $url = "https://".$_SERVER['HTTP_HOST'].'/xieyi/yhxy.html';
  187. }else{
  188. $url = "https://".$_SERVER['HTTP_HOST'].'/xieyi/yszc.html';
  189. }
  190. return response()->json(['url'=>$url]);
  191. }
  192. }
  193. /**
  194. * Get the authenticated User.
  195. *
  196. * @return \Illuminate\Http\JsonResponse
  197. */
  198. public function me()
  199. {
  200. $user = auth('api')->user();
  201. return $this->response->item($user, new UserTransformer());
  202. }
  203. /**
  204. * Log the user out (Invalidate the token).
  205. *
  206. * @return \Illuminate\Http\JsonResponse
  207. */
  208. public function logout()
  209. {
  210. auth('api')->logout();
  211. return response()->json(['message' => '退出成功!']);
  212. }
  213. /**
  214. * Refresh a token.
  215. * 刷新token,如果开启黑名单,以前的token便会失效。
  216. * 值得注意的是用上面的getToken再获取一次Token并不算做刷新,两次获得的Token是并行的,即两个都可用。
  217. * @return \Illuminate\Http\JsonResponse
  218. */
  219. public function refresh()
  220. {
  221. return $this->respondWithToken(Auth::guard('api')->refresh());
  222. }
  223. static public function updateLastLogin(User $user, string $jwtToken)
  224. {
  225. $user->remember_token = $jwtToken;
  226. $user->last_login_time = Carbon::now();
  227. $user->last_login_ip = request()->ip();
  228. $user->save();
  229. }
  230. /**
  231. * Get the token array structure.
  232. *
  233. * @param string $token
  234. *
  235. * @return \Illuminate\Http\JsonResponse
  236. */
  237. protected function respondWithToken($token)
  238. {
  239. return response()->json([
  240. 'access_token' => $token,
  241. 'token_type' => 'Bearer',
  242. 'expires_in' => Auth::guard('api')->factory()->getTTL() * 60
  243. ]);
  244. }
  245. }