Login.php 2.4 KB

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