<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 登錄/注冊 ### 創建登錄控制器 ```php php think make:controller admin/Login --plain ``` > —plain是為了創建控制器更純凈,沒有先創建好的一些方法 ### 下載驗證碼插件 這是配合ThinkPHP 5.1版本的 ```composer composer require topthink/think captcha=2.0.* ``` 在`Login`控制器里添加`verify()`方法用于驗證碼 ```php public function verify() { $config = [ // 驗證碼字體大小 'fontSize' => 16, 'length' => 3, // 關閉驗證碼雜點 'useNoise' => false, 'useCurve' => false, ]; $captcha = new Captcha($config); return $captcha->entry(); } ``` 在*html*頁面中引入 ```php+HTML <img src="{:url('Login/verify')}" class="reloadverify"> ``` #### 點擊驗證碼圖片隨機變換的JS ```javascript <script> $(function () { var verifyimg = $(".reloadverify").attr("src"); $(".reloadverify").click(function () { if (verifyimg.indexOf('?') > 0) { $(".reloadverify").attr("src", verifyimg + '&random=' + Math.random()); } else { $(".reloadverify").attr("src", verifyimg.replace(/\?.*$/, '') + '?' + Math.random()); } }); }); </script> ``` > 這里的`reloadverify`必須要在`img`中進行定義**class** ### 登錄使用Ajax進行登錄 > 必須引入jQuery,在配合layui的layer.js進行彈窗效果提示 > > 在input框中必須輸入對應的name值 js代碼: ```javascript $(function () { $('#login').click(function () { $.ajax({ url: "{:url('admin/Login/login')}", type: 'post', data: $('form').serialize(), dataType: 'json', success: function (data) { if (data.code == 1) { layer.msg(data.msg, { icon: 6, time: 1000 }, function () { location.href = "{:url('admin/Index/index')}"; }) } else { layer.open({ title: '登錄失敗', content: data.msg, ico: 5, anim: 6 }) } } }); return false; }); }); ``` ### 用Ajax進行注冊 ```javascript $(function () { $('#register').click(function () { $.ajax({ url: "{:url('admin/Register/register')}", type: 'post', data: $('form').serialize(), dataType: 'json', success: function (data) { if (data.code == 1) { layer.msg(data.msg, { icon: 6, time: 1000 }, function () { location.href = "{:url('admin/Login/index')}"; }) } else { layer.open({ title: '注冊失敗', content: data.msg, ico: 5, anim: 6 }) } } }); return false; }); }); ``` ## 創建模型 我會將模型創建在`application`下的`common`目錄,屬于前臺和后臺的公共使用 ```php php think make:model common/Manager ``` 用模型進行處理數據 ```php use SoftDelete; // 自動引入時間戳 protected $autoWriteTimestamp = true; // 用戶登錄驗證 public static function login($data) { $validate = new LoginValidate(); if (!$validate->scene('login')->check($data)) { return json(['code' => 0, 'msg' => $validate->getError()]); } $res = self::get(['username' => $data['username']]); if (!$res) { return json(['code' => 0, 'msg' => '賬號不存在']); } if (!$res['status']) { return json(['code' => 0, 'msg' => '賬號禁用']); } if (md5($data['password']) == $res['password']) { session('adminid', $res['id']); session('username', $res['username']); session('nickname', $res['nickname']); return json(['code' => 1, 'msg' => '登錄成功']); } return json(['code' => 0, 'msg' => '密碼不正確']); } // 獲取所有管理員的信息 public static function getAllManager() { $lists = self::where('id', 'asc'); return $lists; } // 添加修改管理員 public static function store($data) { if (isset($data['id'])) { $scene = 'edit'; $msg = '修改'; $action = "update"; } else { $scene = 'add'; $msg = '添加'; $action = "create"; } $data['password'] = md5($data['password']); // 寫入數據庫 unset($data['confirm_password']); $result = self::$action($data); if (!$result) { return json(['code' => 0, 'msg' => $msg.'失敗']); } return json(['code' => 1, 'msg' => $msg.'成功']); } public static function del($id) { $result = self::destroy($id); if ($result) { return json(['code' => 1, 'msg' => '刪除成功']); } return json(['code' => 0, 'msg' => '刪除失敗']); } ``` ## 創建驗證器 ```php php think make:validate common/LoginValidate ``` ```php /** * 定義驗證規則 * 格式:'字段名' => ['規則1','規則2'...] * * @var array */ protected $rule = [ 'username' => 'require|unique:manager', 'password' => 'require|length:6,20', 'confirm_password' => 'require|confirm:password', 'code' => 'require', 'email' => 'require|email|unique:manager' ]; /** * 定義錯誤信息 * 格式:'字段名.規則名' => '錯誤信息' * * @var array */ protected $message = [ 'id.require' => 'id不正確', 'username.require' => '用戶名不能為空', 'username.unique' => '用戶名必須唯一', 'password.require' => '密碼不能為空', 'password.length' => '密碼長度在6~20個字符之內', 'confirm_password.require' => '確認密碼不能為空', 'confirm_password.confirm' => '兩次密碼輸入不相同', 'code.require' => '驗證碼不能為空', 'email.require' => '郵箱不能為空', 'email.email' => '郵箱格式不正確', 'email.unique' => '該郵箱已存在' ]; protected $scene = [ 'login' => ['username' => 'require|unique:manager',"password"], 'add' => ['username', 'password', 'confirm_password', 'email'], 'edit' => ['id'] ]; ``` ## 登錄控制器 ```php /** * 顯示登錄頁面 * @return \think\response\View */ public function index() { return view(); } public function login() { $data = input('post.'); $result = Manager::login($data); return $result; } ``` ## 注冊控制器 ```php // 注冊頁面 public function index() { return view(); } public function register() { $data = input('post.'); $validate = new LoginValidate(); if (!$validate->scene('add')->check($data)) { return json(['code' => 0, 'msg' => $validate->getError()]); } $data['created_at'] = time(); $data['status'] = 1; $result = Manager::store($data); return $result; } ``` ## ThinkPHP5 模型時間戳設置 > 單獨在模型里面設置(推薦) ``` protected $autoWriteTimestamp = true; // int 型 protected $autoWriteTimestamp = 'datetime'; // datetime 類型 protected $autoWriteTimestamp = false; // 關閉自動寫入時間戳 protected $updateTime = false;       // 只關閉自動寫入update_time字段 ``` > 在config里添加全局設置 ``` / 開啟自動寫入時間戳字段(官方手冊這么說,自己并沒有測試出來) 'auto_timestamp' => true, // 默認為int型 'auto_timestamp' => 'datetime', // datetime類 'auto_timestamp' => false,  // 關閉全局自動寫入時間字段 ``` > 如果數據表字段不是默認值(假設數據庫字段名為create_at和update_at) ``` // 定義時間戳字段名 protected $createTime = 'create_at';    // 默認的字段為create_time 和 update_time protected $updateTime = 'update_at'; ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看