| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <?php
- namespace App\Module\Transfer\AdminControllers\Helper;
- use App\Module\Transfer\Models\TransferApp;
- use Dcat\Admin\Form;
- /**
- * 表单辅助类
- */
- class FormHelper
- {
- /**
- * 配置应用表单
- *
- * @param Form $form
- * @return void
- */
- public static function appForm(Form $form): void
- {
- // 基本信息标签页
- $form->tab('基本信息', function (Form $form) {
- $form->text('keyname', '应用标识')
- ->required()
- ->help('唯一标识符,用于API调用识别')
- ->rules('required|string|max:50|unique:kku_transfer_apps,keyname');
- $form->text('title', '应用名称')
- ->required()
- ->help('应用的显示名称')
- ->rules('required|string|max:100');
- $form->textarea('description', '应用描述')
- ->help('应用的详细描述信息')
- ->rules('nullable|string');
- $form->switch('is_enabled', '启用状态')
- ->default(1)
- ->help('是否启用该应用');
- });
- // 外部应用信息标签页
- $form->tab('外部应用信息', function (Form $form) {
- $form->number('out_id', '外部应用ID')
- ->required()
- ->help('外部系统中的应用ID')
- ->rules('required|integer|min:1');
- $form->number('out_id2', '外部应用ID2')
- ->help('开放接口应用ID(可选)')
- ->rules('nullable|integer|min:1');
- $form->number('out_id3', '外部应用ID3')
- ->help('三方平台应用ID(可选)')
- ->rules('nullable|integer|min:1');
- });
- // 资金配置标签页
- $form->tab('资金配置', function (Form $form) {
- $form->number('currency_id', '货币类型ID')
- ->required()
- ->help('对应的货币类型')
- ->rules('required|integer|min:1');
- $form->number('fund_id', '资金账户类型ID')
- ->required()
- ->help('对应的资金账户类型')
- ->rules('required|integer|min:1');
- $form->number('fund_to_uid', '转入目标账户UID')
- ->help('转入时的目标账户UID(可选)')
- ->rules('nullable|integer|min:1');
- $form->number('fund_in_uid', '转入来源账户UID')
- ->help('转入时的来源账户UID(可选)')
- ->rules('nullable|integer|min:1');
- $form->decimal('exchange_rate', '汇率')
- ->default(1.0000)
- ->help('钱包金额与业务金额的汇率')
- ->rules('required|numeric|min:0.0001|max:9999.9999');
- });
- // API配置标签页
- $form->tab('API配置', function (Form $form) {
- $form->url('order_callback_url', '结果通知API地址')
- ->help('订单处理完成后的回调通知地址(为空则不通知)')
- ->rules('nullable|url|max:255');
- $form->url('order_in_info_url', '转入查询API地址')
- ->help('查询转入订单状态的API地址(为空则不查询)')
- ->rules('nullable|url|max:255');
- $form->url('order_out_create_url', '转出创建API地址')
- ->help('创建转出订单的API地址(为空则不创建)')
- ->rules('nullable|url|max:255');
- $form->url('order_out_info_url', '转出查询API地址')
- ->help('查询转出订单状态的API地址(为空则不查询)')
- ->rules('nullable|url|max:255');
- });
- }
- /**
- * 配置订单表单(仅用于查看,不允许编辑)
- *
- * @param Form $form
- * @return void
- */
- public static function orderForm(Form $form): void
- {
- // 禁用所有编辑功能
- $form->disableCreatingCheck();
- $form->disableEditingCheck();
- $form->disableViewCheck();
- // 基本信息
- $form->display('id', '订单ID');
- $form->display('out_order_id', '外部订单ID');
-
- // 应用信息
- $form->select('transfer_app_id', '划转应用')
- ->options(TransferApp::pluck('title', 'id'))
- ->disable();
- // 用户信息
- $form->display('user_id', '用户ID');
- $form->display('out_user_id', '外部用户ID');
- // 订单信息
- $form->select('type', '订单类型')
- ->options([1 => '转入', 2 => '转出'])
- ->disable();
- $form->select('status', '订单状态')
- ->options([
- 1 => '已创建',
- 20 => '处理中',
- 30 => '已回调',
- 100 => '已完成',
- -1 => '失败'
- ])
- ->disable();
- // 金额信息
- $form->display('amount', '内部金额');
- $form->display('out_amount', '外部金额');
- $form->display('exchange_rate', '汇率');
- // 附加信息
- $form->textarea('remark', '备注')->disable();
- $form->textarea('error_message', '错误信息')->disable();
- $form->json('callback_data', '回调数据')->disable();
- // 时间信息
- $form->display('created_at', '创建时间');
- $form->display('processed_at', '处理时间');
- $form->display('callback_at', '回调时间');
- $form->display('completed_at', '完成时间');
- }
- /**
- * 配置表单验证
- *
- * @param Form $form
- * @return void
- */
- public static function configureValidation(Form $form): void
- {
- // 自定义验证规则
- $form->saving(function (Form $form) {
- // 验证应用标识唯一性
- if ($form->keyname) {
- $exists = TransferApp::where('keyname', $form->keyname)
- ->when($form->model()->id, function ($query) use ($form) {
- $query->where('id', '!=', $form->model()->id);
- })
- ->exists();
- if ($exists) {
- return $form->response()->error('应用标识已存在');
- }
- }
- // 验证汇率范围
- if ($form->exchange_rate && ($form->exchange_rate <= 0 || $form->exchange_rate > 9999.9999)) {
- return $form->response()->error('汇率必须在 0.0001 到 9999.9999 之间');
- }
- });
- }
- /**
- * 配置表单工具栏
- *
- * @param Form $form
- * @return void
- */
- public static function configureTools(Form $form): void
- {
- $form->tools(function (Form\Tools $tools) {
- // 添加自定义按钮
- $tools->append('<a class="btn btn-outline-info" href="javascript:void(0)" onclick="testConnection()">
- <i class="fa fa-plug"></i> 测试连接
- </a>');
- });
- }
- /**
- * 配置表单底部按钮
- *
- * @param Form $form
- * @return void
- */
- public static function configureFooter(Form $form): void
- {
- $form->footer(function ($footer) {
- // 隐藏查看按钮
- $footer->disableViewCheck();
-
- // 隐藏继续编辑按钮
- $footer->disableEditingCheck();
-
- // 隐藏继续创建按钮
- $footer->disableCreatingCheck();
- });
- }
- /**
- * 添加自定义字段
- *
- * @param Form $form
- * @param string $type
- * @param string $name
- * @param string $label
- * @param array $options
- * @return void
- */
- public static function addCustomField(Form $form, string $type, string $name, string $label, array $options = []): void
- {
- $field = $form->{$type}($name, $label);
-
- foreach ($options as $method => $value) {
- if (method_exists($field, $method)) {
- $field->{$method}($value);
- }
- }
- }
- }
|