RechargeChannel.php 6.3 KB

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