123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- namespace app\badminapi\controller;
- use app\models\system\SystemBadmin;
- use app\models\system\SystemMenus;
- use crmeb\basic\BaseController;
- use crmeb\services\GroupDataService;
- use crmeb\services\UtilService;
- use crmeb\utils\Captcha;
- use Psr\SimpleCache\InvalidArgumentException;
- use think\db\exception\DataNotFoundException;
- use think\facade\Cache;
- use think\Response;
- /**
- * 后台登陆
- * Class Login
- * @package app\adminapi\controller
- */
- class Login extends BaseController
- {
- /**
- * 验证码
- * @return $this|Response
- */
- public function captcha()
- {
- return (new Captcha())->create();
- }
- /**
- * 登陆
- * @return mixed
- * @throws InvalidArgumentException
- * @throws DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function login()
- {
- [$account, $pwd, $imgcode] = UtilService::postMore([
- 'account', 'pwd', ['imgcode', '']
- ], $this->request, true);
- if (!(new Captcha)->check($imgcode)) {
- return app('json')->fail('验证码错误,请重新输入');
- }
- $this->validate(['account' => $account, 'pwd' => $pwd], \app\badminapi\validates\SystemBAdminValidata::class, 'get');
- $error = Cache::get('badmin_login_error') ?: ['num' => 0, 'time' => time()];
- $error['num'] = 0;
- if ($error['num'] >= 5 && $error['time'] > strtotime('- 5 minutes'))
- return $this->fail('错误次数过多,请稍候再试!');
- $adminInfo = SystemBAdmin::login($account, $pwd);
- if ($adminInfo) {
- $token = SystemBAdmin::createToken($adminInfo, 'badmin');
- if ($token === false) {
- return app('json')->fail(SystemBadmin::getErrorInfo());
- }
- Cache::set('badmin_login_error', null);
- //获取用户菜单
- // $menusModel = new SystemMenus();
- // $menus = $menusModel->getRoute($adminInfo->roles, $adminInfo['level']);
- // $unique_auth = $menusModel->getUniqueAuth($adminInfo->roles, $adminInfo['level']);
- return app('json')->success([
- 'token' => $token['token'],
- 'expires_time' => $token['params']['exp'],
- // 'menus' => $menus,
- // 'unique_auth' => $unique_auth,
- 'user_info' => [
- 'id' => $adminInfo->getData('id'),
- 'account' => $adminInfo->getData('account'),
- 'head_pic' => $adminInfo->getData('head_pic'),
- ],
- 'logo' => sys_config('site_logo'),
- 'logo_square' => sys_config('site_logo_square'),
- // 'version' => get_crmeb_version()
- ]);
- } else {
- $error['num'] += 1;
- $error['time'] = time();
- Cache::set('badmin_login_error', $error);
- return app('json')->fail(SystemBadmin::getErrorInfo('用户名错误,请重新输入'));
- }
- }
- /**
- * 获取后台登录页轮播图以及LOGO
- * @return mixed
- */
- public function info()
- {
- $data['slide'] = GroupDataService::getData('admin_login_slide', 0, false, $this->merId) ?? [];
- $data['logo_square'] = sys_config('site_logo_square');//透明
- $data['logo_rectangle'] = sys_config('site_logo');//方形
- $data['login_logo'] = sys_config('login_logo');//登陆
- return app('json')->success($data);
- }
- }
|