Index.php 2.4 KB
<?php

namespace app\web\controller;


use think\Cache;
use think\Db;
use think\Request;

use think\Cookie;
use think\Session;
/**
 * 首页
 * Class Index
 * @package app\admin\controller
 */
class Index extends Controller
{
    //登录页面
    public function login()
    {
        $this->view->engine->layout(false);
        return $this->fetch();
    }
    public function tologin(Request $request)
    {
        $data = $request->param();

        $where['username']=$data['name'];
        $list= Db::table('sos_user_admin')->where($where)->find();
        if(empty($list)){
            $list= Db::table('sos_user_mechanism')->where($where)->find();
        }

        if(empty($list)){
            $data=[
                'code'=>100,
                'msg'=>'用户名错误'
            ];
            return json_encode($data);
        }elseif($list['password'] != md5(md5($data['pwd']))){

            $data=[
                'code'=>100,
                'msg'=>'密码错误'
            ];
            return json_encode($data);
        }else{
            //Cookie::set('name',$name);
           // session('name', $list['username']);
//            session('id', $list['id']);
            Cache::set('name',$list['username'],3600);
            Cache::set('id',$list['id'],3600);
            $data=[
                'data'=>$list['username'],
                'code'=>200,
                'msg'=>'验证成功'
            ];
            return json_encode($data);

        }
    }

    /**
     * 框架页面
     * @return array|mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        //$user=Cookie::get('name');
        //$user = Session::get('name');
        $user=Cache::get('name');
        if(!$user){
            //重定向到指定的URL地址
            $this->error('您未进行登录', 'web/index/login'); exit;
        }

        $this->view->engine->layout(false);
        return $this->fetch('index');
    }

    public function loginout()
    {
        //Cookie::delete('name');
        //Session::clear();
        Cache::clear();
        $this->view->engine->layout(false);
        return $this->fetch('login');
    }

    public function quick_menu()
    {
        $this->view->engine->layout(false);
        return $this->fetch('quick_menu');
    }
}