MobileLoginTrait.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Traits;
  3. use App\Http\Api\Forms\LoginMobileForm;
  4. use App\Services\LoginService;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Request;
  7. /**
  8. * 手机号快捷登录
  9. *
  10. * Trait MobileLoginTrait
  11. * @package App\Traits
  12. */
  13. trait MobileLoginTrait
  14. {
  15. /**
  16. * 是否开启登录日志记录
  17. *
  18. * @var bool
  19. */
  20. private $is_open_login_log = false;
  21. public function loginInMobile()
  22. {
  23. $formHandler = new LoginMobileForm();
  24. $authorize = $formHandler->authorize(Request::all());
  25. if ($authorize) {
  26. return response()->json(['result' => false, 'code' => 2001, 'msg' => $authorize[0] ?? '']);
  27. }
  28. $data = $formHandler->getData();
  29. //判断手机号是否正确和存在
  30. $resultObj = DB::table('users')->where('store_id', $this->storeId)->where('mobile', $data['mobile'])->first();
  31. if ($resultObj) {
  32. $result = get_object_vars($resultObj);
  33. //设置登录状态
  34. $loginService = new LoginService($this->siteAliasName);
  35. $loginService->setLoginCookie($result);
  36. unset($result['password']);
  37. unset($result['wx_info']);
  38. unset($result['wx_openid']);
  39. $msg = array('result' => true, 'code' => 1001, 'msg' => '登录成功', 'data' => $result);
  40. } else {
  41. $msg = array('result' => false, 'code' => 2006, 'msg' => '帐号不存在,请先注册!');
  42. }
  43. return response()->json($msg);
  44. }
  45. }