Login.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace app\badminapi\controller;
  3. use app\models\system\SystemBadmin;
  4. use app\models\system\SystemMenus;
  5. use crmeb\basic\BaseController;
  6. use crmeb\services\GroupDataService;
  7. use crmeb\services\UtilService;
  8. use crmeb\utils\Captcha;
  9. use Psr\SimpleCache\InvalidArgumentException;
  10. use think\db\exception\DataNotFoundException;
  11. use think\facade\Cache;
  12. use think\Response;
  13. /**
  14. * 后台登陆
  15. * Class Login
  16. * @package app\adminapi\controller
  17. */
  18. class Login extends BaseController
  19. {
  20. /**
  21. * 验证码
  22. * @return $this|Response
  23. */
  24. public function captcha()
  25. {
  26. return (new Captcha())->create();
  27. }
  28. /**
  29. * 登陆
  30. * @return mixed
  31. * @throws InvalidArgumentException
  32. * @throws DataNotFoundException
  33. * @throws \think\db\exception\DbException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. */
  36. public function login()
  37. {
  38. [$account, $pwd, $imgcode] = UtilService::postMore([
  39. 'account', 'pwd', ['imgcode', '']
  40. ], $this->request, true);
  41. if (!(new Captcha)->check($imgcode)) {
  42. return app('json')->fail('验证码错误,请重新输入');
  43. }
  44. $this->validate(['account' => $account, 'pwd' => $pwd], \app\badminapi\validates\SystemBAdminValidata::class, 'get');
  45. $error = Cache::get('badmin_login_error') ?: ['num' => 0, 'time' => time()];
  46. $error['num'] = 0;
  47. if ($error['num'] >= 5 && $error['time'] > strtotime('- 5 minutes'))
  48. return $this->fail('错误次数过多,请稍候再试!');
  49. $adminInfo = SystemBAdmin::login($account, $pwd);
  50. if ($adminInfo) {
  51. $token = SystemBAdmin::createToken($adminInfo, 'badmin');
  52. if ($token === false) {
  53. return app('json')->fail(SystemBadmin::getErrorInfo());
  54. }
  55. Cache::set('badmin_login_error', null);
  56. //获取用户菜单
  57. // $menusModel = new SystemMenus();
  58. // $menus = $menusModel->getRoute($adminInfo->roles, $adminInfo['level']);
  59. // $unique_auth = $menusModel->getUniqueAuth($adminInfo->roles, $adminInfo['level']);
  60. return app('json')->success([
  61. 'token' => $token['token'],
  62. 'expires_time' => $token['params']['exp'],
  63. // 'menus' => $menus,
  64. // 'unique_auth' => $unique_auth,
  65. 'user_info' => [
  66. 'id' => $adminInfo->getData('id'),
  67. 'account' => $adminInfo->getData('account'),
  68. 'head_pic' => $adminInfo->getData('head_pic'),
  69. ],
  70. 'logo' => sys_config('site_logo'),
  71. 'logo_square' => sys_config('site_logo_square'),
  72. // 'version' => get_crmeb_version()
  73. ]);
  74. } else {
  75. $error['num'] += 1;
  76. $error['time'] = time();
  77. Cache::set('badmin_login_error', $error);
  78. return app('json')->fail(SystemBadmin::getErrorInfo('用户名错误,请重新输入'));
  79. }
  80. }
  81. /**
  82. * 获取后台登录页轮播图以及LOGO
  83. * @return mixed
  84. */
  85. public function info()
  86. {
  87. $data['slide'] = GroupDataService::getData('admin_login_slide', 0, false, $this->merId) ?? [];
  88. $data['logo_square'] = sys_config('site_logo_square');//透明
  89. $data['logo_rectangle'] = sys_config('site_logo');//方形
  90. $data['login_logo'] = sys_config('login_logo');//登陆
  91. return app('json')->success($data);
  92. }
  93. }