user-balance
parent
5582972d5b
commit
76868ee967
|
|
@ -23,7 +23,8 @@
|
|||
|
||||
```php
|
||||
$permissions = [
|
||||
'users' => ['name' => '用户管理', 'curd' => true],
|
||||
'users' => ['name' => '用户管理', 'curd' => true, 'children' => ['balance' => '变更余额']],
|
||||
'user-balance' => ['name' => '余额流水', 'curd' => ['index', 'show']],
|
||||
];
|
||||
```
|
||||
|
||||
|
|
@ -31,8 +32,9 @@ $permissions = [
|
|||
|
||||
```php
|
||||
$menus = [
|
||||
['title' => '用户模块', 'icon' => 'feather icon-user', 'uri' => '/users', 'permission' => 'users', 'children' => [
|
||||
['title' => '用户管理', 'icon' => '', 'uri' => '/users', 'permission' => 'users']
|
||||
['title' => '用户模块', 'icon' => 'feather icon-user', 'uri' => '/users', 'permission' => ['users', 'user_balance'], 'children' => [
|
||||
['title' => '用户管理', 'icon' => '', 'uri' => '/users', 'permission' => 'users'],
|
||||
['title' => '余额流水', 'icon' => '', 'uri' => '/user-balance', 'permission' => 'user_balance'],
|
||||
]],
|
||||
];
|
||||
```
|
||||
|
|
|
|||
|
|
@ -7,9 +7,14 @@ return [
|
|||
],
|
||||
'fields' => [
|
||||
'user_id' => '用户',
|
||||
'user' => [
|
||||
'phone' => '用户',
|
||||
],
|
||||
'cate' => '类别',
|
||||
'amount' => '金额',
|
||||
'description' => '描述',
|
||||
'created_at' => '时间',
|
||||
'balance' => '剩余',
|
||||
'remarks' => '备注',
|
||||
]
|
||||
];
|
||||
|
|
|
|||
|
|
@ -11,4 +11,5 @@ Route::group([
|
|||
Route::get('api/users', [UserController::class, 'list'])->name('dcat.admin.api.users');
|
||||
|
||||
Route::resource('users', UserController::class)->names('dcat.admin.users');
|
||||
Route::resource('user-balance', UserBalanceController::class)->names('dcat.admin.user_balance');
|
||||
});
|
||||
|
|
|
|||
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace Peidikeji\User\Action;
|
||||
|
||||
use Dcat\Admin\Show\AbstractTool;
|
||||
use Dcat\Admin\Widgets\Modal;
|
||||
use Peidikeji\User\Form\BalanceForm;
|
||||
|
||||
class ShowBalance extends AbstractTool
|
||||
{
|
||||
protected $style = 'btn btn-sm btn-danger';
|
||||
|
||||
protected $title = '变更余额';
|
||||
|
||||
protected function html()
|
||||
{
|
||||
$model = $this->parent->model();
|
||||
$form = BalanceForm::make()->payload(['balance' => $model->balance, 'id' => $model->id]);
|
||||
return Modal::make()->lg()->title($this->title)->body($form)->button('<button type="button" class="'.$this->style.'">'.$this->title.'</button>');
|
||||
}
|
||||
|
||||
protected function authorize($user): bool
|
||||
{
|
||||
return $user->can('dcat.admin.users.balance');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
namespace Peidikeji\User\Action;
|
||||
|
||||
use Dcat\Admin\Show\AbstractTool;
|
||||
use Dcat\Admin\Widgets\Modal;
|
||||
use Peidikeji\User\Form\RemarkForm;
|
||||
|
||||
class ShowRemark extends AbstractTool
|
||||
{
|
||||
protected $style = 'btn btn-sm btn-primary';
|
||||
|
||||
protected $title = '设置备注';
|
||||
|
||||
protected function html()
|
||||
{
|
||||
$model = $this->parent->model();
|
||||
$form = RemarkForm::make()->payload(['remarks' => $model->remarks ?: '', 'id' => $model->id]);
|
||||
return Modal::make()->lg()->title($this->title)->body($form)->button('<button type="button" class="'.$this->style.'">'.$this->title.'</button>');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
namespace Peidikeji\User\Form;
|
||||
|
||||
use Dcat\Admin\Admin;
|
||||
use Dcat\Admin\Contracts\LazyRenderable;
|
||||
use Dcat\Admin\Traits\LazyWidget;
|
||||
use Dcat\Admin\Widgets\Form;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Peidikeji\User\Models\User;
|
||||
|
||||
class BalanceForm extends Form implements LazyRenderable
|
||||
{
|
||||
use LazyWidget;
|
||||
|
||||
protected $buttons = ['reset' => false, 'submit' => true];
|
||||
|
||||
public function handle(array $input)
|
||||
{
|
||||
$amount = $input['amount'];
|
||||
if ($amount == 0) {
|
||||
$this->response()->error('数量不能为0');
|
||||
}
|
||||
$user = User::findOrFail($this->payload['id']);
|
||||
if ($user->balance + $amount < 0) {
|
||||
return $this->response()->error('余额不能小于0');
|
||||
}
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$user->increment('balance', $amount);
|
||||
|
||||
$admin = Admin::user();
|
||||
$user->balanceLogs()->create([
|
||||
'amount' => $amount,
|
||||
'balance' => $user->balance,
|
||||
'cate' => $input['cate'],
|
||||
'description' => $input['description'],
|
||||
'source_id' => $admin->id,
|
||||
'source_type' => $admin->getMorphClass()
|
||||
]);
|
||||
DB::commit();
|
||||
return $this->response()->success('操作成功')->refresh();
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
return $this->response()->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function form()
|
||||
{
|
||||
$this->display('balance');
|
||||
$this->number('amount', '数量')->help('正数为增加, 负数为减少')->default(0)->required();
|
||||
$this->text('cate', '分类')->required();
|
||||
$this->text('description', '描述')->required();
|
||||
}
|
||||
|
||||
public function default()
|
||||
{
|
||||
return [
|
||||
'balance' => $this->payload['balance']
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace Peidikeji\User\Form;
|
||||
|
||||
use Dcat\Admin\Contracts\LazyRenderable;
|
||||
use Dcat\Admin\Traits\LazyWidget;
|
||||
use Dcat\Admin\Widgets\Form;
|
||||
use Peidikeji\User\Models\UserBalance;
|
||||
|
||||
class RemarkForm extends Form implements LazyRenderable
|
||||
{
|
||||
use LazyWidget;
|
||||
|
||||
protected $buttons = ['reset' => false, 'submit' => true];
|
||||
|
||||
public function handle(array $input)
|
||||
{
|
||||
$info = UserBalance::findOrFail($this->payload['id']);
|
||||
$info->update(['remarks' => $input['remarks']]);
|
||||
|
||||
return $this->response()->success('操作成功')->refresh();
|
||||
}
|
||||
|
||||
public function form()
|
||||
{
|
||||
$this->text('remarks');
|
||||
}
|
||||
|
||||
public function default()
|
||||
{
|
||||
return [
|
||||
'remarks' => $this->payload['remarks']
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
<?php
|
||||
|
||||
namespace Peidikeji\User\Http\Admin;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Dcat\Admin\Grid;
|
||||
use Dcat\Admin\Grid\Filter;
|
||||
use Dcat\Admin\Http\Controllers\AdminController;
|
||||
use Dcat\Admin\Show;
|
||||
use Dcat\Admin\Show\Tools;
|
||||
use Peidikeji\User\Action\ShowRemark;
|
||||
use Peidikeji\User\Models\User;
|
||||
use Peidikeji\User\Models\UserBalance;
|
||||
|
||||
class UserBalanceController extends AdminController
|
||||
{
|
||||
protected $translation = 'dcat-admin-user::user-balance';
|
||||
|
||||
protected function grid()
|
||||
{
|
||||
return Grid::make(UserBalance::with(['user']), function (Grid $grid) {
|
||||
$grid->model()->sort();
|
||||
|
||||
$grid->column('user.phone');
|
||||
$grid->column('cate');
|
||||
$grid->column('description');
|
||||
$grid->column('amount');
|
||||
$grid->column('created_at');
|
||||
|
||||
$grid->disableCreateButton();
|
||||
$grid->disableEditButton();
|
||||
$grid->disableDeleteButton();
|
||||
|
||||
$grid->filter(function (Filter $filter) {
|
||||
$filter->panel();
|
||||
|
||||
$filter->equal('user_id')->select()->ajax('api/users?_paginate=1')->model(User::class, 'id', 'phone')->width(3);
|
||||
$filter->like('cate')->width(3);
|
||||
$filter->whereBetween('created_at', function ($q) {
|
||||
$start = data_get($this->input, 'start');
|
||||
$start = $start ? Carbon::createFromFormat('Y-m-d', $start) : null;
|
||||
$end = data_get($this->input, 'end');
|
||||
$end = $end ? Carbon::createFromFormat('Y-m-d', $end) : null;
|
||||
if ($start) {
|
||||
if ($end) {
|
||||
$q->whereBetween('created_at', [$start, $end]);
|
||||
}
|
||||
$q->where('created_at', '>=', $start);
|
||||
} else if ($end) {
|
||||
$q->where('created_at', '<=', $end);
|
||||
}
|
||||
})->date()->width(3);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
protected function detail($id)
|
||||
{
|
||||
return Show::make($id, UserBalance::with(['user']), function (Show $show) {
|
||||
$show->field('user.phone');
|
||||
$show->field('cate');
|
||||
$show->field('description');
|
||||
$show->field('amount');
|
||||
$show->field('balance');
|
||||
$show->field('remarks');
|
||||
$show->field('created_at');
|
||||
|
||||
$show->tools(function (Tools $tools) {
|
||||
$tools->disableDelete();
|
||||
$tools->disableEdit();
|
||||
$tools->disableList();
|
||||
$tools->append(new ShowRemark());
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -19,6 +19,7 @@ use Illuminate\Validation\Rule;
|
|||
use Peidikeji\User\Models\User;
|
||||
use Peidikeji\User\Models\UserSocialite;
|
||||
use Illuminate\Support\Str;
|
||||
use Peidikeji\User\Action\ShowBalance;
|
||||
|
||||
class UserController extends AdminController
|
||||
{
|
||||
|
|
@ -146,8 +147,9 @@ class UserController extends AdminController
|
|||
$show->field('profit');
|
||||
$show->field('created_at');
|
||||
$show->tools(function (Tools $tools) {
|
||||
$tools->disableBack();
|
||||
$tools->disableList(false);
|
||||
$tools->disableList();
|
||||
|
||||
$tools->append(new ShowBalance());
|
||||
});
|
||||
|
||||
$tab = new Tab();
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ use EloquentFilter\Filterable;
|
|||
use Laravel\Sanctum\HasApiTokens;
|
||||
use Peidikeji\User\Filters\UserFilter;
|
||||
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
||||
use Dcat\Admin\Traits\ModelTree;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
|
|
@ -54,6 +53,11 @@ class User extends Authenticatable
|
|||
return $this->hasMany(UserSocialite::class, 'user_id');
|
||||
}
|
||||
|
||||
public function balanceLogs()
|
||||
{
|
||||
return $this->hasMany(UserBalance::class, 'user_id');
|
||||
}
|
||||
|
||||
public function scopeSort($q)
|
||||
{
|
||||
return $q->latest('id');
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace Peidikeji\User\Models;
|
||||
|
||||
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class UserBalance extends Model
|
||||
{
|
||||
use HasDateTimeFormatter;
|
||||
|
||||
protected $table = 'user_balance_logs';
|
||||
|
||||
protected $fillable = ['amount', 'balance', 'cate', 'description', 'remarks', 'source_id', 'source_type', 'user_id'];
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'user_id');
|
||||
}
|
||||
|
||||
public function source()
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
|
||||
public function scopeSort($q)
|
||||
{
|
||||
return $q->latest('created_at');
|
||||
}
|
||||
}
|
||||
|
|
@ -31,6 +31,7 @@ class UserServiceProvider extends ServiceProvider
|
|||
$menu->add([
|
||||
['id' => 1, 'parent_id' => 0, 'title' => '用户模块', 'icon' => 'feather icon-user', 'uri' => ''],
|
||||
['id' => 2, 'parent_id' => 1, 'title' => '用户管理', 'icon' => '', 'uri' => '/users'],
|
||||
['id' => 3, 'parent_id' => 1, 'title' => '余额流水', 'icon' => '', 'uri' => '/user-balance'],
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue