样机小程序
This commit is contained in:
@@ -0,0 +1,180 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\service;
|
||||
|
||||
use app\admin\logic\UserLogic;
|
||||
use app\common\traits\crud\CrudServiceTrait;
|
||||
use app\common\model\User;
|
||||
use app\common\validate\UserValidate;
|
||||
use support\exception\BusinessException;
|
||||
use support\Request;
|
||||
|
||||
class UserService
|
||||
{
|
||||
|
||||
use CrudServiceTrait;
|
||||
|
||||
/**
|
||||
* 指定当前要操作的模型
|
||||
*/
|
||||
protected string $modelClass = User::class;
|
||||
|
||||
/**
|
||||
* 验证器
|
||||
*/
|
||||
protected $validator = UserValidate::class;
|
||||
|
||||
/**
|
||||
* 获取验证器
|
||||
*/
|
||||
public function getValidator()
|
||||
{
|
||||
return new $this->validator;
|
||||
}
|
||||
|
||||
/** 更新 */
|
||||
public function serviceCreateUser(Request $request, array $data = [])
|
||||
{
|
||||
$config = $this->buildQueryConfig($request, 'create');
|
||||
if(isset($data['avatar']) && empty($data['avatar'])){
|
||||
$data['avatar'] = 'bsh_datax/user/20251021/a4ee815405a1506abeec1af0f738f70f_68f6e76514b66.jpg';
|
||||
}
|
||||
if(empty($data['password'])){
|
||||
$data['password'] = password_hash('12345678', PASSWORD_DEFAULT);
|
||||
}else{
|
||||
$data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
|
||||
}
|
||||
$created = $this->logicCreate($data);
|
||||
return $this->postProcess('Create', $data);
|
||||
}
|
||||
|
||||
/** 更新 */
|
||||
public function serviceUpdateUser(Request $request, array $data = [])
|
||||
{
|
||||
$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 = [])
|
||||
{
|
||||
unset($data['roles']);
|
||||
unset($data['avatar_pull']);
|
||||
$data = $this->preProcess('Update', $data, $request);
|
||||
$config = $this->buildQueryConfig($request, 'edit');
|
||||
$updated = $this->logicUpdateBy($config, $data);
|
||||
return $this->postProcess('Update', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造 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':
|
||||
// $config['where']['status'] = 1;
|
||||
$config['order']['id'] = 'desc';
|
||||
|
||||
// 关键字
|
||||
if (isset($p['keyword']) && $p['keyword']) {
|
||||
$k = '%' . $p['keyword'] . '%';
|
||||
$config['where']['name'] = ['like', $k];
|
||||
}
|
||||
if (isset($p['status']) && $p['status']) {
|
||||
$config['where']['status'] = $p['status'];
|
||||
}
|
||||
if (isset($p['user_id']) && $p['user_id']) {
|
||||
$config['where']['user_id'] = $p['user_id'];
|
||||
}
|
||||
if (isset($p['id']) && $p['id']) {
|
||||
$config['where']['id'] = $p['id'];
|
||||
}
|
||||
if (isset($p['corp_id']) && $p['corp_id']) {
|
||||
$config['where']['corp_id'] = $p['corp_id'];
|
||||
}
|
||||
|
||||
$config['fields'] = ['id','sn', 'name', 'email', 'mobile', 'avatar', 'nickname', 'is_release', 'status', 'created_at'];
|
||||
|
||||
$config['with']['roles'][] = 'id';
|
||||
$config['with']['roles'][] = 'name';
|
||||
$config['with']['systems'][] = 'id';
|
||||
$config['with']['systems'][] = 'name';
|
||||
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;
|
||||
case 'savePassword':
|
||||
$config['where']['id'] = $p['id'];
|
||||
break;
|
||||
case 'edit':
|
||||
case 'updateSign':
|
||||
$config['where']['id'] = $request->user->uid;
|
||||
break;
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 授权
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function serviceUpdateSign(Request $request, array $data)
|
||||
{
|
||||
$config = $this->buildQueryConfig($request, 'updateSign');
|
||||
$user = $this->logicUpdateBy($config,$data);
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user