RechargeChannel.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. $types = array_values(array_unique(array_filter(array_map('strval', $params[$field] ?? []))));
  55. if ($types) {
  56. $existingCount = RechargeChannelModel::query()->where('data_type', $dataType)
  57. ->whereIn('type', $types)->distinct()->count('type');
  58. if ($existingCount !== count($types)) throw new Exception("{$field} 包含不存在的通道类型");
  59. }
  60. $params[$field] = $types;
  61. }
  62. if (empty($params['id'])) {
  63. RechargeChannelGroup::create($params);
  64. } else {
  65. $info = RechargeChannelGroup::where('id', $params['id'])->first();
  66. if (!$info) throw new Exception('数据不存在');
  67. $info->update($params);
  68. $info->save();
  69. }
  70. return $this->success();
  71. } catch (Exception $e) {
  72. return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
  73. }
  74. }
  75. /**
  76. * 充值通道管理列表
  77. */
  78. public function list()
  79. {
  80. try {
  81. $params = request()->validate([
  82. 'page' => ['nullable', 'integer', 'min:1'],
  83. 'limit' => ['nullable', 'integer', 'min:1'],
  84. 'data_type' => ['nullable', 'string'],
  85. 'type' => ['nullable', 'string'],
  86. 'from' => ['nullable', 'integer'],
  87. 'key' => ['nullable', 'string'],
  88. 'name' => ['nullable', 'string'],
  89. ]);
  90. $page = request()->input('page', 1);
  91. $limit = request()->input('limit', 15);
  92. $query = new RechargeChannelModel();
  93. if (!empty($params['data_type'])) {
  94. $query = $query->where('data_type', $params['data_type']);
  95. }
  96. if (!empty($params['type'])) {
  97. $query = $query->where('type', $params['type']);
  98. }
  99. if (isset($params['from'])) {
  100. $query = $query->where('from', $params['from']);
  101. }
  102. if (!empty($params['key'])) {
  103. $query = $query->where('key', $params['key']);
  104. }
  105. if (!empty($params['name'])) {
  106. $query = $query->where('name', 'like', '%'.$params['name'].'%');
  107. }
  108. $count = $query->count();
  109. $list = $query
  110. ->forPage($page, $limit)
  111. ->orderBy('sort', 'asc')
  112. ->get();
  113. } catch (Exception $e) {
  114. return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
  115. }
  116. return $this->success(['total' => $count, 'data' => $list]);
  117. }
  118. /**
  119. * 充值通道管理更新
  120. */
  121. public function update()
  122. {
  123. try {
  124. $params = request()->validate([
  125. 'id' => ['nullable','integer'],
  126. 'from' => ['nullable','integer'],
  127. 'key' => ['nullable','string','max:100'],
  128. 'name' => ['nullable','string','max:100'],
  129. 'data_type' => ['required','integer','in:1,2,3'],
  130. 'type' => ['required','string','max:100'],
  131. 'rate' => ['required','numeric','min:0'],
  132. 'min' => ['nullable','numeric','min:0'],
  133. 'max' => ['nullable','numeric','gte:min'],
  134. 'fixed' => ['nullable','string'],
  135. 'status' => ['nullable','integer','in:0,1'],
  136. 'sort' => ['nullable','integer','min:0'],
  137. ]);
  138. if (empty($params['id'])) {
  139. RechargeChannelModel::create($params);
  140. } else {
  141. $info = RechargeChannelModel::where('id', $params['id'])->first();
  142. if (!$info) throw new Exception('数据不存在');
  143. $info->update($params);
  144. $info->save();
  145. }
  146. return $this->success();
  147. } catch (Exception $e) {
  148. return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
  149. }
  150. }
  151. }