Login.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. public function getPhpToken()
  34. {
  35. $merId = $this->request->param('merId');
  36. $adminInfo = $this->repository->merIdByAdmin($merId);
  37. if (!$adminInfo) return app('json')->fail('数据不存在');
  38. $tokenInfo = $this->repository->createToken($adminInfo);
  39. $admin = $adminInfo->toArray();
  40. unset($admin['pwd']);
  41. $data = [
  42. 'token' => $tokenInfo['token'],
  43. 'exp' => $tokenInfo['out'],
  44. 'admin' => $admin,
  45. 'url' => '/' . config('admin.merchant_prefix')
  46. ];
  47. return app('json')->success($data);
  48. }
  49. /**
  50. * @param LoginValidate $validate
  51. * @return mixed
  52. * @throws DataNotFoundException
  53. * @throws DbException
  54. * @throws ModelNotFoundException
  55. * @author xaboy
  56. * @day 2020-04-10
  57. */
  58. public function login(LoginValidate $validate)
  59. {
  60. $data = $this->request->params(['account', 'password', 'code', 'key']);
  61. $validate->check($data);
  62. $this->repository->checkCode($data['key'], $data['code']);
  63. $adminInfo = $this->repository->login($data['account'], $data['password']);
  64. $tokenInfo = $this->repository->createToken($adminInfo);
  65. $admin = $adminInfo->toArray();
  66. unset($admin['pwd']);
  67. $data = [
  68. 'token' => $tokenInfo['token'],
  69. 'exp' => $tokenInfo['out'],
  70. 'admin' => $admin
  71. ];
  72. return app('json')->success($data);
  73. }
  74. /**
  75. * @return mixed
  76. * @author xaboy
  77. * @day 2020-04-10
  78. */
  79. public function logout()
  80. {
  81. if ($this->request->isLogin())
  82. $this->repository->clearToken($this->request->token());
  83. Cookie::delete('MerName');
  84. Cookie::delete('MerInfo');
  85. return app('json')->success('退出登录');
  86. }
  87. /**
  88. * @return mixed
  89. * @author xaboy
  90. * @day 2020-04-09
  91. */
  92. public function getCaptcha()
  93. {
  94. $codeBuilder = new CaptchaBuilder(null, new PhraseBuilder(4));
  95. $key = $this->repository->createLoginKey($codeBuilder->getPhrase());
  96. $captcha = $codeBuilder->build()->inline();
  97. return app('json')->success(compact('key', 'captcha'));
  98. }
  99. }