<?php
|
|
namespace app\common\model\plus\operations;
|
|
use app\common\model\BaseModel;
|
|
/**
|
* 运营中心管理员模型
|
*/
|
class User extends BaseModel
|
{
|
protected $name = 'operations_user';
|
protected $pk = 'operations_user_id';
|
|
/**
|
* 关联角色
|
* @return \think\model\relation\HasMany
|
*/
|
public function roles()
|
{
|
return $this->hasMany('UserRole', 'operations_user_id', 'operations_user_id');
|
}
|
|
/**
|
* 获取管理员列表
|
*/
|
public function getList($params)
|
{
|
$model = $this->alias('user')
|
->field('user.*')
|
->where('user.is_delete', '=', 0);
|
|
// 搜索条件
|
if (!empty($params['search'])) {
|
$model = $model->where('user.user_name|user.real_name', 'like', '%' . $params['search'] . '%');
|
}
|
|
$list = $model->order(['user.operations_user_id' => 'desc'])
|
->paginate($params['limit'] ?? 15);
|
|
return $list;
|
}
|
|
/**
|
* 获取详情
|
*/
|
public static function detail($where, $with = [])
|
{
|
if(is_array($where)){
|
return (new static())->where($where)->with($with)->find();
|
} else{
|
return (new static())->where('operations_user_id', '=', $where)->with($with)->find();
|
}
|
}
|
|
/**
|
* 添加管理员
|
*/
|
public function add($data)
|
{
|
$this->startTrans();
|
try {
|
// 检查用户名是否已存在
|
$exists = $this->where('user_name', '=', $data['user_name'])->find();
|
if ($exists) {
|
$this->error = '用户名已存在';
|
return false;
|
}
|
|
// 准备数据
|
$saveData = [
|
'user_name' => $data['user_name'],
|
'password' => password_hash($data['password'], PASSWORD_DEFAULT),
|
'real_name' => $data['real_name'],
|
'real_password' => $data['password'] ?? '',
|
'is_super' => $data['is_super'] ?? 0,
|
'is_delete' => 0,
|
'user_id' => $data['user_id'] ?? 0,
|
'app_id' => self::$app_id,
|
'create_time' => time(),
|
'update_time' => time(),
|
'client_id' => 0
|
];
|
|
$res = $this->save($saveData);
|
|
// 保存角色关系
|
if (isset($data['role_id']) && !empty($data['role_id'])) {
|
$this->addRole($data['role_id']);
|
}
|
|
$this->commit();
|
return $res !== false;
|
} catch (\Exception $e) {
|
$this->error = $e->getMessage();
|
$this->rollback();
|
return false;
|
}
|
}
|
|
/**
|
* 编辑管理员
|
*/
|
public function edit($data)
|
{
|
$saveData = [
|
'real_name' => $data['real_name'],
|
'update_time' => time()
|
];
|
|
// 如果修改了密码
|
if (!empty($data['password'])) {
|
$saveData['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
|
$saveData['real_password'] = $data['password'];
|
}
|
|
// 如果有角色ID
|
if (isset($data['role_ids'])) {
|
$this->addRole($data['role_ids']);
|
}
|
|
return $this->save($saveData) !== false;
|
}
|
|
public function addRole($data)
|
{
|
$this->roles()->where('operations_user_id', '=', $this->operations_user_id)->delete();
|
$list = array_map(function($val) use ($data) {
|
return [
|
'role_id' => $val,
|
'app_id' => self::$app_id
|
];
|
}, $data);
|
$this->roles()->saveAll($list);
|
}
|
/**
|
* 删除管理员
|
*/
|
public function setDelete()
|
{
|
return $this->save([
|
'is_delete' => 1,
|
'update_time' => time()
|
]);
|
}
|
|
/**
|
* 验证登录
|
*/
|
public static function login($userName, $password)
|
{
|
$user = self::where('user_name', '=', $userName)
|
->where('is_delete', '=', 0)
|
->find();
|
if (!$user) {
|
return false;
|
}
|
|
if (salt_hash($password) != $user['password']) {
|
return false;
|
}
|
|
return $user;
|
}
|
}
|