RechargeChannel.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\RechargeChannel as RechargeChannelModel;
  5. use App\Models\RechargeChannelGroup;
  6. use App\Services\Payment\PaymentProviderCatalog;
  7. use App\Services\Payment\RechargeChannelConfigurationService;
  8. use App\Services\Payment\PaymentProviderService;
  9. use Illuminate\Validation\Rule;
  10. use Illuminate\Validation\ValidationException;
  11. use Illuminate\Database\Eloquent\ModelNotFoundException;
  12. use Exception;
  13. use App\Constants\HttpStatus;
  14. use App\Constants\Util;
  15. use App\Services\Payment\RechargeChannelLogoService;
  16. class RechargeChannel extends Controller
  17. {
  18. public function logoOptions(\App\Services\Payment\RechargeChannelLogoService $service)
  19. {
  20. return $this->logoResponse(fn () => $service->options());
  21. }
  22. public function saveLogo(\App\Services\Payment\RechargeChannelLogoService $service)
  23. {
  24. return $this->logoResponse(fn () => $service->save(request()->all()));
  25. }
  26. private function logoResponse(callable $callback)
  27. {
  28. try { return $this->success($callback()); }
  29. catch (ValidationException $e) { return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first()); }
  30. catch (\Throwable $e) { \Illuminate\Support\Facades\Log::error('recharge_channel_logo_failed', ['exception' => $e]); return $this->error(HttpStatus::CUSTOM_ERROR, '充值Logo操作失败,请稍后重试'); }
  31. }
  32. public function delete(RechargeChannelConfigurationService $service)
  33. {
  34. try {
  35. $params = request()->validate(['id' => ['required', 'integer', 'min:1']]);
  36. $service->delete((int)$params['id']);
  37. return $this->success();
  38. } catch (ValidationException $e) {
  39. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  40. } catch (ModelNotFoundException $e) {
  41. return $this->error(HttpStatus::CUSTOM_ERROR, '通道不存在');
  42. } catch (\PDOException $e) {
  43. report($e);
  44. return $this->error(HttpStatus::CUSTOM_ERROR, '删除通道失败,请稍后重试');
  45. } catch (Exception $e) {
  46. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  47. }
  48. }
  49. public function options(RechargeChannelConfigurationService $service)
  50. {
  51. try {
  52. $params = request()->validate([
  53. 'data_type' => ['required', 'integer', 'in:1,2'],
  54. 'id' => ['nullable', 'integer', 'min:1'],
  55. ]);
  56. return $this->success($service->options((int)$params['data_type'], isset($params['id']) ? (int)$params['id'] : null));
  57. } catch (ValidationException $e) {
  58. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  59. } catch (ModelNotFoundException $e) {
  60. return $this->error(HttpStatus::CUSTOM_ERROR, '数据不存在');
  61. } catch (\PDOException $e) {
  62. report($e);
  63. return $this->error(HttpStatus::CUSTOM_ERROR, '读取通道选项失败');
  64. } catch (Exception $e) {
  65. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  66. }
  67. }
  68. /** 充值/提现通道操作日志 */
  69. public function logs(RechargeChannelConfigurationService $service)
  70. {
  71. try {
  72. $params = request()->validate([
  73. 'data_type' => ['required', 'integer', 'in:1,2'],
  74. 'channel_id' => ['nullable', 'integer', 'min:1'],
  75. 'operator_name' => ['nullable', 'string', 'max:100'],
  76. 'action' => ['nullable', 'string', 'in:create,update,delete,save'],
  77. 'start_time' => ['nullable', 'date'],
  78. 'end_time' => ['nullable', 'date'],
  79. 'page' => ['nullable', 'integer', 'min:1'],
  80. 'limit' => ['nullable', 'integer', 'min:1', 'max:200'],
  81. ]);
  82. return $this->success($service->logs($params));
  83. } catch (ValidationException $e) {
  84. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  85. } catch (Exception $e) {
  86. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  87. }
  88. }
  89. //获取充值方式
  90. public function getChannel()
  91. {
  92. $data_type = (int)request()->input('data_type', 1) === 2 ? 2 : 1;
  93. $channel = RechargeChannelModel::getChannel($data_type);
  94. return $this->success(['total' => count($channel), 'data' => $channel]);
  95. }
  96. /**
  97. * 充值通道组合列表
  98. */
  99. public function groupList()
  100. {
  101. try {
  102. $page = request()->input('page', 1);
  103. $limit = request()->input('limit', 15);
  104. $query = new RechargeChannelGroup();
  105. $count = $query->count();
  106. $list = $query
  107. ->forPage($page, $limit)
  108. ->get();
  109. foreach($list as &$item) {
  110. $item->rechargeTypes = $item['recharge_type'] ? RechargeChannelModel::getChannel(1, $item['recharge_type']) : [];
  111. $item->withdrawTypes = $item['withdraw_type'] ? RechargeChannelModel::getChannel(2, $item['withdraw_type']) : [];
  112. }
  113. } catch (Exception $e) {
  114. return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
  115. }
  116. return $this->success(['total' => $count, 'data' => $list]);
  117. }
  118. /**
  119. * 充值通道组合管理更新
  120. */
  121. public function updateGroup()
  122. {
  123. try {
  124. $params = request()->validate([
  125. 'id' => ['nullable','integer'],
  126. 'name' => ['required','string'],
  127. 'recharge_type' => ['present','array'],
  128. 'withdraw_type' => ['present','array'],
  129. ]);
  130. $removed = app(RechargeChannelConfigurationService::class)->saveGroup($params);
  131. return $this->success([], $removed === [] ? '' : '保存成功,已清理失效的历史通道类型');
  132. } catch (ValidationException $e) {
  133. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  134. } catch (ModelNotFoundException $e) {
  135. return $this->error(HttpStatus::CUSTOM_ERROR, '层级不存在');
  136. } catch (\PDOException $e) {
  137. report($e);
  138. return $this->error(HttpStatus::CUSTOM_ERROR, '保存层级失败,请稍后重试');
  139. } catch (Exception $e) {
  140. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  141. }
  142. }
  143. public function deleteGroup(RechargeChannelConfigurationService $service)
  144. {
  145. try {
  146. $params = request()->validate(['id' => ['required', 'integer', 'min:1']]);
  147. $service->deleteGroup((int)$params['id']);
  148. return $this->success();
  149. } catch (ValidationException $e) {
  150. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  151. } catch (ModelNotFoundException $e) {
  152. return $this->error(HttpStatus::CUSTOM_ERROR, '层级不存在');
  153. } catch (\PDOException $e) {
  154. report($e);
  155. return $this->error(HttpStatus::CUSTOM_ERROR, '删除层级失败,请稍后重试');
  156. } catch (Exception $e) {
  157. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  158. }
  159. }
  160. /**
  161. * 充值通道管理列表
  162. */
  163. public function list()
  164. {
  165. try {
  166. $params = request()->validate([
  167. 'page' => ['nullable', 'integer', 'min:1'],
  168. 'limit' => ['nullable', 'integer', 'min:1'],
  169. 'data_type' => ['nullable', 'integer', 'in:1,2'],
  170. 'type' => ['nullable', 'string'],
  171. 'from' => ['nullable', 'integer'],
  172. 'key' => ['nullable', 'string'],
  173. 'name' => ['nullable', 'string'],
  174. 'payment_company' => ['nullable', Rule::in(PaymentProviderCatalog::codes())],
  175. ]);
  176. $page = request()->input('page', 1);
  177. $limit = request()->input('limit', 15);
  178. // 只管理充值(1)、提现(2)通道;原活动(3)配置已停用
  179. $query = RechargeChannelModel::query()->whereIn('data_type', [1, 2]);
  180. if (!empty($params['data_type'])) {
  181. $query = $query->where('data_type', $params['data_type']);
  182. }
  183. if (!empty($params['type'])) {
  184. $query = $query->where('type', $params['type']);
  185. }
  186. if (isset($params['from'])) {
  187. $query = $query->where('from', $params['from']);
  188. }
  189. if (!empty($params['key'])) {
  190. $query = $query->where('key', $params['key']);
  191. }
  192. if (!empty($params['name'])) {
  193. $query = $query->where('name', 'like', '%'.$params['name'].'%');
  194. }
  195. if (!empty($params['payment_company'])) {
  196. $direction = !empty($params['data_type']) ? (int)$params['data_type'] : null;
  197. $query = $query->where('from', 1)->whereIn('data_type', [1, 2])->whereIn('type',
  198. PaymentProviderCatalog::channelTypesForProvider($params['payment_company'], $direction));
  199. }
  200. $count = $query->count();
  201. $companyStatuses = PaymentProviderService::statuses();
  202. $channels = $query
  203. ->forPage($page, $limit)
  204. ->orderBy('sort', 'asc')
  205. ->orderBy('id', 'asc')
  206. ->get();
  207. $configuredLogos = [];
  208. $logoTypes = $channels->filter(fn (RechargeChannelModel $channel) => (int)$channel->data_type === 1)
  209. ->pluck('type')->map(fn ($type) => (string)$type)->unique()->values()->all();
  210. if ($logoTypes !== []) $configuredLogos = RechargeChannelLogoService::configured($logoTypes);
  211. $list = $channels->map(function (RechargeChannelModel $channel) use ($companyStatuses, $configuredLogos) {
  212. $row = $channel->toArray();
  213. $dataType = (int)$row['data_type'];
  214. if (RechargeChannelLogoService::supportsDirection($dataType) && ($row['logo'] ?? '') === ''
  215. && $dataType === 1 && isset($configuredLogos[(string)$row['type']])) {
  216. $row['logo'] = (string)Util::ensureUrl($configuredLogos[(string)$row['type']]);
  217. }
  218. return $row + PaymentProviderCatalog::channelDisplay($row, $companyStatuses);
  219. });
  220. } catch (Exception $e) {
  221. return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
  222. }
  223. return $this->success(['total' => $count, 'data' => $list]);
  224. }
  225. /**
  226. * 充值通道管理更新
  227. */
  228. public function update(RechargeChannelConfigurationService $service)
  229. {
  230. try {
  231. $params = request()->validate([
  232. 'id' => ['nullable','integer'],
  233. 'from' => ['nullable','integer','in:1,2,3'],
  234. 'payment_company' => ['nullable', Rule::in(PaymentProviderCatalog::codes())],
  235. 'key' => ['nullable','string','max:100'],
  236. 'name' => ['nullable','string','max:100'],
  237. 'data_type' => ['required','integer','in:1,2'],
  238. 'type' => ['required','string','max:100'],
  239. 'rate' => ['required','numeric','min:0'],
  240. 'min' => ['exclude_if:type,zfbge','nullable','numeric','min:0'],
  241. 'max' => ['exclude_if:type,zfbge','nullable','numeric','gte:min'],
  242. 'fixed' => ['nullable'],
  243. 'status' => ['nullable','integer','in:0,1'],
  244. 'sort' => ['nullable','integer','min:0'],
  245. 'logo' => ['nullable', 'string', 'max:2048'],
  246. ]);
  247. $service->save($params);
  248. return $this->success();
  249. } catch (ValidationException $e) {
  250. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  251. } catch (ModelNotFoundException $e) {
  252. return $this->error(HttpStatus::CUSTOM_ERROR, '数据不存在');
  253. } catch (\PDOException $e) {
  254. report($e);
  255. return $this->error(HttpStatus::CUSTOM_ERROR, '保存通道失败,请稍后重试');
  256. } catch (Exception $e) {
  257. return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
  258. }
  259. }
  260. }