RechargeChannel.php 9.5 KB

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