123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- namespace App\Admin\Controllers;
- use App\Admin\Actions\Grid\ResetPassword;
- use App\Models\Account;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Dcat\Admin\Http\Controllers\AdminController;
- class AccountController extends AdminController
- {
- /**
- * Make a grid builder.
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new Account(), function (Grid $grid) {
- $grid->column('id')->sortable();
- $grid->column('user_name');
- $grid->column('type')
- ->using(config('global.user_type'))
- ->label(['info','primary','success']);;
- $grid->column('account');
- $grid->column('status')
- ->using(config('global.user_status'))
- ->label(['danger','success'])->switch();
- $grid->column('remark')->editable();
- $grid->column('created_at');
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- $actions->append(new ResetPassword(Account::class, $actions->row->id));
- });
- $grid->disableViewButton();
- $grid->export()
- ->titles([
- 'user_name' => '用户名',
- 'type_text' => '用户类型',
- 'account' => '账号',
- 'status_text' => '状态',
- 'remark' => '备注',
- 'created_at' => '添加时间',
- ])->rows(function ($rows){
- foreach ($rows as &$row) {
- $row['type_text']= config('global.user_type')[$row['type']];
- $row['status_text'] = config('global.user_status')[$row['status']];
- }
- return $rows;
- })
- ->xlsx()
- ->disableExportSelectedRow();
- });
- }
- /**
- * Make a show builder.
- *
- * @param mixed $id
- *
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make($id, new Account(), function (Show $show) {
- $show->field('id');
- $show->field('user_name');
- $show->field('account');
- $show->field('password');
- $show->field('type');
- $show->field('remark');
- $show->field('status');
- $show->field('created_at');
- $show->field('updated_at');
- });
- }
- /**
- * Make a form builder.
- *
- * @return Form
- */
- protected function form()
- {
- return Form::make(new Account(), function (Form $form) {
- $form->display('id');
- $form->text('user_name')->required();
- $form->mobile('account')
- ->placeholder('手机号')
- ->required()
- ->rules(function ($form) {
- // 如果不是编辑状态,则添加字段唯一验证
- if (!$form->model()->id) {
- return 'unique:accounts';
- }
- });
- $form->password('password')
- ->placeholder('默认123456');
- $form->radio('type')
- ->options(config('global.user_type'))
- ->default(1);
- $form->radio('status')
- ->options(config('global.user_status'))
- ->default(1);
- $form->textarea('remark');
- // $form->display('created_at');
- //$form->display('updated_at');
- $form->saving(function (Form $form) {
- // 如果不是编辑状态 不填写密码默认123456
- if (!$form->model()->id) {
- if(!$form->password){
- $form->password = \Hash::make(123456);
- }else{
- $form->password = \Hash::make($form->password);
- }
- }else{
- if ($form->password && $form->model()->password != $form->password) {
- $form->password = \Hash::make($form->password);
- }
- if (! $form->password) {
- $form->deleteInput('password');
- }
- }
- });
- $form->disableViewButton();
- $form->disableDeleteButton();
- $form->disableListButton();
- $form->disableEditingCheck();
- $form->disableViewCheck();
- $form->disableCreatingCheck();
- });
- }
- }
|