RechargeChannel.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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,3'],
  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 getChannel()
  70. {
  71. $data_type = request()->input('data_type', 1);
  72. $channel = RechargeChannelModel::getChannel($data_type);
  73. return $this->success(['total' => count($channel), 'data' => $channel]);
  74. }
  75. /**
  76. * 充值通道组合列表
  77. */
  78. public function groupList()
  79. {
  80. try {
  81. $page = request()->input('page', 1);
  82. $limit = request()->input('limit', 15);
  83. $query = new RechargeChannelGroup();
  84. $count = $query->count();
  85. $list = $query
  86. ->forPage($page, $limit)
  87. ->get();
  88. foreach($list as &$item) {
  89. $item->rechargeTypes = $item['recharge_type'] ? RechargeChannelModel::getChannel(1, $item['recharge_type']) : [];
  90. $item->withdrawTypes = $item['withdraw_type'] ? RechargeChannelModel::getChannel(2, $item['withdraw_type']) : [];
  91. $item->activityTypes = $item['activity_type'] ? RechargeChannelModel::getChannel(3, $item['activity_type']) : [];
  92. }
  93. } catch (Exception $e) {
  94. return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
  95. }
  96. return $this->success(['total' => $count, 'data' => $list]);
  97. }
  98. /**
  99. * 充值通道组合管理更新
  100. */
  101. public function updateGroup()
  102. {
  103. try {
  104. $params = request()->validate([
  105. 'id' => ['nullable','integer'],
  106. 'name' => ['required','string'],
  107. 'recharge_type' => ['present','array'],
  108. 'withdraw_type' => ['present','array'],
  109. 'activity_type' => ['nullable','array'],
  110. ]);
  111. $removed = app(RechargeChannelConfigurationService::class)->saveGroup($params);
  112. return $this->success([], $removed === [] ? '' : '保存成功,已清理失效的历史通道类型');
  113. } catch (ValidationException $e) {
  114. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  115. } catch (ModelNotFoundException $e) {
  116. return $this->error(HttpStatus::CUSTOM_ERROR, '层级不存在');
  117. } catch (\PDOException $e) {
  118. report($e);
  119. return $this->error(HttpStatus::CUSTOM_ERROR, '保存层级失败,请稍后重试');
  120. } catch (Exception $e) {
  121. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  122. }
  123. }
  124. public function deleteGroup(RechargeChannelConfigurationService $service)
  125. {
  126. try {
  127. $params = request()->validate(['id' => ['required', 'integer', 'min:1']]);
  128. $service->deleteGroup((int)$params['id']);
  129. return $this->success();
  130. } catch (ValidationException $e) {
  131. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  132. } catch (ModelNotFoundException $e) {
  133. return $this->error(HttpStatus::CUSTOM_ERROR, '层级不存在');
  134. } catch (\PDOException $e) {
  135. report($e);
  136. return $this->error(HttpStatus::CUSTOM_ERROR, '删除层级失败,请稍后重试');
  137. } catch (Exception $e) {
  138. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  139. }
  140. }
  141. /**
  142. * 充值通道管理列表
  143. */
  144. public function list()
  145. {
  146. try {
  147. $params = request()->validate([
  148. 'page' => ['nullable', 'integer', 'min:1'],
  149. 'limit' => ['nullable', 'integer', 'min:1'],
  150. 'data_type' => ['nullable', 'string'],
  151. 'type' => ['nullable', 'string'],
  152. 'from' => ['nullable', 'integer'],
  153. 'key' => ['nullable', 'string'],
  154. 'name' => ['nullable', 'string'],
  155. 'payment_company' => ['nullable', Rule::in(PaymentProviderCatalog::codes())],
  156. ]);
  157. $page = request()->input('page', 1);
  158. $limit = request()->input('limit', 15);
  159. $query = new RechargeChannelModel();
  160. if (!empty($params['data_type'])) {
  161. $query = $query->where('data_type', $params['data_type']);
  162. }
  163. if (!empty($params['type'])) {
  164. $query = $query->where('type', $params['type']);
  165. }
  166. if (isset($params['from'])) {
  167. $query = $query->where('from', $params['from']);
  168. }
  169. if (!empty($params['key'])) {
  170. $query = $query->where('key', $params['key']);
  171. }
  172. if (!empty($params['name'])) {
  173. $query = $query->where('name', 'like', '%'.$params['name'].'%');
  174. }
  175. if (!empty($params['payment_company'])) {
  176. $direction = !empty($params['data_type']) ? (int)$params['data_type'] : null;
  177. $query = $query->where('from', 1)->whereIn('data_type', [1, 2])->whereIn('type',
  178. PaymentProviderCatalog::channelTypesForProvider($params['payment_company'], $direction));
  179. }
  180. $count = $query->count();
  181. $companyStatuses = PaymentProviderService::statuses();
  182. $channels = $query
  183. ->forPage($page, $limit)
  184. ->orderBy('sort', 'asc')
  185. ->orderBy('id', 'asc')
  186. ->get();
  187. $configuredLogos = [];
  188. $logoTypes = $channels->filter(fn (RechargeChannelModel $channel) => (int)$channel->data_type === 1)
  189. ->pluck('type')->map(fn ($type) => (string)$type)->unique()->values()->all();
  190. if ($logoTypes !== []) $configuredLogos = RechargeChannelLogoService::configured($logoTypes);
  191. $list = $channels->map(function (RechargeChannelModel $channel) use ($companyStatuses, $configuredLogos) {
  192. $row = $channel->toArray();
  193. $dataType = (int)$row['data_type'];
  194. if (RechargeChannelLogoService::supportsDirection($dataType) && ($row['logo'] ?? '') === ''
  195. && $dataType === 1 && isset($configuredLogos[(string)$row['type']])) {
  196. $row['logo'] = (string)Util::ensureUrl($configuredLogos[(string)$row['type']]);
  197. }
  198. return $row + PaymentProviderCatalog::channelDisplay($row, $companyStatuses);
  199. });
  200. } catch (Exception $e) {
  201. return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
  202. }
  203. return $this->success(['total' => $count, 'data' => $list]);
  204. }
  205. /**
  206. * 充值通道管理更新
  207. */
  208. public function update(RechargeChannelConfigurationService $service)
  209. {
  210. try {
  211. $params = request()->validate([
  212. 'id' => ['nullable','integer'],
  213. 'from' => ['nullable','integer','in:1,2,3'],
  214. 'payment_company' => ['nullable', Rule::in(PaymentProviderCatalog::codes())],
  215. 'key' => ['nullable','string','max:100'],
  216. 'name' => ['nullable','string','max:100'],
  217. 'data_type' => ['required','integer','in:1,2,3'],
  218. 'type' => ['required','string','max:100'],
  219. 'rate' => ['required','numeric','min:0'],
  220. 'min' => ['exclude_if:type,zfbge','nullable','numeric','min:0'],
  221. 'max' => ['exclude_if:type,zfbge','nullable','numeric','gte:min'],
  222. 'fixed' => ['nullable'],
  223. 'status' => ['nullable','integer','in:0,1'],
  224. 'sort' => ['nullable','integer','min:0'],
  225. 'logo' => ['nullable', 'string', 'max:2048'],
  226. ]);
  227. $service->save($params);
  228. return $this->success();
  229. } catch (ValidationException $e) {
  230. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  231. } catch (ModelNotFoundException $e) {
  232. return $this->error(HttpStatus::CUSTOM_ERROR, '数据不存在');
  233. } catch (\PDOException $e) {
  234. report($e);
  235. return $this->error(HttpStatus::CUSTOM_ERROR, '保存通道失败,请稍后重试');
  236. } catch (Exception $e) {
  237. return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
  238. }
  239. }
  240. }