| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- <?php
- namespace App\Module\Transfer\AdminControllers\Helper;
- use App\Module\Transfer\Models\TransferApp;
- use App\Module\Transfer\Enums\TransferStatus;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Dcat\Admin\Form;
- /**
- * 划转应用管理辅助类
- */
- class TransferAppHelper
- {
- /**
- * 配置Grid表格
- */
- public static function grid(Grid $grid): void
- {
- $grid->column('id', 'ID')->sortable();
- $grid->column('keyname', '应用标识')->copyable();
- $grid->column('title', '应用名称')->limit(20);
- $grid->column('description', '描述')->limit(30);
-
- $grid->column('out_id2', '开放接口ID');
- $grid->column('out_id3', '三方平台ID');
-
- $grid->column('currency_id', '货币类型')->using([
- 1 => '金币',
- 2 => '钻石',
- 3 => '人民币',
- 4 => '美元',
- ])->label([
- 1 => 'warning',
- 2 => 'success',
- 3 => 'danger',
- 4 => 'primary',
- ]);
-
- $grid->column('fund_id', '资金账户');
- $grid->column('exchange_rate', '汇率')->display(function ($value) {
- return number_format($value, 4);
- });
-
- $grid->column('is_enabled', '状态')->switch();
-
- // 运行模式
- $grid->column('mode', '运行模式')->display(function () {
- return $this->isInternalMode() ? '农场内部' : '外部API';
- })->label([
- '农场内部' => 'success',
- '外部API' => 'primary',
- ]);
-
- // 支持功能
- $grid->column('features', '支持功能')->display(function () {
- $features = [];
- if ($this->supportsTransferIn()) $features[] = '转入';
- if ($this->supportsTransferOut()) $features[] = '转出';
- if ($this->supportsCallback()) $features[] = '回调';
- return implode(', ', $features);
- });
-
- $grid->column('created_at', '创建时间')->sortable();
- $grid->column('updated_at', '更新时间')->sortable();
- // 筛选器
- $grid->filter(function (Grid\Filter $filter) {
- $filter->like('keyname', '应用标识');
- $filter->like('title', '应用名称');
- $filter->equal('currency_id', '货币类型')->select([
- 1 => '金币',
- 2 => '钻石',
- 3 => '人民币',
- 4 => '美元',
- ]);
- $filter->equal('is_enabled', '状态')->select([
- 1 => '启用',
- 0 => '禁用',
- ]);
- });
- // 操作按钮
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- $actions->disableDelete(); // 禁用删除
-
- // 添加测试连接按钮
- if (!$this->isInternalMode()) {
- $actions->append('<a href="javascript:void(0)" class="btn btn-xs btn-outline-info test-connection" data-id="'.$this->id.'">测试连接</a>');
- }
- });
- // 批量操作
- $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
- $batch->add(new \App\Module\Transfer\AdminControllers\Tools\EnableAppTool());
- $batch->add(new \App\Module\Transfer\AdminControllers\Tools\DisableAppTool());
- });
- // 工具栏
- $grid->tools(function (Grid\Tools $tools) {
- $tools->append('<a href="javascript:void(0)" class="btn btn-sm btn-success" onclick="refreshStats()">刷新统计</a>');
- });
- }
- /**
- * 配置Show详情
- */
- public static function show(Show $show): void
- {
- $show->field('id', 'ID');
- $show->field('keyname', '应用标识');
- $show->field('title', '应用名称');
- $show->field('description', '描述');
-
- $show->divider('外部应用配置');
- $show->field('out_id2', '开放接口ID');
- $show->field('out_id3', '三方平台ID');
-
- $show->divider('资金配置');
- $show->field('currency_id', '货币类型')->using([
- 1 => '金币',
- 2 => '钻石',
- 3 => '人民币',
- 4 => '美元',
- ]);
- $show->field('fund_id', '资金账户类型');
- $show->field('fund_to_uid', '转入目标账户');
- $show->field('fund_in_uid', '转入来源账户');
- $show->field('exchange_rate', '汇率')->as(function ($value) {
- return number_format($value, 4);
- });
-
- $show->divider('API配置');
- $show->field('order_callback_url', '回调通知URL');
- $show->field('order_in_info_url', '转入查询URL');
- $show->field('order_out_create_url', '转出创建URL');
- $show->field('order_out_info_url', '转出查询URL');
-
- $show->divider('状态信息');
- $show->field('is_enabled', '启用状态')->using([1 => '启用', 0 => '禁用']);
- $show->field('mode', '运行模式')->as(function () {
- return $this->isInternalMode() ? '农场内部模式' : '外部API模式';
- });
-
- $show->divider('时间信息');
- $show->field('created_at', '创建时间');
- $show->field('updated_at', '更新时间');
-
- // 关联订单统计
- $show->divider('订单统计');
- $show->field('orders_count', '总订单数')->as(function () {
- return $this->orders()->count();
- });
- $show->field('completed_orders', '成功订单')->as(function () {
- return $this->orders()->where('status', TransferStatus::COMPLETED)->count();
- });
- $show->field('total_amount', '总金额')->as(function () {
- return number_format($this->orders()->sum('amount'), 2);
- });
- }
- /**
- * 配置Form表单
- */
- public static function form(Form $form): void
- {
- $form->tab('基本信息', function (Form $form) {
- $form->text('keyname', '应用标识')
- ->required()
- ->creationRules('required|string|max:50|unique:' . (new TransferApp)->getTable() . ',keyname')
- ->updateRules('required|string|max:50|unique:' . (new TransferApp)->getTable() . ',keyname,{{id}}')
- ->help('唯一标识符,只能包含字母、数字、下划线');
-
- $form->text('title', '应用名称')
- ->required()
- ->rules('required|string|max:100');
-
- $form->textarea('description', '应用描述')
- ->rows(3)
- ->help('应用的详细描述信息');
- });
- $form->tab('外部应用配置', function (Form $form) {
- $form->number('out_id2', '开放接口ID')
- ->min(1)
- ->help('开放接口应用ID,用于API对接');
- $form->number('out_id3', '三方平台ID')
- ->min(1)
- ->help('第三方平台应用ID,用于平台集成');
- });
- $form->tab('资金配置', function (Form $form) {
- $form->select('currency_id', '货币类型')
- ->options([
- 1 => '金币',
- 2 => '钻石',
- 3 => '人民币',
- 4 => '美元',
- ])
- ->required()
- ->help('选择使用的货币类型');
-
- $form->number('fund_id', '资金账户类型')
- ->required()
- ->min(1)
- ->help('关联的资金账户类型ID');
-
- $form->number('fund_to_uid', '转入目标账户')
- ->min(1)
- ->help('转入操作的目标账户UID');
-
- $form->number('fund_in_uid', '转入来源账户')
- ->min(1)
- ->help('转入操作的来源账户UID');
-
- $form->decimal('exchange_rate', '汇率')
- ->required()
- ->default(1.0000)
- ->help('汇率比例,外部应用金额:业务金额');
- });
- $form->tab('API配置', function (Form $form) {
- $form->url('order_callback_url', '回调通知URL')
- ->help('结果通知API地址,为空则不发送通知');
-
- $form->url('order_in_info_url', '转入查询URL')
- ->help('转入订单查询API地址,为空则不查询');
-
- $form->url('order_out_create_url', '转出创建URL')
- ->help('转出订单创建API地址,为空则不创建');
-
- $form->url('order_out_info_url', '转出查询URL')
- ->help('转出订单查询API地址,为空则不查询');
-
- $form->display('api_note', '说明')
- ->with(function () {
- return '如果所有API地址都为空,系统将运行在农场内部模式';
- });
- });
- $form->tab('状态设置', function (Form $form) {
- $form->switch('is_enabled', '启用状态')
- ->default(1)
- ->help('是否启用该应用');
- });
- // 保存前验证
- $form->saving(function (Form $form) {
- // 验证应用标识符格式
- if (!preg_match('/^[a-zA-Z0-9_]+$/', $form->keyname)) {
- return $form->response()->error('应用标识符格式无效');
- }
-
- // 验证汇率
- if ($form->exchange_rate <= 0) {
- return $form->response()->error('汇率必须大于0');
- }
- });
- }
- }
|