2025-08-05 17:19:12 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\admin\service;
|
|
|
|
|
|
2025-08-14 20:08:57 +08:00
|
|
|
use app\admin\logic\UserLogic;
|
2025-08-05 17:19:12 +08:00
|
|
|
use app\common\traits\crud\CrudServiceTrait;
|
|
|
|
|
use app\common\model\User;
|
2025-08-14 20:08:57 +08:00
|
|
|
use app\common\validate\UserValidate;
|
|
|
|
|
use support\exception\BusinessException;
|
2025-08-05 17:19:12 +08:00
|
|
|
use support\Request;
|
|
|
|
|
|
|
|
|
|
class UserService
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
use CrudServiceTrait;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 指定当前要操作的模型
|
|
|
|
|
*/
|
|
|
|
|
protected string $modelClass = User::class;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 验证器
|
|
|
|
|
*/
|
|
|
|
|
protected $validator = UserValidate::class;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取验证器
|
|
|
|
|
*/
|
|
|
|
|
public function getValidator()
|
|
|
|
|
{
|
|
|
|
|
return new $this->validator;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-14 20:08:57 +08:00
|
|
|
/** 更新 */
|
|
|
|
|
public function serviceUpdateUser(Request $request, array $data = [])
|
|
|
|
|
{
|
2025-08-21 14:02:28 +08:00
|
|
|
if(isset($data['report_to']) && empty($data['report_to'])){
|
|
|
|
|
$data['report_to'] = 0;
|
|
|
|
|
}
|
2025-08-14 20:08:57 +08:00
|
|
|
$data = $this->preProcess('Update', $data, $request);
|
|
|
|
|
$config = $this->buildQueryConfig($request, 'update');
|
|
|
|
|
$updated = $this->logicUpdateBy($config, $data);
|
|
|
|
|
return $this->postProcess('Update', $data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 更新 */
|
|
|
|
|
public function serviceUpdate(Request $request, array $data = [])
|
|
|
|
|
{
|
2025-08-21 14:02:28 +08:00
|
|
|
if(isset($data['report_to']) && empty($data['report_to'])){
|
|
|
|
|
$data['report_to'] = 0;
|
|
|
|
|
}
|
2025-08-14 20:08:57 +08:00
|
|
|
unset($data['roles']);
|
|
|
|
|
unset($data['corp_name']);
|
|
|
|
|
$data = $this->preProcess('Update', $data, $request);
|
|
|
|
|
$config = $this->buildQueryConfig($request, 'edit');
|
|
|
|
|
$updated = $this->logicUpdateBy($config, $data);
|
|
|
|
|
return $this->postProcess('Update', $data);
|
|
|
|
|
}
|
2025-08-05 17:19:12 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造 CrudUtil 查询配置
|
|
|
|
|
*/
|
|
|
|
|
protected function buildQueryConfig(Request $request, string $scene = 'default'): array
|
|
|
|
|
{
|
|
|
|
|
$p = $request->all();
|
|
|
|
|
|
|
|
|
|
$config = [];
|
|
|
|
|
$config['model'] = $this->modelClass;
|
|
|
|
|
switch ($scene) {
|
|
|
|
|
case 'default':
|
|
|
|
|
$config['where']['status'] = 1;
|
|
|
|
|
$config['order']['id'] = 'desc';
|
|
|
|
|
break;
|
|
|
|
|
case 'index':
|
2025-08-07 09:51:30 +08:00
|
|
|
// $config['where']['status'] = 1;
|
2025-08-05 17:19:12 +08:00
|
|
|
$config['order']['id'] = 'desc';
|
2025-08-05 17:58:10 +08:00
|
|
|
|
|
|
|
|
// 关键字
|
|
|
|
|
if (isset($p['keyword']) && $p['keyword']) {
|
|
|
|
|
$k = '%' . $p['keyword'] . '%';
|
|
|
|
|
$config['where']['name'] = ['like', $k];
|
|
|
|
|
}
|
2025-08-20 09:40:52 +08:00
|
|
|
if (isset($p['status']) && $p['status']) {
|
|
|
|
|
$config['where']['status'] = $p['status'];
|
|
|
|
|
}
|
2025-08-08 17:51:08 +08:00
|
|
|
if (isset($p['user_id']) && $p['user_id']) {
|
|
|
|
|
$config['where']['user_id'] = $p['user_id'];
|
|
|
|
|
}
|
2025-08-06 09:45:56 +08:00
|
|
|
if (isset($p['id']) && $p['id']) {
|
|
|
|
|
$config['where']['id'] = $p['id'];
|
|
|
|
|
}
|
2025-08-05 17:58:10 +08:00
|
|
|
|
2025-08-14 20:08:57 +08:00
|
|
|
$config['fields'] = ['id','sn', 'name', 'email', 'sex', 'phone', 'avatar', 'corp_id', 'country_id', 'position', 'report_to', 'status', 'created_at'];
|
|
|
|
|
|
|
|
|
|
$config['with']['roles'][] = 'id';
|
|
|
|
|
$config['with']['roles'][] = 'name';
|
|
|
|
|
$config['with']['systems'][] = 'id';
|
|
|
|
|
$config['with']['systems'][] = 'name';
|
2025-08-05 17:19:12 +08:00
|
|
|
break;
|
|
|
|
|
case 'show':
|
|
|
|
|
$config['where']['id'] = $p['id'];
|
|
|
|
|
break;
|
|
|
|
|
case 'store':
|
|
|
|
|
break;
|
|
|
|
|
case 'update':
|
|
|
|
|
$config['where']['id'] = $p['id'];
|
|
|
|
|
break;
|
|
|
|
|
case 'destroy':
|
|
|
|
|
$config['where']['id'] = $p['id'];
|
|
|
|
|
break;
|
2025-08-14 20:08:57 +08:00
|
|
|
case 'savePassword':
|
|
|
|
|
$config['where']['id'] = $p['id'];
|
|
|
|
|
break;
|
|
|
|
|
case 'edit':
|
|
|
|
|
$config['where']['id'] = $request->user->uid;
|
|
|
|
|
break;
|
2025-08-05 17:19:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $config;
|
|
|
|
|
}
|
2025-08-14 20:08:57 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 授权
|
|
|
|
|
*
|
|
|
|
|
* @param Request $request
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function authorize(Request $request, array $data = [])
|
|
|
|
|
{
|
|
|
|
|
return (new UserLogic())->updateRoleAndSystem($data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 更新用户资源权限
|
|
|
|
|
*
|
|
|
|
|
* @param Request $request
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function updateResources(Request $request, array $data = [])
|
|
|
|
|
{
|
|
|
|
|
return (new UserLogic())->updateUserResources($data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 修改用户密码
|
|
|
|
|
*/
|
|
|
|
|
public function savePassword(Request $request, array $data)
|
|
|
|
|
{
|
|
|
|
|
$config = $this->buildQueryConfig($request, 'savePassword');
|
|
|
|
|
$data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
|
|
|
|
|
$user = $this->logicUpdateBy($config,$data);
|
|
|
|
|
return $user;
|
|
|
|
|
}
|
2025-08-05 17:19:12 +08:00
|
|
|
}
|