| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- namespace app\controller\common;
- use app\extra\basic\Base;
- use app\middleware\AuthMiddleware;
- use app\model\saas\SaasStore;
- use app\model\system\SystemConfig;
- use app\model\system\SystemUser;
- use DI\Attribute\Inject;
- use LinFly\Annotation\Attributes\Route\{GetMapping, Controller, Middleware, PostMapping};
- use support\{Request,Response};
- use Shopwwi\WebmanAuth\Auth;
- use think\facade\Db;
- use Tinywan\Captcha\Captcha;
- #[Controller("/api/login"),Middleware(AuthMiddleware::class)]
- class Login extends Base
- {
- /**
- * @var array|string[]
- */
- protected array $noNeedLogin = ["getSystemData","setLogin"];
- #[Inject]
- protected SystemConfig $config;
- /**
- * 登陆
- * @param Request $request
- * @return Response
- */
- #[PostMapping('user')]
- public function setLogin(Request $request): Response
- {
- try {
- $param = $this->_valid([
- "username.require" => trans("empty.user"),
- "password.require" => trans("empty.passwd"),
- "code.require" => trans("empty.code"),
- "key.require" => trans("empty.data"),
- ],"post");
- if (!is_array($param)) return error($param);
- if (Captcha::check($param['code'],$param['key']) === false) return errorTrans("error.captcha");
- $map = ["is_deleted" => 0,"username" => $param['username']];
- [$state,$msg,$user] = $this->checkLogin($map,2,$param);
- if (!$state) return error($msg);
- return successTrans("success.login",get_object_vars((new Auth)->guard("admin")->login($user)));
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- /**
- * 登录验证处理
- * @param array $map
- * @param int $type
- * @param array $param
- * @return array
- */
- protected function checkLogin(array $map = [],int $type = 1,array $param = []): array
- {
- $user = (new SystemUser)->where($map)->findOrEmpty();
- if ($user->isEmpty()) return [0,trans("error.user-empty"),[]];
- if ($user['status'] <> 1) return [0,trans("error.user-status"),[]];
- if ($user['type'] > 1) {
- $typeUser = $this->getTypeUser($user['agent_id']);
- if (empty($typeUser)) return [0,trans("empty.agent"),[]];
- if ($typeUser['status'] <> 0) return [0,trans("error.agent"),[]];
- if (time() > strtotime($typeUser['vip_end'])) return [0,trans("error.agent-out"),[]];
- $user['shop_name'] = $typeUser['poi_name'];
- }
- if ($type == 2) {
- if (md5($param['password'].$user['salt']) <> $user['password']) return [0,trans("error.passwd"),[]];
- }
- $user->login_at = getDateFull();
- $user->login_ip = request()->getRealIp();
- $user->login_num = Db::raw("login_num+1");
- $user->save();
- return [1,'success',$user->toArray()];
- }
- /**
- * 获取代理信息
- * @param int $agentId
- * @return array
- */
- protected function getTypeUser(int $agentId = 0): array
- {
- return (new SaasStore)->where("poi_id",$agentId)->findOrEmpty()->toArray();
- }
- #[GetMapping('profile')]
- public function getLoginUser()
- {
- try {
- $userData = (new Auth)->guard("admin")->user()->toArray();
- if (isset($userData['password'])) unset($userData['password']);
- $agent = [];
- if (empty($agent['vip_end']))
- {
- $userData['vip_end'] = 0;
- } else {
- $userData['vip_end'] = strtotime($agent['vip_end']);
- }
- return successTrans("success.data",[
- "username" => $userData['username'],
- "truename" => $userData['truename'],
- "vip_at" => $userData['vip_end'],
- "agent_id" => $userData['agent_id'],
- "super" => $userData['is_super'],
- "shop" => $agent['shop_name']??'',
- "type" => $userData['type']
- ]);
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- #[GetMapping('data')]
- public function getSystemData(): Response
- {
- try {
- $captcha = Captcha::base64();
- $service = $this->config->where("type","service")->column("value","name");
- return successTrans("success.data",compact("captcha",'service'));
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- }
|