PHP code example of hahadu / think-userlogin

1. Go to this page and download the library: Download hahadu/think-userlogin library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hahadu / think-userlogin example snippets


return [
    // 指令定义
    'commands' => [
        'createUser' => \Hahadu\ThinkUserLogin\Command\CreateUser::class,
    ],

//用户登录模块控制器Login.php
namespace app\user\controller;
use Hahadu\ThinkUserLogin\controller\BaseLoginController;
use think\facade\View;

class Login extends BaseLoginController
{
    public function index() //用户登录页面
    {
        return view();
    }
    public function login()
    {
        $result = parent::login();
        if($result['code'] == 100003){
            return '登录成功';
        }else{
            return '登录失败';
        }
    }
    public function logout()
    {
        $result = parent::logout();
        if($result['code'] == 100004){
            return '退出登录成功';
        }else{
            return '退出登录失败';
        }
    }
}


return [
    'user_model'=> "\app\model\Users", //用户数据表模型路径
    'login_url' =>'/admin/login', //用户需要登录时的跳转登录页面 /模块/控制器/方法
    'JWT_login' =>true, //是否开启JWT鉴权 true 开启 false关闭
    'token_name' => 'token',//token表单字段名
    'JWT'=>[ //配置jwt 开启jwt有效
        'nbf'=> 1, //令牌生效开始时间 *距离令牌创建的时间
        'exp'=> 3600, //令牌过期时间 *距离令牌创建的时间
        'iss'=> request()->host(), //iss
        'aud'=> request()->host(), //aud

    ]

];

//用户数据表说明
/*
用户表字段:
用户名:username //必须
密码 : password //必须
头像 : avatar  //必须
用户邮箱: email //可选 需要邮箱注册登录的场景可用
用户手机: phone //可选 需要短信注册登录的场景可用
头像为图片链接地址 
*/

//页面跳转遵循hahadu/think-jump-class 的跳转方式
//
return [
    //是否开启ajax返回 true 开启/ false关闭
    //开启JWT鉴权时,ajax必须设置为true
    'ajax'=>true,
    'ajax_type'=> 'json', //支持json|jsonp|xml3种类型、开启ajax有效
    //自定义跳转模板文件路径
    'dispatch_tpl' => '' ,
];


    public function register(){
        $result = parent::email_register();
        if($result['code']==100002){
         return '注册成功';
        }else
            return  '注册失败';
        }
    }
//在email_tpl方法中设置邮箱模板内容,
//邮箱模板是一个数组,必须包含'title'和'content'
//或者复制下面的方法到您的控制器中,按需修改即可
    protected function email_tpl(){
        return ['title'=>'欢迎注册,请查收验证码','content'=>'您好,感谢您的注册,您的验证码是: %s'];
    }




    public function register(){
        $result = parent::sms_register();
        if($result['code']==100002){
         return '注册成功';
        }else
            return  '注册失败';
        }
    }





  public function repassword(Request $request){
     return $this->re_password($request);
  }

    public function verify()
    { 
        /*
         * 此处写你的验证码方法
         */
        return Captcha::create();
    }
shell
   php think createUser
   or
   php think createUser [ModelName] --username=username --password=password
shell
   php think createUser users --username=admin --password=123456