TransferAppHelper.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. namespace App\Module\Transfer\AdminControllers\Helper;
  3. use App\Module\Transfer\Models\TransferApp;
  4. use App\Module\Transfer\Enums\TransferStatus;
  5. use Dcat\Admin\Grid;
  6. use Dcat\Admin\Show;
  7. use Dcat\Admin\Form;
  8. /**
  9. * 划转应用管理辅助类
  10. */
  11. class TransferAppHelper
  12. {
  13. /**
  14. * 配置Grid表格
  15. */
  16. public static function grid(Grid $grid): void
  17. {
  18. $grid->column('id', 'ID')->sortable();
  19. $grid->column('keyname', '应用标识')->copyable();
  20. $grid->column('title', '应用名称')->limit(20);
  21. $grid->column('description', '描述')->limit(30);
  22. $grid->column('out_id2', '开放接口ID');
  23. $grid->column('out_id3', '三方平台ID');
  24. $grid->column('currency_id', '货币类型')->using([
  25. 1 => '金币',
  26. 2 => '钻石',
  27. 3 => '人民币',
  28. 4 => '美元',
  29. ])->label([
  30. 1 => 'warning',
  31. 2 => 'success',
  32. 3 => 'danger',
  33. 4 => 'primary',
  34. ]);
  35. $grid->column('fund_id', '资金账户');
  36. $grid->column('exchange_rate', '汇率')->display(function ($value) {
  37. return number_format($value, 4);
  38. });
  39. $grid->column('is_enabled', '状态')->switch();
  40. // 运行模式
  41. $grid->column('mode', '运行模式')->display(function () {
  42. return $this->isInternalMode() ? '农场内部' : '外部API';
  43. })->label([
  44. '农场内部' => 'success',
  45. '外部API' => 'primary',
  46. ]);
  47. // 支持功能
  48. $grid->column('features', '支持功能')->display(function () {
  49. $features = [];
  50. if ($this->supportsTransferIn()) $features[] = '转入';
  51. if ($this->supportsTransferOut()) $features[] = '转出';
  52. if ($this->supportsCallback()) $features[] = '回调';
  53. return implode(', ', $features);
  54. });
  55. $grid->column('created_at', '创建时间')->sortable();
  56. $grid->column('updated_at', '更新时间')->sortable();
  57. // 筛选器
  58. $grid->filter(function (Grid\Filter $filter) {
  59. $filter->like('keyname', '应用标识');
  60. $filter->like('title', '应用名称');
  61. $filter->equal('currency_id', '货币类型')->select([
  62. 1 => '金币',
  63. 2 => '钻石',
  64. 3 => '人民币',
  65. 4 => '美元',
  66. ]);
  67. $filter->equal('is_enabled', '状态')->select([
  68. 1 => '启用',
  69. 0 => '禁用',
  70. ]);
  71. });
  72. // 操作按钮
  73. $grid->actions(function (Grid\Displayers\Actions $actions) {
  74. $actions->disableDelete(); // 禁用删除
  75. // 添加测试连接按钮
  76. if (!$this->isInternalMode()) {
  77. $actions->append('<a href="javascript:void(0)" class="btn btn-xs btn-outline-info test-connection" data-id="'.$this->id.'">测试连接</a>');
  78. }
  79. });
  80. // 批量操作
  81. $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
  82. $batch->add(new \App\Module\Transfer\AdminControllers\Tools\EnableAppTool());
  83. $batch->add(new \App\Module\Transfer\AdminControllers\Tools\DisableAppTool());
  84. });
  85. // 工具栏
  86. $grid->tools(function (Grid\Tools $tools) {
  87. $tools->append('<a href="javascript:void(0)" class="btn btn-sm btn-success" onclick="refreshStats()">刷新统计</a>');
  88. });
  89. }
  90. /**
  91. * 配置Show详情
  92. */
  93. public static function show(Show $show): void
  94. {
  95. $show->field('id', 'ID');
  96. $show->field('keyname', '应用标识');
  97. $show->field('title', '应用名称');
  98. $show->field('description', '描述');
  99. $show->divider('外部应用配置');
  100. $show->field('out_id2', '开放接口ID');
  101. $show->field('out_id3', '三方平台ID');
  102. $show->divider('资金配置');
  103. $show->field('currency_id', '货币类型')->using([
  104. 1 => '金币',
  105. 2 => '钻石',
  106. 3 => '人民币',
  107. 4 => '美元',
  108. ]);
  109. $show->field('fund_id', '资金账户类型');
  110. $show->field('fund_to_uid', '转入目标账户');
  111. $show->field('fund_in_uid', '转入来源账户');
  112. $show->field('exchange_rate', '汇率')->as(function ($value) {
  113. return number_format($value, 4);
  114. });
  115. $show->divider('API配置');
  116. $show->field('order_callback_url', '回调通知URL');
  117. $show->field('order_in_info_url', '转入查询URL');
  118. $show->field('order_out_create_url', '转出创建URL');
  119. $show->field('order_out_info_url', '转出查询URL');
  120. $show->divider('状态信息');
  121. $show->field('is_enabled', '启用状态')->using([1 => '启用', 0 => '禁用']);
  122. $show->field('mode', '运行模式')->as(function () {
  123. return $this->isInternalMode() ? '农场内部模式' : '外部API模式';
  124. });
  125. $show->divider('时间信息');
  126. $show->field('created_at', '创建时间');
  127. $show->field('updated_at', '更新时间');
  128. // 关联订单统计
  129. $show->divider('订单统计');
  130. $show->field('orders_count', '总订单数')->as(function () {
  131. return $this->orders()->count();
  132. });
  133. $show->field('completed_orders', '成功订单')->as(function () {
  134. return $this->orders()->where('status', TransferStatus::COMPLETED)->count();
  135. });
  136. $show->field('total_amount', '总金额')->as(function () {
  137. return number_format($this->orders()->sum('amount'), 2);
  138. });
  139. }
  140. /**
  141. * 配置Form表单
  142. */
  143. public static function form(Form $form): void
  144. {
  145. $form->tab('基本信息', function (Form $form) {
  146. $form->text('keyname', '应用标识')
  147. ->required()
  148. ->creationRules('required|string|max:50|unique:' . (new TransferApp)->getTable() . ',keyname')
  149. ->updateRules('required|string|max:50|unique:' . (new TransferApp)->getTable() . ',keyname,{{id}}')
  150. ->help('唯一标识符,只能包含字母、数字、下划线');
  151. $form->text('title', '应用名称')
  152. ->required()
  153. ->rules('required|string|max:100');
  154. $form->textarea('description', '应用描述')
  155. ->rows(3)
  156. ->help('应用的详细描述信息');
  157. });
  158. $form->tab('外部应用配置', function (Form $form) {
  159. $form->number('out_id2', '开放接口ID')
  160. ->min(1)
  161. ->help('开放接口应用ID,用于API对接');
  162. $form->number('out_id3', '三方平台ID')
  163. ->min(1)
  164. ->help('第三方平台应用ID,用于平台集成');
  165. });
  166. $form->tab('资金配置', function (Form $form) {
  167. $form->select('currency_id', '货币类型')
  168. ->options([
  169. 1 => '金币',
  170. 2 => '钻石',
  171. 3 => '人民币',
  172. 4 => '美元',
  173. ])
  174. ->required()
  175. ->help('选择使用的货币类型');
  176. $form->number('fund_id', '资金账户类型')
  177. ->required()
  178. ->min(1)
  179. ->help('关联的资金账户类型ID');
  180. $form->number('fund_to_uid', '转入目标账户')
  181. ->min(1)
  182. ->help('转入操作的目标账户UID');
  183. $form->number('fund_in_uid', '转入来源账户')
  184. ->min(1)
  185. ->help('转入操作的来源账户UID');
  186. $form->decimal('exchange_rate', '汇率')
  187. ->required()
  188. ->default(1.0000)
  189. ->help('汇率比例,钱包金额:业务金额');
  190. });
  191. $form->tab('API配置', function (Form $form) {
  192. $form->url('order_callback_url', '回调通知URL')
  193. ->help('结果通知API地址,为空则不发送通知');
  194. $form->url('order_in_info_url', '转入查询URL')
  195. ->help('转入订单查询API地址,为空则不查询');
  196. $form->url('order_out_create_url', '转出创建URL')
  197. ->help('转出订单创建API地址,为空则不创建');
  198. $form->url('order_out_info_url', '转出查询URL')
  199. ->help('转出订单查询API地址,为空则不查询');
  200. $form->display('api_note', '说明')
  201. ->with(function () {
  202. return '如果所有API地址都为空,系统将运行在农场内部模式';
  203. });
  204. });
  205. $form->tab('状态设置', function (Form $form) {
  206. $form->switch('is_enabled', '启用状态')
  207. ->default(1)
  208. ->help('是否启用该应用');
  209. });
  210. // 保存前验证
  211. $form->saving(function (Form $form) {
  212. // 验证应用标识符格式
  213. if (!preg_match('/^[a-zA-Z0-9_]+$/', $form->keyname)) {
  214. return $form->response()->error('应用标识符格式无效');
  215. }
  216. // 验证汇率
  217. if ($form->exchange_rate <= 0) {
  218. return $form->response()->error('汇率必须大于0');
  219. }
  220. });
  221. }
  222. }