FormHelper.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace App\Module\Transfer\AdminControllers\Helper;
  3. use App\Module\Transfer\Models\TransferApp;
  4. use Dcat\Admin\Form;
  5. /**
  6. * 表单辅助类
  7. */
  8. class FormHelper
  9. {
  10. /**
  11. * 配置应用表单
  12. *
  13. * @param Form $form
  14. * @return void
  15. */
  16. public static function appForm(Form $form): void
  17. {
  18. // 基本信息标签页
  19. $form->tab('基本信息', function (Form $form) {
  20. $form->text('keyname', '应用标识')
  21. ->required()
  22. ->help('唯一标识符,用于API调用识别')
  23. ->rules('required|string|max:50|unique:kku_transfer_apps,keyname');
  24. $form->text('title', '应用名称')
  25. ->required()
  26. ->help('应用的显示名称')
  27. ->rules('required|string|max:100');
  28. $form->textarea('description', '应用描述')
  29. ->help('应用的详细描述信息')
  30. ->rules('nullable|string');
  31. $form->switch('is_enabled', '启用状态')
  32. ->default(1)
  33. ->help('是否启用该应用');
  34. });
  35. // 外部应用信息标签页
  36. $form->tab('外部应用信息', function (Form $form) {
  37. $form->number('out_id', '外部应用ID')
  38. ->required()
  39. ->help('外部系统中的应用ID')
  40. ->rules('required|integer|min:1');
  41. $form->number('out_id2', '外部应用ID2')
  42. ->help('开放接口应用ID(可选)')
  43. ->rules('nullable|integer|min:1');
  44. $form->number('out_id3', '外部应用ID3')
  45. ->help('三方平台应用ID(可选)')
  46. ->rules('nullable|integer|min:1');
  47. });
  48. // 资金配置标签页
  49. $form->tab('资金配置', function (Form $form) {
  50. $form->number('currency_id', '货币类型ID')
  51. ->required()
  52. ->help('对应的货币类型')
  53. ->rules('required|integer|min:1');
  54. $form->number('fund_id', '资金账户类型ID')
  55. ->required()
  56. ->help('对应的资金账户类型')
  57. ->rules('required|integer|min:1');
  58. $form->number('fund_to_uid', '转入目标账户UID')
  59. ->help('转入时的目标账户UID(可选)')
  60. ->rules('nullable|integer|min:1');
  61. $form->number('fund_in_uid', '转入来源账户UID')
  62. ->help('转入时的来源账户UID(可选)')
  63. ->rules('nullable|integer|min:1');
  64. $form->decimal('exchange_rate', '汇率')
  65. ->default(1.0000)
  66. ->help('外部应用金额与业务金额的汇率')
  67. ->rules('required|numeric|min:0.0001|max:9999.9999');
  68. });
  69. // API配置标签页
  70. $form->tab('API配置', function (Form $form) {
  71. $form->url('order_callback_url', '结果通知API地址')
  72. ->help('订单处理完成后的回调通知地址(为空则不通知)')
  73. ->rules('nullable|url|max:255');
  74. $form->url('order_in_info_url', '转入查询API地址')
  75. ->help('查询转入订单状态的API地址(为空则不查询)')
  76. ->rules('nullable|url|max:255');
  77. $form->url('order_out_create_url', '转出创建API地址')
  78. ->help('创建转出订单的API地址(为空则不创建)')
  79. ->rules('nullable|url|max:255');
  80. $form->url('order_out_info_url', '转出查询API地址')
  81. ->help('查询转出订单状态的API地址(为空则不查询)')
  82. ->rules('nullable|url|max:255');
  83. });
  84. }
  85. /**
  86. * 配置订单表单(仅用于查看,不允许编辑)
  87. *
  88. * @param Form $form
  89. * @return void
  90. */
  91. public static function orderForm(Form $form): void
  92. {
  93. // 禁用所有编辑功能
  94. $form->disableCreatingCheck();
  95. $form->disableEditingCheck();
  96. $form->disableViewCheck();
  97. // 基本信息
  98. $form->display('id', '订单ID');
  99. $form->display('out_order_id', '外部订单ID');
  100. // 应用信息
  101. $form->select('transfer_app_id', '划转应用')
  102. ->options(TransferApp::pluck('title', 'id'))
  103. ->disable();
  104. // 用户信息
  105. $form->display('user_id', '用户ID');
  106. $form->display('out_user_id', '外部用户ID');
  107. // 订单信息
  108. $form->select('type', '订单类型')
  109. ->options([1 => '转入', 2 => '转出'])
  110. ->disable();
  111. $form->select('status', '订单状态')
  112. ->options([
  113. 1 => '已创建',
  114. 20 => '处理中',
  115. 30 => '已回调',
  116. 100 => '已完成',
  117. -1 => '失败'
  118. ])
  119. ->disable();
  120. // 金额信息
  121. $form->display('amount', '内部金额');
  122. $form->display('out_amount', '外部金额');
  123. $form->display('exchange_rate', '汇率');
  124. // 附加信息
  125. $form->textarea('remark', '备注')->disable();
  126. $form->textarea('error_message', '错误信息')->disable();
  127. $form->json('callback_data', '回调数据')->disable();
  128. // 时间信息
  129. $form->display('created_at', '创建时间');
  130. $form->display('processed_at', '处理时间');
  131. $form->display('callback_at', '回调时间');
  132. $form->display('completed_at', '完成时间');
  133. }
  134. /**
  135. * 配置表单验证
  136. *
  137. * @param Form $form
  138. * @return void
  139. */
  140. public static function configureValidation(Form $form): void
  141. {
  142. // 自定义验证规则
  143. $form->saving(function (Form $form) {
  144. // 验证应用标识唯一性
  145. if ($form->keyname) {
  146. $exists = TransferApp::where('keyname', $form->keyname)
  147. ->when($form->model()->id, function ($query) use ($form) {
  148. $query->where('id', '!=', $form->model()->id);
  149. })
  150. ->exists();
  151. if ($exists) {
  152. return $form->response()->error('应用标识已存在');
  153. }
  154. }
  155. // 验证汇率范围
  156. if ($form->exchange_rate && ($form->exchange_rate <= 0 || $form->exchange_rate > 9999.9999)) {
  157. return $form->response()->error('汇率必须在 0.0001 到 9999.9999 之间');
  158. }
  159. });
  160. }
  161. /**
  162. * 配置表单工具栏
  163. *
  164. * @param Form $form
  165. * @return void
  166. */
  167. public static function configureTools(Form $form): void
  168. {
  169. $form->tools(function (Form\Tools $tools) {
  170. // 添加自定义按钮
  171. $tools->append('<a class="btn btn-outline-info" href="javascript:void(0)" onclick="testConnection()">
  172. <i class="fa fa-plug"></i> 测试连接
  173. </a>');
  174. });
  175. }
  176. /**
  177. * 配置表单底部按钮
  178. *
  179. * @param Form $form
  180. * @return void
  181. */
  182. public static function configureFooter(Form $form): void
  183. {
  184. $form->footer(function ($footer) {
  185. // 隐藏查看按钮
  186. $footer->disableViewCheck();
  187. // 隐藏继续编辑按钮
  188. $footer->disableEditingCheck();
  189. // 隐藏继续创建按钮
  190. $footer->disableCreatingCheck();
  191. });
  192. }
  193. /**
  194. * 添加自定义字段
  195. *
  196. * @param Form $form
  197. * @param string $type
  198. * @param string $name
  199. * @param string $label
  200. * @param array $options
  201. * @return void
  202. */
  203. public static function addCustomField(Form $form, string $type, string $name, string $label, array $options = []): void
  204. {
  205. $field = $form->{$type}($name, $label);
  206. foreach ($options as $method => $value) {
  207. if (method_exists($field, $method)) {
  208. $field->{$method}($value);
  209. }
  210. }
  211. }
  212. }