## 代碼
> 注釋部分,可根據實際情況進行去掉!!!
注釋分別為:
```
<?php
namespace app\agent\controller;
use think\Controller;
class PublicController extends Controller
{
/**
* 登錄驗證
*/
public function doLogin()
{
// $loginAllowed = session("__LOGIN_BY_CMF_ADMIN_PW__");
// if (empty($loginAllowed)) {
// $this->error('非法登錄!', cmf_get_root() . '/');
// }
//驗證碼
$captcha = $this->request->param('captcha');
if (empty($captcha)) {
$this->error('驗證碼不能為空');
}
if (!cmf_captcha_check($captcha)) {
$this->error('驗證碼錯誤');
}
//用戶名郵箱
$name = $this->request->param("username");
if (empty($name)) {
$this->error('用戶名或郵箱不能為空');
}
//密碼
$pass = $this->request->param("password");
if (empty($pass)) {
$this->error('密碼不能為空');
}
if (strpos($name, "@") > 0) {//郵箱登陸
$where['user_email'] = $name;
} else {
$where['user_login'] = $name;
}
//查找
$result = Db::name('user')->where($where)->find();
//用戶不為空
if (!empty($result) && $result['user_type'] == 1) {
if (cmf_compare_password($pass, $result['user_pass'])) {
// $groups = Db::name('RoleUser')
// ->alias("a")
// ->join('__ROLE__ b', 'a.role_id =b.id')
// ->where(["user_id" => $result["id"], "status" => 1])
// ->value("role_id");
if ($result["id"] != 1 && (empty($groups) || empty($result['user_status']))) {
$this->error('用戶已經被禁用');
}
//登入成功頁面跳轉
session('AGENT_ID', $result["id"]);
session('name', $result["user_login"]);
$result['last_login_ip'] = get_client_ip(0, true);
$result['last_login_time'] = time();
// $token = cmf_generate_user_token($result["id"], 'web');
// if (!empty($token)) {
// session('token', $token);
// }
// Db::name('user')->update($result);
cookie("admin_username", $name, 3600 * 24 * 30);
// session("__LOGIN_BY_CMF_ADMIN_PW__", null);
$this->success('登錄成功', url("admin/Index/index"));
} else {
$this->error('密碼錯誤');
}
} else {
$this->error('用戶名不存在');
}
}
}
```