用tp6写的简单的eml的登录和curd

1.登录和主页显示

1.1 登录功能逻辑图

1.2 控制器
 app/controller/index.php
php think make:validate LoginValidate
<?php
namespace app\controller;

use app\BaseController;
use app\model\User;
use app\validate\LoginValidate;
use app\validate\UserValidate;
use liliuwei\think\Jump;
use think\facade\Log;
use think\facade\Request;
use think\facade\Session;
use think\facade\View;

class Index extends BaseController
{
    use Jump;
    protected $user;
    public function __construct(\think\App $app){
        parent::__construct($app);
        $this->user = new User();
    }
    public function index(){
        return View::fetch("login");
    }
    
    public function login(){
        if (Request::isPost()) {
            $data = Request::post();
            $loginValidate = new LoginValidate();
            if($loginValidate->check($data)){
                 $user = $this->user->login($data["username"]);
                 if ($user) {
                    if($data["password"]== $user["password"]){
                        Session::set("role", $user["roleid"]);

                        $this->success("登录成功",url("list"));
                    }else{
                        $this->error("密码错误","login");
                    }
                 }else{
                     $this->error("此账户不存在","login");
                 }
            }else{
                return json(['code'=>0,'message'=>$loginValidate->getError()]);
            }
        }
        return View::fetch();
    }

    public function list(){
        $user = $this->user->getUsers();
        
        $page = $user->render();
        $roleid = Session::get("role");
        View::assign([
            "user"=>$user,
            "page"=>$page,
            "roleid"=>$roleid,
        ]);
        return view::fetch();
    }

    public function add(){
        if(Request::isPost()){
            $data = Request::post();
            $userValidate = new UserValidate();
            if($userValidate->sceneAdd()->check($data)){

                if ($this->user->addUser($data)) {
                    $this->success("添加成功","list");
                }else{
                     $this->error("添加失败","list");
                }
            }else{
                return json(['code'=>0,'message'=>$userValidate->getError()]);
            }
        }
        return View::fetch();
    }

    public function edit($id){
        $id=Request::param("id");
        $row= $this->user->findById($id);
        View::assign(["row"=>$row]);
        return View::fetch();
    }

     /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        if(Request::isPost()){
            $data = Request::post();
             $data["updated_at"]=time();
            $userValidate=new UserValidate();
            if ($userValidate->check($data)){
            if ($this->user->updateUser($data)){
            $this->success("用户修改成功",url("list"));
            }else{
            $this->error("用户修改失败",url("list"));
            }
            }else{
                return $userValidate->getError();
            }
        }
    }

    public function delete($id){
        $user = $this->user->deleteUser($id);
        if($user){
            $this->success("删除成功","list");
        }else{
            $this->error("删除失败","list");
        }
        return View::fetch();
    }

}
1.3 模型
<?php

namespace app\model;

use think\facade\Log;
use think\facade\View;
use think\Model;

/**
 * @mixin \think\Model
 */
class User extends Model
{
    //
    
    
        public function login($username)
        {
         return self::where('username',$username)->find();
        }

          public function findById($id){
            return self::find($id);
        }
        public function getUsers()
        {
            return self::paginate(5);
        }

        public function addUser($data){
              $data["created_at"]=time();
            return self::save($data);
        }

        public function updateUser($data)
        {
            $user=self::find($data['id']);
            return $user->save($data);
        }

        public function deleteUser($id){
            $user=self::find($id);
            return $user->delete();
        }
        
}
1.4 验证器

登录

<?php
declare (strict_types = 1);

namespace app\validate;

use think\Validate;

class LoginValidate extends Validate
{
    /**
     * 定义验证规则
     * 格式:'字段名' =>  ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'username'=>'require|max:25',
        'password'=>'require'
    ];

    /**
     * 定义错误信息
     * 格式:'字段名.规则名' =>  '错误信息'
     *
     * @var array
     */
    protected $message = [
    'username.require' => '用户名不能为空',
    'username.max' => '名称不能大于25',
    'password.require' => '密码不能为空',
    ];
}

用户

<?php
declare (strict_types = 1);

namespace app\validate;

use think\Validate;

class UserValidate extends Validate
{
    /**
     * 定义验证规则
     * 格式:'字段名' =>  ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        "username"=>"require|alphaNum",
        "password"=>"require",
    ];

    /**
     * 定义错误信息
     * 格式:'字段名.规则名' =>  '错误信息'
     *
     * @var array
     */
    protected $message = [
        "username.require"=>"账号不能为空",
         "username.alphaNum"=>"账号只能为字母和数字",
          "password.require"=>"密码不能为空",
    ];

    public function sceneAdd(){
        return $this->only(["username","name","password","phone","tel","email"])
        ->append("name","require")
        ->append("phone","mobile")
        ->append("tel","mobile")
        ->append("email","email");
    }
}
1.5 提交数据
view/index/login.html 页面中 form 表单的 action 写上请求路径 action="{:url('index/login')}"
view/index/login.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>eml通讯录管理系统经典版</title>
<link rel='stylesheet' href='__STATIC__/css/login.css' type='text/css' />
</head>
<body>
<div id="login">
<h1><a href="http://bbs.emlsoft.com" target="_blank" title="">eml企业通讯录管理系统
</a></h1>
<form accept-charset="utf-8" action="{:url('index/login')}" method="post">
<p>
<label>帐号:<!--{// $hello}--></label>
<input class="input" name="username" size="20" type="text" />
</p>
<p>
<label>密码:</label>
<input class="input" name="password" size="20" type="password" />
</p>
<p class="submit">
<input class="button-primary" name="commit" type="submit" value="登录" />
1.5 数据验证
1.5.1 安装验证器
1.5.2 自定义验证器
 validate/LoginValidate.php
<input class="button-primary" name="commit" type="button" value="注册"
onclick="javascript:window.location.href='?action=user&do=reg' "/>
</p>
</form>
</div>
<div align="center">&nbsp;&nbsp; Powered by <a href="http://bbs.emlsoft.com/"
target="_blank">emlSoft</a> <!--{// $cfg.version}--> 2013-2019 Some rights
reserved</div>
</body>
</html>

2. 注意事项

安装跳转扩展 : composer require liliuwei/thinkphp - jump ;
日期格式 {$row.created_at|date="Y/m/d H:i:s"}
请求路径 action="{:url('index/login')}
数据库文件 contacts_general.sql
.env配置:
APP_DEBUG = true

[APP]
DEFAULT_TIMEZONE = Asia/Shanghai

[DATABASE]
TYPE = mysql
HOSTNAME = 127.0.0.1
DATABASE = eml
USERNAME = root
PASSWORD = root
HOSTPORT = 3306
CHARSET = utf8
DEBUG = true

[LANG]
default_lang = zh-cn

  视图调用js/css文件配置:

//文件位置 config/view
 'tpl_replace_string'  =>  [

    '__STATIC__'=>'/static',

    '__JS__' => '/static/javascript',

]

nginx配置:

        if (!-f $request_filename) {
               rewrite  ^(.*)$  /index.php?s=/$1  last;
        }


 

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值