Passport.php 2.5 KB
<?php
namespace app\web\controller;

use think\Validate;
use think\Config;
use think\Session;
use app\web\model\User as UserModel;
use app\common\service\Render as RenderTrait;
/**
 * 登录认证
 * Class Passport
 * @package app\web\controller
 */
class Passport extends Controller
{
    use RenderTrait;

    /**
     * 后台登录
     * @return array|mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function login()
    {
        if ($this->request->isAjax()) {
            return $this->renderError('登录失败');
        }
        $this->view->engine->layout(false);
        return $this->fetch('login');
    }

    /**
     * 退出登录
     */
    public function logout()
    {
        Session::clear();
        $this->redirect('passport/login');
    }

    /**
     * 处理登录请求
     */
    public function loginHandle()
    {

        $data = $this->request->post('data');
        $data = json_decode($data, true);

        $validate = new Validate([
            'username'=>   'require',
            'password'=>   'require',
            'captcha'=>   'require',

        ], [
            'username.require' => '账号不能为空',
            'password.require' => '密码不能为空',
            'captcha.require' => '验证码不能为空',
        ]);
        if (!$validate->check($data)) {
            $errmsg = $validate->getError();
            return json($this->renderError($errmsg));
        }

        if (!captcha_check($data['captcha'])){
           return json($this->renderError('验证码错误'));
        }

        $password = md5(Config::get('password_salt').$data['password']);

        $user = UserModel::where('login_account' , $data['username'])->where('password' , $password)->find();

        if (!$user) {
            return json($this->renderError('账号或密码错误'));
        }

        if ($user->invalid_flag != 1) {
            return json($this->renderError('账号暂不可用'));
        }

        session('user', $user->toArray());
        // 获取组织结构信息
        $organization = new \app\web\controller\Organization();
        $organization_info = $organization->getDetail(['org_code' => $user->org_code]);
        if ($organization_info['code'] == 0) {
            session('organization', $organization_info['data']);
        }


        return json($this->renderSuccess(['url' => url('web/index/index')]));

    }

}