AuthorizationsController.php 8.5 KB

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