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