1,新建auth类
2,新建user 和member模型类
3,调用auth类(auth类调用user)
一,\woo\helper\AuthHxf.php
<?php
namespace woo\helper;
class AuthHxf {
// 用户模型名称比如User
public $userModel = ‘UserHxf’;
protected $connection = ‘db_**’;
private $userModelObject = NULL;
// 登录地址
public $loginAction = ‘’;
// 登出以后跳转地址在这里插入代码片
public $logoutRedirect = ‘’;
// 登录以后跳转地址
public $loginRedirect = ‘’;
// 登录帐号字段
public $userField = ‘username’;
// 登录密码字段
public $pwdField = ‘password’;
// 关联查询模型
public $contain = [];
private $allowMethod = [];
// 登录用户session名
private $authKey;
private $authDefaultKey;
private $request = [];
public function __construct()
{
$addon = parse_name(request()->param(‘addon’));
$controller = parse_name(request()->param(‘control’));
addon) && !empty(KaTeX parse error: Expected 'EOF', got '&' at position 13: controller) &̲& !empty(action)) {
$this->request[‘addon’] = $addon;
$this->request[‘controller’] = $controller;
$this->request[‘action’] = $action;
$this->request[‘is_addon’] = true;
$this->request[‘module’] = ‘addon’;
} else {
$this->request[‘module’] = request()->module();
$this->request[‘controller’] = request()->controller();
$this->request[‘action’] = request()->action();
$this->request[‘is_addon’] = false;
}
$this->authDefaultKey = config(‘auth.default_name’);
$module = $this->request[‘module’];
if (config(‘auth.’ . $module . ‘_name’)) {
$this->authKey = config(‘auth.’ . $module . ‘_name’);
} else {
$this->authKey = config(‘auth.default_name’);
}
this->authDefaultKey ? $this->authDefaultKey : ‘default’);
this->authKey ? $this->authKey : KaTeX parse error: Expected 'EOF', got '}' at position 28: …faultKey); }̲ // Auth获取…this->userModelObject) {
return;
}
if (!$this->userModel) {
exit(‘请设置Auth::userModel’);
}
this->userModel);
}
// Auth登录
public function login(KaTeX parse error: Expected '}', got 'EOF' at end of input: …if(session('?'.this->authKey)) redirect($this->loginRedirect);
data)) {
KaTeX parse error: Expected 'EOF', got '}' at position 32: …st.'); }̲ if (is…data[‘data’])) {
$data = $data[‘data’];
}
data[$this->userModel]) ?
this->userModel] :
data)) {
return false;
}
if (!trim(
this->userField]) || !trim(
this->pwdField])) {
return false;
}
data[$this->userField]));
data[$this->pwdField]));
$login =
this->contain)->where([[$this->userField, ‘=’,
this->pwdField, ‘=’,
login) {
return false;
}
$login =
login[‘logined_session_id’])) {
KaTeX parse error: Expected 'EOF', got '}' at position 53: …_id(); }̲ if (is…login[‘logined_ip’])) {
KaTeX parse error: Expected 'EOF', got '}' at position 48: …>ip(); }̲ if (is…login[‘logined’])) {
KaTeX parse error: Expected 'EOF', got '}' at position 49: …i:s'); }̲ sessio…this->authKey, $login);
if (!session(’?’ . KaTeX parse error: Expected '}', got 'EOF' at end of input: … session(this->authDefaultKey, KaTeX parse error: Expected 'EOF', got '}' at position 17: …ogin); }̲ sessio…data = array())
{
$this->getUserObj();
data[$this->userField]));
$login =
this->contain)->where([[$this->userField, ‘=’,
login) {
return false;
}
$login =
login[‘logined_session_id’])) {
KaTeX parse error: Expected 'EOF', got '}' at position 53: …_id(); }̲ if (is…login[‘logined_ip’])) {
KaTeX parse error: Expected 'EOF', got '}' at position 48: …>ip(); }̲ if (is…login[‘logined’])) {
KaTeX parse error: Expected 'EOF', got '}' at position 49: …i:s'); }̲ sessio…this->authKey, $login);
if (!session(’?’ . KaTeX parse error: Expected '}', got 'EOF' at end of input: … session(this->authDefaultKey, $login);
}
session(‘REFERER’, null);
return true;
}
// Auth登录后更新用户信息并重写session
public function relogin()
{
if (!session(’?’ . $this->authKey)) {
return false;
}
$this->getUserObj();
$login =
this->contain)->where(‘id’,
login) {
$this->logout();
return false;
}
$login =
login[
this->authKey, $login);
this->authDefaultKey);
if ($default[‘id’] == KaTeX parse error: Expected '}', got 'EOF' at end of input: … session(this->authDefaultKey, $login);
}
return true;
}
// Auth登出
public function logout()
{
this->authKey);
this->authDefaultKey);
if ($login[‘id’] == KaTeX parse error: Expected '}', got 'EOF' at end of input: … session(this->authDefaultKey, NULL);
}
session(KaTeX parse error: Expected 'EOF', got '}' at position 27: …ey, NULL); }̲ // Auth获取…key = NULL)
{
if (!session(’?’ . $this->authKey)) {
return false;
}
this->authKey);
if (!$key) {
return KaTeX parse error: Expected 'EOF', got '}' at position 16: login; }̲ if (in…key, array_keys($login))) {
return
key];
}
return KaTeX parse error: Expected 'EOF', got '}' at position 12: login; }̲ // Auth可访…method = NULL)
{
args)) {
GLOBALS[‘controller’]);
return;
}
if (!is_array($method)) {
method);
}
$this->allowMethod += KaTeX parse error: Expected 'EOF', got '}' at position 13: method; }̲ // Auth不可…method = NULL)
{
args)) {
KaTeX parse error: Expected 'EOF', got '}' at position 58: …eturn; }̲ if (!i…method)) {
method);
}
this->allowMethod, $method);
}
// 检查是否登录
public function check()
{
if (!session(’?’ . KaTeX parse error: Expected 'EOF', got '&' at position 16: this->authKey) &̲& !in_array(this->request[‘action’], $this->allowMethod)) {
session(‘REFERER’, request()->url(true));
return false;
}
return true;
}
// 检查是否登录
public function checkloged()
{
if (!session(’?’ . $this->authKey)) {
return false;
}
return true;
}
public function getPowerList()
{
$powerModel = model(‘Power’);
$powerTeeModel = model(‘PowerTree’);
$user_id = $this->user(‘id’);
$user_group_id = $this->user(‘user_group_id’);
$powers =
user_id)], [‘type’, ‘=’, ‘user’]])->find();
if ($powers) {
powers[‘content’]));
} else {
$powers =
user_group_id)], [‘type’, ‘=’, ‘usergroup’]])->find();
if ($powers) {
powers[‘content’]));
} else {
$powers = [];
}
}
return $powers;
}
public function checkPower()
{
GLOBALS[‘controller’]->params[‘controller’]) . ‘::’ . strtolower($GLOBALS[‘controller’]->params[‘action’]);
powerTeeModel->where(‘together’, $together)->count()) {
$powers =
powers)) {
return true;
}
return in_array(
powers);
}
return true;
}
// 密码加密,返回密码
public static function password(KaTeX parse error: Expected '}', got 'EOF' at end of input: …turn md5(crypt(password, config(‘auth.salt’) . substr($password, 0, 2)));
}
public function __destruct()
{
if (request()->isGet() && !request()->isAjax()) {
session(‘last_url’, request()->url(true));
}
} }
二,引用Auth 类
\app\home\controller\Index.php
<?php
namespace app\home\controller;
use app\common\controller\Home;
use think\facade\Cookie;
use think\Db;
use think\facade\Config;
use think\validate\ValidateRule;
use woo\helper\AuthHxf;
class Index extends Home
{
protected $db_config = 'db’;
public function initialize()
{
call_user_func(array(‘parent’, FUNCTION));
}
}*
*数据库配置
\data\config\database.php
<?php
return array (
‘type’ => ‘mysql’,
‘hostname’ => ‘127.0.0.1’,
‘username’ => ‘root’,
‘password’ => ‘root’,
‘hostport’ => ‘3306’,
‘charset’ => ‘utf8’,
‘database’ => '’,
‘prefix’ => ‘fa_’,
//数据库配置知识全库
'db_’ => [
‘type’ => ‘mysql’,
‘hostname’ => ‘127.0.0.1’,
‘username’ => ‘root’,
‘password’ => ‘root’,
‘charset’ => ‘utf8’,
‘database’ => '*’,
‘prefix’ => ‘zf_’,
],
//数据库配置知识全库
‘db_fqa_base’ => [
‘type’ => ‘mysql’,
‘hostname’ => ‘127.0.0.1’,
‘database’ => '’,
‘username’ => ‘root’,
// ‘password’ => ‘mysql’,.
‘password’ => ‘root’,
‘password’ => ‘root’,
‘charset’ => ‘utf8’,
‘prefix’ => ‘zf_’,
],
)
?>*
三,新建模型类user 和member类
\app\common\model\UserHxf.php
<?php
namespace app\common\model;
use woo\helper\AuthHxf;
class UserHxf extends App
{
public $display = ‘username’;
protected $table = ‘zf_user’;
protected $connection = 'db_’;
public $assoc = array(
‘UserGroup’ => array(
‘type’ => ‘belongsTo’
),
‘UserGrade’ => array(
‘type’ => ‘belongsTo’
),
‘Member’ => array(
‘type’ => ‘hasOne’,
‘deleteWith’ => true,
),
‘UserLogin’ => ‘hasMany’
);
public function initialize()
{
$this->form = array(
‘id’ => array(
‘type’ => ‘integer’,
‘name’ => ‘ID’,
‘elem’ => ‘hidden’,
),
‘username’ => array(
‘type’ => ‘string’,
‘name’ => ‘用户名’,
‘elem’ => ‘text’
),
‘password’ => array(
‘type’ => ‘string’,
‘name’ => ‘密码’,
‘elem’ => ‘password’,
‘list’ => ‘password’
),
‘user_group_id’ => array(
‘type’ => ‘integer’,
‘name’ => ‘用户组’,
‘elem’ => ‘assoc_select’,
‘list’ => ‘assoc’,
‘foreign’ => ‘UserGroup.title’,
‘prepare’ => ‘select’
),
‘user_grade_id’ => array(
‘type’ => ‘integer’,
‘name’ => ‘用户等级’,
‘elem’ => ‘format’,
‘list’ => ‘assoc’,
‘foreign’ => ‘UserGrade.title’
),
‘status’ => array(
‘type’ => ‘string’,
‘name’ => ‘状态’,
‘elem’ => ‘radio’,
‘options’ => array(
‘verified’ => ‘正常’,
‘unverified’ => ‘未激活’,
‘banned’ => ‘已禁用’
),
‘list’ => ‘options’,
),
‘email’ => array(
‘type’ => ‘string’,
‘name’ => ‘认证邮箱’,
‘elem’ => ‘text’,
‘list’ => ‘show’
),
‘user_score_sum’ => array(
‘type’ => ‘float’,
‘name’ => ‘积分’,
‘elem’ => 0,
‘list’ => ‘show’
),
‘logined’ => array(
‘type’ => ‘datetime’,
‘name’ => ‘最后登录时间’,
‘elem’ => 0,
‘list’ => ‘datetime’
),
‘logined_ip’ => array(
‘type’ => ‘string’,
‘name’ => ‘最后登录IP’,
‘elem’ => 0
),
‘logined_session_id’ => array(
‘type’ => ‘string’,
‘name’ => ‘最后登录SESSION_ID’,
‘elem’ => 0
),
‘created’ => array(
‘type’ => ‘datetime’,
‘name’ => ‘注册时间’,
‘elem’ => 0,
‘list’ => ‘datetime’
)
);
call_user_func(array(‘parent’, FUNCTION));
}
##当前类验证规则
protected $validate = array(
‘username’ => array(
‘rule’ => array(‘unique’, ‘user’)
),
‘password’ => array(
array(
‘rule’ => ‘require’,
‘on’ => ‘add’
),
array(
‘rule’ => array(‘length’, 4, 16)
)
),
‘email’=>array(
‘allowEmpty’ => true,
‘rule’ => ‘email’
),
‘user_group_id’ => array(
‘rule’ => array(‘egt’, 1),
‘message’ => ‘请选择用户组’
),
‘status’ => array(
‘rule’ => ‘require’
),
);
public function beforeInsertCall()
{
KaTeX parse error: Expected group after '_' at position 39: …rray('parent', _̲_FUNCTION__)); …this[‘password’]) &&
this[‘password’]);
if (isset($this[‘password’]))
this[‘password’]);
return $rslt;
}
public function beforeUpdateCall()
{
KaTeX parse error: Expected group after '_' at position 39: …rray('parent', _̲_FUNCTION__)); …this[‘password’]) &&
this[‘password’]);
if (isset($this[‘password’]))
this[‘password’]);
return $rslt;
}
public function afterInsertCall()
{
$rslt = call_user_func(array(‘parent’, FUNCTION));
$data[‘user_id’] = $this[‘id’];
$data[‘nickname’] = ‘’;
$memberModel = model(‘MemberHxf’);
$memberModel->data([]);
data);
return $rslt;
}
public function afterUpdateCall()
{
$rslt = call_user_func(array(‘parent’, FUNCTION));
$this->checkUserGrade();
return KaTeX parse error: Expected 'EOF', got '}' at position 11: rslt; }̲ protected …this->oldData[‘user_score_sum’]) || !isset(
this[‘id’])) {
return;
}
// 积分有改变
if ($this->oldData[‘user_score_sum’] != $this[‘user_score_sum’]) {
$gradeModel = model(‘UserGrade’);
$grade = $gradeModel
->where([
[‘min’, ‘<=’, $this[‘user_score_sum’]],
[‘max’, ‘>’,
grade) {
$gradeId = $grade[‘id’];
this[‘user_grade_id’]) ? $this[‘user_grade_id’] : $this->where(‘id’, ‘=’,
oldGradeId != $gradeId) {
$this->isValidate(false)->where(‘id’, ‘=’, $this[‘id’])->setField(‘user_grade_id’,
this->isValidate(false)->allowField([‘id’, ‘user_grade_id’])->save([‘user_grade_id’ => $gradeId], [‘id’ => $this[‘id’]]);
}
}
}
}
}*
\app\common\model\MemberHxf.php
<?php
namespace app\common\model;
class MemberHxf extends App
{
//关联模型
public $parentModel = ‘UserHxf’;
protected $table = ‘zf_member’;
protected $connection = 'db_’;
public $assoc = [
‘User’ => ‘belongsTo’
];
public function initialize()
{
$this->form = [
‘id’ => [
‘type’ => ‘integer’,
‘name’ => ‘ID’,
‘elem’ => ‘hidden’,
],
‘user_id’ => array(
‘type’ => ‘integer’,
‘name’ => ‘所属用户’,
‘foreign’ => ‘User.username’,
‘elem’ => ‘format’,
‘list’ => ‘assoc’
),
‘nickname’ => array(
‘type’ => ‘string’,
‘name’ => ‘昵称’,
‘elem’ => ‘text’,
‘list’ => ‘show’,
),
‘truename’ => array(
‘type’ => ‘string’,
‘name’ => ‘真实姓名’,
‘elem’ => ‘text’,
‘list’ => ‘show’,
),
‘headimg’ => array(
‘type’ => ‘string’,
‘name’ => ‘头像’,
‘elem’ => ‘image.upload’,
‘list’ => ‘image’,
‘upload’ => array(
‘maxSize’ => 512,
‘validExt’ => array(‘jpg’, ‘png’, ‘gif’)
)
),
‘sex’ => array(
‘type’ => ‘string’,
‘name’ => ‘性别’,
‘elem’ => ‘radio’,
‘options’ => [‘m’ => ‘男’, ‘f’ => ‘女’, ‘x’ => ‘保密’]
),
‘mobile’ => array(
‘type’ => ‘string’,
‘name’ => ‘手机号码’,
‘elem’ => ‘text’,
‘list’ => ‘show’,
),
‘region’ => array(
‘type’ => ‘none’,
‘name’ => ‘所在地区’,
‘elem’ => ‘multi_select.ajax’,
‘multi_field’ => [
‘province’ => ‘省’,
‘city’ => ‘市’,
‘area’ => ‘区’,
],
‘multi_options’ => [
‘order’ => [‘list_order’ => ‘DESC’,‘id’ => ‘ASC’],
‘where’ => []
],
‘foreign’ => ‘Region.title’,
‘list’ => ‘assoc’
),
‘province’ => array(
‘type’ => ‘integer’,
‘name’ => ‘省’,
‘elem’ => 0
),
‘city’ => array(
‘type’ => ‘integer’,
‘name’ => ‘市’,
‘elem’ => 0
),
‘area’ => array(
‘type’ => ‘integer’,
‘name’ => ‘区’,
‘elem’ => 0
),
‘address1111’ => array(
‘type’ => ‘string’,
‘name’ => ‘联系地址’,
‘elem’ => ‘text’,
‘list’ => ‘show’,
),
];
call_user_func_array([‘parent’, FUNCTION], func_get_args());
}
//数据验证
protected
user_id)
{
$member =
user_id))->fetchSql(true)->find();
echo
member)) {
$userModel = model(‘UserHxf’);
$user =
user_id));
if (empty($user)) {
return false;
}
user_id);
$data[‘nickname’] = ‘’;
data);
$member = $this;
}
return $member;
}
}*