更新
This commit is contained in:
192
app/model/Member.php
Normal file
192
app/model/Member.php
Normal file
@@ -0,0 +1,192 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\model;
|
||||
|
||||
use sent\jwt\facade\JWTAuth;
|
||||
use think\Model;
|
||||
use xin\helper\Server;
|
||||
|
||||
class Member extends Model {
|
||||
|
||||
protected $pk = 'uid';
|
||||
|
||||
protected $createTime = 'reg_time';
|
||||
protected $updateTime = 'last_login_time';
|
||||
|
||||
protected $insert = ['reg_ip'];
|
||||
|
||||
protected $status = [
|
||||
1 => '正常',
|
||||
0 => '禁用',
|
||||
];
|
||||
|
||||
public $loginVisible = ['uid', 'username', 'nickname', 'access_token', 'department']; //用户登录成功返回的字段
|
||||
|
||||
protected function getStatusTextAttr($value, $data) {
|
||||
return isset($this->status[$data['status']]) ? $this->status[$data['status']] : '未知';
|
||||
}
|
||||
|
||||
protected function getAvatarAttr($value, $data) {
|
||||
return avatar($data['uid']);
|
||||
}
|
||||
|
||||
protected function setPasswordAttr($value, $data) {
|
||||
return md5($value . $data['salt']);
|
||||
}
|
||||
|
||||
protected function setRegIpAttr($value) {
|
||||
return Server::getRemoteIp();
|
||||
}
|
||||
|
||||
protected function getAccessTokenAttr($value, $data) {
|
||||
$token = ['data' => ['uid' => $data['uid'], 'username' => $data['username'], 'password' => $data['password'], 'department' => $data['department']]];
|
||||
return JWTAuth::builder($token); //参数为用户认证的信息,请自行添加
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
*/
|
||||
public function login($request) {
|
||||
$username = $request->param('username', '');
|
||||
$password = $request->param('password', '');
|
||||
$type = $request->param('type', 1);
|
||||
$map = array();
|
||||
switch ($type) {
|
||||
case 1:
|
||||
$map['username'] = $username;
|
||||
break;
|
||||
case 2:
|
||||
$map['email'] = $username;
|
||||
break;
|
||||
case 3:
|
||||
$map['mobile'] = $username;
|
||||
break;
|
||||
default:
|
||||
$this->error = "参数错误";
|
||||
return false; //参数错误
|
||||
}
|
||||
if (!$username) {
|
||||
$this->error = "用户名不能为空";
|
||||
return false;
|
||||
}
|
||||
|
||||
$user = $this->where($map)->find();
|
||||
if (isset($user['uid']) && $user['uid'] && $user['status']) {
|
||||
/* 验证用户密码 */
|
||||
if (md5($password . $user['salt']) === $user['password']) {
|
||||
/* 更新登录信息 */
|
||||
$this->record($user);
|
||||
return $user->append(array('access_token', 'avatar'))->visible($this->loginVisible)->toArray(); //登录成功,返回用户信息
|
||||
} else {
|
||||
$this->error = "密码错误";
|
||||
return false; //密码错误
|
||||
}
|
||||
} else {
|
||||
$this->error = "用户不存在或被禁用";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @title: 注册
|
||||
*/
|
||||
public function register($request) {
|
||||
$data = [];
|
||||
$data['username'] = $request->param('username', '');
|
||||
$data['password'] = $request->param('password', '');
|
||||
$data['repassword'] = $request->param('repassword', '');
|
||||
$data['email'] = $request->param('email', '');
|
||||
$data['mobile'] = $request->param('mobile', '');
|
||||
$data['salt'] = rand_string(6);
|
||||
|
||||
$result = self::create($data);
|
||||
if (false !== $result) {
|
||||
$user = $this->where('uid', $result->uid)->find();
|
||||
} else {
|
||||
$this->error = "注册失败!";
|
||||
return false;
|
||||
}
|
||||
/* 更新登录信息 */
|
||||
$this->record($user);
|
||||
return $user->append(array('access_token', 'avatar'))->visible($this->loginVisible)->toArray(); //登录成功,返回用户信息
|
||||
}
|
||||
|
||||
/**
|
||||
* @title: 获取用户列表
|
||||
*/
|
||||
public function getUserList($request) {
|
||||
$map = array();
|
||||
$param = $request->param();
|
||||
|
||||
$order = "status desc, uid desc";
|
||||
|
||||
$map[] = ['status', '>=', 0];
|
||||
|
||||
if (isset($param['status']) && $param['status'] != '') {
|
||||
$map[] = ['status', '=', $param['status']];
|
||||
}
|
||||
|
||||
if (isset($param['department']) && $param['department'] != '') {
|
||||
$map[] = ['department', '=', $param['department']];
|
||||
}
|
||||
|
||||
if (isset($param['email']) && $param['email'] != '') {
|
||||
$map[] = ['email', '=', $param['email']];
|
||||
}
|
||||
if (isset($param['name']) && $param['name'] != '') {
|
||||
$map[] = ['username|nickname', 'LIKE', '%' . $param['name'] . '%'];
|
||||
}
|
||||
|
||||
if (isset($param['task_id']) && $param['task_id']) {
|
||||
$map[] = ['uid', 'IN', function ($query) use ($param) {
|
||||
$query->name('task_user')->where('task_id', $param['task_id'])->field('user_id');
|
||||
}];
|
||||
}
|
||||
|
||||
$list = self::with(['depart', 'role'])->field('uid,username,nickname,status,email,mobile,department,reg_time')->where($map)->order($order)->paginate($request->pageConfig);
|
||||
return $list->append(['avatar', 'status_text'])->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* @title: 获取用户列表
|
||||
*/
|
||||
public function getUserDetail($request) {
|
||||
$uid = $request->param('uid', $request->user['uid']);
|
||||
if (!$uid) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$info = $this->where('uid', $uid)->find();
|
||||
return $info->append(['avatar', 'status_text'])->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户登录信息更新
|
||||
* @param [type] $user [description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function record($user) {
|
||||
/* 更新登录信息 */
|
||||
$data = array(
|
||||
'uid' => $user['uid'],
|
||||
'login' => array('inc', '1'),
|
||||
'last_login_time' => time(),
|
||||
'last_login_ip' => get_client_ip(1),
|
||||
);
|
||||
self::where(array('uid' => $user['uid']))->update($data);
|
||||
}
|
||||
|
||||
public function depart() {
|
||||
return $this->hasOne('Department', 'id', 'department');
|
||||
}
|
||||
|
||||
public function role() {
|
||||
return $this->hasOne('RoleAccess', 'uid', 'uid');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user