TP5框架中实现多条件登录(自写代码,密码未md5()加密)

HTML代码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>前台登录</title>
    <link rel="stylesheet" href="__STATIC__/admin/css/login.css">
</head>
<body>
<div class="login">
    <div class="center">
        <h1>Login</h1>
        <div class="inputLi">
            <strong>账户</strong>
            <input type="text" class="username" name="username" placeholder="用户名/手机号/邮箱">
        </div>
        <div class="inputLi">
            <strong>密码</strong>
            <input type="password" class="userpassword" name="userpassword" placeholder="请输入密码">
        </div>
        <div class="inputLi">
            <button class="" onclick="login()">登录</button>
        </div>
    </div>
</div>
</body>
</html>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
    function login() {
        var username = $('.username').val();
        var userpassword = $('.userpassword').val();
        $.ajax({
            url: "http://www.examtest.com/home/login/loginSuccess",
            type: "POST",
            data: {
                username: username,
                userpassword: userpassword,
                __token__: "{$Request.token}"
            },
            dataType: "JSON",
            success: function (res) {
                if (res.code == 510) {
                    alert(res.message)
                }
                if (res.code == 400) {
                    alert('登录失败')
                }
                if (res.code == 200) {
                    alert(res.message)
                    location.href = '/admin/order/order'
                }

            }
        })

    }


</script>

控制器代码:

<?php

namespace app\home\controller;

use app\home\model\LoginModel;
use think\Controller;
use think\Loader;
use think\Request;
use think\Session;
use think\Validate;

class Login extends Controller
{
    //登录表单
    public static function login()
    {
        return view();
    }

//    处理登录的数据
    public static function loginSuccess()
    {
        //接受值
        $params = \request()->post();
        //验证非空
        $rule = [
            'username|账号' => 'require',
            'userpassword|密码' => 'require',
        ];
        $msg = [
            'username.require' => '账号不可以为空',
            'userpassword.require' => '密码不可以为空',
        ];
        $validate = new Validate($rule, $msg);
        $result = $validate->check($params);
        if (!$result) {
            return json(['code' => 510, 'message' => $validate->getError()]);
        }

        //与数据库进行对比
        $userData = LoginModel::getUserInfo($params);
        //session记录 用户信息
        Session::set('user', $userData);
        if (!$userData) {
            return json(['code' => 510, 'message' => '登录失败']);
        } else {
            return json(['code' => 200, 'message' => '登录成功']);
        }
    }
}

模型代码:

 

 

 

<?php

namespace app\home\model;

use think\Model;

class LoginModel extends Model
{
    //链接数据库表名
    protected $table = 'user';
//获取数据库信息与用户信息对比
    public static function getUserInfo($params)
    {
        $username = self::where('username', $params['username'])
            ->find();
        $tel = self::where('tel', $params['username'])
            ->find();
        if ($username) {
            return $username;
        }
        if ($tel) {
            return $tel;
        }
    }


}

 

posted @ 2021-09-03 10:42  王越666  阅读(141)  评论(0编辑  收藏  举报