RechargeChannel.php 10 KB

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