TransferAppController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace App\Module\Transfer\AdminControllers;
  3. use App\Module\Transfer\AdminControllers\Helper\TransferAppHelper;
  4. use App\Module\Transfer\Models\TransferApp;
  5. use App\Module\Transfer\Repositories\TransferAppRepository;
  6. use Dcat\Admin\Form;
  7. use Dcat\Admin\Grid;
  8. use Dcat\Admin\Show;
  9. use Illuminate\Support\Facades\Http;
  10. use UCore\DcatAdmin\AdminController;
  11. /**
  12. * 划转应用管理控制器
  13. */
  14. class TransferAppController extends AdminController
  15. {
  16. /**
  17. * 页面标题
  18. */
  19. protected $title = '划转应用管理';
  20. /**
  21. * 模型类
  22. */
  23. protected $model = TransferApp::class;
  24. /**
  25. * 仓库类
  26. */
  27. protected $repository = TransferAppRepository::class;
  28. /**
  29. * 配置数据表格
  30. */
  31. protected function grid(): Grid
  32. {
  33. $grid = Grid::make(new TransferApp(), function (Grid $grid) {
  34. // 使用辅助类配置表格
  35. TransferAppHelper::grid($grid);
  36. // 设置每页显示数量
  37. $grid->paginate(20);
  38. // 禁用创建按钮(如果需要)
  39. // $grid->disableCreateButton();
  40. // 设置表格标题
  41. $grid->header(function () {
  42. return view('transfer::admin.app.header');
  43. });
  44. });
  45. return $grid;
  46. }
  47. /**
  48. * 配置数据详情
  49. */
  50. protected function detail($id): Show
  51. {
  52. $show = Show::make($id, new TransferApp(), function (Show $show) {
  53. // 使用辅助类配置详情
  54. TransferAppHelper::show($show);
  55. // 添加自定义面板
  56. $show->panel()
  57. ->title('划转应用详情')
  58. ->tools(function ($tools) {
  59. // 添加测试连接按钮
  60. $tools->append('<a class="btn btn-sm btn-outline-primary" href="javascript:void(0)" onclick="testConnection('.$this->getKey().')">测试连接</a>');
  61. });
  62. });
  63. return $show;
  64. }
  65. /**
  66. * 配置创建和编辑表单
  67. */
  68. protected function form(): Form
  69. {
  70. $form = Form::make(new TransferApp(), function (Form $form) {
  71. // 使用辅助类配置表单
  72. TransferAppHelper::form($form);
  73. // 设置表单标题
  74. $form->title('划转应用配置');
  75. // 表单工具栏
  76. $form->tools(function (Form\Tools $tools) {
  77. // 添加测试按钮
  78. $tools->append('<a class="btn btn-outline-info" href="javascript:void(0)" onclick="testForm()">测试配置</a>');
  79. });
  80. });
  81. return $form;
  82. }
  83. /**
  84. * 测试应用连接
  85. */
  86. public function testConnection($id)
  87. {
  88. try {
  89. $app = TransferApp::findOrFail($id);
  90. if ($app->isInternalMode()) {
  91. return response()->json([
  92. 'status' => true,
  93. 'message' => '农场内部模式,无需测试连接'
  94. ]);
  95. }
  96. // 测试各个API端点
  97. $results = [];
  98. $urls = [
  99. 'callback' => $app->order_callback_url,
  100. 'in_info' => $app->order_in_info_url,
  101. 'out_create' => $app->order_out_create_url,
  102. 'out_info' => $app->order_out_info_url,
  103. ];
  104. foreach ($urls as $type => $url) {
  105. if (empty($url)) {
  106. $results[$type] = ['status' => 'skip', 'message' => '未配置'];
  107. continue;
  108. }
  109. try {
  110. $response = Http::timeout(10)->get($url);
  111. $results[$type] = [
  112. 'status' => $response->successful() ? 'success' : 'error',
  113. 'message' => $response->successful() ? '连接成功' : "HTTP {$response->status()}",
  114. 'response_time' => $response->transferStats->getTransferTime() ?? 0
  115. ];
  116. } catch (\Exception $e) {
  117. $results[$type] = [
  118. 'status' => 'error',
  119. 'message' => $e->getMessage()
  120. ];
  121. }
  122. }
  123. return response()->json([
  124. 'status' => true,
  125. 'message' => '连接测试完成',
  126. 'data' => $results
  127. ]);
  128. } catch (\Exception $e) {
  129. return response()->json([
  130. 'status' => false,
  131. 'message' => '测试失败: ' . $e->getMessage()
  132. ]);
  133. }
  134. }
  135. /**
  136. * 切换应用状态
  137. */
  138. public function toggleStatus($id)
  139. {
  140. try {
  141. $app = TransferApp::findOrFail($id);
  142. $app->is_enabled = !$app->is_enabled;
  143. $app->save();
  144. $status = $app->is_enabled ? '启用' : '禁用';
  145. return response()->json([
  146. 'status' => true,
  147. 'message' => "应用已{$status}"
  148. ]);
  149. } catch (\Exception $e) {
  150. return response()->json([
  151. 'status' => false,
  152. 'message' => '操作失败: ' . $e->getMessage()
  153. ]);
  154. }
  155. }
  156. /**
  157. * 获取应用统计信息
  158. */
  159. public function statistics($id = null)
  160. {
  161. try {
  162. $query = \App\Module\Transfer\Models\TransferOrder::query();
  163. if ($id) {
  164. $query->where('transfer_app_id', $id);
  165. }
  166. $stats = [
  167. 'total_orders' => $query->count(),
  168. 'completed_orders' => (clone $query)->where('status', \App\Module\Transfer\Enums\TransferStatus::COMPLETED)->count(),
  169. 'failed_orders' => (clone $query)->where('status', \App\Module\Transfer\Enums\TransferStatus::FAILED)->count(),
  170. 'total_amount' => $query->sum('amount'),
  171. 'today_orders' => (clone $query)->whereDate('created_at', today())->count(),
  172. 'today_amount' => (clone $query)->whereDate('created_at', today())->sum('amount'),
  173. ];
  174. $stats['success_rate'] = $stats['total_orders'] > 0
  175. ? round($stats['completed_orders'] / $stats['total_orders'] * 100, 2)
  176. : 0;
  177. return response()->json([
  178. 'status' => true,
  179. 'data' => $stats
  180. ]);
  181. } catch (\Exception $e) {
  182. return response()->json([
  183. 'status' => false,
  184. 'message' => '获取统计信息失败: ' . $e->getMessage()
  185. ]);
  186. }
  187. }
  188. }