添加用户调整等级
parent
c9f392e549
commit
7a6fd399cb
|
|
@ -0,0 +1,38 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Admin\Actions\Grid;
|
||||||
|
|
||||||
|
use App\Admin\Forms\UserEditAgent as UserEditAgentForm;
|
||||||
|
use Dcat\Admin\Grid\RowAction;
|
||||||
|
use Dcat\Admin\Widgets\Modal;
|
||||||
|
|
||||||
|
class UserEditAgent extends RowAction
|
||||||
|
{
|
||||||
|
public function title()
|
||||||
|
{
|
||||||
|
if ($this->title) {
|
||||||
|
return $this->title;
|
||||||
|
}
|
||||||
|
return '<i class="feather grid-action-icon icon-shuffle"></i> 修改等级 ';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Model|Authenticatable|HasPermissions|null $user
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function authorize($user): bool
|
||||||
|
{
|
||||||
|
return $user->can('dcat.admin.users.edit_agent');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function render()
|
||||||
|
{
|
||||||
|
$form = UserEditAgentForm::make()->payload(['id'=>$this->getKey()]);
|
||||||
|
return Modal::make()
|
||||||
|
->lg()
|
||||||
|
->title($this->title())
|
||||||
|
->body($form)
|
||||||
|
->button($this->title());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,6 +5,7 @@ namespace App\Admin\Controllers;
|
||||||
use App\Admin\Actions\Grid\DisableUser;
|
use App\Admin\Actions\Grid\DisableUser;
|
||||||
use App\Admin\Actions\Grid\EnableUser;
|
use App\Admin\Actions\Grid\EnableUser;
|
||||||
use App\Admin\Actions\Grid\Frozen;
|
use App\Admin\Actions\Grid\Frozen;
|
||||||
|
use App\Admin\Actions\Grid\UserEditAgent;
|
||||||
use App\Admin\Actions\Show\UserDisableBonus;
|
use App\Admin\Actions\Show\UserDisableBonus;
|
||||||
use App\Admin\Actions\Show\UserEditBank;
|
use App\Admin\Actions\Show\UserEditBank;
|
||||||
use App\Admin\Actions\Show\UserEditPhone;
|
use App\Admin\Actions\Show\UserEditPhone;
|
||||||
|
|
@ -102,10 +103,15 @@ class UserController extends AdminController
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo-冻结资产
|
// 冻结资产
|
||||||
if (Admin::user()->can('dcat.admin.users.frozen')) {
|
if (Admin::user()->can('dcat.admin.users.frozen')) {
|
||||||
$actions->append(new Frozen());
|
$actions->append(new Frozen());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//调整等级
|
||||||
|
if (Admin::user()->can('dcat.admin.users.edit_agent')) {
|
||||||
|
$actions->append(new UserEditAgent());
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$grid->filter(function (Grid\Filter $filter) {
|
$grid->filter(function (Grid\Filter $filter) {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,87 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Admin\Forms;
|
||||||
|
|
||||||
|
use App\Exceptions\BizException;
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Models\UserInfo;
|
||||||
|
use Dcat\Admin\Contracts\LazyRenderable;
|
||||||
|
use Dcat\Admin\Traits\LazyWidget;
|
||||||
|
use Dcat\Admin\Widgets\Form;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
|
class UserEditAgent extends Form implements LazyRenderable
|
||||||
|
{
|
||||||
|
use LazyWidget;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Model|Authenticatable|HasPermissions|null $user
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function authorize($user): bool
|
||||||
|
{
|
||||||
|
return $user->can('dcat.admin.users.edit_agent');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the form request.
|
||||||
|
*
|
||||||
|
* @param array $input
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function handle(array $input)
|
||||||
|
{
|
||||||
|
$id = $this->payload['id'] ?? 0;
|
||||||
|
$user = User::findOrFail($id);
|
||||||
|
// dd($user->userInfo->agent_level, $input['agent_level'], $user->userInfo->agent_level >= $input['agent_level']);
|
||||||
|
if ($user->userInfo->agent_level >= $input['agent_level']) {
|
||||||
|
throw new BizException('请选择大于当前的等级');
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
DB::beginTransaction();
|
||||||
|
//执行自己升级
|
||||||
|
$user->userInfo->update([
|
||||||
|
'agent_level'=>$input['agent_level'],
|
||||||
|
]);
|
||||||
|
//给自己的上级依次升级
|
||||||
|
if (count($pids = $user->userInfo->parent_ids) > 0) {
|
||||||
|
$ancestors = UserInfo::whereIn('user_id', $pids)->latest('depth')->get();
|
||||||
|
|
||||||
|
foreach ($ancestors as $ancestor) {
|
||||||
|
$ancestor->attemptUpgradeAgentLevel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
DB::commit();
|
||||||
|
} catch (Throwable $th) {
|
||||||
|
DB::rollBack();
|
||||||
|
report($th);
|
||||||
|
return $this->response()->error('操作失败:'.$th->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->response()
|
||||||
|
->success(__('admin.update_succeeded'))
|
||||||
|
->refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build a form here.
|
||||||
|
*/
|
||||||
|
public function form()
|
||||||
|
{
|
||||||
|
// dd(UserInfo::$agentLevelTexts);
|
||||||
|
$this->select('agent_level', '管理级别')->options(UserInfo::$agentLevelTexts)->required();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function default()
|
||||||
|
{
|
||||||
|
$id = $this->payload['id'] ?? 0;
|
||||||
|
$user = User::with('userInfo')->findOrFail($id);
|
||||||
|
return [
|
||||||
|
'agent_level' => $user->userInfo->agent_level,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue