init
This commit is contained in:
243
app/admin/model/User.php
Executable file
243
app/admin/model/User.php
Executable file
@@ -0,0 +1,243 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\model;
|
||||
|
||||
use think\Model;
|
||||
use think\Request;
|
||||
use think\Config;
|
||||
use think\Session;
|
||||
|
||||
class User extends Model {
|
||||
|
||||
use \app\common\traits\AdminModel;
|
||||
|
||||
protected $insert = ['stat' => 0, 'register_time', 'register_ip', 'last_update_time'];
|
||||
protected $update = ['last_update_time'];
|
||||
|
||||
public function getRoleUsers($where = null, $order = null, $field = null, $limit = null) {
|
||||
$this->alias('u')->join('auth_role ar', 'u.role_id=ar.id', 'LEFT');
|
||||
if (is_array($where)) {
|
||||
$where = array_merge([], $where);
|
||||
}
|
||||
if ($where) {
|
||||
$this->where($where);
|
||||
}
|
||||
if ($order) {
|
||||
$this->order($order);
|
||||
}
|
||||
if ($field) {
|
||||
$this->field($field);
|
||||
}
|
||||
if (empty($limit)) {
|
||||
$limit = Config::get('list_rows');
|
||||
}
|
||||
$this->group('u.id');
|
||||
//$this->having('max(ud.dept_id)');
|
||||
$object = $this->paginate($limit);
|
||||
return $object;
|
||||
}
|
||||
|
||||
public function getPageLists($where = null, $order = null, $field = null, $limit = null) {
|
||||
$this->alias('u')->join('user_role ur', 'u.role_id=ur.id', 'LEFT');
|
||||
if (is_array($where)) {
|
||||
$where = array_merge(['u.stat' => ['eq', '0']], $where);
|
||||
}
|
||||
if ($where) {
|
||||
$this->where($where);
|
||||
}
|
||||
if ($order) {
|
||||
$this->order($order);
|
||||
}
|
||||
if ($field) {
|
||||
$this->field($field);
|
||||
}
|
||||
if (empty($limit)) {
|
||||
$limit = Config::get('list_rows');
|
||||
}
|
||||
$object = $this->paginate($limit);
|
||||
return $object;
|
||||
}
|
||||
|
||||
public function getOption($id = 0, $where = null, $order = null, $field = null, $limit = null) {
|
||||
$options = '';
|
||||
if ($where) {
|
||||
$this->where($where);
|
||||
}
|
||||
if ($order) {
|
||||
$this->order($order);
|
||||
}
|
||||
if ($field) {
|
||||
$this->field($field);
|
||||
}
|
||||
if ($limit) {
|
||||
$this->limit($limit);
|
||||
}
|
||||
$list = $this->select();
|
||||
if ($list) {
|
||||
//$options = '<option value="0">请选择...</option>' . "\n";
|
||||
foreach ($list as $k => $row) {
|
||||
if ($row['id'] == $id) {
|
||||
$options.='<option value="' . $row['id'] . '" selected>' . $row['username'] . '</option>' . "\n";
|
||||
} else {
|
||||
$options.='<option value="' . $row['id'] . '">' . $row['username'] . '</option>' . "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
return $options;
|
||||
}
|
||||
|
||||
public function insertRow($data, $siteid = 32267) {
|
||||
$row = array(
|
||||
'username' => $data['username'],
|
||||
'email' => $data['email'],
|
||||
'role_id' => $data['role_id'],
|
||||
'stat' => $data['stat'],
|
||||
);
|
||||
if (isset($data['password'])) {
|
||||
//$salt = getstr_random();
|
||||
//$row['password'] = md5($data['password'] . $salt);
|
||||
$row['salt'] = $data['password'];
|
||||
$row['password'] = md5($data['password']);
|
||||
}
|
||||
if (isset($data['picture'])) {
|
||||
$row['picture'] = $data['picture'];
|
||||
}
|
||||
if (!isset($data['position'])) {
|
||||
$row['position'] = 'admin';
|
||||
}
|
||||
$row['siteid'] = $siteid;
|
||||
$object = $this::create($row);
|
||||
return $object;
|
||||
}
|
||||
|
||||
public function updateRow($data = [], $where = [], $field = null) {
|
||||
if (isset($data['id'])) {
|
||||
$row['id'] = $data['id'];
|
||||
}
|
||||
if (isset($data['username'])) {
|
||||
$row['username'] = $data['username'];
|
||||
}
|
||||
if (isset($data['role_id'])) {
|
||||
$row['role_id'] = $data['role_id'];
|
||||
}
|
||||
if (isset($data['stat'])) {
|
||||
$row['stat'] = $data['stat'];
|
||||
}
|
||||
if (isset($data['position'])) {
|
||||
$row['position'] = $data['position'];
|
||||
}
|
||||
if (isset($data['email'])) {
|
||||
$row['email'] = $data['email'];
|
||||
}
|
||||
if (isset($data['picture'])) {
|
||||
$row['picture'] = $data['picture'];
|
||||
}
|
||||
$object = $this::update($row, $where, $field);
|
||||
return $object;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户密码
|
||||
*/
|
||||
public function updatePassword($data) {
|
||||
//$salt = getstr_random();
|
||||
$row = array(
|
||||
'id' => $data['id'],
|
||||
//'password' => md5($data['newpassword'] . $salt),
|
||||
'salt' => $data['newpassword'],
|
||||
'password' => md5($data['newpassword']),
|
||||
);
|
||||
$object = $this::update($row);
|
||||
return $object;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户登录认证
|
||||
* @param string $condition 验证条件如用户名邮箱手机号ID
|
||||
* @param string $password 用户密码
|
||||
* @param integer $type 用户名类型 (1-用户名,2-邮箱,3-手机,4-UID)
|
||||
* @return integer 登录成功-用户ID,登录失败-错误编号
|
||||
*/
|
||||
public function login($condition, $password, $type = 1) {
|
||||
$where = [];
|
||||
switch ($type) {
|
||||
case 1:$where['username'] = $condition;
|
||||
break;
|
||||
case 2:$where['email'] = $condition;
|
||||
break;
|
||||
case 3:$where['mobile'] = $condition;
|
||||
break;
|
||||
case 4:$where['id'] = $condition;
|
||||
break;
|
||||
default:
|
||||
action_log('登录失败', '提交参数错误 被序列化的信息:' . serialize(Request::instance()->request()), -1, Request::instance()->header());
|
||||
return ['status' => false, 'msg' => '参数错误', 'id' => 0]; //参数错误
|
||||
}
|
||||
/* 获取用户数据 */
|
||||
$row = $this->where($where)->find();
|
||||
if (empty($row) || (int) $row->stat < 0) {
|
||||
action_log('登录失败', '用户不存在或被禁用 被序列化的信息:' . serialize(Request::instance()->request()), -1, Request::instance()->header());
|
||||
return ['status' => false, 'msg' => '用户不存在或被禁用', 'id' => 0];
|
||||
}
|
||||
/* 验证用户密码 */
|
||||
if (md5($password) !== $row->password) {
|
||||
action_log('登录失败', '密码错误 被序列化的信息:' . serialize(Request::instance()->request()), -1, Request::instance()->header());
|
||||
return ['status' => false, 'msg' => '密码错误', 'id' => 0];
|
||||
}
|
||||
unset($row->password);
|
||||
unset($row->salt);
|
||||
/* 登录用户 */
|
||||
$this->autoLogin($row->toArray());
|
||||
return ['status' => true, 'msg' => '登录成功', 'id' => $row->id]; //登录成功,返回用户ID
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动登录用户
|
||||
* @param integer $row 用户信息数组
|
||||
*/
|
||||
private function autoLogin($row) {
|
||||
/* 更新登录信息 */
|
||||
$data = [
|
||||
'id' => $row['id'],
|
||||
'login' => \think\Db::raw('`login`+1'),
|
||||
'last_login_time' => Request::instance()->time(),
|
||||
'last_login_ip' => Request::instance()->ip()
|
||||
];
|
||||
$this::update($data);
|
||||
$fields = ['id', 'username', 'email', 'picture', 'last_login_time', 'role_id', 'stat', 'siteid'];
|
||||
foreach ($fields as $field) {
|
||||
$session_user[$field] = $row[$field];
|
||||
}
|
||||
/* 记录登录SESSION和COOKIES */
|
||||
Session::set('user_auth', $session_user);
|
||||
Session::set('user_auth_sign', data_auth_sign($session_user));
|
||||
$content = '用户' . $row['username'] . '(' . $row['id'] . ')上次登录时间:' . date('Y-m-d H:i:s', $row['last_login_time']) . ',上次登录IP:' . $row['last_login_ip'];
|
||||
action_log('登录成功', $content, $row['id'], Request::instance()->header());
|
||||
unset($row);
|
||||
//记录行为
|
||||
//$param = ['action' => 'user_login', 'model' => 'member', 'record_id' => $row['id']];
|
||||
//Hook::listen('user_behavior', $param);
|
||||
}
|
||||
|
||||
public function logout() {
|
||||
Session::delete('user_auth', null);
|
||||
Session::delete('user_auth_sign', null);
|
||||
}
|
||||
|
||||
protected function setRegisterTimeAttr($value, $data) {
|
||||
return time();
|
||||
}
|
||||
|
||||
protected function setLastLoginTimeAttr($value, $data) {
|
||||
return time();
|
||||
}
|
||||
|
||||
protected function setLastUpdateTimeAttr($value, $data) {
|
||||
return time();
|
||||
}
|
||||
|
||||
protected function setRegisterIpAttr() {
|
||||
return Request::instance()->ip();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user