Login.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * Created by PhpStorm
  4. * User:林志杰
  5. * Email:[email protected]
  6. * Motto:纵有疾风起,人生不言弃!
  7. * Time:2020/2/8 1:10
  8. */
  9. declare(strict_types=1);
  10. namespace app\api\controller;
  11. use app\api\validate\User as UserValidate;
  12. use app\BaseController;
  13. use app\common\business\User as UserBusiness;
  14. class Login extends BaseController
  15. {
  16. public function index(): object
  17. {
  18. if (!$this->request->param('phone_number', '', 'trim')) {
  19. return show(config('status.error'), '非法请求');
  20. }
  21. $phoneNumber = $this->request->param("phone_number", '', 'trim');
  22. $code = input('param.code', 0, 'intval');
  23. $type = input('param.type', 0, 'intval');
  24. // 参数校验
  25. $data = [
  26. 'phoneNumber' => $phoneNumber,
  27. 'code' => $code,
  28. 'type' => $type,
  29. ];
  30. $validate = new UserValidate();
  31. if (!$validate->scene('login')->check($data)) {
  32. return show(config('status.error'), $validate->getError());
  33. }
  34. try {
  35. $result = (new UserBusiness())->login($data);
  36. } catch (\Exception $e) {
  37. return show($e->getCode(), $e->getMessage());
  38. }
  39. if ($result) {
  40. return show(config('status.success'), "登陆成功", $result);
  41. }
  42. return show(config('status.error'), "登录失败");
  43. }
  44. }