Login.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-03-24
  7. *
  8. *
  9. */
  10. namespace app\controller\admin\system\admin;
  11. use crmeb\basic\BaseController;
  12. use app\common\repositories\system\admin\AdminRepository;
  13. use app\validate\admin\LoginValidate;
  14. use Gregwar\Captcha\CaptchaBuilder;
  15. use Gregwar\Captcha\PhraseBuilder;
  16. use think\App;
  17. use think\db\exception\DataNotFoundException;
  18. use think\db\exception\DbException;
  19. use think\db\exception\ModelNotFoundException;
  20. class Login extends BaseController
  21. {
  22. protected $repository;
  23. public function __construct(App $app, AdminRepository $repository)
  24. {
  25. parent::__construct($app);
  26. $this->repository = $repository;
  27. }
  28. /**
  29. * @param LoginValidate $validate
  30. * @return mixed
  31. * @throws DataNotFoundException
  32. * @throws DbException
  33. * @throws ModelNotFoundException
  34. * @author xaboy
  35. * @day 2020-04-10
  36. */
  37. public function login(LoginValidate $validate)
  38. {
  39. $data = $this->request->params(['account', 'password', 'code', 'key']);
  40. $validate->check($data);
  41. $this->repository->checkCode($data['key'], $data['code']);
  42. $adminInfo = $this->repository->login($data['account'], $data['password']);
  43. $tokenInfo = $this->repository->createToken($adminInfo);
  44. $admin = $adminInfo->toArray();
  45. unset($admin['pwd']);
  46. $data = [
  47. 'token' => $tokenInfo['token'],
  48. 'exp' => $tokenInfo['out'],
  49. 'admin' => $admin
  50. ];
  51. return app('json')->success($data);
  52. }
  53. /**
  54. * @return mixed
  55. * @author xaboy
  56. * @day 2020-04-10
  57. */
  58. public function logout()
  59. {
  60. if ($this->request->isLogin())
  61. $this->repository->clearToken($this->request->token());
  62. return app('json')->success('退出登录');
  63. }
  64. /**
  65. * @return mixed
  66. * @author xaboy
  67. * @day 2020-04-09
  68. */
  69. public function getCaptcha()
  70. {
  71. $codeBuilder = new CaptchaBuilder(null, new PhraseBuilder(4));
  72. $key = $this->repository->createLoginKey($codeBuilder->getPhrase());
  73. $captcha = $codeBuilder->build()->inline();
  74. return app('json')->success(compact('key', 'captcha'));
  75. }
  76. }