RechargeChannel.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 Illuminate\Validation\Rule;
  8. use Exception;
  9. use App\Constants\HttpStatus;
  10. class RechargeChannel extends Controller
  11. {
  12. //获取充值方式
  13. public function getChannel()
  14. {
  15. $data_type = request()->input('data_type', 1);
  16. $channel = RechargeChannelModel::getChannel($data_type);
  17. return $this->success(['total' => count($channel), 'data' => $channel]);
  18. }
  19. /**
  20. * 充值通道组合列表
  21. */
  22. public function groupList()
  23. {
  24. try {
  25. $page = request()->input('page', 1);
  26. $limit = request()->input('limit', 15);
  27. $query = new RechargeChannelGroup();
  28. $count = $query->count();
  29. $list = $query
  30. ->forPage($page, $limit)
  31. ->get();
  32. foreach($list as &$item) {
  33. $item->rechargeTypes = $item['recharge_type'] ? RechargeChannelModel::getChannel(1, $item['recharge_type']) : [];
  34. $item->withdrawTypes = $item['withdraw_type'] ? RechargeChannelModel::getChannel(2, $item['withdraw_type']) : [];
  35. $item->activityTypes = $item['activity_type'] ? RechargeChannelModel::getChannel(3, $item['activity_type']) : [];
  36. }
  37. } catch (Exception $e) {
  38. return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
  39. }
  40. return $this->success(['total' => $count, 'data' => $list]);
  41. }
  42. /**
  43. * 充值通道组合管理更新
  44. */
  45. public function updateGroup()
  46. {
  47. try {
  48. $params = request()->validate([
  49. 'id' => ['nullable','integer'],
  50. 'name' => ['required','string'],
  51. 'recharge_type' => ['required','array'],
  52. 'withdraw_type' => ['required','array'],
  53. 'activity_type' => ['nullable','array'],
  54. ]);
  55. foreach (['recharge_type' => 1, 'withdraw_type' => 2, 'activity_type' => 3] as $field => $dataType) {
  56. if ($field === 'activity_type' && !request()->exists('activity_type')) {
  57. unset($params['activity_type']);
  58. continue;
  59. }
  60. $types = array_values(array_unique(array_filter(array_map('strval', $params[$field] ?? []))));
  61. if ($types) {
  62. $existingCount = RechargeChannelModel::query()->where('data_type', $dataType)
  63. ->whereIn('type', $types)->distinct()->count('type');
  64. if ($existingCount !== count($types)) throw new Exception("{$field} 包含不存在的通道类型");
  65. }
  66. $params[$field] = $types;
  67. }
  68. if (empty($params['id'])) {
  69. RechargeChannelGroup::create($params);
  70. } else {
  71. $info = RechargeChannelGroup::where('id', $params['id'])->first();
  72. if (!$info) throw new Exception('数据不存在');
  73. $info->update($params);
  74. $info->save();
  75. }
  76. return $this->success();
  77. } catch (Exception $e) {
  78. return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
  79. }
  80. }
  81. /**
  82. * 充值通道管理列表
  83. */
  84. public function list()
  85. {
  86. try {
  87. $params = request()->validate([
  88. 'page' => ['nullable', 'integer', 'min:1'],
  89. 'limit' => ['nullable', 'integer', 'min:1'],
  90. 'data_type' => ['nullable', 'string'],
  91. 'type' => ['nullable', 'string'],
  92. 'from' => ['nullable', 'integer'],
  93. 'key' => ['nullable', 'string'],
  94. 'name' => ['nullable', 'string'],
  95. 'payment_company' => ['nullable', Rule::in(PaymentProviderCatalog::codes())],
  96. ]);
  97. $page = request()->input('page', 1);
  98. $limit = request()->input('limit', 15);
  99. $query = new RechargeChannelModel();
  100. if (!empty($params['data_type'])) {
  101. $query = $query->where('data_type', $params['data_type']);
  102. }
  103. if (!empty($params['type'])) {
  104. $query = $query->where('type', $params['type']);
  105. }
  106. if (isset($params['from'])) {
  107. $query = $query->where('from', $params['from']);
  108. }
  109. if (!empty($params['key'])) {
  110. $query = $query->where('key', $params['key']);
  111. }
  112. if (!empty($params['name'])) {
  113. $query = $query->where('name', 'like', '%'.$params['name'].'%');
  114. }
  115. if (!empty($params['payment_company'])) {
  116. $direction = !empty($params['data_type']) ? (int)$params['data_type'] : null;
  117. $query = $query->where('from', 1)->whereIn('data_type', [1, 2])->whereIn('type',
  118. PaymentProviderCatalog::channelTypesForProvider($params['payment_company'], $direction));
  119. }
  120. $count = $query->count();
  121. $list = $query
  122. ->forPage($page, $limit)
  123. ->orderBy('sort', 'asc')
  124. ->orderBy('id', 'asc')
  125. ->get()->map(function (RechargeChannelModel $channel) {
  126. $row = $channel->toArray();
  127. $identity = PaymentProviderCatalog::channelIdentity($row);
  128. $row['payment_company'] = $identity['provider_code'] ?? null;
  129. $row['payment_company_label'] = $identity ? PaymentProviderCatalog::label($identity['provider_code']) : '';
  130. return $row;
  131. });
  132. } catch (Exception $e) {
  133. return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
  134. }
  135. return $this->success(['total' => $count, 'data' => $list]);
  136. }
  137. /**
  138. * 充值通道管理更新
  139. */
  140. public function update()
  141. {
  142. try {
  143. $params = request()->validate([
  144. 'id' => ['nullable','integer'],
  145. 'from' => ['nullable','integer'],
  146. 'key' => ['nullable','string','max:100'],
  147. 'name' => ['nullable','string','max:100'],
  148. 'data_type' => ['required','integer','in:1,2,3'],
  149. 'type' => ['required','string','max:100'],
  150. 'rate' => ['required','numeric','min:0'],
  151. 'min' => ['nullable','numeric','min:0'],
  152. 'max' => ['nullable','numeric','gte:min'],
  153. 'fixed' => ['nullable','string'],
  154. 'status' => ['nullable','integer','in:0,1'],
  155. 'sort' => ['nullable','integer','min:0'],
  156. ]);
  157. if (empty($params['id'])) {
  158. RechargeChannelModel::create($params);
  159. } else {
  160. $info = RechargeChannelModel::where('id', $params['id'])->first();
  161. if (!$info) throw new Exception('数据不存在');
  162. $info->update($params);
  163. $info->save();
  164. }
  165. return $this->success();
  166. } catch (Exception $e) {
  167. return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
  168. }
  169. }
  170. }