Login.php 3.5 KB

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