RechargeChannel.php 8.4 KB

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