PointsController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace App\Http\Controllers\Points;
  3. use App\Facade\Site;
  4. use App\Libs\ApiResponse;
  5. use App\Libs\Utils;
  6. use App\Services\PointsService;
  7. use App\Transformer\Points\PointsTransformer;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Routing\Controller as BaseController;
  10. use Illuminate\Support\Facades\Validator;
  11. class PointsController extends BaseController
  12. {
  13. use ApiResponse;
  14. protected $pointsService;
  15. public function __construct(PointsService $pointsService)
  16. {
  17. $this->pointsService = $pointsService;
  18. }
  19. /**
  20. * 积分流水列表(前端查看积分明细)
  21. *
  22. * @param Request $request
  23. * @return mixed
  24. */
  25. public function records(Request $request)
  26. {
  27. $result = $this->pointsService->getUserPointsRecords($request->all());
  28. $transformer = new PointsTransformer();
  29. return $this->success([
  30. 'summary' => $result['summary'],
  31. 'meta' => getMeta($result['records']),
  32. 'list' => $transformer->newEachPointsRecord($result['records']),
  33. ]);
  34. }
  35. /**
  36. * 获取系统当前全部积分规则(文生文/图片/视频)
  37. *
  38. * 供前端积分规则管理页展示:
  39. * - chat:文生文(对话)扣分规则(默认10,支持按模型配置)
  40. * - image_models:图片模型计费规则
  41. * - video_models:视频模型计费规则
  42. *
  43. * @param Request $request
  44. * @return mixed
  45. */
  46. public function rules(Request $request)
  47. {
  48. $this->checkSuperadmin();
  49. return $this->success($this->pointsService->getPointsRules());
  50. }
  51. /**
  52. * 编辑积分规则(文生文/图片/视频)
  53. *
  54. * 入参三个区块均可选传,单次至少传一个:
  55. * - text_rules:文生文规则列表 [{model?, charge_type?, price_json?}],model 可不传,不传时更新全部文生文模型
  56. * - image_rules:图片规则列表 [{model, charge_type?, price_json?}]
  57. * - video_rules:视频规则列表 [{model, charge_type?, price_json?}]
  58. * 图片/视频规则 model 必填;每条规则 charge_type / price_json 至少传一个(未传字段保留原值)。
  59. *
  60. * @param Request $request
  61. * @return mixed
  62. */
  63. public function editRules(Request $request)
  64. {
  65. $this->checkSuperadmin();
  66. $data = $request->all();
  67. $validator = Validator::make($data, [
  68. 'text_rules' => 'nullable|array',
  69. 'image_rules' => 'nullable|array',
  70. 'video_rules' => 'nullable|array',
  71. ], [
  72. 'text_rules.array' => 'text_rules必须为数组',
  73. 'image_rules.array' => 'image_rules必须为数组',
  74. 'video_rules.array' => 'video_rules必须为数组',
  75. ]);
  76. if ($validator->fails()) {
  77. Utils::throwError('1002:' . $validator->errors()->first());
  78. }
  79. $hasText = isset($data['text_rules']) && !empty($data['text_rules']);
  80. $hasImage = isset($data['image_rules']) && !empty($data['image_rules']);
  81. $hasVideo = isset($data['video_rules']) && !empty($data['video_rules']);
  82. if (!$hasText && !$hasImage && !$hasVideo) {
  83. Utils::throwError('1002:请至少传入text_rules、image_rules或video_rules之一');
  84. }
  85. $updated = $this->pointsService->updatePointsRules($data);
  86. return $this->success([
  87. 'updated' => $updated,
  88. 'remark' => '计费规则已更新,后续生成任务按新规则扣费',
  89. ]);
  90. }
  91. /**
  92. * 发放/回收积分(平台↔组织 / 组织↔同公司组员,支持批量)
  93. *
  94. * 入参:
  95. * - uid:目标用户ID(必填,支持多个ID用英文逗号隔开,如 142857,142858)
  96. * - points:发放积分数(必填,大于0)
  97. * - action:操作类型 grant-发放(默认)/ revoke-回收(可选)
  98. * - remark:备注文案(可选,回收时校验目标积分充足)
  99. *
  100. * @param Request $request
  101. * @return mixed
  102. */
  103. public function grantPoints(Request $request)
  104. {
  105. $data = $request->all();
  106. $validator = Validator::make($data, [
  107. 'uid' => ['required', 'regex:/^[\d\s,]+$/'],
  108. 'points' => 'required|integer|gt:0',
  109. // 'action' => 'nullable|string|in:grant,revoke',
  110. 'remark' => 'nullable|string|max:200',
  111. ], [
  112. 'uid.required' => '请传入目标用户uid',
  113. 'uid.regex' => 'uid格式不正确,多个用户ID请用英文逗号隔开,如 142857,142858',
  114. 'points.required' => '请传入发放积分数',
  115. 'points.integer' => '发放积分数必须为整数',
  116. 'points.gt' => '发放积分数必须大于0',
  117. // 'action.in' => 'action仅支持grant(发放)或revoke(回收)',
  118. 'remark.max' => '备注不能超过200个字符',
  119. ]);
  120. if ($validator->fails()) {
  121. Utils::throwError('1002:' . $validator->errors()->first());
  122. }
  123. $result = $this->pointsService->grantPoints($data);
  124. return $this->success($result);
  125. }
  126. /**
  127. * 获取可发放积分的用户列表
  128. *
  129. * 同一接口按当前角色返回:
  130. * - 平台(superadmin):所有组织
  131. * - 组织(admin):同公司组员
  132. *
  133. * @param Request $request
  134. * @return mixed
  135. */
  136. public function grantUsers(Request $request)
  137. {
  138. return $this->success([
  139. 'list' => $this->pointsService->getGrantUserList(),
  140. ]);
  141. }
  142. /**
  143. * 校验当前用户是否为平台
  144. *
  145. * @return void
  146. */
  147. private function checkSuperadmin(): void
  148. {
  149. if (Site::getRole() !== 'superadmin') {
  150. Utils::throwError('1005:仅平台可操作积分规则');
  151. }
  152. }
  153. }