<?php
|
|
namespace app\operations\model\plus\region;
|
|
use app\common\model\plus\operations\UserLogin as UserLoginModel;
|
|
/**
|
* 区域代理用户模型
|
*/
|
class UserLogin extends UserLoginModel
|
{
|
public function getList($params)
|
{
|
return $this->with(['roles.role'])->where('is_delete', '=', 0)
|
->order(['create_time' => 'desc'])
|
->paginate($params);
|
}
|
|
/**
|
* 获取所有角色
|
*/
|
private function getAll()
|
{
|
$data = $this->order(['sort' => 'asc', 'create_time' => 'asc'])->select();
|
return $data ? $data->toArray() : [];
|
}
|
|
public function add($data)
|
{
|
$this->startTrans();
|
try {
|
$arr = [
|
'user_name' => trim($data['user_name']),
|
'password' => salt_hash($data['password']),
|
'real_name' => trim($data['real_name']),
|
'mobile' => $data['mobile'] ?? '',
|
'user_id' => session('jjjshop_operations')['user']['user_id'],
|
'app_id' => self::$app_id,
|
'create_time' => time(),
|
'update_time' => time()
|
];
|
|
self::create($arr);
|
// 支持role_id或access_id字段
|
$roleIds = isset($data['role_id']) ? $data['role_id'] : (isset($data['access_id']) ? $data['access_id'] : []);
|
if (!empty($roleIds)) {
|
$this->addRole($roleIds);
|
}
|
|
// 事务提交
|
$this->commit();
|
return true;
|
} catch (\Exception $e) {
|
$this->error = $e->getMessage();
|
$this->rollback();
|
return false;
|
}
|
}
|
|
public function getUserName($where, $operations_user_id = 0)
|
{
|
if ($operations_user_id > 0) {
|
return $this->where($where)->where('operations_user_id', '<>', $operations_user_id)->count();
|
}
|
return $this->where($where)->count();
|
}
|
|
public function edit($data)
|
{
|
$this->startTrans();
|
try {
|
$arr = [
|
'user_name' => $data['user_name'],
|
'real_name' => $data['real_name'],
|
'mobile' => $data['mobile'] ?? '',
|
'update_time' => time()
|
];
|
|
if (!empty($data['password'])) {
|
$arr['password'] = salt_hash($data['password']);
|
}
|
$this->save($arr);
|
// 支持role_id或access_id字段
|
$roleIds = isset($data['role_ids']) ? $data['role_ids'] : (isset($data['access_id']) ? $data['access_id'] : []);
|
if (!empty($roleIds)) {
|
$this->addRole($roleIds);
|
}
|
|
// 事务提交
|
$this->commit();
|
return true;
|
} catch (\Exception $e) {
|
$this->error = $e->getMessage();
|
$this->rollback();
|
return false;
|
}
|
}
|
public function del($where)
|
{
|
self::update(['is_delete' => 1, 'update_time' => time()], $where);
|
return UserRole::where('operations_user_id', '=', $where['user_id'])->delete();
|
}
|
}
|