PointsController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Http\Controllers\Points;
  3. use App\Libs\ApiResponse;
  4. use App\Libs\Utils;
  5. use App\Services\PointsService;
  6. use App\Transformer\Points\PointsTransformer;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Routing\Controller as BaseController;
  9. use Illuminate\Support\Facades\Validator;
  10. class PointsController extends BaseController
  11. {
  12. use ApiResponse;
  13. protected $pointsService;
  14. public function __construct(PointsService $pointsService)
  15. {
  16. $this->pointsService = $pointsService;
  17. }
  18. /**
  19. * 积分流水列表(前端查看积分明细)
  20. *
  21. * @param Request $request
  22. * @return mixed
  23. */
  24. public function records(Request $request)
  25. {
  26. $result = $this->pointsService->getUserPointsRecords($request->all());
  27. $transformer = new PointsTransformer();
  28. return $this->success([
  29. 'summary' => $result['summary'],
  30. 'meta' => getMeta($result['records']),
  31. 'list' => $transformer->newEachPointsRecord($result['records']),
  32. ]);
  33. }
  34. /**
  35. * 获取系统当前全部积分规则(文生文/图片/视频)
  36. *
  37. * 供前端积分规则管理页展示:
  38. * - chat:文生文(对话)扣分规则(默认10,支持按模型配置)
  39. * - image_models:图片模型计费规则
  40. * - video_models:视频模型计费规则
  41. *
  42. * @param Request $request
  43. * @return mixed
  44. */
  45. public function rules(Request $request)
  46. {
  47. return $this->success($this->pointsService->getPointsRules());
  48. }
  49. /**
  50. * 编辑积分规则(文生文/图片/视频)
  51. *
  52. * 入参三个区块均可选传,单次至少传一个:
  53. * - text_rules:文生文规则列表 [{model?, charge_type?, price_json?}],model 可不传,不传时更新全部文生文模型
  54. * - image_rules:图片规则列表 [{model, charge_type?, price_json?}]
  55. * - video_rules:视频规则列表 [{model, charge_type?, price_json?}]
  56. * 图片/视频规则 model 必填;每条规则 charge_type / price_json 至少传一个(未传字段保留原值)。
  57. *
  58. * @param Request $request
  59. * @return mixed
  60. */
  61. public function editRules(Request $request)
  62. {
  63. $data = $request->all();
  64. $validator = Validator::make($data, [
  65. 'text_rules' => 'nullable|array',
  66. 'image_rules' => 'nullable|array',
  67. 'video_rules' => 'nullable|array',
  68. ], [
  69. 'text_rules.array' => 'text_rules必须为数组',
  70. 'image_rules.array' => 'image_rules必须为数组',
  71. 'video_rules.array' => 'video_rules必须为数组',
  72. ]);
  73. if ($validator->fails()) {
  74. Utils::throwError('1002:' . $validator->errors()->first());
  75. }
  76. $hasText = isset($data['text_rules']) && !empty($data['text_rules']);
  77. $hasImage = isset($data['image_rules']) && !empty($data['image_rules']);
  78. $hasVideo = isset($data['video_rules']) && !empty($data['video_rules']);
  79. if (!$hasText && !$hasImage && !$hasVideo) {
  80. Utils::throwError('1002:请至少传入text_rules、image_rules或video_rules之一');
  81. }
  82. $updated = $this->pointsService->updatePointsRules($data);
  83. return $this->success([
  84. 'updated' => $updated,
  85. 'remark' => '计费规则已更新,后续生成任务按新规则扣费',
  86. ]);
  87. }
  88. }