| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- namespace app\controller\exe;
- use app\extra\basic\Base;
- use app\middleware\ExeMiddleware;
- use app\model\saas\SaasAgent;
- use app\model\system\SystemUser;
- use LinFly\Annotation\Route\Controller;
- use LinFly\Annotation\Route\Route;
- use Shopwwi\WebmanAuth\Auth;
- use support\Request;
- use support\Response;
- use think\facade\Db;
- use Tinywan\Captcha\Captcha;
- use Webman\Annotation\Middleware;
- #[Controller(prefix: "/exe/login")]
- class Login extends Base
- {
- /**
- * 登陆
- * @param Request $request
- * @return Response
- */
- #[Route(path: "user",methods: "post")]
- public function setLogin(Request $request): Response
- {
- try {
- $param = $this->_valid([
- "username.require" => trans("empty.user"),
- "password.require" => trans("empty.passwd"),
- ],"post");
- if (!is_array($param)) return error($param);
- $map = ["is_deleted" => 0,"username" => $param['username']];
- [$state,$msg,$user] = $this->checkLogin($map,2,$param);
- if (!$state) return error($msg);
- return successTrans("success.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['shop_name'];
- }
- if ($type == 2) {
- if (md5(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 SaasAgent)->where("agent_id",$agentId)->findOrEmpty()->toArray();
- }
- /**
- * @return Response
- */
- #[Route(path: "profile",methods: "get"),Middleware(ExeMiddleware::class)]
- public function getLoginUser(Request $request): Response
- {
- try {
- $agent = (new SaasAgent)->where("agent_id",$request->uuid)->findOrEmpty();
- return successTrans("success.data",[
- "vip_at" => empty($agent['vip_end'])?'无限期':date('Y-m-d',strtotime($agent['vip_end'])),
- "agent_id" => $agent['agent_id'],
- "shop" => $agent['shop_name']??'',
- "address" => $agent['shop_address']??'',
- "sys" => sConf("service.title"),
- "logo" => sConf("service.logo")
- ]);
- } catch (\Throwable $exception){
- return error($exception->getMessage());
- }
- }
- }
|